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