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