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