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