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