use group by to ensure row count correctness in any case.
[civicrm-core.git] / CRM / Contribute / BAO / Query.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35class CRM_Contribute_BAO_Query {
36
37 /**
38 * static field for all the export/import contribution fields
39 *
40 * @var array
41 * @static
42 */
43 static $_contributionFields = NULL;
44
3dbf477c
DS
45 static $_contribOrSoftCredit = "only_contribs";
46
6a488035
TO
47 /**
48 * Function get the import/export fields for contribution
49 *
50 * @return array self::$_contributionFields associative array of contribution fields
51 * @static
52 */
53 static function &getFields() {
54 if (!self::$_contributionFields) {
55 self::$_contributionFields = array();
56
57 $fields = CRM_Contribute_BAO_Contribution::exportableFields();
58
59 unset($fields['contribution_contact_id']);
60
61 self::$_contributionFields = $fields;
62 }
63 return self::$_contributionFields;
64 }
65
66 /**
67 * if contributions are involved, add the specific contribute fields
68 *
69 * @return void
70 * @access public
71 */
72 static function select(&$query) {
73 // if contribute mode add contribution id
74 if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
75 $query->_select['contribution_id'] = "civicrm_contribution.id as contribution_id";
76 $query->_element['contribution_id'] = 1;
77 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
78 }
79
80 // get financial_type
a7488080 81 if (!empty($query->_returnProperties['financial_type'])) {
6a488035
TO
82 $query->_select['financial_type'] = "civicrm_financial_type.name as financial_type";
83 $query->_element['financial_type'] = 1;
84 $query->_tables['civicrm_contribution'] = 1;
85 $query->_tables['civicrm_financial_type'] = 1;
86 }
87
88 // get accounting code
a7488080 89 if (!empty($query->_returnProperties['accounting_code'])) {
6a488035
TO
90 $query->_select['accounting_code'] = "civicrm_financial_account.accounting_code as accounting_code";
91 $query->_element['accounting_code'] = 1;
92 $query->_tables['civicrm_accounting_code'] = 1;
93 $query->_tables['civicrm_financial_account'] = 1;
94 }
39b795ba 95
a7488080 96 if (!empty($query->_returnProperties['contribution_note'])) {
6a488035
TO
97 $query->_select['contribution_note'] = "civicrm_note.note as contribution_note";
98 $query->_element['contribution_note'] = 1;
99 $query->_tables['contribution_note'] = 1;
100 }
101
a7488080 102 if (!empty($query->_returnProperties['contribution_batch'])) {
6a488035
TO
103 $query->_select['contribution_batch'] = "civicrm_batch.title as contribution_batch";
104 $query->_element['contribution_batch'] = 1;
105 $query->_tables['contribution_batch'] = 1;
106 }
107
108 // get contribution_status
a7488080 109 if (!empty($query->_returnProperties['contribution_status_id'])) {
6a488035
TO
110 $query->_select['contribution_status_id'] = "contribution_status.value as contribution_status_id";
111 $query->_element['contribution_status_id'] = 1;
112 $query->_tables['civicrm_contribution'] = 1;
113 $query->_tables['contribution_status'] = 1;
114 }
115
116 // get contribution_status label
a7488080 117 if (!empty($query->_returnProperties['contribution_status'])) {
6a488035
TO
118 $query->_select['contribution_status'] = "contribution_status.label as contribution_status";
119 $query->_element['contribution_status'] = 1;
120 $query->_tables['civicrm_contribution'] = 1;
121 $query->_tables['contribution_status'] = 1;
122 }
123
124 // get payment instruments
a7488080 125 if (!empty($query->_returnProperties['payment_instrument'])) {
6a488035
TO
126 $query->_select['contribution_payment_instrument'] = "payment_instrument.name as contribution_payment_instrument";
127 $query->_element['contribution_payment_instrument'] = 1;
128 $query->_tables['civicrm_contribution'] = 1;
129 $query->_tables['contribution_payment_instrument'] = 1;
130 }
131
a7488080 132 if (!empty($query->_returnProperties['check_number'])) {
6a488035
TO
133 $query->_select['contribution_check_number'] = "civicrm_contribution.check_number as contribution_check_number";
134 $query->_element['contribution_check_number'] = 1;
135 $query->_tables['civicrm_contribution'] = 1;
136 }
137
a7488080 138 if (!empty($query->_returnProperties['contribution_campaign_id'])) {
6a488035
TO
139 $query->_select['contribution_campaign_id'] = 'civicrm_contribution.campaign_id as contribution_campaign_id';
140 $query->_element['contribution_campaign_id'] = 1;
141 $query->_tables['civicrm_contribution'] = 1;
142 }
143
144 // LCD 716
81ec6180 145 if (!empty($query->_returnProperties['contribution_soft_credit_name'])) {
6a488035
TO
146 $query->_select['contribution_soft_credit_name'] = "civicrm_contact_d.display_name as contribution_soft_credit_name";
147 $query->_element['contribution_soft_credit_name'] = 1;
d4e2b978
DS
148
149 // also include contact id. Will help build hyperlinks
150 $query->_select['contribution_soft_credit_contact_id'] = "civicrm_contact_d.id as contribution_soft_credit_contact_id";
151 $query->_element['contribution_soft_credit_contact_id'] = 1;
152
6a488035
TO
153 $query->_tables['civicrm_contribution'] = 1;
154 $query->_tables['civicrm_contribution_soft'] = 1;
155 $query->_tables['civicrm_contribution_soft_contact'] = 1;
156 }
157
d4e2b978
DS
158 if (!empty($query->_returnProperties['contribution_soft_credit_contact_id'])) {
159 $query->_tables['civicrm_contribution'] = 1;
160 $query->_tables['civicrm_contribution_soft'] = 1;
161 }
162
81ec6180 163 if (!empty($query->_returnProperties['contribution_soft_credit_amount'])) {
36bf52ef
DS
164 $query->_select['contribution_soft_credit_amount'] = "civicrm_contribution_soft.amount as contribution_soft_credit_amount";
165 $query->_element['contribution_soft_credit_amount'] = 1;
166 $query->_tables['civicrm_contribution'] = 1;
167 $query->_tables['civicrm_contribution_soft'] = 1;
168 }
169
81ec6180 170 if (!empty($query->_returnProperties['contribution_soft_credit_type'])) {
36bf52ef
DS
171 $query->_select['contribution_soft_credit_type'] = "contribution_softcredit_type.label as contribution_soft_credit_type";
172 $query->_element['contribution_soft_credit_type'] = 1;
173 $query->_tables['civicrm_contribution'] = 1;
174 $query->_tables['contribution_softcredit_type'] = 1;
175 }
176
81ec6180
DS
177 if (!empty($query->_returnProperties['contribution_soft_credit_contribution_id'])) {
178 $query->_select['contribution_soft_credit_contribution_id'] = "civicrm_contribution_soft.contribution_id as contribution_soft_credit_contribution_id";
179 $query->_element['contribution_soft_credit_contribution_id'] = 1;
180 $query->_tables['civicrm_contribution'] = 1;
181 $query->_tables['civicrm_contribution_soft'] = 1;
182 }
183
184 if (!empty($query->_returnProperties['contribution_soft_credit_email'])) {
6a488035
TO
185 $query->_select['contribution_soft_credit_email'] = "soft_email.email as contribution_soft_credit_email";
186 $query->_element['contribution_soft_credit_email'] = 1;
187 $query->_tables['civicrm_contribution'] = 1;
188 $query->_tables['civicrm_contribution_soft'] = 1;
189 $query->_tables['civicrm_contribution_soft_contact'] = 1;
190 $query->_tables['civicrm_contribution_soft_email'] = 1;
191 }
192
81ec6180 193 if (!empty($query->_returnProperties['contribution_soft_credit_phone'])) {
6a488035
TO
194 $query->_select['contribution_soft_credit_email'] = "soft_phone.phone as contribution_soft_credit_phone";
195 $query->_element['contribution_soft_credit_phone'] = 1;
196 $query->_tables['civicrm_contribution'] = 1;
197 $query->_tables['civicrm_contribution_soft'] = 1;
198 $query->_tables['civicrm_contribution_soft_contact'] = 1;
199 $query->_tables['civicrm_contribution_soft_phone'] = 1;
200 }
201 // LCD 716 END
202 }
203
204 static function where(&$query) {
4b58de38 205 $grouping = NULL;
3dbf477c 206
f654cacf 207 self::initializeAnySoftCreditClause($query);
6a488035 208 foreach (array_keys($query->_params) as $id) {
a7488080 209 if (empty($query->_params[$id][0])) {
6a488035
TO
210 continue;
211 }
212 if (substr($query->_params[$id][0], 0, 13) == 'contribution_' || substr($query->_params[$id][0], 0, 10) == 'financial_') {
213 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
214 $query->_useDistinct = TRUE;
215 }
39b795ba
DL
216 // CRM-12065
217 if (
218 $query->_params[$id][0] == 'contribution_type_id' ||
219 $query->_params[$id][0] == 'contribution_type'
220 ) {
221 CRM_Core_Session::setStatus(
222 ts('The contribution type criteria is now obsolete, please update your smart group'),
223 '',
224 'alert'
225 );
226 continue;
227 }
228
6a488035
TO
229 $grouping = $query->_params[$id][3];
230 self::whereClauseSingle($query->_params[$id], $query);
231 }
232 }
6a488035
TO
233 }
234
235 static function whereClauseSingle(&$values, &$query) {
236 list($name, $op, $value, $grouping, $wildcard) = $values;
237
6a488035 238 $fields = self::getFields();
163b1da9
KJ
239
240 if (!empty($value) && !is_array($value)) {
6a488035
TO
241 $quoteValue = "\"$value\"";
242 }
243
244 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
b32bc653 245 foreach (self::getRecurringFields() as $dateField => $dateFieldTitle) {
a91e698a 246 if(self::buildDateWhere($values, $query, $name, $dateField, $dateFieldTitle)) {
247 return;
248 }
249 }
6a488035
TO
250 switch ($name) {
251 case 'contribution_date':
252 case 'contribution_date_low':
253 case 'contribution_date_low_time':
254 case 'contribution_date_high':
255 case 'contribution_date_high_time':
256 // process to / from date
257 $query->dateQueryBuilder($values,
258 'civicrm_contribution', 'contribution_date', 'receive_date', 'Contribution Date'
259 );
260 return;
261
262 case 'contribution_amount':
263 case 'contribution_amount_low':
264 case 'contribution_amount_high':
265 // process min/max amount
266 $query->numberRangeBuilder($values,
267 'civicrm_contribution', 'contribution_amount',
268 'total_amount', 'Contribution Amount',
269 NULL
270 );
271 return;
272
273 case 'contribution_total_amount':
274 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.total_amount",
275 $op, $value, "Money"
276 );
277 $query->_qill[$grouping][] = ts('Contribution Total Amount %1 %2', array(1 => $op, 2 => $value));
278 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
279 return;
280
281 case 'contribution_thankyou_date_is_not_null':
282 if ($value) {
283 $op = "IS NOT NULL";
284 $query->_qill[$grouping][] = ts('Contribution Thank-you Sent');
285 }
286 else {
287 $op = "IS NULL";
288 $query->_qill[$grouping][] = ts('Contribution Thank-you Not Sent');
289 }
290 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.thankyou_date", $op);
291 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
292 return;
293
294 case 'contribution_receipt_date_is_not_null':
295 if ($value) {
296 $op = "IS NOT NULL";
297 $query->_qill[$grouping][] = ts('Contribution Receipt Sent');
298 }
299 else {
300 $op = "IS NULL";
301 $query->_qill[$grouping][] = ts('Contribution Receipt Not Sent');
302 }
303 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.receipt_date", $op);
304 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
305 return;
306
4ca8b3e8
JM
307 case 'financial_type_id':
308 case 'financial_type':
5bbd985a
JM
309 // The financial_type_id might be an array (from aggregate contributions custom search)
310 // In this case, we need to change the query.
311 if (is_array($value)) {
312 $val = array();
313 // Rebuild the array to get the data we're interested in as array
314 // values not array keys.
315 foreach ($value as $k => $v) {
316 if ($v) {
317 $val[] = $k;
318 }
319 }
1f0d8c92 320 if (count($val) > 0) {
5bbd985a
JM
321 // Overwrite $value so it works with an IN where statement.
322 $op = 'IN';
323 $value = '(' . implode(',', $val) . ')';
324 }
325 else {
326 // If we somehow have an empty array, just return
327 return;
328 }
329 }
330
289c8051 331 $types = CRM_Contribute_PseudoConstant::financialType();
5bbd985a
JM
332
333 // Ensure we have a sensible string to display to the user.
334 $names = array();
335 if (isset($val) && is_array($val)) {
336 foreach($val as $id) {
1f0d8c92 337 $names[] = CRM_Utils_Array::value($id, $types);
5bbd985a
JM
338 }
339 }
340 else {
341 $names[] = $types[$value];
342 }
343
289c8051 344 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.financial_type_id",
6a488035
TO
345 $op, $value, "Integer"
346 );
5bbd985a 347 $query->_qill[$grouping][] = ts('Financial Type %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
6a488035
TO
348 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
349 return;
350
351 case 'contribution_page_id':
352 $cPage = $value;
353 $pages = CRM_Contribute_PseudoConstant::contributionPage();
354 $query->_where[$grouping][] = "civicrm_contribution.contribution_page_id = $cPage";
355 $query->_qill[$grouping][] = ts('Contribution Page - %1', array(1 => $pages[$cPage]));
356 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
357 return;
358
359 case 'contribution_pcp_made_through_id':
360 $pcPage = $value;
361 $pcpages = CRM_Contribute_PseudoConstant::pcPage();
362 $query->_where[$grouping][] = "civicrm_contribution_soft.pcp_id = $pcPage";
363 $query->_qill[$grouping][] = ts('Personal Campaign Page - %1', array(1 => $pcpages[$pcPage]));
364 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
365 return;
366
36bf52ef
DS
367 case 'contribution_or_softcredits':
368 if ($value == 'only_scredits') {
3dbf477c 369 $query->_where[$grouping][] = "contribution_search_scredit_combined.scredit_id IS NOT NULL";
36bf52ef 370 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits Only');
ed81a415 371 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
36bf52ef 372 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
3dbf477c
DS
373 } else if ($value == 'both_related') {
374 $query->_where[$grouping][] = "contribution_search_scredit_combined.filter_id IS NOT NULL";
e0b774f0 375 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both But Related');
ed81a415 376 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
3dbf477c 377 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
36bf52ef
DS
378 } else if ($value == 'both') {
379 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both');
ed81a415 380 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
36bf52ef
DS
381 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
382 }
383 // default option: $value == 'only_contribs'
384 return;
385
e88186f4
DS
386 case 'contribution_soft_credit_type_id':
387 $names = array();
388 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type");
389 if (is_array($value)) {
390 $val = array();
391 foreach ($value as $k => $v) {
392 if ($v) {
393 $val[$k] = $v;
394 $names[] = $softCreditTypes[$v];
395 }
396 }
397 $scTypes = (count($val) > 0) ? implode(',', $val) : '';
398 if ($scTypes) {
399 $op = 'IN';
400 $scTypes = "({$scTypes})";
401 }
402 }
403 else {
404 $op = '=';
405 $scTypes = $value;
406 $names[] = $softCreditTypes[$value];
407 }
408 $query->_qill[$grouping][] = ts('Contribution Status %1', array(1 => $op)) . " '" . implode("' " . ts('or') . " '", $names) . "'";
409 $query->_where[$grouping][] =
410 CRM_Contact_BAO_Query::buildClause(
411 "civicrm_contribution_soft.soft_credit_type_id",
412 $op,
413 $scTypes,
414 "Integer"
415 );
416 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
417 return;
418
6a488035
TO
419 case 'contribution_payment_instrument_id':
420 case 'contribution_payment_instrument':
421 $pi = $value;
422 $pis = CRM_Contribute_PseudoConstant::paymentInstrument();
423 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.payment_instrument_id",
424 $op, $value, "Integer"
425 );
426
427 $query->_qill[$grouping][] = ts('Paid By - %1', array(1 => $pis[$pi]));
428 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
429 return;
430
6a488035
TO
431 case 'contribution_status':
432 case 'contribution_status_id':
433 if (is_array($value)) {
434 foreach ($value as $k => $v) {
435 if ($v) {
436 $val[$k] = $k;
437 }
438 }
439
440 $status = implode(',', $val);
441
442 if (count($val) > 1) {
443 $op = 'IN';
444 $status = "({$status})";
445 }
446 }
447 else {
448 $op = '=';
449 $status = $value;
450 }
451
452 $statusValues = CRM_Core_OptionGroup::values("contribution_status");
453
454 $names = array();
455 if (isset($val) &&
456 is_array($val)
457 ) {
458 foreach ($val as $id => $dontCare) {
459 $names[] = $statusValues[$id];
460 }
461 }
462 else {
463 $names[] = $statusValues[$value];
464 }
465
466 $query->_qill[$grouping][] = ts('Contribution Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
467 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_status_id",
468 $op,
469 $status,
470 "Integer"
471 );
472 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
473 return;
474
475 case 'contribution_source':
476 $value = $strtolower(CRM_Core_DAO::escapeString($value));
477 if ($wildcard) {
478 $value = "%$value%";
479 $op = 'LIKE';
480 }
481 $wc = ($op != 'LIKE') ? "LOWER(civicrm_contribution.source)" : "civicrm_contribution.source";
482 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
483 $query->_qill[$grouping][] = ts('Contribution Source %1 %2', array(1 => $op, 2 => $quoteValue));
484 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
485 return;
486
487 case 'contribution_trxn_id':
488 case 'contribution_transaction_id':
489 $wc = ($op != 'LIKE') ? "LOWER(civicrm_contribution.trxn_id)" : "civicrm_contribution.trxn_id";
490 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
491 $query->_qill[$grouping][] = ts('Transaction ID %1 %2', array(1 => $op, 2 => $quoteValue));
492 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
493 return;
494
495 case 'contribution_check_number':
496 $wc = ($op != 'LIKE') ? "LOWER(civicrm_contribution.check_number)" : "civicrm_contribution.check_number";
497 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
498 $query->_qill[$grouping][] = ts('Check Number %1 %2', array(1 => $op, 2 => $quoteValue));
499 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
500 return;
501
502 case 'contribution_is_test':
503 case 'contribution_test':
4b58de38
CW
504 // We dont want to include all tests for sql OR CRM-7827
505 if (!$value || $query->getOperator() != 'OR') {
506 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_test", $op, $value, "Boolean");
507 if ($value) {
508 $query->_qill[$grouping][] = ts("Only Display Test Contributions");
509 }
510 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
6a488035 511 }
6a488035
TO
512 return;
513
514 case 'contribution_is_pay_later':
515 case 'contribution_pay_later':
516 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_pay_later", $op, $value, "Boolean");
517 if ($value) {
518 $query->_qill[$grouping][] = ts("Find Pay Later Contributions");
519 }
520 else {
521 $query->_qill[$grouping][] = ts("Exclude Pay Later Contributions");
522 }
523 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
524 return;
525
526 case 'contribution_recurring':
527 if ($value) {
528 $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NOT NULL";
529 $query->_qill[$grouping][] = ts("Find Recurring Contributions");
530 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
531 }
532 else {
533 $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NULL";
534 $query->_qill[$grouping][] = ts("Exclude Recurring Contributions");
535 }
536 return;
537
538 case 'contribution_recur_id':
539 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_recur_id",
540 $op, $value, "Integer"
541 );
542 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
543 return;
544
545 case 'contribution_id':
546 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.id", $op, $value, "Integer");
547 $query->_qill[$grouping][] = ts('Contribution ID %1 %2', array(1 => $op, 2 => $quoteValue));
548 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
549 return;
550
551 case 'contribution_note':
552 $value = $strtolower(CRM_Core_DAO::escapeString($value));
553 if ($wildcard) {
554 $value = "%$value%";
555 $op = 'LIKE';
556 }
557 $wc = ($op != 'LIKE') ? "LOWER(civicrm_note.note)" : "civicrm_note.note";
558 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
559 $query->_qill[$grouping][] = ts('Contribution Note %1 %2', array(1 => $op, 2 => $quoteValue));
560 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = $query->_whereTables['contribution_note'] = 1;
561 return;
562
563 case 'contribution_membership_id':
564 $query->_where[$grouping][] = " civicrm_membership.id $op $value";
565 $query->_tables['contribution_membership'] = $query->_whereTables['contribution_membership'] = 1;
566
567 return;
568
569 case 'contribution_participant_id':
570 $query->_where[$grouping][] = " civicrm_participant.id $op $value";
571 $query->_tables['contribution_participant'] = $query->_whereTables['contribution_participant'] = 1;
572 return;
573
574 case 'contribution_pcp_display_in_roll':
575 $query->_where[$grouping][] = " civicrm_contribution_soft.pcp_display_in_roll $op '$value'";
576 if ($value) {
577 $query->_qill[$grouping][] = ts("Personal Campaign Page Honor Roll");
578 }
579 else {
580 $query->_qill[$grouping][] = ts("NOT Personal Campaign Page Honor Roll");
581 }
582 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
583 return;
584
585 // Supporting search for currency type -- CRM-4711
586
587 case 'contribution_currency_type':
bd44e0df 588 $currencySymbol = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name'));
6a488035
TO
589 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.currency",
590 $op, $currencySymbol[$value], "String"
591 );
592 $query->_qill[$grouping][] = ts('Currency Type - %1', array(1 => $currencySymbol[$value]));
593 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
594 return;
595
596 case 'contribution_campaign_id':
597 $campParams = array(
598 'op' => $op,
599 'campaign' => $value,
600 'grouping' => $grouping,
601 'tableName' => 'civicrm_contribution',
602 );
603 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
604 return;
605
606 case 'contribution_batch_id':
91aff94c 607 $batches = CRM_Contribute_PseudoConstant::batch();
6a488035
TO
608 $query->_where[$grouping][] = " civicrm_entity_batch.batch_id $op $value";
609 $query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $op, 2 => $batches[$value]));
39b795ba 610 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
6a488035
TO
611 $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1;
612 return;
613
614 default:
615 //all other elements are handle in this case
616 $fldName = substr($name, 13);
ec8c4582
DL
617 if (!isset($fields[$fldName])) {
618 // CRM-12597
619 CRM_Core_Session::setStatus(ts(
fb5b081b 620 'We did not recognize the search field: %1. Please check and fix your contribution related smart groups.',
ec8c4582
DL
621 array(1 => $fldName)
622 )
623 );
624 return;
625 }
6a488035
TO
626 $whereTable = $fields[$fldName];
627 $value = trim($value);
628
629 //contribution fields (decimal fields) which don't require a quote in where clause.
630 $moneyFields = array('non_deductible_amount', 'fee_amount', 'net_amount');
631 //date fields
632 $dateFields = array('receive_date', 'cancel_date', 'receipt_date', 'thankyou_date', 'fulfilled_date');
633
634 if (in_array($fldName, $dateFields)) {
635 $dataType = "Date";
636 }
637 elseif (in_array($fldName, $moneyFields)) {
638 $dataType = "Money";
639 }
640 else {
641 $dataType = "String";
642 }
643
644 $wc = ($op != 'LIKE' && $dataType != 'Date') ? "LOWER($whereTable[where])" : "$whereTable[where]";
645 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, $dataType);
646 $query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue";
647 list($tableName, $fieldName) = explode('.', $whereTable['where'], 2);
648 $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
649 if ($tableName == 'civicrm_contribution_product') {
650 $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
651 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
652 }
653 else {
654 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
655 }
656 }
657 }
658
659 static function from($name, $mode, $side) {
660 $from = NULL;
661 switch ($name) {
3dbf477c
DS
662 case 'civicrm_contribution':
663 $from = " $side JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id ";
664 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
665 // switch the from table if its only soft credit search
666 $from = " $side JOIN contribution_search_scredit_combined ON contribution_search_scredit_combined.contact_id = contact_a.id ";
667 $from .= " $side JOIN civicrm_contribution ON civicrm_contribution.id = contribution_search_scredit_combined.id ";
668 }
669 break;
6a488035
TO
670
671 case 'civicrm_contribution_recur':
86845f9b 672 if ($mode == 1) {
673 // in contact mode join directly onto profile - in case no contributions exist yet
674 $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id ";
675 }
676 else {
677 $from = " $side JOIN civicrm_contribution_recur ON civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id ";
678 }
6a488035
TO
679 break;
680
681 case 'civicrm_financial_type':
682 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
683 $from = " INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
684 }
685 else {
686 $from = " $side JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
687 }
688 break;
689
690 case 'civicrm_accounting_code':
289c8051 691 $from = " $side JOIN civicrm_entity_financial_account ON civicrm_entity_financial_account.entity_id = civicrm_contribution.financial_type_id AND civicrm_entity_financial_account.entity_table = 'civicrm_financial_type' ";
6a488035
TO
692 $from .= " INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id ";
693 $from .= " INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is' ";
694 $from .= " INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship' ";
39b795ba 695 break;
6a488035
TO
696
697 case 'civicrm_contribution_page':
698 $from = " $side JOIN civicrm_contribution_page ON civicrm_contribution.contribution_page ON civicrm_contribution.contribution_page.id";
699 break;
700
701 case 'civicrm_product':
702 $from = " $side JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id";
703 $from .= " $side JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id ";
704 break;
705
706 case 'contribution_payment_instrument':
707 $from = " $side JOIN civicrm_option_group option_group_payment_instrument ON ( option_group_payment_instrument.name = 'payment_instrument')";
708 $from .= " $side JOIN civicrm_option_value payment_instrument ON (civicrm_contribution.payment_instrument_id = payment_instrument.value
709 AND option_group_payment_instrument.id = payment_instrument.option_group_id ) ";
710 break;
711
6a488035
TO
712 case 'contribution_status':
713 $from = " $side JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status')";
714 $from .= " $side JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value
715 AND option_group_contribution_status.id = contribution_status.option_group_id ) ";
716 break;
717
36bf52ef
DS
718 case 'contribution_softcredit_type':
719 $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON
720 (option_group_contribution_softcredit_type.name = 'soft_credit_type')";
721 $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON
722 ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value
723 AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )";
724 break;
725
6a488035
TO
726 case 'contribution_note':
727 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND
728 civicrm_contribution.id = civicrm_note.entity_id )";
729 break;
730
6a488035
TO
731 case 'contribution_membership':
732 $from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id";
733 $from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id ";
734 break;
735
736 case 'contribution_participant':
737 $from = " $side JOIN civicrm_participant_payment ON civicrm_participant_payment.contribution_id = civicrm_contribution.id";
738 $from .= " $side JOIN civicrm_participant ON civicrm_participant_payment.participant_id = civicrm_participant.id ";
739 break;
740
741 case 'civicrm_contribution_soft':
3dbf477c
DS
742 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
743 $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.id = contribution_search_scredit_combined.scredit_id";
744 } else {
745 $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id";
746 }
6a488035
TO
747 break;
748
749 case 'civicrm_contribution_soft_contact':
3dbf477c
DS
750 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
751 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution.contact_id = civicrm_contact_d.id )
752 AND contribution_search_scredit_combined.scredit_id IS NOT NULL";
753 } else {
754 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution_soft.contact_id = civicrm_contact_d.id )";
755 }
6a488035
TO
756 break;
757
758 case 'civicrm_contribution_soft_email':
759 $from .= " $side JOIN civicrm_email as soft_email ON (civicrm_contact_d.id = soft_email.contact_id )";
760 break;
761
762 case 'civicrm_contribution_soft_phone':
763 $from .= " $side JOIN civicrm_phone as soft_phone ON (civicrm_contact_d.id = soft_phone.contact_id )";
764 break;
39b795ba 765
6a488035 766 case 'contribution_batch':
61412579 767 $from .= " $side JOIN civicrm_entity_financial_trxn ON (
768 civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution'
769 AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )";
770
771 $from .= " $side JOIN civicrm_financial_trxn ON (
772 civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )";
773
774 $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn'
775 AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )";
776
6a488035
TO
777 $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id";
778 break;
779 }
780 return $from;
781 }
782
f654cacf 783 static function initializeAnySoftCreditClause(&$query) {
d4e2b978 784 if (self::isSoftCreditOptionEnabled($query->_params)) {
f654cacf 785 if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
ed81a415 786 unset($query->_distinctComponentClause);
f654cacf 787 $query->_rowCountClause = " count(civicrm_contribution.id)";
ed81a415 788 $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
36bf52ef 789 }
f654cacf
DS
790 }
791 }
792
d4e2b978 793 static function isSoftCreditOptionEnabled($queryParams = array()) {
f654cacf
DS
794 static $tempTableFilled = FALSE;
795 if (!empty($queryParams)) {
796 foreach (array_keys($queryParams) as $id) {
797 if (empty($queryParams[$id][0])) {
798 continue;
799 }
800 if ($queryParams[$id][0] == 'contribution_or_softcredits' &&
801 in_array($queryParams[$id][2], array("only_scredits", "both_related", "both"))) {
802 self::$_contribOrSoftCredit = $queryParams[$id][2];
803 }
804 }
805 }
806 if (in_array(self::$_contribOrSoftCredit,
807 array("only_scredits", "both_related", "both"))) {
f654cacf 808 if (!$tempTableFilled) {
3dbf477c
DS
809 $tempQuery = "
810 CREATE TEMPORARY TABLE IF NOT EXISTS contribution_search_scredit_combined AS
811 SELECT con.id as id, con.contact_id, cso.id as filter_id, NULL as scredit_id
812 FROM civicrm_contribution con
813 LEFT JOIN civicrm_contribution_soft cso ON con.id = cso.contribution_id
814 UNION ALL
815 SELECT scredit.contribution_id as id, scredit.contact_id, scredit.id as filter_id, scredit.id as scredit_id
816 FROM civicrm_contribution_soft as scredit";
817 CRM_Core_DAO::executeQuery($tempQuery);
f654cacf 818 $tempTableFilled = TRUE;
3dbf477c 819 }
f654cacf
DS
820 return TRUE;
821 }
36bf52ef
DS
822 return FALSE;
823 }
824
f654cacf 825 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
6a488035
TO
826 $properties = NULL;
827 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
828 $properties = array(
829 'contact_type' => 1,
830 'contact_sub_type' => 1,
831 'sort_name' => 1,
832 'display_name' => 1,
833 'financial_type' => 1,
834 'contribution_source' => 1,
835 'receive_date' => 1,
836 'thankyou_date' => 1,
837 'cancel_date' => 1,
838 'total_amount' => 1,
839 'accounting_code' => 1,
840 'payment_instrument' => 1,
841 'check_number' => 1,
842 'non_deductible_amount' => 1,
843 'fee_amount' => 1,
844 'net_amount' => 1,
845 'trxn_id' => 1,
846 'invoice_id' => 1,
847 'currency' => 1,
848 'cancel_reason' => 1,
849 'receipt_date' => 1,
850 'product_name' => 1,
851 'sku' => 1,
852 'product_option' => 1,
853 'fulfilled_date' => 1,
854 'contribution_start_date' => 1,
855 'contribution_end_date' => 1,
856 'is_test' => 1,
857 'is_pay_later' => 1,
858 'contribution_status' => 1,
859 'contribution_status_id' => 1,
860 'contribution_recur_id' => 1,
861 'amount_level' => 1,
862 'contribution_note' => 1,
863 'contribution_batch' => 1,
36bf52ef 864 'contribution_campaign_id' => 1,
6a488035 865 );
d4e2b978 866 if (self::isSoftCreditOptionEnabled()) {
36bf52ef
DS
867 $properties = array_merge(
868 $properties, array(
81ec6180
DS
869 'contribution_soft_credit_name' => 1,
870 'contribution_soft_credit_amount' => 1,
871 'contribution_soft_credit_type' => 1,
872 'contribution_soft_credit_contribution_id' => 1,
36bf52ef
DS
873 )
874 );
875 }
6a488035
TO
876 if ($includeCustomFields) {
877 // also get all the custom contribution properties
878 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution');
879 if (!empty($fields)) {
880 foreach ($fields as $name => $dontCare) {
881 $properties[$name] = 1;
882 }
883 }
884 }
885 }
886 return $properties;
887 }
888
889 /**
890 * add all the elements shared between contribute search and advnaced search
891 *
892 * @access public
893 *
894 * @return void
895 * @static
896 */
897 static function buildSearchForm(&$form) {
898
899 // Added contribution source
900 $form->addElement('text', 'contribution_source', ts('Contribution Source'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'source'));
901
bc3f7f04 902 CRM_Core_Form_Date::buildDateRange($form, 'contribution_date', 1, '_low', '_high', ts('From:'), FALSE);
6a488035
TO
903
904 $form->add('text', 'contribution_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
905 $form->addRule('contribution_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
906
907 $form->add('text', 'contribution_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
908 $form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
909
910 // Adding select option for curreny type -- CRM-4711
911 $form->add('select', 'contribution_currency_type',
912 ts('Currency Type'),
913 array(
914 '' => ts('- any -')) +
ab345ca5
CW
915 CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')),
916 FALSE, array('class' => 'crm-select2')
6a488035
TO
917 );
918
1f0d8c92 919 // CRM-13848
15cf9198
CW
920 $form->addSelect('financial_type_id',
921 array('entity' => 'contribution', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
6a488035
TO
922 );
923
924 $form->add('select', 'contribution_page_id',
925 ts('Contribution Page'),
926 array(
927 '' => ts('- any -')) +
ab345ca5
CW
928 CRM_Contribute_PseudoConstant::contributionPage(),
929 FALSE, array('class' => 'crm-select2')
6a488035
TO
930 );
931
932
933 $form->add('select', 'contribution_payment_instrument_id',
934 ts('Payment Instrument'),
935 array(
936 '' => ts('- any -')) +
ab345ca5
CW
937 CRM_Contribute_PseudoConstant::paymentInstrument(),
938 FALSE, array('class' => 'crm-select2')
6a488035
TO
939 );
940
941 $form->add('select', 'contribution_pcp_made_through_id',
942 ts('Personal Campaign Page'),
943 array(
944 '' => ts('- any -')) +
ab345ca5
CW
945 CRM_Contribute_PseudoConstant::pcPage(),
946 FALSE, array('class' => 'crm-select2')
6a488035
TO
947 );
948
949 $status = array();
950
951 $statusValues = CRM_Core_OptionGroup::values("contribution_status");
952 // Remove status values that are only used for recurring contributions or pledges (In Progress, Overdue).
953 unset($statusValues['5'], $statusValues['6']);
954
955 foreach ($statusValues as $key => $val) {
956 $status[] = $form->createElement('advcheckbox', $key, NULL, $val);
957 }
958
959 $form->addGroup($status, 'contribution_status_id', ts('Contribution Status'));
960
961 // Add fields for thank you and receipt
8a4f27dc
CW
962 $form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'), TRUE);
963 $form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'), TRUE);
6a488035 964
8a4f27dc
CW
965 $form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'), TRUE);
966 $form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'), TRUE);
b32bc653
CW
967
968 // Recurring contribution fields
969 foreach (self::getRecurringFields() as $key => $label) {
15cf9198 970 CRM_Core_Form_Date::buildDateRange($form, $key, 1, '_low', '_high');
b32bc653
CW
971 // If data has been entered for a recurring field, tell the tpl layer to open the pane
972 if (!empty($form->_formValues[$key . '_relative']) || !empty($form->_formValues[$key . '_low']) || !empty($form->_formValues[$key . '_high'])) {
973 $form->assign('contribution_recur_pane_open', TRUE);
974 }
975 }
a91e698a 976
8a4f27dc 977 $form->addYesNo('contribution_test', ts('Contribution is a Test?'), TRUE);
6a488035
TO
978
979 // Add field for transaction ID search
980 $form->addElement('text', 'contribution_transaction_id', ts("Transaction ID"));
981
982 $form->addElement('text', 'contribution_check_number', ts('Check Number'));
983
984 // Add field for pcp display in roll search
8a4f27dc 985 $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE);
6a488035 986
36bf52ef
DS
987 // Soft credit related fields
988 $options = array(
3dbf477c 989 'only_contribs' => ts('Contributions Only'),
e0b774f0
DS
990 'only_scredits' => ts('Soft Credits Only'),
991 'both_related' => ts('Both But Related'),
36bf52ef
DS
992 'both' => ts('Both'),
993 );
994 $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, array('class' => "crm-select2"));
e88186f4
DS
995 $form->addSelect(
996 'contribution_soft_credit_type_id',
997 array(
998 'entity' => 'contribution_soft',
999 'field' => 'soft_credit_type_id',
1000 'multiple' => 'multiple',
1001 'option_url' => NULL,
1002 'placeholder' => ts('- any -')
1003 )
1004 );
36bf52ef 1005
6a488035
TO
1006 // Add all the custom searchable fields
1007 $contribution = array('Contribution');
1008 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $contribution);
1009 if ($groupDetails) {
1010 $form->assign('contributeGroupTree', $groupDetails);
1011 foreach ($groupDetails as $group) {
1012 foreach ($group['fields'] as $field) {
1013 $fieldId = $field['id'];
1014 $elementName = 'custom_' . $fieldId;
1015 CRM_Core_BAO_CustomField::addQuickFormElement($form,
1016 $elementName,
1017 $fieldId,
1018 FALSE, FALSE, TRUE
1019 );
1020 }
1021 }
1022 }
1023
1024 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'contribution_campaign_id');
1025
1026 // Add batch select
91aff94c 1027 $batches = CRM_Contribute_PseudoConstant::batch();
39b795ba 1028
6a488035
TO
1029 if ( !empty( $batches ) ) {
1030 $form->add('select', 'contribution_batch_id',
1031 ts('Batch Name'),
ab345ca5
CW
1032 array('' => ts('- any -')) + $batches,
1033 FALSE, array('class' => 'crm-select2')
1034 );
6a488035
TO
1035 }
1036
1037 $form->assign('validCiviContribute', TRUE);
1038 $form->setDefaults(array('contribution_test' => 0));
1039 }
1040
6a488035
TO
1041 static function searchAction(&$row, $id) {
1042 }
1043
1044 static function tableNames(&$tables) {
1045 // Add contribution table
a7488080 1046 if (!empty($tables['civicrm_product'])) {
6a488035
TO
1047 $tables = array_merge(array('civicrm_contribution' => 1), $tables);
1048 }
1049
8cc574cf 1050 if (!empty($tables['civicrm_contribution_product']) && empty($tables['civicrm_product'])) {
6a488035
TO
1051 $tables['civicrm_product'] = 1;
1052 }
1053 }
a91e698a 1054
1055 /**
1056 * Add the where for dates
1057 * @param array $values array of query values
1058 * @param object $query the query object
1059 * @param string $name query field that is set
1060 * @param string $field name of field to be set
1061 * @param string $title title of the field
1062 */
1063 static function buildDateWhere(&$values, $query, $name, $field, $title) {
1064 $fieldPart = strpos($name, $field);
1065 if($fieldPart === FALSE) {
1066 return;
1067 }
1068 // we only have recurring dates using this ATM so lets' short cut to find the table name
1069 $table = 'contribution_recur';
1070 $fieldName = split($table . '_', $field);
1071 $query->dateQueryBuilder($values,
1072 'civicrm_' . $table, $field, $fieldName[1], $title
1073 );
1074 return TRUE;
1075 }
b32bc653
CW
1076
1077 static function getRecurringFields() {
1078 return array(
1079 'contribution_recur_start_date' => ts('Recurring Contribution Start Date'),
1080 'contribution_recur_next_sched_contribution_date' => ts('Next Scheduled Recurring Contribution'),
1081 'contribution_recur_cancel_date' => ts('Recurring Contribution Cancel Date'),
1082 'contribution_recur_end_date' => ts('Recurring Contribution End Date'),
1083 'contribution_recur_create_date' => ('Recurring Contribution Create Date'),
1084 'contribution_recur_modified_date' => ('Recurring Contribution Modified Date'),
1085 'contribution_recur_failure_retry_date' => ts('Failed Recurring Contribution Retry Date'),
1086 );
1087 }
6a488035
TO
1088}
1089