Merge pull request #15976 from eileenmcnaughton/5.20
[civicrm-core.git] / CRM / Contribute / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query {
34
35 public static $_contribOrSoftCredit = "only_contribs";
36
37 public static $_contribRecurPayment = NULL;
38
39 /**
40 * Function get the searchable fields for contribution.
41 *
42 * This is basically the contribution fields plus some related entity fields.
43 *
44 * @param bool $checkPermission
45 *
46 * @return array
47 * Associative array of contribution fields
48 */
49 public static function getFields($checkPermission = TRUE) {
50 if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['fields']) || !isset(\Civi::$statics[__CLASS__]['fields']['contribution'])) {
51 $recurFields = CRM_Contribute_DAO_ContributionRecur::fields();
52 foreach ($recurFields as $fieldKey => $field) {
53 // We can only safely add in those with unique names as those without could clobber others.
54 // The array is keyed by unique names so if it doesn't match the key there is no unique name & we unset
55 // Refer to CRM_Contribute_Form_SearchTest for existing tests ... and to add more!
56 if ($field['name'] === $fieldKey) {
57 unset($recurFields[$fieldKey]);
58 }
59 }
60 $fields = array_merge($recurFields, CRM_Contribute_BAO_Contribution::exportableFields($checkPermission));
61 CRM_Contribute_BAO_Contribution::appendPseudoConstantsToFields($fields);
62 unset($fields['contribution_contact_id']);
63 \Civi::$statics[__CLASS__]['fields']['contribution'] = $fields;
64 }
65 return \Civi::$statics[__CLASS__]['fields']['contribution'];
66 }
67
68 /**
69 * If contributions are involved, add the specific contribute fields.
70 *
71 * @param CRM_Contact_BAO_Query $query
72 */
73 public static function select(&$query) {
74 // if contribute mode add contribution id
75 if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
76 $query->_select['contribution_id'] = "civicrm_contribution.id as contribution_id";
77 $query->_element['contribution_id'] = 1;
78 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
79 }
80
81 // get accounting code
82 if (!empty($query->_returnProperties['accounting_code'])) {
83 $query->_select['accounting_code'] = "civicrm_financial_account.accounting_code as accounting_code";
84 $query->_element['accounting_code'] = 1;
85 $query->_tables['civicrm_accounting_code'] = 1;
86 $query->_tables['civicrm_financial_account'] = 1;
87 }
88
89 if (!empty($query->_returnProperties['contribution_note'])) {
90 $query->_select['contribution_note'] = "civicrm_note.note as contribution_note";
91 $query->_element['contribution_note'] = 1;
92 $query->_tables['contribution_note'] = 1;
93 $query->_tables['civicrm_contribution'] = 1;
94 }
95
96 if (!empty($query->_returnProperties['contribution_batch'])) {
97 $query->_select['contribution_batch'] = "civicrm_batch.title as contribution_batch";
98 $query->_element['contribution_batch'] = 1;
99 $query->_tables['civicrm_financial_trxn'] = 1;
100 $query->_tables['contribution_batch'] = 1;
101 }
102
103 if (!empty($query->_returnProperties['contribution_campaign_title'])) {
104 $query->_select['contribution_campaign_title'] = "civicrm_campaign.title as contribution_campaign_title";
105 $query->_element['contribution_campaign_title'] = $query->_tables['civicrm_campaign'] = 1;
106 }
107
108 self::addSoftCreditFields($query);
109 }
110
111 /**
112 * Get where clause.
113 *
114 * @param CRM_Contact_BAO_Query $query
115 */
116 public static function where(&$query) {
117 self::initializeAnySoftCreditClause($query);
118 foreach (array_keys($query->_params) as $id) {
119 if (empty($query->_params[$id][0])) {
120 continue;
121 }
122 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_') {
123 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
124 $query->_useDistinct = TRUE;
125 }
126 // CRM-12065
127 if (
128 $query->_params[$id][0] == 'contribution_type_id' ||
129 $query->_params[$id][0] == 'contribution_type'
130 ) {
131 CRM_Core_Session::setStatus(
132 ts('The contribution type criteria is now obsolete, please update your smart group'),
133 '',
134 'alert'
135 );
136 continue;
137 }
138
139 self::whereClauseSingle($query->_params[$id], $query);
140 }
141 }
142 }
143
144 /**
145 * Get where clause for a single value.
146 *
147 * @param array $values
148 * @param CRM_Contact_BAO_Query $query
149 *
150 * @throws \CiviCRM_API3_Exception
151 * @throws \CRM_Core_Exception
152 */
153 public static function whereClauseSingle(&$values, &$query) {
154 list($name, $op, $value, $grouping, $wildcard) = $values;
155
156 $quoteValue = NULL;
157 $fields = self::getFields();
158
159 if (!empty($value) && !is_array($value)) {
160 $quoteValue = "\"$value\"";
161 }
162
163 $fieldAliases = self::getLegacySupportedFields();
164
165 $fieldName = $name = self::getFieldName($values);
166 $qillName = $name;
167 if (in_array($name, $fieldAliases)) {
168 $qillName = array_search($name, $fieldAliases);
169 }
170 $pseudoExtraParam = [];
171 $fieldSpec = CRM_Utils_Array::value($fieldName, $fields, []);
172 $tableName = CRM_Utils_Array::value('table_name', $fieldSpec, 'civicrm_contribution');
173 $dataType = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $fieldSpec));
174 if ($dataType === 'Timestamp' || $dataType === 'Date') {
175 $title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title'];
176 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
177 $query->dateQueryBuilder($values,
178 $tableName, $fieldName, $fieldSpec['name'], $title
179 );
180 return;
181 }
182
183 switch ($name) {
184 case 'contribution_date':
185 case 'contribution_date_low':
186 case 'contribution_date_low_time':
187 case 'contribution_date_high':
188 case 'contribution_date_high_time':
189 CRM_Core_Error::deprecatedFunctionWarning('search by receive_date');
190 // process to / from date
191 $query->dateQueryBuilder($values,
192 'civicrm_contribution', 'contribution_date', 'receive_date', ts('Contribution Date')
193 );
194 return;
195
196 case 'contribution_amount':
197 case 'contribution_amount_low':
198 case 'contribution_amount_high':
199 // process min/max amount
200 $query->numberRangeBuilder($values,
201 'civicrm_contribution', 'contribution_amount',
202 'total_amount', 'Contribution Amount',
203 NULL
204 );
205 return;
206
207 case 'contribution_thankyou_date_is_not_null':
208 if ($value) {
209 $op = "IS NOT NULL";
210 $query->_qill[$grouping][] = ts('Contribution Thank-you Sent');
211 }
212 else {
213 $op = "IS NULL";
214 $query->_qill[$grouping][] = ts('Contribution Thank-you Not Sent');
215 }
216 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.thankyou_date", $op);
217 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
218 return;
219
220 case 'contribution_receipt_date_is_not_null':
221 if ($value) {
222 $op = "IS NOT NULL";
223 $query->_qill[$grouping][] = ts('Contribution Receipt Sent');
224 }
225 else {
226 $op = "IS NULL";
227 $query->_qill[$grouping][] = ts('Contribution Receipt Not Sent');
228 }
229 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.receipt_date", $op);
230 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
231 return;
232
233 case 'financial_type_id':
234 case 'invoice_id':
235 case 'invoice_number':
236 case 'payment_instrument_id':
237 case 'contribution_payment_instrument_id':
238 case 'contribution_page_id':
239 case 'contribution_status_id':
240 case 'contribution_id':
241 case 'contribution_currency_type':
242 case 'contribution_currency':
243 case 'contribution_source':
244 case 'contribution_trxn_id':
245 case 'contribution_check_number':
246 case 'contribution_contact_id':
247 case (strpos($name, '_amount') !== FALSE):
248 case 'contribution_campaign_id':
249
250 $fieldNamesNotToStripContributionFrom = [
251 'contribution_currency_type',
252 'contribution_status_id',
253 'contribution_page_id',
254 ];
255 // @todo these are mostly legacy params. Find a better way to deal with them.
256 if (!in_array($name, $fieldNamesNotToStripContributionFrom)
257 ) {
258 if (!isset($fields[$name])) {
259 $qillName = str_replace('contribution_', '', $qillName);
260 }
261 $name = str_replace('contribution_', '', $name);
262 }
263 if (in_array($name, ['contribution_currency', 'contribution_currency_type'])) {
264 $qillName = $name = 'currency';
265 $pseudoExtraParam = ['labelColumn' => 'name'];
266 }
267
268 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
269
270 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.$name", $op, $value, $dataType);
271 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', $name, $value, $op, $pseudoExtraParam);
272 if (!($name == 'id' && $value == 0)) {
273 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
274 }
275 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
276 return;
277
278 case 'contribution_pcp_made_through_id':
279 case 'contribution_soft_credit_type_id':
280 $qillName = $name;
281 if ($name == 'contribution_pcp_made_through_id') {
282 $qillName = $name = 'pcp_id';
283 $fields[$name] = ['title' => ts('Personal Campaign Page'), 'type' => 2];
284 }
285 if ($name == 'contribution_soft_credit_type_id') {
286 $qillName = str_replace('_id', '', $qillName);
287 $fields[$qillName]['type'] = $fields[$qillName]['data_type'];
288 $name = str_replace('contribution_', '', $name);
289 }
290 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_soft.$name",
291 $op, $value, CRM_Utils_Type::typeToString($fields[$qillName]['type'])
292 );
293 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_ContributionSoft', $name, $value, $op);
294 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $op, 3 => $value]);
295 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
296 return;
297
298 case 'contribution_or_softcredits':
299 if ($value == 'only_scredits') {
300 $query->_where[$grouping][] = "contribution_search_scredit_combined.scredit_id IS NOT NULL";
301 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits Only');
302 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
303 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
304 }
305 elseif ($value == 'both_related') {
306 $query->_where[$grouping][] = "contribution_search_scredit_combined.filter_id IS NOT NULL";
307 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits with related Hard Credit');
308 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
309 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
310 }
311 elseif ($value == 'both') {
312 $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both');
313 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
314 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
315 }
316 // default option: $value == 'only_contribs'
317 return;
318
319 case 'contribution_is_test':
320 // By default is Contribution Search form we choose is_test = 0 in otherwords always show active contribution
321 // so in case if any one choose any Yes/No avoid the default clause otherwise it will be conflict in whereClause
322 $key = array_search('civicrm_contribution.is_test = 0', $query->_where[$grouping]);
323 if (!empty($key)) {
324 unset($query->_where[$grouping][$key]);
325 }
326 case 'contribution_test':
327 // We dont want to include all tests for sql OR CRM-7827
328 if (!$value || $query->getOperator() != 'OR') {
329 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_test", $op, $value, "Boolean");
330 if ($value) {
331 $query->_qill[$grouping][] = ts("Only Display Test Contributions");
332 }
333 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
334 }
335 return;
336
337 case 'contribution_is_pay_later':
338 case 'contribution_pay_later':
339 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_pay_later", $op, $value, "Boolean");
340 if ($value) {
341 $query->_qill[$grouping][] = ts("Find Pay Later Contributions");
342 }
343 else {
344 $query->_qill[$grouping][] = ts("Exclude Pay Later Contributions");
345 }
346 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
347 return;
348
349 case 'contribution_recurring':
350 if ($value) {
351 $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NOT NULL";
352 $query->_qill[$grouping][] = ts("Find Recurring Contributions");
353 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
354 }
355 else {
356 $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NULL";
357 $query->_qill[$grouping][] = ts("Exclude Recurring Contributions");
358 }
359 return;
360
361 case 'contribution_recur_id':
362 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_recur_id",
363 $op, $value, "Integer"
364 );
365 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
366 return;
367
368 case 'contribution_recur_payment_processor_id':
369 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.payment_processor_id", $op, $value, "String");
370 $paymentProcessors = civicrm_api3('PaymentProcessor', 'get', []);
371 $paymentProcessorNames = [];
372 foreach ($value as $paymentProcessorId) {
373 $paymentProcessorNames[] = $paymentProcessors['values'][$paymentProcessorId]['name'];
374 }
375 $query->_qill[$grouping][] = ts("Recurring Contribution Payment Processor %1 %2", [1 => $op, 2 => implode(', ', $paymentProcessorNames)]);
376 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
377 return;
378
379 case 'contribution_recur_processor_id':
380 case 'contribution_recur_trxn_id':
381 $spec = $fields[$name];
382 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($spec['where'],
383 $op, $value, "String"
384 );
385 $query->_qill[$grouping][] = ts("Recurring Contribution %1 %2 '%3'", [1 => $fields[$name]['title'], 2 => $op, 3 => $value]);
386 $query->_tables[$spec['table_name']] = $query->_whereTables[$spec['table_name']] = 1;
387 return;
388
389 case 'contribution_recur_payment_made':
390 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.id", 'IS NOT EMPTY');
391 if ($value == 2) {
392 $query->_qill[$grouping][] = ts("Recurring contributions with at least one payment");
393 self::$_contribRecurPayment = TRUE;
394 }
395 else {
396 $query->_qill[$grouping][] = ts("All recurring contributions regardless of payments");
397 self::$_contribRecurPayment = FALSE;
398 }
399 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
400 return;
401
402 case 'contribution_recur_contribution_status_id':
403 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.contribution_status_id", $op, $value, 'String');
404 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_ContributionRecur', 'contribution_status_id', $value, $op, $pseudoExtraParam);
405 $query->_qill[$grouping][] = ts("Recurring Contribution Status %1 '%2'", [1 => $op, 2 => $value]);
406 $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1;
407 return;
408
409 case 'contribution_note':
410 $value = CRM_Core_DAO::escapeString($value);
411 if ($wildcard) {
412 $value = "%$value%";
413 $op = 'LIKE';
414 }
415 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_note.note', $op, $value, "String");
416 $query->_qill[$grouping][] = ts('Contribution Note %1 %2', [1 => $op, 2 => $quoteValue]);
417 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = $query->_whereTables['contribution_note'] = 1;
418 return;
419
420 case 'contribution_membership_id':
421 $query->_where[$grouping][] = " civicrm_membership.id $op $value";
422 $query->_tables['contribution_membership'] = $query->_whereTables['contribution_membership'] = 1;
423
424 return;
425
426 case 'contribution_participant_id':
427 $query->_where[$grouping][] = " civicrm_participant.id $op $value";
428 $query->_tables['contribution_participant'] = $query->_whereTables['contribution_participant'] = 1;
429 return;
430
431 case 'contribution_pcp_display_in_roll':
432 $query->_where[$grouping][] = " civicrm_contribution_soft.pcp_display_in_roll $op '$value'";
433 if ($value) {
434 $query->_qill[$grouping][] = ts("Personal Campaign Page Honor Roll");
435 }
436 else {
437 $query->_qill[$grouping][] = ts("NOT Personal Campaign Page Honor Roll");
438 }
439 $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1;
440 return;
441
442 case 'contribution_batch_id':
443 list($qillOp, $qillValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Batch_BAO_EntityBatch', 'batch_id', $value, $op);
444 $query->_qill[$grouping][] = ts('Batch Name %1 %2', [1 => $qillOp, 2 => $qillValue]);
445 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_entity_batch.batch_id', $op, $value);
446 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
447 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
448 $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1;
449 return;
450
451 case 'contribution_product_id':
452 // CRM-16713 - contribution search by premiums on 'Find Contribution' form.
453 $qillName = $name;
454 list($operator, $productValue) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Product', $name, $value, $op);
455 $query->_qill[$grouping][] = ts('%1 %2 %3', [1 => $fields[$qillName]['title'], 2 => $operator, 3 => $productValue]);
456 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_product.id", $op, $value);
457 $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
458 return;
459
460 case 'contribution_is_payment':
461 $query->_where[$grouping][] = " civicrm_financial_trxn.is_payment $op $value";
462 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
463 return;
464
465 case 'financial_trxn_card_type_id':
466 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_financial_trxn.card_type_id', $op, $value);
467 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
468 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
469 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', $value, $op);
470 $query->_qill[$grouping][] = ts('Card Type %1 %2', [1 => $op, 2 => $value]);
471 return;
472
473 case 'financial_trxn_pan_truncation':
474 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_financial_trxn.pan_truncation', $op, $value);
475 $query->_tables['civicrm_financial_trxn'] = $query->_whereTables['civicrm_financial_trxn'] = 1;
476 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
477 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Financial_DAO_FinancialTrxn', 'pan_truncation', $value, $op);
478 $query->_qill[$grouping][] = ts('Card Number %1 %2', [1 => $op, 2 => $value]);
479 return;
480
481 default:
482 //all other elements are handle in this case
483 $fldName = substr($name, 13);
484 if (!isset($fields[$fldName])) {
485 return;
486 }
487 $whereTable = $fields[$fldName];
488 if (!is_array($value)) {
489 $value = trim($value);
490 }
491
492 $dataType = 'String';
493 if (!empty($whereTable['type'])) {
494 $dataType = CRM_Utils_Type::typeToString($whereTable['type']);
495 }
496
497 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($whereTable['where'], $op, $value, $dataType);
498 $query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue";
499 list($tableName) = explode('.', $whereTable['where'], 2);
500 $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
501 if ($tableName === 'civicrm_contribution_product') {
502 $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
503 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
504 }
505 else {
506 $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
507 }
508 }
509 }
510
511 /**
512 * Get from clause.
513 *
514 * @param string $name
515 * @param string $mode
516 * @param string $side
517 *
518 * @return NULL|string
519 */
520 public static function from($name, $mode, $side) {
521 $from = NULL;
522 switch ($name) {
523 case 'civicrm_contribution':
524 $from = " $side JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id ";
525 if (in_array(self::$_contribOrSoftCredit, ["only_scredits", "both_related", "both"])) {
526 // switch the from table if its only soft credit search
527 $from = " $side JOIN " . \Civi::$statics[__CLASS__]['soft_credit_temp_table_name'] . " as contribution_search_scredit_combined ON contribution_search_scredit_combined.contact_id = contact_a.id ";
528 $from .= " $side JOIN civicrm_contribution ON civicrm_contribution.id = contribution_search_scredit_combined.id ";
529 $from .= " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.id = contribution_search_scredit_combined.scredit_id";
530 }
531 break;
532
533 case 'civicrm_contribution_recur':
534 if ($mode == 1) {
535 // 'Made payment for the recurring contributions?' is ticked yes
536 $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id ";
537 if (self::$_contribRecurPayment == TRUE) {
538 $from .= " INNER JOIN civicrm_contribution cr ON cr.contribution_recur_id = civicrm_contribution_recur.id ";
539 }
540 }
541 else {
542 $from = " $side JOIN civicrm_contribution_recur ON civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id ";
543 }
544 break;
545
546 case 'civicrm_financial_type':
547 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
548 $from = " INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
549 }
550 else {
551 $from = " $side JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id ";
552 }
553 break;
554
555 case 'civicrm_financial_account':
556 if ($mode & CRM_Contact_BAO_Query::MODE_CONTACTS) {
557 $from = " $side JOIN civicrm_financial_account ON contact_a.id = civicrm_financial_account.contact_id ";
558 }
559 break;
560
561 case 'civicrm_accounting_code':
562 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
563 $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' ";
564 $from .= " INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id ";
565 $from .= " INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is' ";
566 $from .= " INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship' ";
567 }
568 break;
569
570 case 'civicrm_contribution_page':
571 $from = " $side JOIN civicrm_contribution_page ON civicrm_contribution.contribution_page_id = civicrm_contribution_page.id";
572 break;
573
574 case 'civicrm_product':
575 $from = " $side JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id";
576 $from .= " $side JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id ";
577 break;
578
579 case 'contribution_softcredit_type':
580 $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON
581 (option_group_contribution_softcredit_type.name = 'soft_credit_type')";
582 $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON
583 ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value
584 AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )";
585 break;
586
587 case 'contribution_note':
588 $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND
589 civicrm_contribution.id = civicrm_note.entity_id )";
590 break;
591
592 case 'contribution_membership':
593 $from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id";
594 $from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id ";
595 break;
596
597 case 'civicrm_campaign':
598 //CRM-16764 - get survey clause from campaign bao
599 if (!CRM_Campaign_BAO_Query::$_applySurveyClause) {
600 $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_contribution.campaign_id";
601 }
602 break;
603
604 case 'contribution_participant':
605 $from = " $side JOIN civicrm_participant_payment ON civicrm_participant_payment.contribution_id = civicrm_contribution.id";
606 $from .= " $side JOIN civicrm_participant ON civicrm_participant_payment.participant_id = civicrm_participant.id ";
607 break;
608
609 case 'civicrm_contribution_soft':
610 if (!in_array(self::$_contribOrSoftCredit, ["only_scredits", "both_related", "both"])) {
611 $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id";
612 }
613 break;
614
615 case 'civicrm_contribution_soft_contact':
616 if (in_array(self::$_contribOrSoftCredit, ["only_scredits", "both_related", "both"])) {
617 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution.contact_id = civicrm_contact_d.id )
618 AND contribution_search_scredit_combined.scredit_id IS NOT NULL";
619 }
620 else {
621 $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution_soft.contact_id = civicrm_contact_d.id )";
622 }
623 break;
624
625 case 'civicrm_contribution_soft_email':
626 $from .= " $side JOIN civicrm_email as soft_email ON (civicrm_contact_d.id = soft_email.contact_id )";
627 break;
628
629 case 'civicrm_contribution_soft_phone':
630 $from .= " $side JOIN civicrm_phone as soft_phone ON (civicrm_contact_d.id = soft_phone.contact_id )";
631 break;
632
633 case 'contribution_batch':
634 $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn'
635 AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )";
636
637 $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id";
638 break;
639
640 case 'civicrm_financial_trxn':
641 $from .= " $side JOIN civicrm_entity_financial_trxn ON (
642 civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution'
643 AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )";
644
645 $from .= " $side JOIN civicrm_financial_trxn ON (
646 civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )";
647 break;
648 }
649 return $from;
650 }
651
652 /**
653 * Initialise the soft credit clause.
654 *
655 * @param CRM_Contact_BAO_Query $query
656 */
657 public static function initializeAnySoftCreditClause(&$query) {
658 // @todo have a generic initialize on all components that gets called every query
659 // & rename this to match that fn name.
660 if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
661 if (self::isSoftCreditOptionEnabled($query->_params)) {
662 unset($query->_distinctComponentClause);
663 $query->_rowCountClause = " count(civicrm_contribution.id)";
664 $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
665 }
666 else {
667 $query->_distinctComponentClause = ' civicrm_contribution.id';
668 }
669 }
670 }
671
672 /**
673 * Check if soft credits are enables.
674 *
675 * @param array $queryParams
676 *
677 * @return bool
678 */
679 public static function isSoftCreditOptionEnabled($queryParams = []) {
680 if (!empty($queryParams)) {
681 foreach (array_keys($queryParams) as $id) {
682 if (empty($queryParams[$id][0])) {
683 continue;
684 }
685 if ($queryParams[$id][0] == 'contribution_or_softcredits') {
686 self::$_contribOrSoftCredit = $queryParams[$id][2];
687 }
688 }
689 }
690 if (in_array(self::$_contribOrSoftCredit,
691 ["only_scredits", "both_related", "both"])) {
692 if (!isset(\Civi::$statics[__CLASS__]['soft_credit_temp_table_name'])) {
693 // build a temp table which is union of contributions and soft credits
694 // note: group-by in first part ensures uniqueness in counts
695 $tempQuery = '
696 SELECT con.id as id, con.contact_id, cso.id as filter_id, NULL as scredit_id
697 FROM civicrm_contribution con
698 LEFT JOIN civicrm_contribution_soft cso ON con.id = cso.contribution_id
699 GROUP BY id, contact_id, scredit_id, cso.id
700 UNION ALL
701 SELECT scredit.contribution_id as id, scredit.contact_id, scredit.id as filter_id, scredit.id as scredit_id
702 FROM civicrm_contribution_soft as scredit';
703 \Civi::$statics[__CLASS__]['soft_credit_temp_table_name'] = CRM_Utils_SQL_TempTable::build()->createWithQuery(
704 $tempQuery
705 )->getName();
706 }
707 return TRUE;
708 }
709 return FALSE;
710 }
711
712 /**
713 * Get return properties for soft credits.
714 *
715 * @param bool $isExportMode
716 *
717 * @return array
718 */
719 public static function softCreditReturnProperties($isExportMode = FALSE) {
720 $properties = [
721 'contribution_soft_credit_name' => 1,
722 'contribution_soft_credit_amount' => 1,
723 'contribution_soft_credit_type' => 1,
724 ];
725 if ($isExportMode) {
726 $properties['contribution_soft_credit_contact_id'] = 1;
727 $properties['contribution_soft_credit_contribution_id'] = 1;
728 }
729 return $properties;
730 }
731
732 /**
733 * Get the list of fields required to populate the selector.
734 *
735 * The default return properties array returns far too many fields for 'everyday use. Every field you add to this array
736 * kills a small kitten so add carefully.
737 *
738 * @param array $queryParams
739 * @return array
740 */
741 public static function selectorReturnProperties($queryParams) {
742 $properties = [
743 'contact_type' => 1,
744 'contact_sub_type' => 1,
745 'sort_name' => 1,
746 'financial_type' => 1,
747 'contribution_source' => 1,
748 'is_test' => 1,
749 'receive_date' => 1,
750 'is_pay_later' => 1,
751 'thankyou_date' => 1,
752 'total_amount' => 1,
753 // Without this value here we get an e-notice BUT the value does not appear to be rendered anywhere.
754 'contribution_campaign_id' => 1,
755 'contribution_status_id' => 1,
756 // @todo return this & fix query to do pseudoconstant thing.
757 'contribution_status' => 1,
758 'currency' => 1,
759 'contribution_cancel_date' => 1,
760 'contribution_recur_id' => 1,
761 ];
762 if (self::isSiteHasProducts()) {
763 $properties['product_name'] = 1;
764 $properties['contribution_product_id'] = 1;
765 }
766 if (self::isSoftCreditOptionEnabled($queryParams)) {
767 $properties = array_merge($properties, self::softCreditReturnProperties());
768 }
769
770 return $properties;
771 }
772
773 /**
774 * Do any products exist in this site's database.
775 *
776 * @return bool
777 */
778 public static function isSiteHasProducts() {
779 if (!isset(\Civi::$statics[__CLASS__]['has_products'])) {
780 \Civi::$statics[__CLASS__]['has_products'] = (bool) CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_contribution_product LIMIT 1');
781 }
782 return \Civi::$statics[__CLASS__]['has_products'];
783 }
784
785 /**
786 * Function you should avoid.
787 *
788 * This function returns default properties for contribution queries. However, they are
789 * far more than are required in 'most' cases and you should always try to return the return properties
790 * you actually require.
791 *
792 * It would be nice to throw an e-notice when this is called but it would trash the tests :-(.
793 *
794 * @param int $mode
795 * @param bool $includeCustomFields
796 *
797 * @return array|NULL
798 */
799 public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
800 $properties = NULL;
801 if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
802 $properties = [
803 // add
804 'contact_type' => 1,
805 // fields
806 'contact_sub_type' => 1,
807 // to
808 'sort_name' => 1,
809 //this
810 'display_name' => 1,
811 // array
812 'financial_type' => 1,
813 // to
814 'contribution_source' => 1,
815 // strangle
816 'receive_date' => 1,
817 // site
818 'thankyou_date' => 1,
819 // performance
820 'contribution_cancel_date' => 1,
821 // and
822 'total_amount' => 1,
823 // torture
824 'accounting_code' => 1,
825 // small
826 'payment_instrument' => 1,
827 // kittens
828 'payment_instrument_id' => 1,
829 // argh
830 'contribution_check_number' => 1,
831 // no
832 'non_deductible_amount' => 1,
833 // not
834 'fee_amount' => 1,
835 // another
836 'net_amount' => 1,
837 // expensive
838 'trxn_id' => 1,
839 // join
840 'invoice_id' => 1,
841 'invoice_number' => 1,
842 // added
843 'currency' => 1,
844 // to
845 'cancel_reason' => 1,
846 //every
847 'receipt_date' => 1,
848 // query
849 //whether
850 // or
851 // not
852 // the
853 // field
854 // is
855 'is_test' => 1,
856 // actually
857 'is_pay_later' => 1,
858 // required
859 'contribution_status' => 1,
860 // instead
861 'contribution_status_id' => 1,
862 // of
863 'contribution_recur_id' => 1,
864 // adding
865 'amount_level' => 1,
866 // here
867 'contribution_note' => 1,
868 // set
869 'contribution_batch' => 1,
870 // return properties
871 'contribution_campaign_title' => 1,
872 // on
873 'contribution_campaign_id' => 1,
874 // calling
875 //function
876 ];
877 if (self::isSiteHasProducts()) {
878 $properties['fulfilled_date'] = 1;
879 $properties['product_name'] = 1;
880 $properties['contribution_product_id'] = 1;
881 $properties['product_option'] = 1;
882 $properties['sku'] = 1;
883 $properties['contribution_start_date'] = 1;
884 $properties['contribution_end_date'] = 1;
885 }
886 if (self::isSoftCreditOptionEnabled()) {
887 $properties = array_merge($properties, self::softCreditReturnProperties());
888 }
889 if ($includeCustomFields) {
890 // also get all the custom contribution properties
891 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution');
892 if (!empty($fields)) {
893 foreach ($fields as $name => $dontCare) {
894 $properties[$name] = 1;
895 }
896 }
897 }
898 }
899 return $properties;
900 }
901
902 /**
903 * Get the metadata for fields to be included on the search form.
904 *
905 * @throws \CiviCRM_API3_Exception
906 */
907 public static function getSearchFieldMetadata() {
908 $fields = [
909 'contribution_source',
910 'cancel_reason',
911 'invoice_number',
912 'receive_date',
913 'contribution_cancel_date',
914 ];
915 $metadata = civicrm_api3('Contribution', 'getfields', [])['values'];
916 return array_intersect_key($metadata, array_flip($fields));
917 }
918
919 /**
920 * Add all the elements shared between contribute search and advanced search.
921 *
922 * @param \CRM_Contribute_Form_Search $form
923 *
924 * @throws \CRM_Core_Exception
925 * @throws \CiviCRM_API3_Exception
926 */
927 public static function buildSearchForm(&$form) {
928
929 $form->addSearchFieldMetadata(['Contribution' => self::getSearchFieldMetadata()]);
930 $form->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur::getContributionRecurSearchFieldMetadata()]);
931 $form->addFormFieldsFromMetadata();
932
933 $form->add('text', 'contribution_amount_low', ts('From'), ['size' => 8, 'maxlength' => 8]);
934 $form->addRule('contribution_amount_low', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('9.99', ' ')]), 'money');
935
936 $form->add('text', 'contribution_amount_high', ts('To'), ['size' => 8, 'maxlength' => 8]);
937 $form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('99.99', ' ')]), 'money');
938
939 // Adding select option for curreny type -- CRM-4711
940 $form->add('select', 'contribution_currency_type',
941 ts('Currency Type'),
942 [
943 '' => ts('- any -'),
944 ] +
945 CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', ['labelColumn' => 'name']),
946 FALSE, ['class' => 'crm-select2']
947 );
948
949 // CRM-13848
950 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::VIEW);
951 $form->addSelect('financial_type_id',
952 ['entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => $financialTypes]
953 );
954
955 $form->add('select', 'contribution_page_id',
956 ts('Contribution Page'),
957 CRM_Contribute_PseudoConstant::contributionPage(),
958 FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')]
959 );
960
961 // use contribution_payment_instrument_id instead of payment_instrument_id
962 // Contribution Edit form (pop-up on contribution/Contact(display Result as Contribution) open on search form),
963 // then payment method change action not working properly because of same html ID present two time on one page
964 $form->addSelect('contribution_payment_instrument_id',
965 ['entity' => 'contribution', 'field' => 'payment_instrument_id', 'multiple' => 'multiple', 'label' => ts('Payment Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')]
966 );
967
968 $form->add('select',
969 'contribution_pcp_made_through_id',
970 ts('Personal Campaign Page'),
971 CRM_Contribute_PseudoConstant::pcPage(), FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')]);
972
973 $statusValues = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'search');
974 $form->add('select', 'contribution_status_id',
975 ts('Contribution Status'), $statusValues,
976 FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple']
977 );
978
979 // Add fields for thank you and receipt
980 $form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'), TRUE);
981 $form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'), TRUE);
982
983 $form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'), TRUE);
984 $form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'), TRUE);
985
986 $form->addYesNo('contribution_test', ts('Contribution is a Test?'), TRUE);
987
988 // Add field for transaction ID search
989 $form->addElement('text', 'contribution_trxn_id', ts("Transaction ID"));
990 $form->addElement('text', 'contribution_check_number', ts('Check Number'));
991
992 // Add field for pcp display in roll search
993 $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE);
994
995 // Soft credit related fields
996 $options = [
997 'only_contribs' => ts('Contributions Only'),
998 'only_scredits' => ts('Soft Credits Only'),
999 'both_related' => ts('Soft Credits with related Hard Credit'),
1000 'both' => ts('Both'),
1001 ];
1002 $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, ['class' => "crm-select2"]);
1003 $form->addSelect(
1004 'contribution_soft_credit_type_id',
1005 [
1006 'entity' => 'contribution_soft',
1007 'field' => 'soft_credit_type_id',
1008 'multiple' => TRUE,
1009 'context' => 'search',
1010 ]
1011 );
1012
1013 $form->addField('financial_trxn_card_type_id', ['entity' => 'FinancialTrxn', 'name' => 'card_type_id', 'action' => 'get']);
1014
1015 $form->add('text', 'financial_trxn_pan_truncation', ts('Card Number'), [
1016 'size' => 5,
1017 'maxlength' => 4,
1018 'autocomplete' => 'off',
1019 ]);
1020
1021 if (CRM_Contribute_BAO_Query::isSiteHasProducts()) {
1022 // CRM-16713 - contribution search by premiums on 'Find Contribution' form.
1023 $form->add('select', 'contribution_product_id',
1024 ts('Premium'),
1025 CRM_Contribute_PseudoConstant::products(),
1026 FALSE, [
1027 'class' => 'crm-select2',
1028 'multiple' => 'multiple',
1029 'placeholder' => ts('- any -'),
1030 ]
1031 );
1032 }
1033
1034 self::addCustomFormFields($form, ['Contribution']);
1035
1036 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'contribution_campaign_id');
1037
1038 // Add batch select
1039 $batches = CRM_Contribute_PseudoConstant::batch();
1040
1041 if (!empty($batches)) {
1042 $form->add('select', 'contribution_batch_id',
1043 ts('Batch Name'),
1044 [
1045 '' => ts('- any -'),
1046 // CRM-19325
1047 'IS NULL' => ts('None'),
1048 ] + $batches,
1049 FALSE, ['class' => 'crm-select2']
1050 );
1051 }
1052
1053 $form->assign('validCiviContribute', TRUE);
1054 $form->setDefaults(['contribution_test' => 0]);
1055
1056 CRM_Contribute_BAO_ContributionRecur::recurringContribution($form);
1057 }
1058
1059 /**
1060 * Get table names.
1061 *
1062 * @todo better function comment needed - what IS the point of this?
1063 *
1064 * @param array $tables
1065 */
1066 public static function tableNames(&$tables) {
1067 // Add contribution table
1068 if (!empty($tables['civicrm_product'])) {
1069 $tables = array_merge(['civicrm_contribution' => 1], $tables);
1070 }
1071
1072 if (!empty($tables['civicrm_contribution_product']) && empty($tables['civicrm_product'])) {
1073 $tables['civicrm_product'] = 1;
1074 }
1075 }
1076
1077 /**
1078 * Add the where for dates.
1079 *
1080 * @param array $values
1081 * Array of query values.
1082 * @param object $query
1083 * The query object.
1084 * @param string $name
1085 * Query field that is set.
1086 * @param string $field
1087 * Name of field to be set.
1088 * @param string $title
1089 * Title of the field.
1090 *
1091 * @return bool
1092 */
1093 public static function buildDateWhere(&$values, $query, $name, $field, $title) {
1094 $fieldPart = strpos($name, $field);
1095 if ($fieldPart === FALSE) {
1096 return NULL;
1097 }
1098 // we only have recurring dates using this ATM so lets' short cut to find the table name
1099 $table = 'contribution_recur';
1100 $fieldName = explode($table . '_', $field);
1101 $query->dateQueryBuilder($values,
1102 'civicrm_' . $table, $field, $fieldName[1], $title
1103 );
1104 return TRUE;
1105 }
1106
1107 /**
1108 * Add the soft credit fields to the select fields.
1109 *
1110 * Extracted into separate function to improve readability of main select function.
1111 *
1112 * @param $query
1113 */
1114 private static function addSoftCreditFields(&$query) {
1115 $includeSoftCredits = self::isSoftCreditOptionEnabled($query->_params);
1116 if (!empty($query->_returnProperties['contribution_soft_credit_name'])) {
1117 if ($includeSoftCredits) {
1118 $query->_select['contribution_soft_credit_name'] = "civicrm_contact_d.sort_name as contribution_soft_credit_name";
1119 // also include contact id. Will help build hyperlinks
1120 $query->_select['contribution_soft_credit_contact_id'] = "civicrm_contact_d.id as contribution_soft_credit_contact_id";
1121 }
1122 $query->_element['contribution_soft_credit_name'] = 1;
1123 $query->_element['contribution_soft_credit_contact_id'] = 1;
1124
1125 $query->_tables['civicrm_contribution'] = 1;
1126 $query->_tables['civicrm_contribution_soft'] = 1;
1127 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1128 }
1129
1130 if (!empty($query->_returnProperties['contribution_soft_credit_contact_id'])) {
1131 $query->_tables['civicrm_contribution'] = 1;
1132 $query->_tables['civicrm_contribution_soft'] = 1;
1133 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1134 }
1135
1136 if (!empty($query->_returnProperties['contribution_soft_credit_amount'])) {
1137 if ($includeSoftCredits) {
1138 $query->_select['contribution_soft_credit_amount'] = "civicrm_contribution_soft.amount as contribution_soft_credit_amount";
1139 }
1140 $query->_element['contribution_soft_credit_amount'] = 1;
1141 $query->_tables['civicrm_contribution'] = 1;
1142 $query->_tables['civicrm_contribution_soft'] = 1;
1143 }
1144
1145 if (!empty($query->_returnProperties['contribution_soft_credit_type'])) {
1146 if ($includeSoftCredits) {
1147 $query->_select['contribution_soft_credit_type'] = "contribution_softcredit_type.label as contribution_soft_credit_type";
1148 }
1149 $query->_element['contribution_soft_credit_type'] = 1;
1150 $query->_tables['civicrm_contribution'] = 1;
1151 $query->_tables['contribution_softcredit_type'] = 1;
1152 }
1153
1154 if (!empty($query->_returnProperties['contribution_soft_credit_contribution_id'])) {
1155 if ($includeSoftCredits) {
1156 $query->_select['contribution_soft_credit_contribution_id'] = "civicrm_contribution_soft.contribution_id as contribution_soft_credit_contribution_id";
1157 }
1158 $query->_element['contribution_soft_credit_contribution_id'] = 1;
1159 $query->_tables['civicrm_contribution'] = 1;
1160 $query->_tables['civicrm_contribution_soft'] = 1;
1161 }
1162
1163 if (!empty($query->_returnProperties['contribution_soft_credit_email'])) {
1164 $query->_select['contribution_soft_credit_email'] = "soft_email.email as contribution_soft_credit_email";
1165 $query->_element['contribution_soft_credit_email'] = 1;
1166 $query->_tables['civicrm_contribution'] = 1;
1167 $query->_tables['civicrm_contribution_soft'] = 1;
1168 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1169 $query->_tables['civicrm_contribution_soft_email'] = 1;
1170 }
1171
1172 if (!empty($query->_returnProperties['contribution_soft_credit_phone'])) {
1173 $query->_select['contribution_soft_credit_email'] = "soft_phone.phone as contribution_soft_credit_phone";
1174 $query->_element['contribution_soft_credit_phone'] = 1;
1175 $query->_tables['civicrm_contribution'] = 1;
1176 $query->_tables['civicrm_contribution_soft'] = 1;
1177 $query->_tables['civicrm_contribution_soft_contact'] = 1;
1178 $query->_tables['civicrm_contribution_soft_phone'] = 1;
1179 }
1180
1181 if (!empty($query->_returnProperties['contribution_soft_credit_pcp_id'])) {
1182 $query->_select['contribution_soft_credit_pcp_id'] = "civicrm_contribution_soft.pcp_id as contribution_soft_credit_pcp_id";
1183 $query->_element['contribution_soft_credit_pcp_id'] = 1;
1184 $query->_tables['civicrm_contribution'] = 1;
1185 $query->_tables['civicrm_contribution_soft'] = 1;
1186 }
1187 }
1188
1189 }