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