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