Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
39de6fd5 | 5 | | CiviCRM version 4.6 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Grant_BAO_Query { | |
e0ef6999 EM |
37 | /** |
38 | * @return array | |
39 | */ | |
00be9182 | 40 | public static function &getFields() { |
6a488035 TO |
41 | $fields = array(); |
42 | $fields = CRM_Grant_BAO_Grant::exportableFields(); | |
43 | return $fields; | |
44 | } | |
45 | ||
46 | /** | |
100fef9d | 47 | * Build select for CiviGrant |
6a488035 | 48 | * |
77b97be7 EM |
49 | * @param $query |
50 | * | |
6a488035 | 51 | * @return void |
6a488035 | 52 | */ |
00be9182 | 53 | public static function select(&$query) { |
8ae90f85 | 54 | if (($query->_mode & CRM_Contact_BAO_Query::MODE_GRANT) || !empty($query->_returnProperties)) { |
a7488080 | 55 | if (!empty($query->_returnProperties['grant_status_id'])) { |
6a488035 TO |
56 | $query->_select['grant_status_id'] = 'grant_status.id as grant_status_id'; |
57 | $query->_element['grant_status'] = 1; | |
58 | $query->_tables['grant_status'] = $query->_whereTables['grant_status'] = 1; | |
59 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
60 | } | |
61 | ||
a7488080 | 62 | if (!empty($query->_returnProperties['grant_status'])) { |
6a488035 TO |
63 | $query->_select['grant_status'] = 'grant_status.label as grant_status'; |
64 | $query->_element['grant_status'] = 1; | |
65 | $query->_tables['grant_status'] = $query->_whereTables['grant_status'] = 1; | |
66 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
67 | } | |
68 | ||
a7488080 | 69 | if (!empty($query->_returnProperties['grant_type_id'])) { |
6a488035 TO |
70 | $query->_select['grant_type_id'] = 'grant_type.id as grant_type_id'; |
71 | $query->_element['grant_type'] = 1; | |
72 | $query->_tables['grant_type'] = $query->_whereTables['grant_type'] = 1; | |
73 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
74 | } | |
75 | ||
a7488080 | 76 | if (!empty($query->_returnProperties['grant_type'])) { |
6a488035 TO |
77 | $query->_select['grant_type'] = 'grant_type.label as grant_type'; |
78 | $query->_element['grant_type'] = 1; | |
79 | $query->_tables['grant_type'] = $query->_whereTables['grant_type'] = 1; | |
80 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
81 | } | |
82 | ||
a7488080 | 83 | if (!empty($query->_returnProperties['grant_note'])) { |
6a488035 TO |
84 | $query->_select['grant_note'] = "civicrm_note.note as grant_note"; |
85 | $query->_element['grant_note'] = 1; | |
86 | $query->_tables['grant_note'] = 1; | |
87 | } | |
88 | $query->_select['grant_amount_requested'] = 'civicrm_grant.amount_requested as grant_amount_requested'; | |
89 | $query->_select['grant_amount_granted'] = 'civicrm_grant.amount_granted as grant_amount_granted'; | |
90 | $query->_select['grant_amount_total'] = 'civicrm_grant.amount_total as grant_amount_total'; | |
91 | $query->_select['grant_application_received_date'] = 'civicrm_grant.application_received_date as grant_application_received_date '; | |
92 | $query->_select['grant_report_received'] = 'civicrm_grant.grant_report_received as grant_report_received'; | |
93 | $query->_select['grant_money_transfer_date'] = 'civicrm_grant.money_transfer_date as grant_money_transfer_date'; | |
94 | $query->_element['grant_type_id'] = 1; | |
95 | $query->_element['grant_status_id'] = 1; | |
96 | $query->_tables['civicrm_grant'] = 1; | |
97 | $query->_whereTables['civicrm_grant'] = 1; | |
98 | } | |
99 | } | |
100 | ||
101 | /** | |
102 | * Given a list of conditions in params generate the required | |
103 | * where clause | |
104 | * | |
6c8f6e67 EM |
105 | * @param $query |
106 | * | |
6a488035 | 107 | * @return void |
6a488035 | 108 | */ |
00be9182 | 109 | public static function where(&$query) { |
6a488035 TO |
110 | foreach ($query->_params as $id => $values) { |
111 | if (!is_array($values) || count($values) != 5) { | |
112 | continue; | |
113 | } | |
114 | ||
115 | if (substr($values[0], 0, 6) == 'grant_') { | |
116 | self::whereClauseSingle($values, $query); | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
e0ef6999 EM |
121 | /** |
122 | * @param $values | |
123 | * @param $query | |
124 | */ | |
00be9182 | 125 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
126 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
127 | list($name, $op, $value, $grouping, $wildcard) = $values; | |
8ae90f85 | 128 | $val = $names = array(); |
6a488035 TO |
129 | switch ($name) { |
130 | case 'grant_money_transfer_date_low': | |
131 | case 'grant_money_transfer_date_high': | |
132 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
133 | 'grant_money_transfer_date', 'money_transfer_date', | |
134 | 'Money Transfer Date' | |
135 | ); | |
136 | return; | |
137 | ||
138 | case 'grant_money_transfer_date_notset': | |
139 | $query->_where[$grouping][] = "civicrm_grant.money_transfer_date IS NULL"; | |
140 | $query->_qill[$grouping][] = ts("Grant Money Transfer Date is NULL"); | |
141 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
142 | return; | |
143 | ||
144 | case 'grant_application_received_date_low': | |
145 | case 'grant_application_received_date_high': | |
146 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
147 | 'grant_application_received_date', | |
148 | 'application_received_date', 'Application Received Date' | |
149 | ); | |
150 | return; | |
151 | ||
152 | case 'grant_application_received_notset': | |
153 | $query->_where[$grouping][] = "civicrm_grant.application_received_date IS NULL"; | |
154 | $query->_qill[$grouping][] = ts("Grant Application Received Date is NULL"); | |
155 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
156 | return; | |
157 | ||
158 | case 'grant_due_date_low': | |
159 | case 'grant_due_date_high': | |
160 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
161 | 'grant_due_date', | |
162 | 'grant_due_date', 'Grant Due Date' | |
163 | ); | |
164 | return; | |
165 | ||
166 | case 'grant_due_date_notset': | |
167 | $query->_where[$grouping][] = "civicrm_grant.grant_due_date IS NULL"; | |
168 | $query->_qill[$grouping][] = ts("Grant Due Date is NULL"); | |
169 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
170 | return; | |
171 | ||
172 | case 'grant_decision_date_low': | |
173 | case 'grant_decision_date_high': | |
174 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
175 | 'grant_decision_date', | |
176 | 'decision_date', 'Grant Decision Date' | |
177 | ); | |
178 | return; | |
179 | ||
180 | case 'grant_decision_date_notset': | |
181 | $query->_where[$grouping][] = "civicrm_grant.decision_date IS NULL"; | |
182 | $query->_qill[$grouping][] = ts("Grant Decision Date is NULL"); | |
183 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
184 | return; | |
185 | ||
186 | case 'grant_type_id': | |
8ae90f85 | 187 | case 'grant_type': |
188 | $grantTypes = CRM_Core_OptionGroup::values('grant_type'); | |
189 | if (is_array($value)) { | |
190 | foreach ($value as $k => $v) { | |
191 | if ($v) { | |
192 | $val[] = $v; | |
193 | } | |
194 | } | |
195 | if (count($val) > 0) { | |
196 | // Overwrite $value so it works with an IN where statement. | |
197 | $op = 'IN'; | |
198 | $value = '(' . implode(',', $val) . ')'; | |
199 | } | |
200 | } | |
201 | if (!empty($val)) { | |
22e263ad | 202 | foreach ($val as $id) { |
8ae90f85 | 203 | $names[] = CRM_Utils_Array::value($id, $grantTypes); |
204 | } | |
205 | } | |
206 | else { | |
207 | if (!empty($value)) { | |
208 | $names[] = $grantTypes[$value]; | |
209 | } | |
210 | } | |
6a488035 | 211 | |
8ae90f85 | 212 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_grant.grant_type_id', $op, $value, "Integer"); |
6a488035 | 213 | |
8ae90f85 | 214 | $query->_qill[$grouping][] = ts('Grant Type %2 %1', array(1 => implode(' ' . ts('or') . ' ', $names), 2 => $op)); |
6a488035 TO |
215 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; |
216 | ||
217 | return; | |
218 | ||
219 | case 'grant_status_id': | |
8ae90f85 | 220 | case 'grant_status': |
6a488035 | 221 | $grantStatus = CRM_Core_OptionGroup::values('grant_status'); |
8ae90f85 | 222 | if (is_array($value)) { |
223 | foreach ($value as $k => $v) { | |
224 | if ($v) { | |
225 | $val[] = $v; | |
226 | } | |
227 | } | |
228 | if (count($val) > 0) { | |
229 | // Overwrite $value so it works with an IN where statement. | |
230 | $op = 'IN'; | |
231 | $value = '(' . implode(',', $val) . ')'; | |
232 | } | |
233 | } | |
234 | if (!empty($val)) { | |
22e263ad | 235 | foreach ($val as $id) { |
8ae90f85 | 236 | $names[] = CRM_Utils_Array::value($id, $grantStatus); |
237 | } | |
238 | } | |
239 | else { | |
240 | if (!empty($value)) { | |
241 | $names[] = $grantStatus[$value]; | |
242 | } | |
243 | } | |
244 | ||
245 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_grant.status_id', $op, $value, "Integer"); | |
6a488035 | 246 | |
8ae90f85 | 247 | $query->_qill[$grouping][] = ts('Grant Status %2 %1', array(1 => implode(' ' . ts('or') . ' ', $names), 2 => $op)); |
6a488035 TO |
248 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; |
249 | ||
250 | return; | |
251 | ||
252 | case 'grant_report_received': | |
253 | ||
254 | if ($value == 1) { | |
255 | $yesNo = 'Yes'; | |
256 | $query->_where[$grouping][] = "civicrm_grant.grant_report_received $op $value"; | |
257 | } | |
258 | elseif ($value == 0) { | |
259 | $yesNo = 'No'; | |
260 | $query->_where[$grouping][] = "civicrm_grant.grant_report_received IS NULL"; | |
261 | } | |
262 | ||
263 | $query->_qill[$grouping][] = "Grant Report Received = $yesNo "; | |
264 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
265 | ||
266 | return; | |
267 | ||
268 | case 'grant_amount': | |
269 | case 'grant_amount_low': | |
270 | case 'grant_amount_high': | |
271 | $query->numberRangeBuilder($values, | |
272 | 'civicrm_grant', 'grant_amount', 'amount_total', 'Total Amount' | |
273 | ); | |
274 | } | |
275 | } | |
276 | ||
e0ef6999 | 277 | /** |
100fef9d | 278 | * @param string $name |
e0ef6999 EM |
279 | * @param $mode |
280 | * @param $side | |
281 | * | |
282 | * @return null|string | |
283 | */ | |
00be9182 | 284 | public static function from($name, $mode, $side) { |
6a488035 TO |
285 | $from = NULL; |
286 | switch ($name) { | |
287 | case 'civicrm_grant': | |
288 | $from = " $side JOIN civicrm_grant ON civicrm_grant.contact_id = contact_a.id "; | |
289 | break; | |
290 | ||
291 | case 'grant_status': | |
292 | $from .= " $side JOIN civicrm_option_group option_group_grant_status ON (option_group_grant_status.name = 'grant_status')"; | |
293 | $from .= " $side JOIN civicrm_option_value grant_status ON (civicrm_grant.status_id = grant_status.value AND option_group_grant_status.id = grant_status.option_group_id ) "; | |
294 | break; | |
295 | ||
296 | case 'grant_type': | |
297 | $from .= " $side JOIN civicrm_option_group option_group_grant_type ON (option_group_grant_type.name = 'grant_type')"; | |
298 | if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) { | |
299 | $from .= " INNER JOIN civicrm_option_value grant_type ON (civicrm_grant.grant_type_id = grant_type.value AND option_group_grant_type.id = grant_type.option_group_id ) "; | |
300 | } | |
301 | else { | |
302 | $from .= " $side JOIN civicrm_option_value grant_type ON (civicrm_grant.grant_type_id = grant_type.value AND option_group_grant_type.id = grant_type.option_group_id ) "; | |
303 | } | |
304 | break; | |
305 | ||
306 | case 'grant_note': | |
307 | $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_grant' AND | |
308 | civicrm_grant.id = civicrm_note.entity_id )"; | |
309 | break; | |
310 | } | |
311 | return $from; | |
312 | } | |
313 | ||
314 | /** | |
100fef9d | 315 | * Getter for the qill object |
6a488035 TO |
316 | * |
317 | * @return string | |
6a488035 | 318 | */ |
00be9182 | 319 | public function qill() { |
6a488035 TO |
320 | return (isset($this->_qill)) ? $this->_qill : ""; |
321 | } | |
322 | ||
e0ef6999 EM |
323 | /** |
324 | * @param $mode | |
325 | * @param bool $includeCustomFields | |
326 | * | |
327 | * @return array|null | |
328 | */ | |
73714296 TO |
329 | static function defaultReturnProperties( |
330 | $mode, | |
6a488035 TO |
331 | $includeCustomFields = TRUE |
332 | ) { | |
333 | $properties = NULL; | |
334 | if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) { | |
335 | $properties = array( | |
336 | 'contact_type' => 1, | |
337 | 'contact_sub_type' => 1, | |
338 | 'sort_name' => 1, | |
339 | 'grant_id' => 1, | |
340 | 'grant_type' => 1, | |
341 | 'grant_status' => 1, | |
342 | 'grant_amount_requested' => 1, | |
343 | 'grant_application_received_date' => 1, | |
344 | 'grant_report_received' => 1, | |
345 | 'grant_money_transfer_date' => 1, | |
346 | 'grant_note' => 1, | |
347 | ); | |
348 | } | |
349 | ||
350 | return $properties; | |
351 | } | |
352 | ||
353 | /** | |
100fef9d | 354 | * Add all the elements shared between grant search and advanaced search |
6a488035 | 355 | * |
6a488035 | 356 | * |
c490a46a | 357 | * @param CRM_Core_Form $form |
77b97be7 | 358 | * |
6a488035 TO |
359 | * @return void |
360 | * @static | |
361 | */ | |
00be9182 | 362 | public static function buildSearchForm(&$form) { |
6a488035 TO |
363 | |
364 | $grantType = CRM_Core_OptionGroup::values('grant_type'); | |
8ae90f85 | 365 | $form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE, |
366 | array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2') | |
6a488035 TO |
367 | ); |
368 | ||
369 | $grantStatus = CRM_Core_OptionGroup::values('grant_status'); | |
8ae90f85 | 370 | $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE, |
371 | array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2') | |
6a488035 TO |
372 | ); |
373 | ||
374 | $form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate')); | |
375 | $form->addDate('grant_application_received_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); | |
376 | ||
377 | $form->addElement('checkbox', 'grant_application_received_notset', ts(''), NULL); | |
378 | ||
379 | $form->addDate('grant_money_transfer_date_low', ts('Money Sent Date - From'), FALSE, array('formatType' => 'searchDate')); | |
380 | $form->addDate('grant_money_transfer_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); | |
381 | ||
382 | $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts(''), NULL); | |
383 | ||
384 | $form->addDate('grant_due_date_low', ts('Report Due Date - From'), FALSE, array('formatType' => 'searchDate')); | |
385 | $form->addDate('grant_due_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); | |
386 | ||
387 | $form->addElement('checkbox', 'grant_due_date_notset', ts(''), NULL); | |
388 | ||
389 | $form->addDate('grant_decision_date_low', ts('Grant Decision Date - From'), FALSE, array('formatType' => 'searchDate')); | |
390 | $form->addDate('grant_decision_date_high', ts('To'), FALSE, array('formatType' => 'searchDate')); | |
391 | ||
392 | $form->addElement('checkbox', 'grant_decision_date_notset', ts(''), NULL); | |
393 | ||
8a4f27dc | 394 | $form->addYesNo('grant_report_received', ts('Grant report received?'), TRUE); |
6a488035 TO |
395 | |
396 | $form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8)); | |
397 | $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money'); | |
398 | ||
399 | $form->add('text', 'grant_amount_high', ts('Maximum Amount'), array('size' => 8, 'maxlength' => 8)); | |
400 | $form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money'); | |
401 | ||
402 | // add all the custom searchable fields | |
403 | $grant = array('Grant'); | |
404 | $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $grant); | |
405 | if ($groupDetails) { | |
406 | $form->assign('grantGroupTree', $groupDetails); | |
407 | foreach ($groupDetails as $group) { | |
408 | foreach ($group['fields'] as $field) { | |
409 | $fieldId = $field['id']; | |
410 | $elementName = 'custom_' . $fieldId; | |
411 | CRM_Core_BAO_CustomField::addQuickFormElement($form, | |
412 | $elementName, | |
413 | $fieldId, | |
414 | FALSE, FALSE, TRUE | |
415 | ); | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
420 | $form->assign('validGrant', TRUE); | |
421 | } | |
422 | ||
e0ef6999 EM |
423 | /** |
424 | * @param $row | |
100fef9d | 425 | * @param int $id |
e0ef6999 | 426 | */ |
1afc557a TO |
427 | public static function searchAction(&$row, $id) { |
428 | } | |
6a488035 | 429 | |
e0ef6999 EM |
430 | /** |
431 | * @param $tables | |
432 | */ | |
1afc557a TO |
433 | public static function tableNames(&$tables) { |
434 | } | |
6a488035 | 435 | } |