65c343814713c770bc0b7563e3b081d4a19c90cb
[civicrm-core.git] / CRM / Grant / BAO / Query.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Grant_BAO_Query {
37 static function &getFields() {
38 $fields = array();
39 $fields = CRM_Grant_BAO_Grant::exportableFields();
40 return $fields;
41 }
42
43 /**
44 * build select for CiviGrant
45 *
46 * @param $query
47 *
48 * @return void
49 * @access public
50 */
51 static function select(&$query) {
52 if ($query->_mode & CRM_Contact_BAO_Query::MODE_GRANT) {
53 if (!empty($query->_returnProperties['grant_status_id'])) {
54 $query->_select['grant_status_id'] = 'grant_status.id as grant_status_id';
55 $query->_element['grant_status'] = 1;
56 $query->_tables['grant_status'] = $query->_whereTables['grant_status'] = 1;
57 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
58 }
59
60 if (!empty($query->_returnProperties['grant_status'])) {
61 $query->_select['grant_status'] = 'grant_status.label as grant_status';
62 $query->_element['grant_status'] = 1;
63 $query->_tables['grant_status'] = $query->_whereTables['grant_status'] = 1;
64 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
65 }
66
67 if (!empty($query->_returnProperties['grant_type_id'])) {
68 $query->_select['grant_type_id'] = 'grant_type.id as grant_type_id';
69 $query->_element['grant_type'] = 1;
70 $query->_tables['grant_type'] = $query->_whereTables['grant_type'] = 1;
71 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
72 }
73
74 if (!empty($query->_returnProperties['grant_type'])) {
75 $query->_select['grant_type'] = 'grant_type.label as grant_type';
76 $query->_element['grant_type'] = 1;
77 $query->_tables['grant_type'] = $query->_whereTables['grant_type'] = 1;
78 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
79 }
80
81 if (!empty($query->_returnProperties['grant_note'])) {
82 $query->_select['grant_note'] = "civicrm_note.note as grant_note";
83 $query->_element['grant_note'] = 1;
84 $query->_tables['grant_note'] = 1;
85 }
86 $query->_select['grant_amount_requested'] = 'civicrm_grant.amount_requested as grant_amount_requested';
87 $query->_select['grant_amount_granted'] = 'civicrm_grant.amount_granted as grant_amount_granted';
88 $query->_select['grant_amount_total'] = 'civicrm_grant.amount_total as grant_amount_total';
89 $query->_select['grant_application_received_date'] = 'civicrm_grant.application_received_date as grant_application_received_date ';
90 $query->_select['grant_report_received'] = 'civicrm_grant.grant_report_received as grant_report_received';
91 $query->_select['grant_money_transfer_date'] = 'civicrm_grant.money_transfer_date as grant_money_transfer_date';
92 $query->_element['grant_type_id'] = 1;
93 $query->_element['grant_status_id'] = 1;
94 $query->_tables['civicrm_grant'] = 1;
95 $query->_whereTables['civicrm_grant'] = 1;
96 }
97 }
98
99 /**
100 * Given a list of conditions in params generate the required
101 * where clause
102 *
103 * @param $query
104 *
105 * @return void
106 * @access public
107 */
108 static function where(&$query) {
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
120 static function whereClauseSingle(&$values, &$query) {
121 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
122 list($name, $op, $value, $grouping, $wildcard) = $values;
123 switch ($name) {
124 case 'grant_money_transfer_date_low':
125 case 'grant_money_transfer_date_high':
126 $query->dateQueryBuilder($values, 'civicrm_grant',
127 'grant_money_transfer_date', 'money_transfer_date',
128 'Money Transfer Date'
129 );
130 return;
131
132 case 'grant_money_transfer_date_notset':
133 $query->_where[$grouping][] = "civicrm_grant.money_transfer_date IS NULL";
134 $query->_qill[$grouping][] = ts("Grant Money Transfer Date is NULL");
135 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
136 return;
137
138 case 'grant_application_received_date_low':
139 case 'grant_application_received_date_high':
140 $query->dateQueryBuilder($values, 'civicrm_grant',
141 'grant_application_received_date',
142 'application_received_date', 'Application Received Date'
143 );
144 return;
145
146 case 'grant_application_received_notset':
147 $query->_where[$grouping][] = "civicrm_grant.application_received_date IS NULL";
148 $query->_qill[$grouping][] = ts("Grant Application Received Date is NULL");
149 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
150 return;
151
152 case 'grant_due_date_low':
153 case 'grant_due_date_high':
154 $query->dateQueryBuilder($values, 'civicrm_grant',
155 'grant_due_date',
156 'grant_due_date', 'Grant Due Date'
157 );
158 return;
159
160 case 'grant_due_date_notset':
161 $query->_where[$grouping][] = "civicrm_grant.grant_due_date IS NULL";
162 $query->_qill[$grouping][] = ts("Grant Due Date is NULL");
163 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
164 return;
165
166 case 'grant_decision_date_low':
167 case 'grant_decision_date_high':
168 $query->dateQueryBuilder($values, 'civicrm_grant',
169 'grant_decision_date',
170 'decision_date', 'Grant Decision Date'
171 );
172 return;
173
174 case 'grant_decision_date_notset':
175 $query->_where[$grouping][] = "civicrm_grant.decision_date IS NULL";
176 $query->_qill[$grouping][] = ts("Grant Decision Date is NULL");
177 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
178 return;
179
180 case 'grant_type_id':
181
182 $value = $strtolower(CRM_Core_DAO::escapeString(trim($value)));
183
184 $query->_where[$grouping][] = "civicrm_grant.grant_type_id $op '{$value}'";
185
186 $grantTypes = CRM_Core_OptionGroup::values('grant_type');
187 $value = $grantTypes[$value];
188 $query->_qill[$grouping][] = ts('Grant Type %2 %1', array(1 => $value, 2 => $op));
189 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
190
191 return;
192
193 case 'grant_status_id':
194
195 $value = $strtolower(CRM_Core_DAO::escapeString(trim($value)));
196
197 $query->_where[$grouping][] = "civicrm_grant.status_id $op '{$value}'";
198
199
200 $grantStatus = CRM_Core_OptionGroup::values('grant_status');
201 $value = $grantStatus[$value];
202
203 $query->_qill[$grouping][] = ts('Grant Status %2 %1', array(1 => $value, 2 => $op));
204 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
205
206 return;
207
208 case 'grant_report_received':
209
210 if ($value == 1) {
211 $yesNo = 'Yes';
212 $query->_where[$grouping][] = "civicrm_grant.grant_report_received $op $value";
213 }
214 elseif ($value == 0) {
215 $yesNo = 'No';
216 $query->_where[$grouping][] = "civicrm_grant.grant_report_received IS NULL";
217 }
218
219 $query->_qill[$grouping][] = "Grant Report Received = $yesNo ";
220 $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
221
222 return;
223
224 case 'grant_amount':
225 case 'grant_amount_low':
226 case 'grant_amount_high':
227 $query->numberRangeBuilder($values,
228 'civicrm_grant', 'grant_amount', 'amount_total', 'Total Amount'
229 );
230 }
231 }
232
233 static function from($name, $mode, $side) {
234 $from = NULL;
235 switch ($name) {
236 case 'civicrm_grant':
237 $from = " $side JOIN civicrm_grant ON civicrm_grant.contact_id = contact_a.id ";
238 break;
239
240 case 'grant_status':
241 $from .= " $side JOIN civicrm_option_group option_group_grant_status ON (option_group_grant_status.name = 'grant_status')";
242 $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 ) ";
243 break;
244
245 case 'grant_type':
246 $from .= " $side JOIN civicrm_option_group option_group_grant_type ON (option_group_grant_type.name = 'grant_type')";
247 if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) {
248 $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 ) ";
249 }
250 else {
251 $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 ) ";
252 }
253 break;
254
255 case 'grant_note':
256 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_grant' AND
257 civicrm_grant.id = civicrm_note.entity_id )";
258 break;
259 }
260 return $from;
261 }
262
263 /**
264 * getter for the qill object
265 *
266 * @return string
267 * @access public
268 */
269 function qill() {
270 return (isset($this->_qill)) ? $this->_qill : "";
271 }
272
273 static function defaultReturnProperties($mode,
274 $includeCustomFields = TRUE
275 ) {
276 $properties = NULL;
277 if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) {
278 $properties = array(
279 'contact_type' => 1,
280 'contact_sub_type' => 1,
281 'sort_name' => 1,
282 'grant_id' => 1,
283 'grant_type' => 1,
284 'grant_status' => 1,
285 'grant_amount_requested' => 1,
286 'grant_application_received_date' => 1,
287 'grant_report_received' => 1,
288 'grant_money_transfer_date' => 1,
289 'grant_note' => 1,
290 );
291 }
292
293 return $properties;
294 }
295
296 /**
297 * add all the elements shared between grant search and advanaced search
298 *
299 * @access public
300 *
301 * @param $form
302 *
303 * @return void
304 * @static
305 */
306 static function buildSearchForm(&$form) {
307
308 $grantType = CRM_Core_OptionGroup::values('grant_type');
309 $form->add('select', 'grant_type_id', ts('Grant Type'),
310 array('' => ts('- any -')) + $grantType,
311 FALSE, array('class' => 'crm-select2')
312 );
313
314 $grantStatus = CRM_Core_OptionGroup::values('grant_status');
315 $form->add('select', 'grant_status_id', ts('Grant Status'),
316 array('' => ts('- any -')) + $grantStatus,
317 FALSE, array('class' => 'crm-select2')
318 );
319
320 $form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate'));
321 $form->addDate('grant_application_received_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
322
323 $form->addElement('checkbox', 'grant_application_received_notset', ts(''), NULL);
324
325 $form->addDate('grant_money_transfer_date_low', ts('Money Sent Date - From'), FALSE, array('formatType' => 'searchDate'));
326 $form->addDate('grant_money_transfer_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
327
328 $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts(''), NULL);
329
330 $form->addDate('grant_due_date_low', ts('Report Due Date - From'), FALSE, array('formatType' => 'searchDate'));
331 $form->addDate('grant_due_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
332
333 $form->addElement('checkbox', 'grant_due_date_notset', ts(''), NULL);
334
335 $form->addDate('grant_decision_date_low', ts('Grant Decision Date - From'), FALSE, array('formatType' => 'searchDate'));
336 $form->addDate('grant_decision_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
337
338 $form->addElement('checkbox', 'grant_decision_date_notset', ts(''), NULL);
339
340 $form->addYesNo('grant_report_received', ts('Grant report received?'), TRUE);
341
342 $form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8));
343 $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
344
345 $form->add('text', 'grant_amount_high', ts('Maximum Amount'), array('size' => 8, 'maxlength' => 8));
346 $form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
347
348 // add all the custom searchable fields
349 $grant = array('Grant');
350 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $grant);
351 if ($groupDetails) {
352 $form->assign('grantGroupTree', $groupDetails);
353 foreach ($groupDetails as $group) {
354 foreach ($group['fields'] as $field) {
355 $fieldId = $field['id'];
356 $elementName = 'custom_' . $fieldId;
357 CRM_Core_BAO_CustomField::addQuickFormElement($form,
358 $elementName,
359 $fieldId,
360 FALSE, FALSE, TRUE
361 );
362 }
363 }
364 }
365
366 $form->assign('validGrant', TRUE);
367 }
368
369 static function searchAction(&$row, $id) {}
370
371 static function tableNames(&$tables) {}
372 }
373