Merge pull request #15208 from civicrm/5.17
[civicrm-core.git] / CRM / Pledge / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Pledge_BAO_Query extends CRM_Core_BAO_Query {
34
35 /**
36 * Get pledge fields.
37 *
38 * @param bool $checkPermission
39 *
40 * @return array
41 */
42 public static function getFields($checkPermission = TRUE) {
43 return CRM_Pledge_BAO_Pledge::exportableFields($checkPermission);
44 }
45
46 /**
47 * Build select for Pledge.
48 *
49 * @param CRM_Contact_BAO_Query $query
50 */
51 public static function select(&$query) {
52
53 $statusId = implode(',', array_keys(CRM_Core_PseudoConstant::accountOptionValues("contribution_status", NULL, " AND v.name IN ('Pending', 'Overdue')")));
54 if (($query->_mode & CRM_Contact_BAO_Query::MODE_PLEDGE) || !empty($query->_returnProperties['pledge_id'])) {
55 $query->_select['pledge_id'] = 'civicrm_pledge.id as pledge_id';
56 $query->_element['pledge_id'] = 1;
57 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
58 }
59
60 // add pledge select
61 if (!empty($query->_returnProperties['pledge_amount'])) {
62 $query->_select['pledge_amount'] = 'civicrm_pledge.amount as pledge_amount';
63 $query->_element['pledge_amount'] = 1;
64 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
65 }
66
67 if (!empty($query->_returnProperties['pledge_original_installment_amount'])) {
68 $query->_select['pledge_original_installment_amount'] = 'civicrm_pledge.original_installment_amount as pledge_original_installment_amount';
69 $query->_element['pledge_original_installment_amount'] = 1;
70 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
71 }
72
73 if (!empty($query->_returnProperties['installments'])) {
74 $query->_select['installments'] = 'civicrm_pledge.installments as installments';
75 $query->_element['installments'] = 1;
76 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
77 }
78
79 if (!empty($query->_returnProperties['pledge_create_date'])) {
80 $query->_select['pledge_create_date'] = 'civicrm_pledge.create_date as pledge_create_date';
81 $query->_element['pledge_create_date'] = 1;
82 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
83 }
84
85 if (!empty($query->_returnProperties['pledge_end_date'])) {
86 $query->_select['pledge_end_date'] = 'civicrm_pledge.end_date as pledge_end_date';
87 $query->_element['pledge_end_date'] = 1;
88 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
89 }
90
91 if (!empty($query->_returnProperties['pledge_start_date'])) {
92 $query->_select['pledge_start_date'] = 'civicrm_pledge.start_date as pledge_start_date';
93 $query->_element['pledge_start_date'] = 1;
94 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
95 }
96
97 if (!empty($query->_returnProperties['pledge_status_id'])) {
98 $query->_select['pledge_status_id'] = 'pledge_status.value as pledge_status_id';
99 $query->_element['pledge_status'] = 1;
100 $query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
101 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
102 }
103
104 if (!empty($query->_returnProperties['pledge_status'])) {
105 $query->_select['pledge_status'] = 'pledge_status.label as pledge_status';
106 $query->_element['pledge_status'] = 1;
107 $query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
108 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
109 }
110
111 if (!empty($query->_returnProperties['pledge_total_paid'])) {
112 $query->_select['pledge_total_paid'] = ' (SELECT sum(civicrm_pledge_payment.actual_amount) FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id = 1 ) as pledge_total_paid';
113 $query->_element['pledge_total_paid'] = 1;
114 }
115
116 if (!empty($query->_returnProperties['pledge_next_pay_date'])) {
117 $query->_select['pledge_next_pay_date'] = " (SELECT civicrm_pledge_payment.scheduled_date FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id IN ({$statusId}) ORDER BY civicrm_pledge_payment.scheduled_date ASC LIMIT 0, 1) as pledge_next_pay_date";
118 $query->_element['pledge_next_pay_date'] = 1;
119 }
120
121 if (!empty($query->_returnProperties['pledge_next_pay_amount'])) {
122 $query->_select['pledge_next_pay_amount'] = " (SELECT civicrm_pledge_payment.scheduled_amount FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id IN ({$statusId}) ORDER BY civicrm_pledge_payment.scheduled_date ASC LIMIT 0, 1) as pledge_next_pay_amount";
123 $query->_element['pledge_next_pay_amount'] = 1;
124
125 $query->_select['pledge_outstanding_amount'] = " (SELECT sum(civicrm_pledge_payment.scheduled_amount) FROM civicrm_pledge_payment WHERE civicrm_pledge_payment.pledge_id = civicrm_pledge.id AND civicrm_pledge_payment.status_id = 6 ) as pledge_outstanding_amount";
126 $query->_element['pledge_outstanding_amount'] = 1;
127 }
128
129 if (!empty($query->_returnProperties['pledge_financial_type'])) {
130 $query->_select['pledge_financial_type'] = "(SELECT civicrm_financial_type.name FROM civicrm_financial_type WHERE civicrm_financial_type.id = civicrm_pledge.financial_type_id) as pledge_financial_type";
131 $query->_element['pledge_financial_type'] = 1;
132 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
133 }
134
135 if (!empty($query->_returnProperties['pledge_contribution_page_id'])) {
136 $query->_select['pledge_contribution_page_id'] = 'civicrm_pledge.contribution_page_id as pledge_contribution_page_id';
137 $query->_element['pledge_contribution_page_id'] = 1;
138 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
139 }
140
141 if (!empty($query->_returnProperties['pledge_payment_id'])) {
142 $query->_select['pledge_payment_id'] = 'civicrm_pledge_payment.id as pledge_payment_id';
143 $query->_element['pledge_payment_id'] = 1;
144 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
145 }
146
147 if (!empty($query->_returnProperties['pledge_payment_scheduled_amount'])) {
148 $query->_select['pledge_payment_scheduled_amount'] = 'civicrm_pledge_payment.scheduled_amount as pledge_payment_scheduled_amount';
149 $query->_element['pledge_payment_scheduled_amount'] = 1;
150 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
151 }
152
153 if (!empty($query->_returnProperties['pledge_payment_scheduled_date'])) {
154 $query->_select['pledge_payment_scheduled_date'] = 'civicrm_pledge_payment.scheduled_date as pledge_payment_scheduled_date';
155 $query->_element['pledge_payment_scheduled_date'] = 1;
156 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
157 }
158
159 if (!empty($query->_returnProperties['pledge_payment_paid_amount'])) {
160 $query->_select['pledge_payment_paid_amount'] = 'civicrm_pledge_payment.actual_amount as pledge_payment_paid_amount';
161 $query->_element['pledge_payment_paid_amount'] = 1;
162 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
163 }
164
165 if (!empty($query->_returnProperties['pledge_payment_paid_date'])) {
166 $query->_select['pledge_payment_paid_date'] = 'payment_contribution.receive_date as pledge_payment_paid_date';
167 $query->_element['pledge_payment_paid_date'] = 1;
168 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
169 $query->_tables['payment_contribution'] = $query->_whereTables['payment_contribution'] = 1;
170 }
171
172 if (!empty($query->_returnProperties['pledge_payment_reminder_date'])) {
173 $query->_select['pledge_payment_reminder_date'] = 'civicrm_pledge_payment.reminder_date as pledge_payment_reminder_date';
174 $query->_element['pledge_payment_reminder_date'] = 1;
175 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
176 }
177
178 if (!empty($query->_returnProperties['pledge_payment_reminder_count'])) {
179 $query->_select['pledge_payment_reminder_count'] = 'civicrm_pledge_payment.reminder_count as pledge_payment_reminder_count';
180 $query->_element['pledge_payment_reminder_count'] = 1;
181 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
182 }
183
184 if (!empty($query->_returnProperties['pledge_payment_status_id'])) {
185 $query->_select['pledge_payment_status_id'] = 'payment_status.name as pledge_payment_status_id';
186 $query->_element['pledge_payment_status_id'] = 1;
187 $query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
188 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
189 }
190
191 if (!empty($query->_returnProperties['pledge_payment_status'])) {
192 $query->_select['pledge_payment_status'] = 'payment_status.label as pledge_payment_status';
193 $query->_element['pledge_payment_status'] = 1;
194 $query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
195 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
196 }
197
198 if (!empty($query->_returnProperties['pledge_frequency_interval'])) {
199 $query->_select['pledge_frequency_interval'] = 'civicrm_pledge.frequency_interval as pledge_frequency_interval';
200 $query->_element['pledge_frequency_interval'] = 1;
201 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
202 }
203
204 if (!empty($query->_returnProperties['pledge_frequency_unit'])) {
205 $query->_select['pledge_frequency_unit'] = 'civicrm_pledge.frequency_unit as pledge_frequency_unit';
206 $query->_element['pledge_frequency_unit'] = 1;
207 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
208 }
209
210 if (!empty($query->_returnProperties['pledge_is_test'])) {
211 $query->_select['pledge_is_test'] = 'civicrm_pledge.is_test as pledge_is_test';
212 $query->_element['pledge_is_test'] = 1;
213 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
214 }
215 if (!empty($query->_returnProperties['pledge_campaign_id'])) {
216 $query->_select['pledge_campaign_id'] = 'civicrm_pledge.campaign_id as pledge_campaign_id';
217 $query->_element['pledge_campaign_id'] = 1;
218 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
219 }
220
221 if (!empty($query->_returnProperties['pledge_currency'])) {
222 $query->_select['pledge_currency'] = 'civicrm_pledge.currency as pledge_currency';
223 $query->_element['pledge_currency'] = 1;
224 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
225 }
226 }
227
228 /**
229 * @param $query
230 */
231 public static function where(&$query) {
232 $grouping = NULL;
233 foreach (array_keys($query->_params) as $id) {
234 if (empty($query->_params[$id][0])) {
235 continue;
236 }
237 if (substr($query->_params[$id][0], 0, 7) == 'pledge_') {
238 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
239 $query->_useDistinct = TRUE;
240 }
241 $grouping = $query->_params[$id][3];
242 self::whereClauseSingle($query->_params[$id], $query);
243 }
244 }
245 }
246
247 /**
248 * Get where clause for field.
249 *
250 * @todo most of this could be replaced by using metadata.
251 *
252 * @param array $values
253 * @param \CRM_Contact_BAO_Query $query
254 *
255 * @throws \CRM_Core_Exception
256 */
257 public static function whereClauseSingle(&$values, &$query) {
258 if ($query->buildDateRangeQuery($values)) {
259 // @todo - move this to Contact_Query in or near the call to
260 // $this->buildRelativeDateQuery($values);
261 return;
262 }
263 list($name, $op, $value, $grouping, $wildcard) = $values;
264
265 switch ($name) {
266 case 'pledge_amount':
267 case 'pledge_amount_low':
268 case 'pledge_amount_high':
269 // process min/max amount
270 $query->numberRangeBuilder($values,
271 'civicrm_pledge', 'pledge_amount', 'amount', 'Pledge Amount'
272 );
273 return;
274
275 case 'pledge_installments_low':
276 case 'pledge_installments_high':
277 // process min/max amount
278 $query->numberRangeBuilder($values,
279 'civicrm_pledge', 'pledge_installments', 'installments', 'Number of Installments'
280 );
281 return;
282
283 case 'pledge_acknowledge_date_is_not_null':
284 if ($value) {
285 $op = "IS NOT NULL";
286 $query->_qill[$grouping][] = ts('Pledge Acknowledgement Sent');
287 }
288 else {
289 $op = "IS NULL";
290 $query->_qill[$grouping][] = ts('Pledge Acknowledgement Not Sent');
291 }
292 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.acknowledge_date", $op);
293 return;
294
295 case 'pledge_payment_status_id':
296 case 'pledge_status_id':
297 if ($name == 'pledge_status_id') {
298 $tableName = 'civicrm_pledge';
299 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
300 $label = "Pledge Status";
301 $qillDAO = 'CRM_Pledge_DAO_Pledge';
302 $qillField = 'status_id';
303 }
304 else {
305 $tableName = 'civicrm_pledge_payment';
306 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
307 $label = "Pledge Payment Status";
308 $qillDAO = 'CRM_Contribute_DAO_Contribution';
309 $qillField = 'contribution_status_id';
310 }
311 $name = 'status_id';
312 if (!empty($value) && is_array($value) && !in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
313 $value = ['IN' => $value];
314 }
315
316 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$tableName.$name",
317 $op,
318 $value,
319 'Integer'
320 );
321 list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($qillDAO, $qillField, $value, $op);
322 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $label, 2 => $qillop, 3 => $qillVal]);
323 return;
324
325 case 'pledge_test':
326 case 'pledge_is_test':
327 // We dont want to include all tests for sql OR CRM-7827
328 if (!$value || $query->getOperator() != 'OR') {
329 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.is_test',
330 $op,
331 $value,
332 'Boolean'
333 );
334 if ($value) {
335 $query->_qill[$grouping][] = ts('Pledge is a Test');
336 }
337 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
338 }
339 return;
340
341 case 'pledge_financial_type_id':
342 $type = CRM_Contribute_PseudoConstant::financialType($value);
343 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.financial_type_id',
344 $op,
345 $value,
346 'Integer'
347 );
348 $query->_qill[$grouping][] = ts('Financial Type - %1', [1 => $type]);
349 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
350 return;
351
352 case 'pledge_contribution_page_id':
353 $page = CRM_Contribute_PseudoConstant::contributionPage($value);
354 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.contribution_page_id',
355 $op,
356 $value,
357 'Integer'
358 );
359 $query->_qill[$grouping][] = ts('Financial Page - %1', [1 => $page]);
360 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
361 return;
362
363 case 'pledge_id':
364 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.id",
365 $op,
366 $value,
367 "Integer"
368 );
369 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
370 return;
371
372 case 'pledge_frequency_interval':
373 $query->_where[$grouping][] = "civicrm_pledge.frequency_interval $op $value";
374 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
375 return;
376
377 case 'pledge_frequency_unit':
378 $query->_where[$grouping][] = "civicrm_pledge.frequency_unit $op $value";
379 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
380 return;
381
382 case 'pledge_contact_id':
383 case 'pledge_campaign_id':
384 $name = str_replace('pledge_', '', $name);
385 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_pledge.$name", $op, $value, 'Integer');
386 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Pledge_DAO_Pledge', $name, $value, $op);
387 $label = ($name == 'campaign_id') ? 'Campaign' : 'Contact ID';
388 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $label, 2 => $op, 3 => $value]);
389 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
390 return;
391 }
392 }
393
394 /**
395 * From clause.
396 *
397 * @param string $name
398 * @param string $mode
399 * @param string $side
400 *
401 * @return null|string
402 */
403 public static function from($name, $mode, $side) {
404 $from = NULL;
405
406 switch ($name) {
407 case 'civicrm_pledge':
408 $from = " $side JOIN civicrm_pledge ON civicrm_pledge.contact_id = contact_a.id ";
409 break;
410
411 case 'pledge_status':
412 $from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'pledge_status')";
413 $from .= " $side JOIN civicrm_option_value pledge_status ON (civicrm_pledge.status_id = pledge_status.value AND option_group_pledge_status.id = pledge_status.option_group_id ) ";
414 break;
415
416 case 'pledge_financial_type':
417 $from .= " $side JOIN civicrm_financial_type ON civicrm_pledge.financial_type_id = civicrm_financial_type.id ";
418 break;
419
420 case 'civicrm_pledge_payment':
421 $from .= " $side JOIN civicrm_pledge_payment ON civicrm_pledge_payment.pledge_id = civicrm_pledge.id ";
422 break;
423
424 case 'payment_contribution':
425 $from .= " $side JOIN civicrm_contribution payment_contribution ON civicrm_pledge_payment.contribution_id = payment_contribution.id ";
426 break;
427
428 case 'payment_status':
429 $from .= " $side JOIN civicrm_option_group option_group_payment_status ON (option_group_payment_status.name = 'contribution_status')";
430 $from .= " $side JOIN civicrm_option_value payment_status ON (civicrm_pledge_payment.status_id = payment_status.value AND option_group_payment_status.id = payment_status.option_group_id ) ";
431 break;
432 }
433
434 return $from;
435 }
436
437 /**
438 * Ideally this function should include fields that are displayed in the selector.
439 *
440 * @param int $mode
441 * @param bool $includeCustomFields
442 *
443 * @return array|null
444 */
445 public static function defaultReturnProperties(
446 $mode,
447 $includeCustomFields = TRUE
448 ) {
449 $properties = NULL;
450
451 if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
452 $properties = [
453 'contact_type' => 1,
454 'contact_sub_type' => 1,
455 'sort_name' => 1,
456 'display_name' => 1,
457 'pledge_id' => 1,
458 'pledge_amount' => 1,
459 'pledge_total_paid' => 1,
460 'pledge_create_date' => 1,
461 'pledge_start_date' => 1,
462 'pledge_next_pay_date' => 1,
463 'pledge_next_pay_amount' => 1,
464 'pledge_status' => 1,
465 'pledge_status_id' => 1,
466 'pledge_is_test' => 1,
467 'pledge_contribution_page_id' => 1,
468 'pledge_financial_type' => 1,
469 'pledge_frequency_interval' => 1,
470 'pledge_frequency_unit' => 1,
471 'pledge_currency' => 1,
472 'pledge_campaign_id' => 1,
473 ];
474 }
475 return $properties;
476 }
477
478 /**
479 * This includes any extra fields that might need for export etc.
480 *
481 * @param string $mode
482 *
483 * @return array|null
484 */
485 public static function extraReturnProperties($mode) {
486 $properties = NULL;
487
488 if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
489 $properties = [
490 'pledge_balance_amount' => 1,
491 'pledge_payment_id' => 1,
492 'pledge_payment_scheduled_amount' => 1,
493 'pledge_payment_scheduled_date' => 1,
494 'pledge_payment_paid_amount' => 1,
495 'pledge_payment_paid_date' => 1,
496 'pledge_payment_reminder_date' => 1,
497 'pledge_payment_reminder_count' => 1,
498 'pledge_payment_status_id' => 1,
499 'pledge_payment_status' => 1,
500 ];
501
502 // also get all the custom pledge properties
503 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Pledge');
504 if (!empty($fields)) {
505 foreach ($fields as $name => $dontCare) {
506 $properties[$name] = 1;
507 }
508 }
509 }
510 return $properties;
511 }
512
513 /**
514 * Get the metadata for fields to be included on the grant search form.
515 *
516 * @throws \CiviCRM_API3_Exception
517 */
518 public static function getSearchFieldMetadata() {
519 $fields = [
520 'pledge_status_id',
521 'pledge_start_date',
522 'pledge_end_date',
523 'pledge_create_date',
524 ];
525 $metadata = civicrm_api3('Pledge', 'getfields', [])['values'];
526 return array_intersect_key($metadata, array_flip($fields));
527 }
528
529 /**
530 * Get the metadata for fields to be included on the grant search form.
531 *
532 * @throws \CiviCRM_API3_Exception
533 */
534 public static function getPledgePaymentSearchFieldMetadata() {
535 $fields = [
536 'pledge_payment_scheduled_date',
537 ];
538 $metadata = civicrm_api3('PledgePayment', 'getfields', [])['values'];
539 return array_intersect_key($metadata, array_flip($fields));
540 }
541
542 /**
543 * Build the search for for pledges.
544 *
545 * @param CRM_Pledge_Form_Search|\CRM_Contact_Form_Search_Advanced $form
546 *
547 * @throws \CRM_Core_Exception
548 * @throws \CiviCRM_API3_Exception
549 */
550 public static function buildSearchForm(&$form) {
551 // pledge related dates
552 $form->addSearchFieldMetadata(['Pledge' => self::getSearchFieldMetadata()]);
553 $form->addSearchFieldMetadata(['PledgePayment' => self::getPledgePaymentSearchFieldMetadata()]);
554 $form->addFormFieldsFromMetadata();
555
556 $form->addYesNo('pledge_test', ts('Pledge is a Test?'), TRUE);
557 $form->add('text', 'pledge_amount_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
558 $form->addRule('pledge_amount_low', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('9.99', ' ')]), 'money');
559
560 $form->add('text', 'pledge_amount_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
561 $form->addRule('pledge_amount_high', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('99.99', ' ')]), 'money');
562
563 $form->addYesNo('pledge_acknowledge_date_is_not_null', ts('Acknowledgement sent?'), TRUE);
564
565 $form->add('text', 'pledge_installments_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
566 $form->addRule('pledge_installments_low', ts('Please enter a number'), 'integer');
567
568 $form->add('text', 'pledge_installments_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
569 $form->addRule('pledge_installments_high', ts('Please enter number.'), 'integer');
570
571 $form->add('select', 'pledge_payment_status_id',
572 ts('Pledge Payment Status'), CRM_Pledge_BAO_PledgePayment::buildOptions('status_id'),
573 FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple']
574 );
575
576 $form->add('select', 'pledge_financial_type_id',
577 ts('Financial Type'),
578 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::financialType(),
579 FALSE, ['class' => 'crm-select2']
580 );
581
582 $form->add('select', 'pledge_contribution_page_id',
583 ts('Contribution Page'),
584 ['' => ts('- any -')] + CRM_Contribute_PseudoConstant::contributionPage(),
585 FALSE, ['class' => 'crm-select2']
586 );
587
588 // add fields for pledge frequency
589 $form->add('text', 'pledge_frequency_interval', ts('Every'), ['size' => 8, 'maxlength' => 8]);
590 $form->addRule('pledge_frequency_interval', ts('Please enter valid Pledge Frequency Interval'), 'integer');
591 $frequencies = CRM_Core_OptionGroup::values('recur_frequency_units');
592 foreach ($frequencies as $val => $label) {
593 $freqUnitsDisplay["'{$val}'"] = ts('%1(s)', [1 => $label]);
594 }
595
596 $form->add('select', 'pledge_frequency_unit',
597 ts('Pledge Frequency'),
598 ['' => ts('- any -')] + $freqUnitsDisplay
599 );
600
601 self::addCustomFormFields($form, ['Pledge']);
602
603 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'pledge_campaign_id');
604
605 $form->assign('validCiviPledge', TRUE);
606 $form->setDefaults(['pledge_test' => 0]);
607 }
608
609 /**
610 * @param $tables
611 */
612 public static function tableNames(&$tables) {
613 // add status table
614 if (!empty($tables['pledge_status']) || !empty($tables['civicrm_pledge_payment'])) {
615 $tables = array_merge(['civicrm_pledge' => 1], $tables);
616 }
617 }
618
619 }