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