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