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