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