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