Merge pull request #843 from pradpnayak/CRM-12680
[civicrm-core.git] / CRM / Pledge / BAO / Query.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36 class CRM_Pledge_BAO_Query {
37 static function &getFields() {
38 $fields = CRM_Pledge_BAO_Pledge::exportableFields();
39 return $fields;
40 }
41
42 /**
43 * build select for Pledge
44 *
45 * @return void
46 * @access public
47 */
48 static function select(&$query) {
49 if (($query->_mode & CRM_Contact_BAO_Query::MODE_PLEDGE) ||
50 CRM_Utils_Array::value('pledge_id', $query->_returnProperties)
51 ) {
52 $query->_select['pledge_id'] = 'civicrm_pledge.id as pledge_id';
53 $query->_element['pledge_id'] = 1;
54 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
55 }
56
57 //add pledge select
58 if (CRM_Utils_Array::value('pledge_amount', $query->_returnProperties)) {
59 $query->_select['pledge_amount'] = 'civicrm_pledge.amount as pledge_amount';
60 $query->_element['pledge_amount'] = 1;
61 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
62 }
63
64 if (CRM_Utils_Array::value('pledge_create_date', $query->_returnProperties)) {
65 $query->_select['pledge_create_date'] = 'civicrm_pledge.create_date as pledge_create_date';
66 $query->_element['pledge_create_date'] = 1;
67 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
68 }
69
70 if (CRM_Utils_Array::value('pledge_status_id', $query->_returnProperties)) {
71 $query->_select['pledge_status_id'] = 'pledge_status.value as pledge_status_id';
72 $query->_element['pledge_status'] = 1;
73 $query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
74 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
75 }
76
77 if (CRM_Utils_Array::value('pledge_status', $query->_returnProperties)) {
78 $query->_select['pledge_status'] = 'pledge_status.label as pledge_status';
79 $query->_element['pledge_status'] = 1;
80 $query->_tables['pledge_status'] = $query->_whereTables['pledge_status'] = 1;
81 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
82 }
83
84 if (CRM_Utils_Array::value('pledge_total_paid', $query->_returnProperties)) {
85 $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';
86 $query->_element['pledge_total_paid'] = 1;
87 }
88
89 if (CRM_Utils_Array::value('pledge_next_pay_date', $query->_returnProperties)) {
90 $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";
91 $query->_element['pledge_next_pay_date'] = 1;
92 }
93
94 if (CRM_Utils_Array::value('pledge_next_pay_amount', $query->_returnProperties)) {
95 $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";
96 $query->_element['pledge_next_pay_amount'] = 1;
97
98 $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";
99 $query->_element['pledge_outstanding_amount'] = 1;
100 }
101
102 if (CRM_Utils_Array::value('pledge_financial_type', $query->_returnProperties)) {
103 $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";
104 $query->_element['pledge_financial_type'] = 1;
105 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
106 }
107
108 if (CRM_Utils_Array::value('pledge_contribution_page_id', $query->_returnProperties)) {
109 $query->_select['pledge_contribution_page_id'] = 'civicrm_pledge.contribution_page_id as pledge_contribution_page_id';
110 $query->_element['pledge_contribution_page_id'] = 1;
111 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
112 }
113
114 if (CRM_Utils_Array::value('pledge_payment_id', $query->_returnProperties)) {
115 $query->_select['pledge_payment_id'] = 'civicrm_pledge_payment.id as pledge_payment_id';
116 $query->_element['pledge_payment_id'] = 1;
117 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
118 }
119
120 if (CRM_Utils_Array::value('pledge_payment_scheduled_amount', $query->_returnProperties)) {
121 $query->_select['pledge_payment_scheduled_amount'] = 'civicrm_pledge_payment.scheduled_amount as pledge_payment_scheduled_amount';
122 $query->_element['pledge_payment_scheduled_amount'] = 1;
123 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
124 }
125
126 if (CRM_Utils_Array::value('pledge_payment_scheduled_date', $query->_returnProperties)) {
127 $query->_select['pledge_payment_scheduled_date'] = 'civicrm_pledge_payment.scheduled_date as pledge_payment_scheduled_date';
128 $query->_element['pledge_payment_scheduled_date'] = 1;
129 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
130 }
131
132 if (CRM_Utils_Array::value('pledge_payment_paid_amount', $query->_returnProperties)) {
133 $query->_select['pledge_payment_paid_amount'] = 'civicrm_pledge_payment.actual_amount as pledge_payment_paid_amount';
134 $query->_element['pledge_payment_paid_amount'] = 1;
135 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
136 }
137
138 if (CRM_Utils_Array::value('pledge_payment_paid_date', $query->_returnProperties)) {
139 $query->_select['pledge_payment_paid_date'] = 'payment_contribution.receive_date as pledge_payment_paid_date';
140 $query->_element['pledge_payment_paid_date'] = 1;
141 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
142 $query->_tables['payment_contribution'] = $query->_whereTables['payment_contribution'] = 1;
143 }
144
145 if (CRM_Utils_Array::value('pledge_payment_reminder_date', $query->_returnProperties)) {
146 $query->_select['pledge_payment_reminder_date'] = 'civicrm_pledge_payment.reminder_date as pledge_payment_reminder_date';
147 $query->_element['pledge_payment_reminder_date'] = 1;
148 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
149 }
150
151 if (CRM_Utils_Array::value('pledge_payment_reminder_count', $query->_returnProperties)) {
152 $query->_select['pledge_payment_reminder_count'] = 'civicrm_pledge_payment.reminder_count as pledge_payment_reminder_count';
153 $query->_element['pledge_payment_reminder_count'] = 1;
154 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
155 }
156
157 if (CRM_Utils_Array::value('pledge_payment_status_id', $query->_returnProperties)) {
158 $query->_select['pledge_payment_status_id'] = 'payment_status.name as pledge_payment_status_id';
159 $query->_element['pledge_payment_status_id'] = 1;
160 $query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
161 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
162 }
163
164 if (CRM_Utils_Array::value('pledge_payment_status', $query->_returnProperties)) {
165 $query->_select['pledge_payment_status'] = 'payment_status.label as pledge_payment_status';
166 $query->_element['pledge_payment_status'] = 1;
167 $query->_tables['payment_status'] = $query->_whereTables['payment_status'] = 1;
168 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
169 }
170
171 if (CRM_Utils_Array::value('pledge_frequency_interval', $query->_returnProperties)) {
172 $query->_select['pledge_frequency_interval'] = 'civicrm_pledge.frequency_interval as pledge_frequency_interval';
173 $query->_element['pledge_frequency_interval'] = 1;
174 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
175 }
176
177 if (CRM_Utils_Array::value('pledge_frequency_unit', $query->_returnProperties)) {
178 $query->_select['pledge_frequency_unit'] = 'civicrm_pledge.frequency_unit as pledge_frequency_unit';
179 $query->_element['pledge_frequency_unit'] = 1;
180 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
181 }
182
183 if (CRM_Utils_Array::value('pledge_is_test', $query->_returnProperties)) {
184 $query->_select['pledge_is_test'] = 'civicrm_pledge.is_test as pledge_is_test';
185 $query->_element['pledge_is_test'] = 1;
186 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
187 }
188 if (CRM_Utils_Array::value('pledge_campaign_id', $query->_returnProperties)) {
189 $query->_select['pledge_campaign_id'] = 'civicrm_pledge.campaign_id as pledge_campaign_id';
190 $query->_element['pledge_campaign_id'] = 1;
191 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
192 }
193
194 if (CRM_Utils_Array::value('pledge_currency', $query->_returnProperties)) {
195 $query->_select['pledge_currency'] = 'civicrm_pledge.currency as pledge_currency';
196 $query->_element['pledge_currency'] = 1;
197 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
198 }
199 }
200
201 static function where(&$query) {
202 $grouping = NULL;
203 foreach (array_keys($query->_params) as $id) {
204 if (!CRM_Utils_Array::value(0, $query->_params[$id])) {
205 continue;
206 }
207 if (substr($query->_params[$id][0], 0, 7) == 'pledge_') {
208 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
209 $query->_useDistinct = TRUE;
210 }
211 $grouping = $query->_params[$id][3];
212 self::whereClauseSingle($query->_params[$id], $query);
213 }
214 }
215 }
216
217 static function whereClauseSingle(&$values, &$query) {
218 list($name, $op, $value, $grouping, $wildcard) = $values;
219
220 switch ($name) {
221 case 'pledge_create_date_low':
222 case 'pledge_create_date_high':
223 // process to / from date
224 $query->dateQueryBuilder($values,
225 'civicrm_pledge', 'pledge_create_date', 'create_date', 'Pledge Made'
226 );
227 case 'pledge_start_date_low':
228 case 'pledge_start_date_high':
229 // process to / from date
230 $query->dateQueryBuilder($values,
231 'civicrm_pledge', 'pledge_start_date', 'start_date', 'Pledge Start Date'
232 );
233 return;
234
235 case 'pledge_end_date_low':
236 case 'pledge_end_date_high':
237 // process to / from date
238 $query->dateQueryBuilder($values,
239 'civicrm_pledge', 'pledge_end_date', 'end_date', 'Pledge End Date'
240 );
241 return;
242
243 case 'pledge_payment_date_low':
244 case 'pledge_payment_date_high':
245 // process to / from date
246 $query->dateQueryBuilder($values,
247 'civicrm_pledge_payment', 'pledge_payment_date', 'scheduled_date', 'Payment Scheduled'
248 );
249 return;
250
251 case 'pledge_amount':
252 case 'pledge_amount_low':
253 case 'pledge_amount_high':
254 // process min/max amount
255 $query->numberRangeBuilder($values,
256 'civicrm_pledge', 'pledge_amount', 'amount', 'Pledge Amount'
257 );
258 return;
259
260 case 'pledge_status_id':
261 if (is_array($value)) {
262 foreach ($value as $k => $v) {
263 if ($v) {
264 $val[$k] = $k;
265 }
266 }
267
268 $status = implode(',', $val);
269
270 if (count($val) > 1) {
271 $op = 'IN';
272 $status = "({$status})";
273 }
274 }
275 else {
276 $op = '=';
277 $status = $value;
278 }
279
280 $statusValues = CRM_Core_OptionGroup::values('contribution_status');
281
282 $names = array();
283 if (isset($val) && is_array($val)) {
284 foreach ($val as $id => $dontCare) {
285 $names[] = $statusValues[$id];
286 }
287 }
288 else {
289 $names[] = $statusValues[$value];
290 }
291
292 $query->_qill[$grouping][] = ts('Pledge Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
293 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.status_id',
294 $op,
295 $status,
296 'Integer'
297 );
298 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
299 return;
300
301 case 'pledge_payment_status_id':
302 if (is_array($value)) {
303 foreach ($value as $k => $v) {
304 if ($v) {
305 $val[$k] = $k;
306 }
307 }
308
309 $status = implode(',', $val);
310
311 if (count($val) > 1) {
312 $op = 'IN';
313 $status = "({$status})";
314 }
315 }
316 else {
317 $op = '=';
318 $status = $value;
319 }
320
321 $statusValues = CRM_Core_OptionGroup::values('contribution_status');
322
323 $names = array();
324 if (is_array($val)) {
325 foreach ($val as $id => $dontCare) {
326 $names[] = $statusValues[$id];
327 }
328 }
329 else {
330 $names[] = $statusValues[$value];
331 }
332
333 $query->_qill[$grouping][] = ts('Pledge Payment Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
334 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge_payment.status_id',
335 $op,
336 $status,
337 'Integer'
338 );
339 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
340 return;
341
342 case 'pledge_test':
343 case 'pledge_is_test':
344 // We dont want to include all tests for sql OR CRM-7827
345 if (!$value || $query->getOperator() != 'OR') {
346 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.is_test',
347 $op,
348 $value,
349 'Boolean'
350 );
351 if ($value) {
352 $query->_qill[$grouping][] = ts('Pledge is a Test');
353 }
354 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
355 }
356 return;
357
358 case 'pledge_financial_type_id':
359 $type = CRM_Contribute_PseudoConstant::financialType($value);
360 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.financial_type_id',
361 $op,
362 $value,
363 'Integer'
364 );
365 $query->_qill[$grouping][] = ts( 'Financial Type - %1', array( 1 => $type ) );
366 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
367 return;
368
369 case 'pledge_contribution_page_id':
370 $page = CRM_Contribute_PseudoConstant::contributionPage($value);
371 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.contribution_page_id',
372 $op,
373 $value,
374 'Integer'
375 );
376 $query->_qill[$grouping][] = ts('Financial Page - %1', array(1 => $page));
377 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
378 return;
379
380 case 'pledge_in_honor_of':
381 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
382 $name = trim($value);
383 $newName = str_replace(',', " ", $name);
384 $pieces = explode(' ', $newName);
385 foreach ($pieces as $piece) {
386 $value = $strtolower(CRM_Core_DAO::escapeString(trim($piece)));
387 $value = "'%$value%'";
388 $sub[] = " ( pledge_contact_b.sort_name LIKE $value )";
389 }
390
391 $query->_where[$grouping][] = ' ( ' . implode(' OR ', $sub) . ' ) ';
392 $query->_qill[$grouping][] = ts('Honor name like - \'%1\'', array(1 => $name));
393 $query->_tables['pledge_contact_b'] = $query->_whereTables['pledge_contact_b'] = 1;
394 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
395 return;
396
397 case 'pledge_id':
398 $query->_where[$grouping][] = "civicrm_pledge.id $op $value";
399 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
400 return;
401
402 case 'pledge_frequency_interval':
403 $query->_where[$grouping][] = "civicrm_pledge.frequency_interval $op $value";
404 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
405 return;
406
407 case 'pledge_frequency_unit':
408 $query->_where[$grouping][] = "civicrm_pledge.frequency_unit $op $value";
409 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
410 return;
411
412 case 'pledge_campaign_id':
413 $campParams = array(
414 'op' => $op,
415 'campaign' => $value,
416 'grouping' => $grouping,
417 'tableName' => 'civicrm_pledge',
418 );
419 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
420 return;
421 }
422 }
423
424 static function from($name, $mode, $side) {
425 $from = NULL;
426
427 switch ($name) {
428 case 'civicrm_pledge':
429 $from = " $side JOIN civicrm_pledge ON civicrm_pledge.contact_id = contact_a.id ";
430 break;
431
432 case 'pledge_status':
433 $from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'contribution_status')";
434 $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 ) ";
435 break;
436
437 case 'pledge_financial_type':
438 $from .= " $side JOIN civicrm_financial_type ON civicrm_pledge.financial_type_id = civicrm_financial_type.id ";
439 break;
440
441 case 'pledge_contact_b':
442 $from .= " $side JOIN civicrm_contact pledge_contact_b ON (civicrm_pledge.honor_contact_id = pledge_contact_b.id )";
443 break;
444
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 /**
463 * getter for the qill object
464 *
465 * @return string
466 * @access public
467 */
468 function qill() {
469 return (isset($this->_qill)) ? $this->_qill : "";
470 }
471
472 /**
473 * Ideally this function should include fields that are displayed in the selector
474 */
475 static function defaultReturnProperties($mode,
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 */
509 static function extraReturnProperties($mode) {
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
537 static function buildSearchForm(&$form) {
538 // pledge related dates
539 CRM_Core_Form_Date::buildDateRange($form, 'pledge_start_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
540 CRM_Core_Form_Date::buildDateRange($form, 'pledge_end_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
541 CRM_Core_Form_Date::buildDateRange($form, 'pledge_create_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
542
543 // pledge payment related dates
544 CRM_Core_Form_Date::buildDateRange($form, 'pledge_payment_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
545
546 $form->addYesNo('pledge_test', ts('Pledge is a Test?'));
547 $form->add('text', 'pledge_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
548 $form->addRule('pledge_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
549
550 $form->add('text', 'pledge_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
551 $form->addRule('pledge_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
552
553 $statusValues = CRM_Contribute_PseudoConstant::contributionStatus();
554
555 // Remove status values that are only used for recurring contributions for now (Failed and In Progress).
556 unset($statusValues['4']);
557
558 foreach ($statusValues as $key => $val) {
559 $status[] = $form->createElement('advcheckbox', $key, NULL, $val);
560 }
561
562 $form->addGroup($status, 'pledge_status_id', ts('Pledge Status'));
563
564 //unset in progress for payment
565 unset($statusValues['5']);
566
567 foreach ($statusValues as $key => $val) {
568 $paymentStatus[] = $form->createElement('advcheckbox', $key, NULL, $val);
569 }
570
571 $form->addGroup($paymentStatus, 'pledge_payment_status_id', ts('Pledge Payment Status'));
572
573 $form->add('select', 'pledge_financial_type_id',
574 ts( 'Financial Type' ),
575 array(
576 '' => ts('- select -')) +
577 CRM_Contribute_PseudoConstant::financialType()
578 );
579
580 $form->add('select', 'pledge_contribution_page_id',
581 ts('Contribution Page'),
582 array(
583 '' => ts('- any -')) +
584 CRM_Contribute_PseudoConstant::contributionPage()
585 );
586
587 //add fields for honor search
588 $form->addElement('text', 'pledge_in_honor_of', ts('In Honor Of'));
589
590 //add fields for pledge frequency
591 $form->add('text', 'pledge_frequency_interval', ts('Every'), array('size' => 8, 'maxlength' => 8));
592
593 $frequencies = CRM_Core_OptionGroup::values('recur_frequency_units');
594 foreach ($frequencies as $val => $label) {
595 $freqUnitsDisplay["'{$val}'"] = ts('%1(s)', array(1 => $label));
596 }
597
598 $form->add('select', 'pledge_frequency_unit',
599 ts('Pledge Frequency'),
600 array(
601 '' => ts('- any -')) + $freqUnitsDisplay
602 );
603
604 // add all the custom searchable fields
605 $pledge = array('Pledge');
606 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $pledge);
607 if ($groupDetails) {
608 $form->assign('pledgeGroupTree', $groupDetails);
609 foreach ($groupDetails as $group) {
610 foreach ($group['fields'] as $field) {
611 $fieldId = $field['id'];
612 $elementName = 'custom_' . $fieldId;
613 CRM_Core_BAO_CustomField::addQuickFormElement($form,
614 $elementName,
615 $fieldId,
616 FALSE, FALSE, TRUE
617 );
618 }
619 }
620 }
621
622 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'pledge_campaign_id');
623
624 $form->assign('validCiviPledge', TRUE);
625 $form->setDefaults(array('pledge_test' => 0));
626 }
627
628 static function searchAction(&$row, $id) {}
629
630 static function tableNames(&$tables) {
631 //add status table
632 if (CRM_Utils_Array::value('pledge_status', $tables) ||
633 CRM_Utils_Array::value('civicrm_pledge_payment', $tables)
634 ) {
635 $tables = array_merge(array('civicrm_pledge' => 1), $tables);
636 }
637 }
638 }
639