Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
86a0d21e | 35 | class CRM_Grant_BAO_Query extends CRM_Core_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) { |
5eec97d5 | 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 | } | |
6a488035 | 66 | |
5eec97d5 | 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 | } | |
6a488035 | 73 | |
5eec97d5 | 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 | } | |
6a488035 | 80 | |
5eec97d5 | 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 | } | |
6a488035 | 86 | |
5eec97d5 | 87 | if ($query->_mode & CRM_Contact_BAO_Query::MODE_GRANT) { |
6a488035 TO |
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 | /** | |
fe482240 | 102 | * Given a list of conditions in params generate the required. |
6a488035 TO |
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 | |
e2123607 | 123 | * @param \CRM_Contact_BAO_Query $query |
e0ef6999 | 124 | */ |
00be9182 | 125 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
126 | list($name, $op, $value, $grouping, $wildcard) = $values; |
127 | switch ($name) { | |
128 | case 'grant_money_transfer_date_low': | |
129 | case 'grant_money_transfer_date_high': | |
130 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
131 | 'grant_money_transfer_date', 'money_transfer_date', | |
132 | 'Money Transfer Date' | |
133 | ); | |
134 | return; | |
135 | ||
136 | case 'grant_money_transfer_date_notset': | |
137 | $query->_where[$grouping][] = "civicrm_grant.money_transfer_date IS NULL"; | |
138 | $query->_qill[$grouping][] = ts("Grant Money Transfer Date is NULL"); | |
139 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
140 | return; | |
141 | ||
142 | case 'grant_application_received_date_low': | |
143 | case 'grant_application_received_date_high': | |
144 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
145 | 'grant_application_received_date', | |
146 | 'application_received_date', 'Application Received Date' | |
147 | ); | |
148 | return; | |
149 | ||
e2123607 | 150 | case 'grant_application_received_date_notset': |
6a488035 TO |
151 | $query->_where[$grouping][] = "civicrm_grant.application_received_date IS NULL"; |
152 | $query->_qill[$grouping][] = ts("Grant Application Received Date is NULL"); | |
153 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
154 | return; | |
155 | ||
156 | case 'grant_due_date_low': | |
157 | case 'grant_due_date_high': | |
158 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
159 | 'grant_due_date', | |
cd67b950 | 160 | 'grant_due_date', ts('Grant Due Date') |
6a488035 TO |
161 | ); |
162 | return; | |
163 | ||
164 | case 'grant_due_date_notset': | |
165 | $query->_where[$grouping][] = "civicrm_grant.grant_due_date IS NULL"; | |
166 | $query->_qill[$grouping][] = ts("Grant Due Date is NULL"); | |
167 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
168 | return; | |
169 | ||
170 | case 'grant_decision_date_low': | |
171 | case 'grant_decision_date_high': | |
172 | $query->dateQueryBuilder($values, 'civicrm_grant', | |
173 | 'grant_decision_date', | |
cd67b950 | 174 | 'decision_date', ts('Grant Decision Date') |
6a488035 TO |
175 | ); |
176 | return; | |
177 | ||
178 | case 'grant_decision_date_notset': | |
179 | $query->_where[$grouping][] = "civicrm_grant.decision_date IS NULL"; | |
180 | $query->_qill[$grouping][] = ts("Grant Decision Date is NULL"); | |
181 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; | |
182 | return; | |
183 | ||
184 | case 'grant_type_id': | |
8ae90f85 | 185 | case 'grant_type': |
6a488035 | 186 | case 'grant_status_id': |
8ae90f85 | 187 | case 'grant_status': |
a0cbe4fa | 188 | |
189 | if (strstr($name, 'type')) { | |
190 | $name = 'grant_type_id'; | |
cd67b950 | 191 | $label = ts('Grant Type(s)'); |
8ae90f85 | 192 | } |
193 | else { | |
a0cbe4fa | 194 | $name = 'status_id'; |
cd67b950 | 195 | $label = ts('Grant Status(s)'); |
8ae90f85 | 196 | } |
197 | ||
a0cbe4fa | 198 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_grant.$name", $op, $value, "Integer"); |
6a488035 | 199 | |
a0cbe4fa | 200 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Grant_DAO_Grant', $name, $value, $op); |
201 | $query->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal)); | |
6a488035 TO |
202 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; |
203 | ||
204 | return; | |
205 | ||
206 | case 'grant_report_received': | |
207 | ||
208 | if ($value == 1) { | |
e60e0c01 | 209 | $yesNo = ts('Yes'); |
6a488035 TO |
210 | $query->_where[$grouping][] = "civicrm_grant.grant_report_received $op $value"; |
211 | } | |
212 | elseif ($value == 0) { | |
e60e0c01 | 213 | $yesNo = ts('No'); |
6a488035 TO |
214 | $query->_where[$grouping][] = "civicrm_grant.grant_report_received IS NULL"; |
215 | } | |
fb3bcb77 | 216 | |
217 | $query->_qill[$grouping][] = ts('Grant Report Received = %1', array(1 => $yesNo)); | |
6a488035 TO |
218 | $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1; |
219 | ||
220 | return; | |
221 | ||
222 | case 'grant_amount': | |
223 | case 'grant_amount_low': | |
224 | case 'grant_amount_high': | |
225 | $query->numberRangeBuilder($values, | |
226 | 'civicrm_grant', 'grant_amount', 'amount_total', 'Total Amount' | |
227 | ); | |
228 | } | |
229 | } | |
230 | ||
e0ef6999 | 231 | /** |
100fef9d | 232 | * @param string $name |
e0ef6999 EM |
233 | * @param $mode |
234 | * @param $side | |
235 | * | |
236 | * @return null|string | |
237 | */ | |
00be9182 | 238 | public static function from($name, $mode, $side) { |
6a488035 TO |
239 | $from = NULL; |
240 | switch ($name) { | |
241 | case 'civicrm_grant': | |
242 | $from = " $side JOIN civicrm_grant ON civicrm_grant.contact_id = contact_a.id "; | |
243 | break; | |
244 | ||
245 | case 'grant_status': | |
246 | $from .= " $side JOIN civicrm_option_group option_group_grant_status ON (option_group_grant_status.name = 'grant_status')"; | |
247 | $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 ) "; | |
248 | break; | |
249 | ||
250 | case 'grant_type': | |
251 | $from .= " $side JOIN civicrm_option_group option_group_grant_type ON (option_group_grant_type.name = 'grant_type')"; | |
252 | if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) { | |
253 | $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 ) "; | |
254 | } | |
255 | else { | |
256 | $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 ) "; | |
257 | } | |
258 | break; | |
259 | ||
260 | case 'grant_note': | |
261 | $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_grant' AND | |
262 | civicrm_grant.id = civicrm_note.entity_id )"; | |
263 | break; | |
264 | } | |
265 | return $from; | |
266 | } | |
267 | ||
e0ef6999 EM |
268 | /** |
269 | * @param $mode | |
270 | * @param bool $includeCustomFields | |
271 | * | |
272 | * @return array|null | |
273 | */ | |
317fceb4 | 274 | public static function defaultReturnProperties( |
73714296 | 275 | $mode, |
6a488035 TO |
276 | $includeCustomFields = TRUE |
277 | ) { | |
278 | $properties = NULL; | |
279 | if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) { | |
280 | $properties = array( | |
281 | 'contact_type' => 1, | |
282 | 'contact_sub_type' => 1, | |
283 | 'sort_name' => 1, | |
284 | 'grant_id' => 1, | |
285 | 'grant_type' => 1, | |
286 | 'grant_status' => 1, | |
287 | 'grant_amount_requested' => 1, | |
288 | 'grant_application_received_date' => 1, | |
289 | 'grant_report_received' => 1, | |
290 | 'grant_money_transfer_date' => 1, | |
291 | 'grant_note' => 1, | |
292 | ); | |
293 | } | |
294 | ||
295 | return $properties; | |
296 | } | |
297 | ||
0bcac7e7 | 298 | /** |
299 | * Get the metadata for fields to be included on the activity search form. | |
300 | */ | |
301 | public static function getSearchFieldMetadata() { | |
302 | $fields = [ | |
303 | 'grant_report_received', | |
304 | 'grant_application_received_date', | |
305 | 'grant_decision_date', | |
306 | 'grant_money_transfer_date', | |
307 | 'grant_due_date', | |
308 | ]; | |
309 | $metadata = civicrm_api3('Grant', 'getfields', [])['values']; | |
310 | return array_intersect_key($metadata, array_flip($fields)); | |
311 | } | |
312 | ||
e2123607 | 313 | /** |
314 | * Transitional function for specifying which fields the tpl can iterate through. | |
315 | */ | |
316 | public static function getTemplateHandlableSearchFields() { | |
317 | return array_diff_key(self::getSearchFieldMetadata(), ['grant_report_received' => 1]); | |
318 | } | |
319 | ||
6a488035 | 320 | /** |
fe482240 | 321 | * Add all the elements shared between grant search and advanaced search. |
6a488035 | 322 | * |
6a488035 | 323 | * |
0bcac7e7 | 324 | * @param \CRM_Grant_Form_Search $form |
77b97be7 | 325 | * |
6a488035 | 326 | * @return void |
6a488035 | 327 | */ |
00be9182 | 328 | public static function buildSearchForm(&$form) { |
6a488035 TO |
329 | |
330 | $grantType = CRM_Core_OptionGroup::values('grant_type'); | |
0bcac7e7 | 331 | $form->addSearchFieldMetadata(['Grant' => self::getSearchFieldMetadata()]); |
332 | $form->addFormFieldsFromMetadata(); | |
e2123607 | 333 | $form->assign('grantSearchFields', self::getTemplateHandlableSearchFields()); |
8ae90f85 | 334 | $form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE, |
335 | array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2') | |
6a488035 TO |
336 | ); |
337 | ||
338 | $grantStatus = CRM_Core_OptionGroup::values('grant_status'); | |
8ae90f85 | 339 | $form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE, |
340 | array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2') | |
6a488035 | 341 | ); |
e2123607 | 342 | $form->addElement('checkbox', 'grant_application_received_date_notset', ts('Date is not set'), NULL); |
2f00bb9c | 343 | $form->addElement('checkbox', 'grant_money_transfer_date_notset', ts('Date is not set'), NULL); |
2f00bb9c | 344 | $form->addElement('checkbox', 'grant_due_date_notset', ts('Date is not set'), NULL); |
2f00bb9c | 345 | $form->addElement('checkbox', 'grant_decision_date_notset', ts('Date is not set'), NULL); |
6a488035 | 346 | |
6a488035 TO |
347 | $form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8)); |
348 | $form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money'); | |
349 | ||
350 | $form->add('text', 'grant_amount_high', ts('Maximum Amount'), array('size' => 8, 'maxlength' => 8)); | |
351 | $form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money'); | |
352 | ||
86a0d21e | 353 | self::addCustomFormFields($form, array('Grant')); |
6a488035 TO |
354 | |
355 | $form->assign('validGrant', TRUE); | |
356 | } | |
357 | ||
6a488035 | 358 | } |