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