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