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