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