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