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