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