soft credit statistics, hyperlink for soft-credit-for-name
[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
DS
370 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits Only');
371 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
3dbf477c
DS
372 } else if ($value == 'both_related') {
373 $query->_where[$grouping][] = "contribution_search_scredit_combined.filter_id IS NOT NULL";
374 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Related To Each Other');
375 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
36bf52ef
DS
376 } else if ($value == 'both') {
377 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both');
378 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
379 }
380 // default option: $value == 'only_contribs'
381 return;
382
e88186f4
DS
383 case 'contribution_soft_credit_type_id':
384 $names = array();
385 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type");
386 if (is_array($value)) {
387 $val = array();
388 foreach ($value as $k => $v) {
389 if ($v) {
390 $val[$k] = $v;
391 $names[] = $softCreditTypes[$v];
392 }
393 }
394 $scTypes = (count($val) > 0) ? implode(',', $val) : '';
395 if ($scTypes) {
396 $op = 'IN';
397 $scTypes = "({$scTypes})";
398 }
399 }
400 else {
401 $op = '=';
402 $scTypes = $value;
403 $names[] = $softCreditTypes[$value];
404 }
405 $query->_qill[$grouping][] = ts('Contribution Status %1', array(1 => $op)) . " '" . implode("' " . ts('or') . " '", $names) . "'";
406 $query->_where[$grouping][] =
407 CRM_Contact_BAO_Query::buildClause(
408 "civicrm_contribution_soft.soft_credit_type_id",
409 $op,
410 $scTypes,
411 "Integer"
412 );
413 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
414 return;
415
6a488035
TO
416 case 'contribution_payment_instrument_id':
417 case 'contribution_payment_instrument':
418 $pi = $value;
419 $pis = CRM_Contribute_PseudoConstant::paymentInstrument();
420 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.payment_instrument_id",
421 $op, $value, "Integer"
422 );
423
424 $query->_qill[$grouping][] = ts('Paid By - %1', array(1 => $pis[$pi]));
425 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
426 return;
427
6a488035
TO
428 case 'contribution_status':
429 case 'contribution_status_id':
430 if (is_array($value)) {
431 foreach ($value as $k => $v) {
432 if ($v) {
433 $val[$k] = $k;
434 }
435 }
436
437 $status = implode(',', $val);
438
439 if (count($val) > 1) {
440 $op = 'IN';
441 $status = "({$status})";
442 }
443 }
444 else {
445 $op = '=';
446 $status = $value;
447 }
448
449 $statusValues = CRM_Core_OptionGroup::values("contribution_status");
450
451 $names = array();
452 if (isset($val) &&
453 is_array($val)
454 ) {
455 foreach ($val as $id => $dontCare) {
456 $names[] = $statusValues[$id];
457 }
458 }
459 else {
460 $names[] = $statusValues[$value];
461 }
462
463 $query->_qill[$grouping][] = ts('Contribution Status %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names);
464 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_status_id",
465 $op,
466 $status,
467 "Integer"
468 );
469 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
470 return;
471
472 case 'contribution_source':
473 $value = $strtolower(CRM_Core_DAO::escapeString($value));
474 if ($wildcard) {
475 $value = "%$value%";
476 $op = 'LIKE';
477 }
478 $wc = ($op != 'LIKE') ? "LOWER(civicrm_contribution.source)" : "civicrm_contribution.source";
479 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
480 $query->_qill[$grouping][] = ts('Contribution Source %1 %2', array(1 => $op, 2 => $quoteValue));
481 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
482 return;
483
484 case 'contribution_trxn_id':
485 case 'contribution_transaction_id':
486 $wc = ($op != 'LIKE') ? "LOWER(civicrm_contribution.trxn_id)" : "civicrm_contribution.trxn_id";
487 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
488 $query->_qill[$grouping][] = ts('Transaction ID %1 %2', array(1 => $op, 2 => $quoteValue));
489 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
490 return;
491
492 case 'contribution_check_number':
493 $wc = ($op != 'LIKE') ? "LOWER(civicrm_contribution.check_number)" : "civicrm_contribution.check_number";
494 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
495 $query->_qill[$grouping][] = ts('Check Number %1 %2', array(1 => $op, 2 => $quoteValue));
496 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
497 return;
498
499 case 'contribution_is_test':
500 case 'contribution_test':
4b58de38
CW
501 // We dont want to include all tests for sql OR CRM-7827
502 if (!$value || $query->getOperator() != 'OR') {
503 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_test", $op, $value, "Boolean");
504 if ($value) {
505 $query->_qill[$grouping][] = ts("Only Display Test Contributions");
506 }
507 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
6a488035 508 }
6a488035
TO
509 return;
510
511 case 'contribution_is_pay_later':
512 case 'contribution_pay_later':
513 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_pay_later", $op, $value, "Boolean");
514 if ($value) {
515 $query->_qill[$grouping][] = ts("Find Pay Later Contributions");
516 }
517 else {
518 $query->_qill[$grouping][] = ts("Exclude Pay Later Contributions");
519 }
520 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
521 return;
522
523 case 'contribution_recurring':
524 if ($value) {
525 $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NOT NULL";
526 $query->_qill[$grouping][] = ts("Find Recurring Contributions");
527 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
528 }
529 else {
530 $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NULL";
531 $query->_qill[$grouping][] = ts("Exclude Recurring Contributions");
532 }
533 return;
534
535 case 'contribution_recur_id':
536 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_recur_id",
537 $op, $value, "Integer"
538 );
539 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
540 return;
541
542 case 'contribution_id':
543 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.id", $op, $value, "Integer");
544 $query->_qill[$grouping][] = ts('Contribution ID %1 %2', array(1 => $op, 2 => $quoteValue));
545 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
546 return;
547
548 case 'contribution_note':
549 $value = $strtolower(CRM_Core_DAO::escapeString($value));
550 if ($wildcard) {
551 $value = "%$value%";
552 $op = 'LIKE';
553 }
554 $wc = ($op != 'LIKE') ? "LOWER(civicrm_note.note)" : "civicrm_note.note";
555 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String");
556 $query->_qill[$grouping][] = ts('Contribution Note %1 %2', array(1 => $op, 2 => $quoteValue));
557 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = $query->_whereTables['contribution_note'] = 1;
558 return;
559
560 case 'contribution_membership_id':
561 $query->_where[$grouping][] = " civicrm_membership.id $op $value";
562 $query->_tables['contribution_membership'] = $query->_whereTables['contribution_membership'] = 1;
563
564 return;
565
566 case 'contribution_participant_id':
567 $query->_where[$grouping][] = " civicrm_participant.id $op $value";
568 $query->_tables['contribution_participant'] = $query->_whereTables['contribution_participant'] = 1;
569 return;
570
571 case 'contribution_pcp_display_in_roll':
572 $query->_where[$grouping][] = " civicrm_contribution_soft.pcp_display_in_roll $op '$value'";
573 if ($value) {
574 $query->_qill[$grouping][] = ts("Personal Campaign Page Honor Roll");
575 }
576 else {
577 $query->_qill[$grouping][] = ts("NOT Personal Campaign Page Honor Roll");
578 }
579 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
580 return;
581
582 // Supporting search for currency type -- CRM-4711
583
584 case 'contribution_currency_type':
bd44e0df 585 $currencySymbol = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name'));
6a488035
TO
586 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.currency",
587 $op, $currencySymbol[$value], "String"
588 );
589 $query->_qill[$grouping][] = ts('Currency Type - %1', array(1 => $currencySymbol[$value]));
590 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
591 return;
592
593 case 'contribution_campaign_id':
594 $campParams = array(
595 'op' => $op,
596 'campaign' => $value,
597 'grouping' => $grouping,
598 'tableName' => 'civicrm_contribution',
599 );
600 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
601 return;
602
603 case 'contribution_batch_id':
91aff94c 604 $batches = CRM_Contribute_PseudoConstant::batch();
6a488035
TO
605 $query->_where[$grouping][] = " civicrm_entity_batch.batch_id $op $value";
606 $query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $op, 2 => $batches[$value]));
39b795ba 607 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
6a488035
TO
608 $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1;
609 return;
610
611 default:
612 //all other elements are handle in this case
613 $fldName = substr($name, 13);
ec8c4582
DL
614 if (!isset($fields[$fldName])) {
615 // CRM-12597
616 CRM_Core_Session::setStatus(ts(
fb5b081b 617 'We did not recognize the search field: %1. Please check and fix your contribution related smart groups.',
ec8c4582
DL
618 array(1 => $fldName)
619 )
620 );
621 return;
622 }
6a488035
TO
623 $whereTable = $fields[$fldName];
624 $value = trim($value);
625
626 //contribution fields (decimal fields) which don't require a quote in where clause.
627 $moneyFields = array('non_deductible_amount', 'fee_amount', 'net_amount');
628 //date fields
629 $dateFields = array('receive_date', 'cancel_date', 'receipt_date', 'thankyou_date', 'fulfilled_date');
630
631 if (in_array($fldName, $dateFields)) {
632 $dataType = "Date";
633 }
634 elseif (in_array($fldName, $moneyFields)) {
635 $dataType = "Money";
636 }
637 else {
638 $dataType = "String";
639 }
640
641 $wc = ($op != 'LIKE' && $dataType != 'Date') ? "LOWER($whereTable[where])" : "$whereTable[where]";
642 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, $dataType);
643 $query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue";
644 list($tableName, $fieldName) = explode('.', $whereTable['where'], 2);
645 $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
646 if ($tableName == 'civicrm_contribution_product') {
647 $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
648 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
649 }
650 else {
651 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
652 }
653 }
654 }
655
656 static function from($name, $mode, $side) {
657 $from = NULL;
658 switch ($name) {
3dbf477c
DS
659 case 'civicrm_contribution':
660 $from = " $side JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id ";
661 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
662 // switch the from table if its only soft credit search
663 $from = " $side JOIN contribution_search_scredit_combined ON contribution_search_scredit_combined.contact_id = contact_a.id ";
664 $from .= " $side JOIN civicrm_contribution ON civicrm_contribution.id = contribution_search_scredit_combined.id ";
665 }
666 break;
6a488035
TO
667
668 case 'civicrm_contribution_recur':
86845f9b 669 if ($mode == 1) {
670 // in contact mode join directly onto profile - in case no contributions exist yet
671 $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id ";
672 }
673 else {
674 $from = " $side JOIN civicrm_contribution_recur ON civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id ";
675 }
6a488035
TO
676 break;
677
678 case 'civicrm_financial_type':
679 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
680 $from = " INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
681 }
682 else {
683 $from = " $side JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
684 }
685 break;
686
687 case 'civicrm_accounting_code':
289c8051 688 $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
689 $from .= " INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id ";
690 $from .= " INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is' ";
691 $from .= " INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship' ";
39b795ba 692 break;
6a488035
TO
693
694 case 'civicrm_contribution_page':
695 $from = " $side JOIN civicrm_contribution_page ON civicrm_contribution.contribution_page ON civicrm_contribution.contribution_page.id";
696 break;
697
698 case 'civicrm_product':
699 $from = " $side JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id";
700 $from .= " $side JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id ";
701 break;
702
703 case 'contribution_payment_instrument':
704 $from = " $side JOIN civicrm_option_group option_group_payment_instrument ON ( option_group_payment_instrument.name = 'payment_instrument')";
705 $from .= " $side JOIN civicrm_option_value payment_instrument ON (civicrm_contribution.payment_instrument_id = payment_instrument.value
706 AND option_group_payment_instrument.id = payment_instrument.option_group_id ) ";
707 break;
708
6a488035
TO
709 case 'contribution_status':
710 $from = " $side JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status')";
711 $from .= " $side JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value
712 AND option_group_contribution_status.id = contribution_status.option_group_id ) ";
713 break;
714
36bf52ef
DS
715 case 'contribution_softcredit_type':
716 $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON
717 (option_group_contribution_softcredit_type.name = 'soft_credit_type')";
718 $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON
719 ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value
720 AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )";
721 break;
722
6a488035
TO
723 case 'contribution_note':
724 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND
725 civicrm_contribution.id = civicrm_note.entity_id )";
726 break;
727
6a488035
TO
728 case 'contribution_membership':
729 $from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id";
730 $from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id ";
731 break;
732
733 case 'contribution_participant':
734 $from = " $side JOIN civicrm_participant_payment ON civicrm_participant_payment.contribution_id = civicrm_contribution.id";
735 $from .= " $side JOIN civicrm_participant ON civicrm_participant_payment.participant_id = civicrm_participant.id ";
736 break;
737
738 case 'civicrm_contribution_soft':
3dbf477c
DS
739 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
740 $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.id = contribution_search_scredit_combined.scredit_id";
741 } else {
742 $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id";
743 }
6a488035
TO
744 break;
745
746 case 'civicrm_contribution_soft_contact':
3dbf477c
DS
747 if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) {
748 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution.contact_id = civicrm_contact_d.id )
749 AND contribution_search_scredit_combined.scredit_id IS NOT NULL";
750 } else {
751 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution_soft.contact_id = civicrm_contact_d.id )";
752 }
6a488035
TO
753 break;
754
755 case 'civicrm_contribution_soft_email':
756 $from .= " $side JOIN civicrm_email as soft_email ON (civicrm_contact_d.id = soft_email.contact_id )";
757 break;
758
759 case 'civicrm_contribution_soft_phone':
760 $from .= " $side JOIN civicrm_phone as soft_phone ON (civicrm_contact_d.id = soft_phone.contact_id )";
761 break;
39b795ba 762
6a488035 763 case 'contribution_batch':
61412579 764 $from .= " $side JOIN civicrm_entity_financial_trxn ON (
765 civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution'
766 AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )";
767
768 $from .= " $side JOIN civicrm_financial_trxn ON (
769 civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )";
770
771 $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn'
772 AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )";
773
6a488035
TO
774 $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id";
775 break;
776 }
777 return $from;
778 }
779
f654cacf 780 static function initializeAnySoftCreditClause(&$query) {
d4e2b978 781 if (self::isSoftCreditOptionEnabled($query->_params)) {
f654cacf
DS
782 if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
783 unset($query->_distinctComponentClause, $query->_groupByComponentClause);
784 $query->_rowCountClause = " count(civicrm_contribution.id)";
36bf52ef 785 }
f654cacf
DS
786 }
787 }
788
d4e2b978 789 static function isSoftCreditOptionEnabled($queryParams = array()) {
f654cacf
DS
790 static $tempTableFilled = FALSE;
791 if (!empty($queryParams)) {
792 foreach (array_keys($queryParams) as $id) {
793 if (empty($queryParams[$id][0])) {
794 continue;
795 }
796 if ($queryParams[$id][0] == 'contribution_or_softcredits' &&
797 in_array($queryParams[$id][2], array("only_scredits", "both_related", "both"))) {
798 self::$_contribOrSoftCredit = $queryParams[$id][2];
799 }
800 }
801 }
802 if (in_array(self::$_contribOrSoftCredit,
803 array("only_scredits", "both_related", "both"))) {
f654cacf 804 if (!$tempTableFilled) {
3dbf477c
DS
805 $tempQuery = "
806 CREATE TEMPORARY TABLE IF NOT EXISTS contribution_search_scredit_combined AS
807 SELECT con.id as id, con.contact_id, cso.id as filter_id, NULL as scredit_id
808 FROM civicrm_contribution con
809 LEFT JOIN civicrm_contribution_soft cso ON con.id = cso.contribution_id
810 UNION ALL
811 SELECT scredit.contribution_id as id, scredit.contact_id, scredit.id as filter_id, scredit.id as scredit_id
812 FROM civicrm_contribution_soft as scredit";
813 CRM_Core_DAO::executeQuery($tempQuery);
f654cacf 814 $tempTableFilled = TRUE;
3dbf477c 815 }
f654cacf
DS
816 return TRUE;
817 }
36bf52ef
DS
818 return FALSE;
819 }
820
f654cacf 821 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
6a488035
TO
822 $properties = NULL;
823 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
824 $properties = array(
825 'contact_type' => 1,
826 'contact_sub_type' => 1,
827 'sort_name' => 1,
828 'display_name' => 1,
829 'financial_type' => 1,
830 'contribution_source' => 1,
831 'receive_date' => 1,
832 'thankyou_date' => 1,
833 'cancel_date' => 1,
834 'total_amount' => 1,
835 'accounting_code' => 1,
836 'payment_instrument' => 1,
837 'check_number' => 1,
838 'non_deductible_amount' => 1,
839 'fee_amount' => 1,
840 'net_amount' => 1,
841 'trxn_id' => 1,
842 'invoice_id' => 1,
843 'currency' => 1,
844 'cancel_reason' => 1,
845 'receipt_date' => 1,
846 'product_name' => 1,
847 'sku' => 1,
848 'product_option' => 1,
849 'fulfilled_date' => 1,
850 'contribution_start_date' => 1,
851 'contribution_end_date' => 1,
852 'is_test' => 1,
853 'is_pay_later' => 1,
854 'contribution_status' => 1,
855 'contribution_status_id' => 1,
856 'contribution_recur_id' => 1,
857 'amount_level' => 1,
858 'contribution_note' => 1,
859 'contribution_batch' => 1,
36bf52ef 860 'contribution_campaign_id' => 1,
6a488035 861 );
d4e2b978 862 if (self::isSoftCreditOptionEnabled()) {
36bf52ef
DS
863 $properties = array_merge(
864 $properties, array(
81ec6180
DS
865 'contribution_soft_credit_name' => 1,
866 'contribution_soft_credit_amount' => 1,
867 'contribution_soft_credit_type' => 1,
868 'contribution_soft_credit_contribution_id' => 1,
36bf52ef
DS
869 )
870 );
871 }
6a488035
TO
872 if ($includeCustomFields) {
873 // also get all the custom contribution properties
874 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution');
875 if (!empty($fields)) {
876 foreach ($fields as $name => $dontCare) {
877 $properties[$name] = 1;
878 }
879 }
880 }
881 }
882 return $properties;
883 }
884
885 /**
886 * add all the elements shared between contribute search and advnaced search
887 *
888 * @access public
889 *
890 * @return void
891 * @static
892 */
893 static function buildSearchForm(&$form) {
894
895 // Added contribution source
896 $form->addElement('text', 'contribution_source', ts('Contribution Source'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'source'));
897
bc3f7f04 898 CRM_Core_Form_Date::buildDateRange($form, 'contribution_date', 1, '_low', '_high', ts('From:'), FALSE);
6a488035
TO
899
900 $form->add('text', 'contribution_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
901 $form->addRule('contribution_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
902
903 $form->add('text', 'contribution_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
904 $form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
905
906 // Adding select option for curreny type -- CRM-4711
907 $form->add('select', 'contribution_currency_type',
908 ts('Currency Type'),
909 array(
910 '' => ts('- any -')) +
ab345ca5
CW
911 CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')),
912 FALSE, array('class' => 'crm-select2')
6a488035
TO
913 );
914
1f0d8c92 915 // CRM-13848
15cf9198
CW
916 $form->addSelect('financial_type_id',
917 array('entity' => 'contribution', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
6a488035
TO
918 );
919
920 $form->add('select', 'contribution_page_id',
921 ts('Contribution Page'),
922 array(
923 '' => ts('- any -')) +
ab345ca5
CW
924 CRM_Contribute_PseudoConstant::contributionPage(),
925 FALSE, array('class' => 'crm-select2')
6a488035
TO
926 );
927
928
929 $form->add('select', 'contribution_payment_instrument_id',
930 ts('Payment Instrument'),
931 array(
932 '' => ts('- any -')) +
ab345ca5
CW
933 CRM_Contribute_PseudoConstant::paymentInstrument(),
934 FALSE, array('class' => 'crm-select2')
6a488035
TO
935 );
936
937 $form->add('select', 'contribution_pcp_made_through_id',
938 ts('Personal Campaign Page'),
939 array(
940 '' => ts('- any -')) +
ab345ca5
CW
941 CRM_Contribute_PseudoConstant::pcPage(),
942 FALSE, array('class' => 'crm-select2')
6a488035
TO
943 );
944
945 $status = array();
946
947 $statusValues = CRM_Core_OptionGroup::values("contribution_status");
948 // Remove status values that are only used for recurring contributions or pledges (In Progress, Overdue).
949 unset($statusValues['5'], $statusValues['6']);
950
951 foreach ($statusValues as $key => $val) {
952 $status[] = $form->createElement('advcheckbox', $key, NULL, $val);
953 }
954
955 $form->addGroup($status, 'contribution_status_id', ts('Contribution Status'));
956
957 // Add fields for thank you and receipt
8a4f27dc
CW
958 $form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'), TRUE);
959 $form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'), TRUE);
6a488035 960
8a4f27dc
CW
961 $form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'), TRUE);
962 $form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'), TRUE);
b32bc653
CW
963
964 // Recurring contribution fields
965 foreach (self::getRecurringFields() as $key => $label) {
15cf9198 966 CRM_Core_Form_Date::buildDateRange($form, $key, 1, '_low', '_high');
b32bc653
CW
967 // If data has been entered for a recurring field, tell the tpl layer to open the pane
968 if (!empty($form->_formValues[$key . '_relative']) || !empty($form->_formValues[$key . '_low']) || !empty($form->_formValues[$key . '_high'])) {
969 $form->assign('contribution_recur_pane_open', TRUE);
970 }
971 }
a91e698a 972
8a4f27dc 973 $form->addYesNo('contribution_test', ts('Contribution is a Test?'), TRUE);
6a488035
TO
974
975 // Add field for transaction ID search
976 $form->addElement('text', 'contribution_transaction_id', ts("Transaction ID"));
977
978 $form->addElement('text', 'contribution_check_number', ts('Check Number'));
979
980 // Add field for pcp display in roll search
8a4f27dc 981 $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE);
6a488035 982
36bf52ef
DS
983 // Soft credit related fields
984 $options = array(
3dbf477c 985 'both_related' => ts('Related To Each Other'),
e88186f4 986 'only_scredits' => ts('Soft Credits Only'),
3dbf477c 987 'only_contribs' => ts('Contributions Only'),
36bf52ef
DS
988 'both' => ts('Both'),
989 );
990 $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, array('class' => "crm-select2"));
e88186f4
DS
991 $form->addSelect(
992 'contribution_soft_credit_type_id',
993 array(
994 'entity' => 'contribution_soft',
995 'field' => 'soft_credit_type_id',
996 'multiple' => 'multiple',
997 'option_url' => NULL,
998 'placeholder' => ts('- any -')
999 )
1000 );
36bf52ef 1001
6a488035
TO
1002 // Add all the custom searchable fields
1003 $contribution = array('Contribution');
1004 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $contribution);
1005 if ($groupDetails) {
1006 $form->assign('contributeGroupTree', $groupDetails);
1007 foreach ($groupDetails as $group) {
1008 foreach ($group['fields'] as $field) {
1009 $fieldId = $field['id'];
1010 $elementName = 'custom_' . $fieldId;
1011 CRM_Core_BAO_CustomField::addQuickFormElement($form,
1012 $elementName,
1013 $fieldId,
1014 FALSE, FALSE, TRUE
1015 );
1016 }
1017 }
1018 }
1019
1020 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'contribution_campaign_id');
1021
1022 // Add batch select
91aff94c 1023 $batches = CRM_Contribute_PseudoConstant::batch();
39b795ba 1024
6a488035
TO
1025 if ( !empty( $batches ) ) {
1026 $form->add('select', 'contribution_batch_id',
1027 ts('Batch Name'),
ab345ca5
CW
1028 array('' => ts('- any -')) + $batches,
1029 FALSE, array('class' => 'crm-select2')
1030 );
6a488035
TO
1031 }
1032
1033 $form->assign('validCiviContribute', TRUE);
1034 $form->setDefaults(array('contribution_test' => 0));
1035 }
1036
6a488035
TO
1037 static function searchAction(&$row, $id) {
1038 }
1039
1040 static function tableNames(&$tables) {
1041 // Add contribution table
a7488080 1042 if (!empty($tables['civicrm_product'])) {
6a488035
TO
1043 $tables = array_merge(array('civicrm_contribution' => 1), $tables);
1044 }
1045
8cc574cf 1046 if (!empty($tables['civicrm_contribution_product']) && empty($tables['civicrm_product'])) {
6a488035
TO
1047 $tables['civicrm_product'] = 1;
1048 }
1049 }
a91e698a 1050
1051 /**
1052 * Add the where for dates
1053 * @param array $values array of query values
1054 * @param object $query the query object
1055 * @param string $name query field that is set
1056 * @param string $field name of field to be set
1057 * @param string $title title of the field
1058 */
1059 static function buildDateWhere(&$values, $query, $name, $field, $title) {
1060 $fieldPart = strpos($name, $field);
1061 if($fieldPart === FALSE) {
1062 return;
1063 }
1064 // we only have recurring dates using this ATM so lets' short cut to find the table name
1065 $table = 'contribution_recur';
1066 $fieldName = split($table . '_', $field);
1067 $query->dateQueryBuilder($values,
1068 'civicrm_' . $table, $field, $fieldName[1], $title
1069 );
1070 return TRUE;
1071 }
b32bc653
CW
1072
1073 static function getRecurringFields() {
1074 return array(
1075 'contribution_recur_start_date' => ts('Recurring Contribution Start Date'),
1076 'contribution_recur_next_sched_contribution_date' => ts('Next Scheduled Recurring Contribution'),
1077 'contribution_recur_cancel_date' => ts('Recurring Contribution Cancel Date'),
1078 'contribution_recur_end_date' => ts('Recurring Contribution End Date'),
1079 'contribution_recur_create_date' => ('Recurring Contribution Create Date'),
1080 'contribution_recur_modified_date' => ('Recurring Contribution Modified Date'),
1081 'contribution_recur_failure_retry_date' => ts('Failed Recurring Contribution Retry Date'),
1082 );
1083 }
6a488035
TO
1084}
1085