Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-13-15-43-24
[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 $testCondition = $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 if ($query->_params[$id][0] == 'participant_test') {
212 $testCondition = $id;
213 continue;
214 }
215 $grouping = $query->_params[$id][3];
216 self::whereClauseSingle($query->_params[$id], $query);
217 }
218 }
219 // Only add test condition if other fields are selected
220 if ($grouping !== NULL && $testCondition &&
221 // we dont want to include all tests for sql OR CRM-7827
222 $query->getOperator() != 'OR'
223 ) {
224 self::whereClauseSingle($query->_params[$testCondition], $query);
225 }
226 }
227
228 static function whereClauseSingle(&$values, &$query) {
229 list($name, $op, $value, $grouping, $wildcard) = $values;
230
231 switch ($name) {
232 case 'pledge_create_date_low':
233 case 'pledge_create_date_high':
234 // process to / from date
235 $query->dateQueryBuilder($values,
236 'civicrm_pledge', 'pledge_create_date', 'create_date', 'Pledge Made'
237 );
238 case 'pledge_start_date_low':
239 case 'pledge_start_date_high':
240 // process to / from date
241 $query->dateQueryBuilder($values,
242 'civicrm_pledge', 'pledge_start_date', 'start_date', 'Pledge Start Date'
243 );
244 return;
245
246 case 'pledge_end_date_low':
247 case 'pledge_end_date_high':
248 // process to / from date
249 $query->dateQueryBuilder($values,
250 'civicrm_pledge', 'pledge_end_date', 'end_date', 'Pledge End Date'
251 );
252 return;
253
254 case 'pledge_payment_date_low':
255 case 'pledge_payment_date_high':
256 // process to / from date
257 $query->dateQueryBuilder($values,
258 'civicrm_pledge_payment', 'pledge_payment_date', 'scheduled_date', 'Payment Scheduled'
259 );
260 return;
261
262 case 'pledge_amount':
263 case 'pledge_amount_low':
264 case 'pledge_amount_high':
265 // process min/max amount
266 $query->numberRangeBuilder($values,
267 'civicrm_pledge', 'pledge_amount', 'amount', 'Pledge Amount'
268 );
269 return;
270
271 case 'pledge_status_id':
272 if (is_array($value)) {
273 foreach ($value as $k => $v) {
274 if ($v) {
275 $val[$k] = $k;
276 }
277 }
278
279 $status = implode(',', $val);
280
281 if (count($val) > 1) {
282 $op = 'IN';
283 $status = "({$status})";
284 }
285 }
286 else {
287 $op = '=';
288 $status = $value;
289 }
290
291 $statusValues = CRM_Core_OptionGroup::values('contribution_status');
292
293 $names = array();
294 if (isset($val) && is_array($val)) {
295 foreach ($val as $id => $dontCare) {
296 $names[] = $statusValues[$id];
297 }
298 }
299 else {
300 $names[] = $statusValues[$value];
301 }
302
303 $query->_qill[$grouping][] = ts('Pledge Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
304 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.status_id',
305 $op,
306 $status,
307 'Integer'
308 );
309 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
310 return;
311
312 case 'pledge_payment_status_id':
313 if (is_array($value)) {
314 foreach ($value as $k => $v) {
315 if ($v) {
316 $val[$k] = $k;
317 }
318 }
319
320 $status = implode(',', $val);
321
322 if (count($val) > 1) {
323 $op = 'IN';
324 $status = "({$status})";
325 }
326 }
327 else {
328 $op = '=';
329 $status = $value;
330 }
331
332 $statusValues = CRM_Core_OptionGroup::values('contribution_status');
333
334 $names = array();
335 if (is_array($val)) {
336 foreach ($val as $id => $dontCare) {
337 $names[] = $statusValues[$id];
338 }
339 }
340 else {
341 $names[] = $statusValues[$value];
342 }
343
344 $query->_qill[$grouping][] = ts('Pledge Payment Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
345 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge_payment.status_id',
346 $op,
347 $status,
348 'Integer'
349 );
350 $query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
351 return;
352
353 case 'pledge_test':
354 case 'pledge_is_test':
355 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.is_test',
356 $op,
357 $value,
358 'Boolean'
359 );
360 if ($value) {
361 $query->_qill[$grouping][] = ts('Pledge is a Test');
362 }
363 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
364 return;
365
366 case 'pledge_financial_type_id':
367 $type = CRM_Contribute_PseudoConstant::financialType($value);
368 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.financial_type_id',
369 $op,
370 $value,
371 'Integer'
372 );
373 $query->_qill[$grouping][] = ts( 'Financial Type - %1', array( 1 => $type ) );
374 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
375 return;
376
377 case 'pledge_contribution_page_id':
378 $page = CRM_Contribute_PseudoConstant::contributionPage($value);
379 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_pledge.contribution_page_id',
380 $op,
381 $value,
382 'Integer'
383 );
384 $query->_qill[$grouping][] = ts('Financial Page - %1', array(1 => $page));
385 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
386 return;
387
388 case 'pledge_in_honor_of':
389 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
390 $name = trim($value);
391 $newName = str_replace(',', " ", $name);
392 $pieces = explode(' ', $newName);
393 foreach ($pieces as $piece) {
394 $value = $strtolower(CRM_Core_DAO::escapeString(trim($piece)));
395 $value = "'%$value%'";
396 $sub[] = " ( pledge_contact_b.sort_name LIKE $value )";
397 }
398
399 $query->_where[$grouping][] = ' ( ' . implode(' OR ', $sub) . ' ) ';
400 $query->_qill[$grouping][] = ts('Honor name like - \'%1\'', array(1 => $name));
401 $query->_tables['pledge_contact_b'] = $query->_whereTables['pledge_contact_b'] = 1;
402 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
403 return;
404
405 case 'pledge_id':
406 $query->_where[$grouping][] = "civicrm_pledge.id $op $value";
407 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
408 return;
409
410 case 'pledge_frequency_interval':
411 $query->_where[$grouping][] = "civicrm_pledge.frequency_interval $op $value";
412 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
413 return;
414
415 case 'pledge_frequency_unit':
416 $query->_where[$grouping][] = "civicrm_pledge.frequency_unit $op $value";
417 $query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
418 return;
419
420 case 'pledge_campaign_id':
421 $campParams = array(
422 'op' => $op,
423 'campaign' => $value,
424 'grouping' => $grouping,
425 'tableName' => 'civicrm_pledge',
426 );
427 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
428 return;
429 }
430 }
431
432 static function from($name, $mode, $side) {
433 $from = NULL;
434
435 switch ($name) {
436 case 'civicrm_pledge':
437 $from = " $side JOIN civicrm_pledge ON civicrm_pledge.contact_id = contact_a.id ";
438 break;
439
440 case 'pledge_status':
441 $from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'contribution_status')";
442 $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 ) ";
443 break;
444
445 case 'pledge_financial_type':
446 $from .= " $side JOIN civicrm_financial_type ON civicrm_pledge.financial_type_id = civicrm_financial_type.id ";
447 break;
448
449 case 'pledge_contact_b':
450 $from .= " $side JOIN civicrm_contact pledge_contact_b ON (civicrm_pledge.honor_contact_id = pledge_contact_b.id )";
451 break;
452
453 case 'civicrm_pledge_payment':
454 $from .= " $side JOIN civicrm_pledge_payment ON civicrm_pledge_payment.pledge_id = civicrm_pledge.id ";
455 break;
456
457 case 'payment_contribution':
458 $from .= " $side JOIN civicrm_contribution payment_contribution ON civicrm_pledge_payment.contribution_id = payment_contribution.id ";
459 break;
460
461 case 'payment_status':
462 $from .= " $side JOIN civicrm_option_group option_group_payment_status ON (option_group_payment_status.name = 'contribution_status')";
463 $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 ) ";
464 break;
465 }
466
467 return $from;
468 }
469
470 /**
471 * getter for the qill object
472 *
473 * @return string
474 * @access public
475 */
476 function qill() {
477 return (isset($this->_qill)) ? $this->_qill : "";
478 }
479
480 /**
481 * Ideally this function should include fields that are displayed in the selector
482 */
483 static function defaultReturnProperties($mode,
484 $includeCustomFields = TRUE
485 ) {
486 $properties = NULL;
487
488 if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
489 $properties = array(
490 'contact_type' => 1,
491 'contact_sub_type' => 1,
492 'sort_name' => 1,
493 'display_name' => 1,
494 'pledge_id' => 1,
495 'pledge_amount' => 1,
496 'pledge_total_paid' => 1,
497 'pledge_create_date' => 1,
498 'pledge_next_pay_date' => 1,
499 'pledge_next_pay_amount' => 1,
500 'pledge_status' => 1,
501 'pledge_status_id' => 1,
502 'pledge_is_test' => 1,
503 'pledge_contribution_page_id' => 1,
504 'pledge_financial_type' => 1,
505 'pledge_frequency_interval' => 1,
506 'pledge_frequency_unit' => 1,
507 'pledge_currency' => 1,
508 'pledge_campaign_id' => 1,
509 );
510 }
511 return $properties;
512 }
513
514 /**
515 * This includes any extra fields that might need for export etc
516 */
517 static function extraReturnProperties($mode) {
518 $properties = NULL;
519
520 if ($mode & CRM_Contact_BAO_Query::MODE_PLEDGE) {
521 $properties = array(
522 'pledge_balance_amount' => 1,
523 'pledge_payment_id' => 1,
524 'pledge_payment_scheduled_amount' => 1,
525 'pledge_payment_scheduled_date' => 1,
526 'pledge_payment_paid_amount' => 1,
527 'pledge_payment_paid_date' => 1,
528 'pledge_payment_reminder_date' => 1,
529 'pledge_payment_reminder_count' => 1,
530 'pledge_payment_status_id' => 1,
531 'pledge_payment_status' => 1,
532 );
533
534 // also get all the custom pledge properties
535 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Pledge');
536 if (!empty($fields)) {
537 foreach ($fields as $name => $dontCare) {
538 $properties[$name] = 1;
539 }
540 }
541 }
542 return $properties;
543 }
544
545 static function buildSearchForm(&$form) {
546 // pledge related dates
547 CRM_Core_Form_Date::buildDateRange($form, 'pledge_start_date', 1, '_low', '_high', ts('From'), FALSE);
548 CRM_Core_Form_Date::buildDateRange($form, 'pledge_end_date', 1, '_low', '_high', ts('From'), FALSE);
549 CRM_Core_Form_Date::buildDateRange($form, 'pledge_create_date', 1, '_low', '_high', ts('From'), FALSE);
550
551 // pledge payment related dates
552 CRM_Core_Form_Date::buildDateRange($form, 'pledge_payment_date', 1, '_low', '_high', ts('From'), FALSE);
553
554 $form->addYesNo('pledge_test', ts('Pledge is a Test?'));
555 $form->add('text', 'pledge_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
556 $form->addRule('pledge_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
557
558 $form->add('text', 'pledge_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
559 $form->addRule('pledge_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
560
561 $statusValues = CRM_Contribute_PseudoConstant::contributionStatus();
562
563 // Remove status values that are only used for recurring contributions for now (Failed and In Progress).
564 unset($statusValues['4']);
565
566 foreach ($statusValues as $key => $val) {
567 $status[] = $form->createElement('advcheckbox', $key, NULL, $val);
568 }
569
570 $form->addGroup($status, 'pledge_status_id', ts('Pledge Status'));
571
572 //unset in progress for payment
573 unset($statusValues['5']);
574
575 foreach ($statusValues as $key => $val) {
576 $paymentStatus[] = $form->createElement('advcheckbox', $key, NULL, $val);
577 }
578
579 $form->addGroup($paymentStatus, 'pledge_payment_status_id', ts('Pledge Payment Status'));
580
581 $form->add('select', 'pledge_financial_type_id',
582 ts( 'Financial Type' ),
583 array(
584 '' => ts('- select -')) +
585 CRM_Contribute_PseudoConstant::financialType()
586 );
587
588 $form->add('select', 'pledge_contribution_page_id',
589 ts('Contribution Page'),
590 array(
591 '' => ts('- any -')) +
592 CRM_Contribute_PseudoConstant::contributionPage()
593 );
594
595 //add fields for honor search
596 $form->addElement('text', 'pledge_in_honor_of', ts('In Honor Of'));
597
598 //add fields for pledge frequency
599 $form->add('text', 'pledge_frequency_interval', ts('Every'), array('size' => 8, 'maxlength' => 8));
600
601 $frequencies = CRM_Core_OptionGroup::values('recur_frequency_units');
602 foreach ($frequencies as $val => $label) {
603 $freqUnitsDisplay["'{$val}'"] = ts('%1(s)', array(1 => $label));
604 }
605
606 $form->add('select', 'pledge_frequency_unit',
607 ts('Pledge Frequency'),
608 array(
609 '' => ts('- any -')) + $freqUnitsDisplay
610 );
611
612 // add all the custom searchable fields
613 $pledge = array('Pledge');
614 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $pledge);
615 if ($groupDetails) {
616 $form->assign('pledgeGroupTree', $groupDetails);
617 foreach ($groupDetails as $group) {
618 foreach ($group['fields'] as $field) {
619 $fieldId = $field['id'];
620 $elementName = 'custom_' . $fieldId;
621 CRM_Core_BAO_CustomField::addQuickFormElement($form,
622 $elementName,
623 $fieldId,
624 FALSE, FALSE, TRUE
625 );
626 }
627 }
628 }
629
630 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'pledge_campaign_id');
631
632 $form->assign('validCiviPledge', TRUE);
633 $form->setDefaults(array('pledge_test' => 0));
634 }
635
636 static function searchAction(&$row, $id) {}
637
638 static function tableNames(&$tables) {
639 //add status table
640 if (CRM_Utils_Array::value('pledge_status', $tables) ||
641 CRM_Utils_Array::value('civicrm_pledge_payment', $tables)
642 ) {
643 $tables = array_merge(array('civicrm_pledge' => 1), $tables);
644 }
645 }
646 }
647