Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 | 32 | * $Id$ |
6a488035 TO |
33 | */ |
34 | class CRM_Contribute_BAO_Query { | |
35 | ||
36 | /** | |
7fe37828 | 37 | * Static field for all the export/import contribution fields. |
6a488035 TO |
38 | * |
39 | * @var array | |
6a488035 TO |
40 | */ |
41 | static $_contributionFields = NULL; | |
42 | ||
3dbf477c DS |
43 | static $_contribOrSoftCredit = "only_contribs"; |
44 | ||
6a488035 TO |
45 | /** |
46 | * Function get the import/export fields for contribution | |
47 | * | |
a6c01b45 CW |
48 | * @return array |
49 | * self::$_contributionFields associative array of contribution fields | |
6a488035 | 50 | */ |
00be9182 | 51 | public static function &getFields() { |
6a488035 TO |
52 | if (!self::$_contributionFields) { |
53 | self::$_contributionFields = array(); | |
54 | ||
55 | $fields = CRM_Contribute_BAO_Contribution::exportableFields(); | |
56 | ||
57 | unset($fields['contribution_contact_id']); | |
58 | ||
59 | self::$_contributionFields = $fields; | |
60 | } | |
61 | return self::$_contributionFields; | |
62 | } | |
63 | ||
64 | /** | |
7fe37828 | 65 | * If contributions are involved, add the specific contribute fields. |
6a488035 | 66 | * |
dd244018 EM |
67 | * @param $query |
68 | * | |
6a488035 | 69 | * @return void |
6a488035 | 70 | */ |
00be9182 | 71 | public static function select(&$query) { |
6a488035 TO |
72 | // if contribute mode add contribution id |
73 | if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
74 | $query->_select['contribution_id'] = "civicrm_contribution.id as contribution_id"; | |
75 | $query->_element['contribution_id'] = 1; | |
76 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
77 | } | |
78 | ||
79 | // get financial_type | |
a7488080 | 80 | if (!empty($query->_returnProperties['financial_type'])) { |
353ffa53 | 81 | $query->_select['financial_type'] = "civicrm_financial_type.name as financial_type"; |
6a488035 TO |
82 | $query->_element['financial_type'] = 1; |
83 | $query->_tables['civicrm_contribution'] = 1; | |
84 | $query->_tables['civicrm_financial_type'] = 1; | |
85 | } | |
86 | ||
87 | // get accounting code | |
a7488080 | 88 | if (!empty($query->_returnProperties['accounting_code'])) { |
353ffa53 | 89 | $query->_select['accounting_code'] = "civicrm_financial_account.accounting_code as accounting_code"; |
6a488035 TO |
90 | $query->_element['accounting_code'] = 1; |
91 | $query->_tables['civicrm_accounting_code'] = 1; | |
92 | $query->_tables['civicrm_financial_account'] = 1; | |
93 | } | |
39b795ba | 94 | |
a7488080 | 95 | if (!empty($query->_returnProperties['contribution_note'])) { |
6a488035 TO |
96 | $query->_select['contribution_note'] = "civicrm_note.note as contribution_note"; |
97 | $query->_element['contribution_note'] = 1; | |
98 | $query->_tables['contribution_note'] = 1; | |
739189e4 | 99 | $query->_tables['civicrm_contribution'] = 1; |
6a488035 TO |
100 | } |
101 | ||
a7488080 | 102 | if (!empty($query->_returnProperties['contribution_batch'])) { |
6a488035 TO |
103 | $query->_select['contribution_batch'] = "civicrm_batch.title as contribution_batch"; |
104 | $query->_element['contribution_batch'] = 1; | |
105 | $query->_tables['contribution_batch'] = 1; | |
106 | } | |
107 | ||
08fe8c7e | 108 | if (!empty($query->_returnProperties['contribution_source'])) { |
109 | $query->_select['contribution_source'] = "civicrm_contribution.source as contribution_source"; | |
110 | $query->_element['contribution_source'] = 1; | |
111 | $query->_tables['civicrm_contribution'] = 1; | |
112 | } | |
113 | ||
6a488035 | 114 | // get contribution_status |
a7488080 | 115 | if (!empty($query->_returnProperties['contribution_status_id'])) { |
6a488035 TO |
116 | $query->_select['contribution_status_id'] = "contribution_status.value as contribution_status_id"; |
117 | $query->_element['contribution_status_id'] = 1; | |
118 | $query->_tables['civicrm_contribution'] = 1; | |
119 | $query->_tables['contribution_status'] = 1; | |
120 | } | |
121 | ||
122 | // get contribution_status label | |
a7488080 | 123 | if (!empty($query->_returnProperties['contribution_status'])) { |
6a488035 TO |
124 | $query->_select['contribution_status'] = "contribution_status.label as contribution_status"; |
125 | $query->_element['contribution_status'] = 1; | |
126 | $query->_tables['civicrm_contribution'] = 1; | |
127 | $query->_tables['contribution_status'] = 1; | |
128 | } | |
129 | ||
243a6bce | 130 | // get payment instrument |
afa0b07c | 131 | if (!empty($query->_returnProperties['payment_instrument'])) { |
243a6bce | 132 | $query->_select['payment_instrument'] = "contribution_payment_instrument.label as payment_instrument"; |
133 | $query->_element['payment_instrument'] = 1; | |
134 | $query->_tables['civicrm_contribution'] = 1; | |
135 | $query->_tables['contribution_payment_instrument'] = 1; | |
136 | } | |
137 | ||
3a6eb174 | 138 | // get payment instrument id |
139 | if (!empty($query->_returnProperties['payment_instrument_id'])) { | |
7c550ca0 | 140 | $query->_select['instrument_id'] = "contribution_payment_instrument.value as instrument_id"; |
3a6eb174 | 141 | $query->_select['payment_instrument_id'] = "contribution_payment_instrument.value as payment_instrument_id"; |
7d543448 | 142 | $query->_element['instrument_id'] = $query->_element['payment_instrument_id'] = 1; |
3a6eb174 | 143 | $query->_tables['civicrm_contribution'] = 1; |
144 | $query->_tables['contribution_payment_instrument'] = 1; | |
145 | } | |
146 | ||
a7488080 | 147 | if (!empty($query->_returnProperties['check_number'])) { |
6a488035 TO |
148 | $query->_select['contribution_check_number'] = "civicrm_contribution.check_number as contribution_check_number"; |
149 | $query->_element['contribution_check_number'] = 1; | |
150 | $query->_tables['civicrm_contribution'] = 1; | |
151 | } | |
152 | ||
a7488080 | 153 | if (!empty($query->_returnProperties['contribution_campaign_id'])) { |
6a488035 TO |
154 | $query->_select['contribution_campaign_id'] = 'civicrm_contribution.campaign_id as contribution_campaign_id'; |
155 | $query->_element['contribution_campaign_id'] = 1; | |
156 | $query->_tables['civicrm_contribution'] = 1; | |
157 | } | |
158 | ||
159 | // LCD 716 | |
81ec6180 | 160 | if (!empty($query->_returnProperties['contribution_soft_credit_name'])) { |
4f3846df | 161 | $query->_select['contribution_soft_credit_name'] = "civicrm_contact_d.sort_name as contribution_soft_credit_name"; |
6a488035 | 162 | $query->_element['contribution_soft_credit_name'] = 1; |
d4e2b978 DS |
163 | |
164 | // also include contact id. Will help build hyperlinks | |
353ffa53 | 165 | $query->_select['contribution_soft_credit_contact_id'] = "civicrm_contact_d.id as contribution_soft_credit_contact_id"; |
d4e2b978 DS |
166 | $query->_element['contribution_soft_credit_contact_id'] = 1; |
167 | ||
6a488035 TO |
168 | $query->_tables['civicrm_contribution'] = 1; |
169 | $query->_tables['civicrm_contribution_soft'] = 1; | |
170 | $query->_tables['civicrm_contribution_soft_contact'] = 1; | |
171 | } | |
172 | ||
d4e2b978 DS |
173 | if (!empty($query->_returnProperties['contribution_soft_credit_contact_id'])) { |
174 | $query->_tables['civicrm_contribution'] = 1; | |
175 | $query->_tables['civicrm_contribution_soft'] = 1; | |
176 | } | |
177 | ||
81ec6180 | 178 | if (!empty($query->_returnProperties['contribution_soft_credit_amount'])) { |
36bf52ef DS |
179 | $query->_select['contribution_soft_credit_amount'] = "civicrm_contribution_soft.amount as contribution_soft_credit_amount"; |
180 | $query->_element['contribution_soft_credit_amount'] = 1; | |
181 | $query->_tables['civicrm_contribution'] = 1; | |
182 | $query->_tables['civicrm_contribution_soft'] = 1; | |
183 | } | |
184 | ||
81ec6180 | 185 | if (!empty($query->_returnProperties['contribution_soft_credit_type'])) { |
36bf52ef DS |
186 | $query->_select['contribution_soft_credit_type'] = "contribution_softcredit_type.label as contribution_soft_credit_type"; |
187 | $query->_element['contribution_soft_credit_type'] = 1; | |
188 | $query->_tables['civicrm_contribution'] = 1; | |
189 | $query->_tables['contribution_softcredit_type'] = 1; | |
190 | } | |
191 | ||
81ec6180 DS |
192 | if (!empty($query->_returnProperties['contribution_soft_credit_contribution_id'])) { |
193 | $query->_select['contribution_soft_credit_contribution_id'] = "civicrm_contribution_soft.contribution_id as contribution_soft_credit_contribution_id"; | |
194 | $query->_element['contribution_soft_credit_contribution_id'] = 1; | |
195 | $query->_tables['civicrm_contribution'] = 1; | |
196 | $query->_tables['civicrm_contribution_soft'] = 1; | |
197 | } | |
198 | ||
199 | if (!empty($query->_returnProperties['contribution_soft_credit_email'])) { | |
6a488035 TO |
200 | $query->_select['contribution_soft_credit_email'] = "soft_email.email as contribution_soft_credit_email"; |
201 | $query->_element['contribution_soft_credit_email'] = 1; | |
202 | $query->_tables['civicrm_contribution'] = 1; | |
203 | $query->_tables['civicrm_contribution_soft'] = 1; | |
204 | $query->_tables['civicrm_contribution_soft_contact'] = 1; | |
205 | $query->_tables['civicrm_contribution_soft_email'] = 1; | |
206 | } | |
207 | ||
81ec6180 | 208 | if (!empty($query->_returnProperties['contribution_soft_credit_phone'])) { |
6a488035 TO |
209 | $query->_select['contribution_soft_credit_email'] = "soft_phone.phone as contribution_soft_credit_phone"; |
210 | $query->_element['contribution_soft_credit_phone'] = 1; | |
211 | $query->_tables['civicrm_contribution'] = 1; | |
212 | $query->_tables['civicrm_contribution_soft'] = 1; | |
213 | $query->_tables['civicrm_contribution_soft_contact'] = 1; | |
214 | $query->_tables['civicrm_contribution_soft_phone'] = 1; | |
215 | } | |
216 | // LCD 716 END | |
217 | } | |
218 | ||
186c9c17 EM |
219 | /** |
220 | * @param $query | |
221 | */ | |
00be9182 | 222 | public static function where(&$query) { |
4b58de38 | 223 | $grouping = NULL; |
f654cacf | 224 | self::initializeAnySoftCreditClause($query); |
6a488035 | 225 | foreach (array_keys($query->_params) as $id) { |
a7488080 | 226 | if (empty($query->_params[$id][0])) { |
6a488035 TO |
227 | continue; |
228 | } | |
afa0b07c | 229 | 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_') { |
6a488035 TO |
230 | if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) { |
231 | $query->_useDistinct = TRUE; | |
232 | } | |
39b795ba DL |
233 | // CRM-12065 |
234 | if ( | |
235 | $query->_params[$id][0] == 'contribution_type_id' || | |
236 | $query->_params[$id][0] == 'contribution_type' | |
237 | ) { | |
238 | CRM_Core_Session::setStatus( | |
239 | ts('The contribution type criteria is now obsolete, please update your smart group'), | |
240 | '', | |
241 | 'alert' | |
242 | ); | |
243 | continue; | |
244 | } | |
245 | ||
6a488035 TO |
246 | $grouping = $query->_params[$id][3]; |
247 | self::whereClauseSingle($query->_params[$id], $query); | |
248 | } | |
249 | } | |
6a488035 TO |
250 | } |
251 | ||
186c9c17 EM |
252 | /** |
253 | * @param $values | |
254 | * @param $query | |
255 | */ | |
00be9182 | 256 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
257 | list($name, $op, $value, $grouping, $wildcard) = $values; |
258 | ||
dbc6f6d6 | 259 | $quoteValue = NULL; |
afa0b07c | 260 | $fields = array_merge(CRM_Contribute_BAO_Contribution::fields(), self::getFields()); |
163b1da9 KJ |
261 | |
262 | if (!empty($value) && !is_array($value)) { | |
6a488035 TO |
263 | $quoteValue = "\"$value\""; |
264 | } | |
265 | ||
266 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; | |
b32bc653 | 267 | foreach (self::getRecurringFields() as $dateField => $dateFieldTitle) { |
22e263ad | 268 | if (self::buildDateWhere($values, $query, $name, $dateField, $dateFieldTitle)) { |
a91e698a | 269 | return; |
270 | } | |
271 | } | |
6a488035 TO |
272 | switch ($name) { |
273 | case 'contribution_date': | |
274 | case 'contribution_date_low': | |
275 | case 'contribution_date_low_time': | |
276 | case 'contribution_date_high': | |
277 | case 'contribution_date_high_time': | |
278 | // process to / from date | |
279 | $query->dateQueryBuilder($values, | |
280 | 'civicrm_contribution', 'contribution_date', 'receive_date', 'Contribution Date' | |
281 | ); | |
282 | return; | |
283 | ||
284 | case 'contribution_amount': | |
285 | case 'contribution_amount_low': | |
286 | case 'contribution_amount_high': | |
287 | // process min/max amount | |
288 | $query->numberRangeBuilder($values, | |
289 | 'civicrm_contribution', 'contribution_amount', | |
290 | 'total_amount', 'Contribution Amount', | |
291 | NULL | |
292 | ); | |
293 | return; | |
294 | ||
6a488035 TO |
295 | case 'contribution_thankyou_date_is_not_null': |
296 | if ($value) { | |
297 | $op = "IS NOT NULL"; | |
298 | $query->_qill[$grouping][] = ts('Contribution Thank-you Sent'); | |
299 | } | |
300 | else { | |
301 | $op = "IS NULL"; | |
302 | $query->_qill[$grouping][] = ts('Contribution Thank-you Not Sent'); | |
303 | } | |
304 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.thankyou_date", $op); | |
305 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
306 | return; | |
307 | ||
308 | case 'contribution_receipt_date_is_not_null': | |
309 | if ($value) { | |
310 | $op = "IS NOT NULL"; | |
311 | $query->_qill[$grouping][] = ts('Contribution Receipt Sent'); | |
312 | } | |
313 | else { | |
314 | $op = "IS NULL"; | |
315 | $query->_qill[$grouping][] = ts('Contribution Receipt Not Sent'); | |
316 | } | |
317 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.receipt_date", $op); | |
318 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
319 | return; | |
320 | ||
4ca8b3e8 | 321 | case 'financial_type_id': |
3c151c70 | 322 | case 'contribution_page_id': |
3c151c70 | 323 | case 'contribution_status_id': |
324 | case 'contribution_id': | |
325 | case 'contribution_currency_type': | |
326 | case 'contribution_currency': | |
327 | case 'contribution_source': | |
3c151c70 | 328 | case 'contribution_trxn_id': |
3a6eb174 | 329 | case 'contribution_check_number': |
3c151c70 | 330 | case (strpos($name, '_amount') !== FALSE): |
331 | case (strpos($name, '_date') !== FALSE): | |
332 | $qillName = $name; | |
333 | $pseudoExtraParam = NULL; | |
3c151c70 | 334 | if ((strpos($name, '_amount') !== FALSE) || (strpos($name, '_date') !== FALSE) || in_array($name, |
335 | array( | |
336 | 'contribution_id', | |
337 | 'contribution_currency', | |
3c151c70 | 338 | 'contribution_source', |
339 | 'contribution_trxn_id', | |
243a6bce | 340 | 'contribution_check_number', |
3c151c70 | 341 | ) |
342 | ) | |
343 | ) { | |
344 | $name = str_replace('contribution_', '', $name); | |
243a6bce | 345 | if (!in_array($name, array('source', 'id'))) { |
3c151c70 | 346 | $qillName = str_replace('contribution_', '', $qillName); |
dbc6f6d6 | 347 | } |
5bbd985a | 348 | } |
3c151c70 | 349 | if (in_array($name, array('contribution_currency', 'contribution_currency_type'))) { |
350 | $qillName = $name = 'currency'; | |
351 | $pseudoExtraParam = array('labelColumn' => 'name'); | |
352 | } | |
5bbd985a | 353 | |
3c151c70 | 354 | $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String'; |
355 | ||
356 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.$name", $op, $value, $dataType); | |
9ab34172 | 357 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', $name, $value, $op, $pseudoExtraParam); |
3c151c70 | 358 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value)); |
6a488035 TO |
359 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
360 | return; | |
361 | ||
afa0b07c | 362 | case 'financial_type': |
363 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fields[$name]['where'], $op, $value, 'String'); | |
afa0b07c | 364 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', $name, $value, $op); |
365 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value)); | |
366 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
367 | $query->_tables['civicrm_financial_type'] = $query->_whereTables['civicrm_financial_type'] = 1; | |
368 | return; | |
369 | ||
3c151c70 | 370 | case 'contribution_page': |
371 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fields[$name]['where'], $op, $value, 'String'); | |
9ab34172 | 372 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', $name, $value, $op); |
3c151c70 | 373 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value)); |
374 | $query->_tables['civicrm_contribution_page'] = $query->_whereTables['civicrm_contribution_page'] = 1; | |
6a488035 TO |
375 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
376 | return; | |
377 | ||
7d543448 | 378 | case 'contribution_payment_instrument': |
379 | case 'contribution_payment_instrument_id': | |
380 | $name = str_replace('contribution_', '', $name); | |
afa0b07c | 381 | case 'payment_instrument': |
3a6eb174 | 382 | case 'payment_instrument_id': |
383 | if ($name == 'payment_instrument') { | |
384 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("contribution_payment_instrument.label", $op, $value); | |
385 | } | |
386 | else { | |
387 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("contribution_payment_instrument.value", $op, $value, 'Int'); | |
388 | } | |
afa0b07c | 389 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', 'payment_instrument_id', $value, $op); |
3a6eb174 | 390 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields['payment_instrument']['title'], 2 => $op, 3 => $value)); |
afa0b07c | 391 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
392 | $query->_tables['contribution_payment_instrument'] = $query->_whereTables['contribution_payment_instrument'] = 1; | |
393 | return; | |
394 | ||
243a6bce | 395 | case 'contribution_status': |
243a6bce | 396 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, 'String'); |
397 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', $name, $value, $op); | |
243a6bce | 398 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value)); |
399 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
afa0b07c | 400 | $query->_tables[$name] = $query->_whereTables[$name] = 1; |
243a6bce | 401 | break; |
402 | ||
6a488035 | 403 | case 'contribution_pcp_made_through_id': |
3c151c70 | 404 | case 'contribution_soft_credit_type_id': |
405 | $qillName = $name; | |
406 | if ($name == 'contribution_pcp_made_through_id') { | |
407 | $qillName = $name = 'pcp_id'; | |
408 | $fields[$name] = array('title' => ts('Personal Campaign Page'), 'type' => 2); | |
409 | } | |
410 | if ($name == 'contribution_soft_credit_type_id') { | |
411 | $qillName = str_replace('_id', '', $qillName); | |
412 | $fields[$qillName]['type'] = $fields[$qillName]['data_type']; | |
413 | $name = str_replace('contribution_', '', $name); | |
414 | } | |
415 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_soft.$name", | |
416 | $op, $value, CRM_Utils_Type::typeToString($fields[$qillName]['type']) | |
417 | ); | |
9ab34172 | 418 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_ContributionSoft', $name, $value, $op); |
3c151c70 | 419 | $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value)); |
6a488035 TO |
420 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
421 | return; | |
422 | ||
36bf52ef DS |
423 | case 'contribution_or_softcredits': |
424 | if ($value == 'only_scredits') { | |
3dbf477c | 425 | $query->_where[$grouping][] = "contribution_search_scredit_combined.scredit_id IS NOT NULL"; |
36bf52ef | 426 | $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits Only'); |
ed81a415 | 427 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
36bf52ef | 428 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
0db6c3e1 | 429 | } |
4c9b6178 | 430 | elseif ($value == 'both_related') { |
3dbf477c | 431 | $query->_where[$grouping][] = "contribution_search_scredit_combined.filter_id IS NOT NULL"; |
4f3846df | 432 | $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Soft Credits with related Hard Credit'); |
ed81a415 | 433 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
3dbf477c | 434 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
0db6c3e1 | 435 | } |
4c9b6178 | 436 | elseif ($value == 'both') { |
36bf52ef | 437 | $query->_qill[$grouping][] = ts('Contributions OR Soft Credits? - Both'); |
ed81a415 | 438 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
36bf52ef DS |
439 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; |
440 | } | |
441 | // default option: $value == 'only_contribs' | |
442 | return; | |
443 | ||
6a488035 | 444 | case 'contribution_is_test': |
3a6eb174 | 445 | // By default is Contribution Search form we choose is_test = 0 in otherwords always show active contribution |
446 | // so in case if any one choose any Yes/No avoid the default clause otherwise it will be conflict in whereClause | |
447 | $key = array_search('civicrm_contribution.is_test = 0', $query->_where[$grouping]); | |
448 | if (!empty($key)) { | |
449 | unset($query->_where[$grouping][$key]); | |
450 | } | |
6a488035 | 451 | case 'contribution_test': |
4b58de38 CW |
452 | // We dont want to include all tests for sql OR CRM-7827 |
453 | if (!$value || $query->getOperator() != 'OR') { | |
454 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_test", $op, $value, "Boolean"); | |
455 | if ($value) { | |
456 | $query->_qill[$grouping][] = ts("Only Display Test Contributions"); | |
457 | } | |
458 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
6a488035 | 459 | } |
6a488035 TO |
460 | return; |
461 | ||
462 | case 'contribution_is_pay_later': | |
463 | case 'contribution_pay_later': | |
464 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.is_pay_later", $op, $value, "Boolean"); | |
465 | if ($value) { | |
466 | $query->_qill[$grouping][] = ts("Find Pay Later Contributions"); | |
467 | } | |
468 | else { | |
469 | $query->_qill[$grouping][] = ts("Exclude Pay Later Contributions"); | |
470 | } | |
471 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
472 | return; | |
473 | ||
474 | case 'contribution_recurring': | |
475 | if ($value) { | |
476 | $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NOT NULL"; | |
477 | $query->_qill[$grouping][] = ts("Find Recurring Contributions"); | |
478 | $query->_tables['civicrm_contribution_recur'] = $query->_whereTables['civicrm_contribution_recur'] = 1; | |
479 | } | |
480 | else { | |
481 | $query->_where[$grouping][] = "civicrm_contribution.contribution_recur_id IS NULL"; | |
482 | $query->_qill[$grouping][] = ts("Exclude Recurring Contributions"); | |
483 | } | |
484 | return; | |
485 | ||
486 | case 'contribution_recur_id': | |
487 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.contribution_recur_id", | |
488 | $op, $value, "Integer" | |
489 | ); | |
490 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
491 | return; | |
492 | ||
6a488035 TO |
493 | case 'contribution_note': |
494 | $value = $strtolower(CRM_Core_DAO::escapeString($value)); | |
495 | if ($wildcard) { | |
496 | $value = "%$value%"; | |
497 | $op = 'LIKE'; | |
498 | } | |
499 | $wc = ($op != 'LIKE') ? "LOWER(civicrm_note.note)" : "civicrm_note.note"; | |
500 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, "String"); | |
501 | $query->_qill[$grouping][] = ts('Contribution Note %1 %2', array(1 => $op, 2 => $quoteValue)); | |
502 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = $query->_whereTables['contribution_note'] = 1; | |
503 | return; | |
504 | ||
505 | case 'contribution_membership_id': | |
506 | $query->_where[$grouping][] = " civicrm_membership.id $op $value"; | |
507 | $query->_tables['contribution_membership'] = $query->_whereTables['contribution_membership'] = 1; | |
508 | ||
509 | return; | |
510 | ||
511 | case 'contribution_participant_id': | |
512 | $query->_where[$grouping][] = " civicrm_participant.id $op $value"; | |
513 | $query->_tables['contribution_participant'] = $query->_whereTables['contribution_participant'] = 1; | |
514 | return; | |
515 | ||
516 | case 'contribution_pcp_display_in_roll': | |
517 | $query->_where[$grouping][] = " civicrm_contribution_soft.pcp_display_in_roll $op '$value'"; | |
518 | if ($value) { | |
519 | $query->_qill[$grouping][] = ts("Personal Campaign Page Honor Roll"); | |
520 | } | |
521 | else { | |
522 | $query->_qill[$grouping][] = ts("NOT Personal Campaign Page Honor Roll"); | |
523 | } | |
524 | $query->_tables['civicrm_contribution_soft'] = $query->_whereTables['civicrm_contribution_soft'] = 1; | |
525 | return; | |
526 | ||
6a488035 TO |
527 | case 'contribution_campaign_id': |
528 | $campParams = array( | |
529 | 'op' => $op, | |
530 | 'campaign' => $value, | |
531 | 'grouping' => $grouping, | |
532 | 'tableName' => 'civicrm_contribution', | |
533 | ); | |
534 | CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query); | |
535 | return; | |
536 | ||
537 | case 'contribution_batch_id': | |
91aff94c | 538 | $batches = CRM_Contribute_PseudoConstant::batch(); |
6a488035 TO |
539 | $query->_where[$grouping][] = " civicrm_entity_batch.batch_id $op $value"; |
540 | $query->_qill[$grouping][] = ts('Batch Name %1 %2', array(1 => $op, 2 => $batches[$value])); | |
39b795ba | 541 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; |
6a488035 TO |
542 | $query->_tables['contribution_batch'] = $query->_whereTables['contribution_batch'] = 1; |
543 | return; | |
544 | ||
545 | default: | |
546 | //all other elements are handle in this case | |
353ffa53 | 547 | $fldName = substr($name, 13); |
ec8c4582 DL |
548 | if (!isset($fields[$fldName])) { |
549 | // CRM-12597 | |
550 | CRM_Core_Session::setStatus(ts( | |
fb5b081b | 551 | 'We did not recognize the search field: %1. Please check and fix your contribution related smart groups.', |
ec8c4582 DL |
552 | array(1 => $fldName) |
553 | ) | |
554 | ); | |
555 | return; | |
556 | } | |
6a488035 | 557 | $whereTable = $fields[$fldName]; |
353ffa53 | 558 | $value = trim($value); |
6a488035 | 559 | |
3c151c70 | 560 | $dataType = "String"; |
561 | if (!empty($whereTable['type'])) { | |
562 | $dataType = CRM_Utils_Type::typeToString($whereTable['type']); | |
6a488035 TO |
563 | } |
564 | ||
565 | $wc = ($op != 'LIKE' && $dataType != 'Date') ? "LOWER($whereTable[where])" : "$whereTable[where]"; | |
566 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($wc, $op, $value, $dataType); | |
567 | $query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue"; | |
568 | list($tableName, $fieldName) = explode('.', $whereTable['where'], 2); | |
569 | $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1; | |
570 | if ($tableName == 'civicrm_contribution_product') { | |
571 | $query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1; | |
572 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
573 | } | |
574 | else { | |
575 | $query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1; | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
186c9c17 | 580 | /** |
100fef9d | 581 | * @param string $name |
186c9c17 EM |
582 | * @param $mode |
583 | * @param $side | |
584 | * | |
e60f24eb | 585 | * @return NULL|string |
186c9c17 | 586 | */ |
00be9182 | 587 | public static function from($name, $mode, $side) { |
6a488035 TO |
588 | $from = NULL; |
589 | switch ($name) { | |
353ffa53 TO |
590 | case 'civicrm_contribution': |
591 | $from = " $side JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id "; | |
592 | if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) { | |
593 | // switch the from table if its only soft credit search | |
594 | $from = " $side JOIN contribution_search_scredit_combined ON contribution_search_scredit_combined.contact_id = contact_a.id "; | |
595 | $from .= " $side JOIN civicrm_contribution ON civicrm_contribution.id = contribution_search_scredit_combined.id "; | |
596 | $from .= " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.id = contribution_search_scredit_combined.scredit_id"; | |
597 | } | |
598 | break; | |
6a488035 TO |
599 | |
600 | case 'civicrm_contribution_recur': | |
86845f9b | 601 | if ($mode == 1) { |
602 | // in contact mode join directly onto profile - in case no contributions exist yet | |
603 | $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id "; | |
604 | } | |
605 | else { | |
606 | $from = " $side JOIN civicrm_contribution_recur ON civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id "; | |
607 | } | |
6a488035 TO |
608 | break; |
609 | ||
610 | case 'civicrm_financial_type': | |
611 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
612 | $from = " INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id "; | |
613 | } | |
614 | else { | |
615 | $from = " $side JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id "; | |
616 | } | |
617 | break; | |
618 | ||
6ef17211 | 619 | case 'civicrm_financial_account': |
620 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTACTS) { | |
621 | $from = " $side JOIN civicrm_financial_account ON contact_a.id = civicrm_financial_account.contact_id "; | |
622 | } | |
623 | break; | |
624 | ||
6a488035 | 625 | case 'civicrm_accounting_code': |
6ef17211 | 626 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { |
627 | $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' "; | |
628 | $from .= " INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id "; | |
629 | $from .= " INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is' "; | |
630 | $from .= " INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship' "; | |
631 | } | |
39b795ba | 632 | break; |
6a488035 TO |
633 | |
634 | case 'civicrm_contribution_page': | |
3c151c70 | 635 | $from = " $side JOIN civicrm_contribution_page ON civicrm_contribution.contribution_page_id = civicrm_contribution_page.id"; |
6a488035 TO |
636 | break; |
637 | ||
638 | case 'civicrm_product': | |
639 | $from = " $side JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id"; | |
640 | $from .= " $side JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id "; | |
641 | break; | |
642 | ||
643 | case 'contribution_payment_instrument': | |
644 | $from = " $side JOIN civicrm_option_group option_group_payment_instrument ON ( option_group_payment_instrument.name = 'payment_instrument')"; | |
243a6bce | 645 | $from .= " $side JOIN civicrm_option_value contribution_payment_instrument ON (civicrm_contribution.payment_instrument_id = contribution_payment_instrument.value |
646 | AND option_group_payment_instrument.id = contribution_payment_instrument.option_group_id ) "; | |
6a488035 TO |
647 | break; |
648 | ||
6a488035 TO |
649 | case 'contribution_status': |
650 | $from = " $side JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status')"; | |
651 | $from .= " $side JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value | |
652 | AND option_group_contribution_status.id = contribution_status.option_group_id ) "; | |
653 | break; | |
654 | ||
36bf52ef | 655 | case 'contribution_softcredit_type': |
353ffa53 | 656 | $from = " $side JOIN civicrm_option_group option_group_contribution_softcredit_type ON |
36bf52ef | 657 | (option_group_contribution_softcredit_type.name = 'soft_credit_type')"; |
77b97be7 | 658 | $from .= " $side JOIN civicrm_option_value contribution_softcredit_type ON |
36bf52ef DS |
659 | ( civicrm_contribution_soft.soft_credit_type_id = contribution_softcredit_type.value |
660 | AND option_group_contribution_softcredit_type.id = contribution_softcredit_type.option_group_id )"; | |
661 | break; | |
662 | ||
6a488035 TO |
663 | case 'contribution_note': |
664 | $from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND | |
665 | civicrm_contribution.id = civicrm_note.entity_id )"; | |
666 | break; | |
667 | ||
6a488035 TO |
668 | case 'contribution_membership': |
669 | $from = " $side JOIN civicrm_membership_payment ON civicrm_membership_payment.contribution_id = civicrm_contribution.id"; | |
670 | $from .= " $side JOIN civicrm_membership ON civicrm_membership_payment.membership_id = civicrm_membership.id "; | |
671 | break; | |
672 | ||
673 | case 'contribution_participant': | |
674 | $from = " $side JOIN civicrm_participant_payment ON civicrm_participant_payment.contribution_id = civicrm_contribution.id"; | |
675 | $from .= " $side JOIN civicrm_participant ON civicrm_participant_payment.participant_id = civicrm_participant.id "; | |
676 | break; | |
677 | ||
678 | case 'civicrm_contribution_soft': | |
0b256244 | 679 | if (!in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) { |
3dbf477c DS |
680 | $from = " $side JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id"; |
681 | } | |
6a488035 TO |
682 | break; |
683 | ||
684 | case 'civicrm_contribution_soft_contact': | |
3dbf477c | 685 | if (in_array(self::$_contribOrSoftCredit, array("only_scredits", "both_related", "both"))) { |
77b97be7 | 686 | $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution.contact_id = civicrm_contact_d.id ) |
3dbf477c | 687 | AND contribution_search_scredit_combined.scredit_id IS NOT NULL"; |
0db6c3e1 TO |
688 | } |
689 | else { | |
3dbf477c DS |
690 | $from .= " $side JOIN civicrm_contact civicrm_contact_d ON (civicrm_contribution_soft.contact_id = civicrm_contact_d.id )"; |
691 | } | |
6a488035 TO |
692 | break; |
693 | ||
694 | case 'civicrm_contribution_soft_email': | |
695 | $from .= " $side JOIN civicrm_email as soft_email ON (civicrm_contact_d.id = soft_email.contact_id )"; | |
696 | break; | |
697 | ||
698 | case 'civicrm_contribution_soft_phone': | |
699 | $from .= " $side JOIN civicrm_phone as soft_phone ON (civicrm_contact_d.id = soft_phone.contact_id )"; | |
700 | break; | |
39b795ba | 701 | |
6a488035 | 702 | case 'contribution_batch': |
61412579 | 703 | $from .= " $side JOIN civicrm_entity_financial_trxn ON ( |
704 | civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution' | |
705 | AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )"; | |
706 | ||
707 | $from .= " $side JOIN civicrm_financial_trxn ON ( | |
708 | civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )"; | |
709 | ||
710 | $from .= " $side JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn' | |
711 | AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )"; | |
712 | ||
6a488035 TO |
713 | $from .= " $side JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id"; |
714 | break; | |
715 | } | |
716 | return $from; | |
717 | } | |
718 | ||
186c9c17 EM |
719 | /** |
720 | * @param $query | |
721 | */ | |
00be9182 | 722 | public static function initializeAnySoftCreditClause(&$query) { |
d4e2b978 | 723 | if (self::isSoftCreditOptionEnabled($query->_params)) { |
f654cacf | 724 | if ($query->_mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { |
ed81a415 | 725 | unset($query->_distinctComponentClause); |
f654cacf | 726 | $query->_rowCountClause = " count(civicrm_contribution.id)"; |
ed81a415 | 727 | $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id "; |
36bf52ef | 728 | } |
f654cacf DS |
729 | } |
730 | } | |
731 | ||
186c9c17 EM |
732 | /** |
733 | * @param array $queryParams | |
734 | * | |
735 | * @return bool | |
736 | */ | |
00be9182 | 737 | public static function isSoftCreditOptionEnabled($queryParams = array()) { |
f654cacf DS |
738 | static $tempTableFilled = FALSE; |
739 | if (!empty($queryParams)) { | |
740 | foreach (array_keys($queryParams) as $id) { | |
741 | if (empty($queryParams[$id][0])) { | |
742 | continue; | |
743 | } | |
38e1cb31 DS |
744 | if ($queryParams[$id][0] == 'contribution_or_softcredits') { |
745 | self::$_contribOrSoftCredit = $queryParams[$id][2]; | |
746 | } | |
f654cacf DS |
747 | } |
748 | } | |
77b97be7 | 749 | if (in_array(self::$_contribOrSoftCredit, |
f654cacf | 750 | array("only_scredits", "both_related", "both"))) { |
353ffa53 TO |
751 | if (!$tempTableFilled) { |
752 | // build a temp table which is union of contributions and soft credits | |
753 | // note: group-by in first part ensures uniqueness in counts | |
754 | $tempQuery = " | |
77b97be7 EM |
755 | CREATE TEMPORARY TABLE IF NOT EXISTS contribution_search_scredit_combined AS |
756 | SELECT con.id as id, con.contact_id, cso.id as filter_id, NULL as scredit_id | |
3dbf477c | 757 | FROM civicrm_contribution con |
4f3846df DS |
758 | LEFT JOIN civicrm_contribution_soft cso ON con.id = cso.contribution_id |
759 | GROUP BY id, contact_id, scredit_id | |
3dbf477c | 760 | UNION ALL |
77b97be7 | 761 | SELECT scredit.contribution_id as id, scredit.contact_id, scredit.id as filter_id, scredit.id as scredit_id |
3dbf477c | 762 | FROM civicrm_contribution_soft as scredit"; |
353ffa53 TO |
763 | CRM_Core_DAO::executeQuery($tempQuery); |
764 | $tempTableFilled = TRUE; | |
f654cacf | 765 | } |
353ffa53 TO |
766 | return TRUE; |
767 | } | |
36bf52ef DS |
768 | return FALSE; |
769 | } | |
770 | ||
186c9c17 EM |
771 | /** |
772 | * @param bool $isExportMode | |
773 | * | |
774 | * @return array | |
775 | */ | |
353ffa53 | 776 | public static function softCreditReturnProperties($isExportMode = FALSE) { |
4f3846df | 777 | $properties = array( |
353ffa53 | 778 | 'contribution_soft_credit_name' => 1, |
4f3846df | 779 | 'contribution_soft_credit_amount' => 1, |
353ffa53 | 780 | 'contribution_soft_credit_type' => 1, |
4f3846df DS |
781 | ); |
782 | if ($isExportMode) { | |
783 | $properties['contribution_soft_credit_contribution_id'] = 1; | |
784 | } | |
785 | return $properties; | |
786 | } | |
787 | ||
186c9c17 EM |
788 | /** |
789 | * @param $mode | |
790 | * @param bool $includeCustomFields | |
791 | * | |
e60f24eb | 792 | * @return array|NULL |
186c9c17 | 793 | */ |
00be9182 | 794 | public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) { |
6a488035 TO |
795 | $properties = NULL; |
796 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
797 | $properties = array( | |
798 | 'contact_type' => 1, | |
799 | 'contact_sub_type' => 1, | |
800 | 'sort_name' => 1, | |
801 | 'display_name' => 1, | |
802 | 'financial_type' => 1, | |
803 | 'contribution_source' => 1, | |
804 | 'receive_date' => 1, | |
805 | 'thankyou_date' => 1, | |
806 | 'cancel_date' => 1, | |
807 | 'total_amount' => 1, | |
808 | 'accounting_code' => 1, | |
afa0b07c | 809 | 'payment_instrument' => 1, |
3a6eb174 | 810 | 'payment_instrument_id' => 1, |
6a488035 TO |
811 | 'check_number' => 1, |
812 | 'non_deductible_amount' => 1, | |
813 | 'fee_amount' => 1, | |
814 | 'net_amount' => 1, | |
815 | 'trxn_id' => 1, | |
816 | 'invoice_id' => 1, | |
817 | 'currency' => 1, | |
818 | 'cancel_reason' => 1, | |
819 | 'receipt_date' => 1, | |
820 | 'product_name' => 1, | |
821 | 'sku' => 1, | |
822 | 'product_option' => 1, | |
823 | 'fulfilled_date' => 1, | |
824 | 'contribution_start_date' => 1, | |
825 | 'contribution_end_date' => 1, | |
826 | 'is_test' => 1, | |
827 | 'is_pay_later' => 1, | |
828 | 'contribution_status' => 1, | |
829 | 'contribution_status_id' => 1, | |
830 | 'contribution_recur_id' => 1, | |
831 | 'amount_level' => 1, | |
832 | 'contribution_note' => 1, | |
833 | 'contribution_batch' => 1, | |
36bf52ef | 834 | 'contribution_campaign_id' => 1, |
6a488035 | 835 | ); |
d4e2b978 | 836 | if (self::isSoftCreditOptionEnabled()) { |
4f3846df | 837 | $properties = array_merge($properties, self::softCreditReturnProperties()); |
36bf52ef | 838 | } |
6a488035 TO |
839 | if ($includeCustomFields) { |
840 | // also get all the custom contribution properties | |
841 | $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Contribution'); | |
842 | if (!empty($fields)) { | |
843 | foreach ($fields as $name => $dontCare) { | |
844 | $properties[$name] = 1; | |
845 | } | |
846 | } | |
847 | } | |
848 | } | |
849 | return $properties; | |
850 | } | |
851 | ||
852 | /** | |
fe482240 | 853 | * Add all the elements shared between contribute search and advnaced search. |
6a488035 | 854 | * |
6a488035 | 855 | * |
c490a46a | 856 | * @param CRM_Core_Form $form |
fd31fa4c | 857 | * |
6a488035 | 858 | * @return void |
6a488035 | 859 | */ |
00be9182 | 860 | public static function buildSearchForm(&$form) { |
6a488035 TO |
861 | |
862 | // Added contribution source | |
863 | $form->addElement('text', 'contribution_source', ts('Contribution Source'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'source')); | |
864 | ||
bc3f7f04 | 865 | CRM_Core_Form_Date::buildDateRange($form, 'contribution_date', 1, '_low', '_high', ts('From:'), FALSE); |
6a488035 TO |
866 | |
867 | $form->add('text', 'contribution_amount_low', ts('From'), array('size' => 8, 'maxlength' => 8)); | |
868 | $form->addRule('contribution_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money'); | |
869 | ||
870 | $form->add('text', 'contribution_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8)); | |
871 | $form->addRule('contribution_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money'); | |
872 | ||
873 | // Adding select option for curreny type -- CRM-4711 | |
874 | $form->add('select', 'contribution_currency_type', | |
875 | ts('Currency Type'), | |
876 | array( | |
7c550ca0 | 877 | '' => ts('- any -'), |
353ffa53 | 878 | ) + |
ab345ca5 CW |
879 | CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')), |
880 | FALSE, array('class' => 'crm-select2') | |
6a488035 TO |
881 | ); |
882 | ||
1f0d8c92 | 883 | // CRM-13848 |
15cf9198 | 884 | $form->addSelect('financial_type_id', |
76773c5a | 885 | array('entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search') |
6a488035 TO |
886 | ); |
887 | ||
888 | $form->add('select', 'contribution_page_id', | |
889 | ts('Contribution Page'), | |
890 | array( | |
7c550ca0 | 891 | '' => ts('- any -'), |
353ffa53 | 892 | ) + |
ab345ca5 CW |
893 | CRM_Contribute_PseudoConstant::contributionPage(), |
894 | FALSE, array('class' => 'crm-select2') | |
6a488035 TO |
895 | ); |
896 | ||
3a6eb174 | 897 | $form->addSelect('payment_instrument_id', |
898 | array('entity' => 'contribution', 'label' => ts('Payment Method'), 'option_url' => NULL, 'placeholder' => ts('- any -')) | |
6a488035 TO |
899 | ); |
900 | ||
901 | $form->add('select', 'contribution_pcp_made_through_id', | |
902 | ts('Personal Campaign Page'), | |
903 | array( | |
7c550ca0 | 904 | '' => ts('- any -'), |
353ffa53 | 905 | ) + |
ab345ca5 CW |
906 | CRM_Contribute_PseudoConstant::pcPage(), |
907 | FALSE, array('class' => 'crm-select2') | |
6a488035 TO |
908 | ); |
909 | ||
910 | $status = array(); | |
911 | ||
3c151c70 | 912 | $statusValues = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status'); |
6a488035 TO |
913 | // Remove status values that are only used for recurring contributions or pledges (In Progress, Overdue). |
914 | unset($statusValues['5'], $statusValues['6']); | |
915 | ||
243a6bce | 916 | $form->addSelect('contribution_status_id', |
917 | array('entity' => 'contribution', 'multiple' => 'multiple', 'label' => ts('Contribution Status(s)'), 'option_url' => NULL, 'placeholder' => ts('- any -')) | |
3c151c70 | 918 | ); |
6a488035 TO |
919 | |
920 | // Add fields for thank you and receipt | |
8a4f27dc CW |
921 | $form->addYesNo('contribution_thankyou_date_is_not_null', ts('Thank-you sent?'), TRUE); |
922 | $form->addYesNo('contribution_receipt_date_is_not_null', ts('Receipt sent?'), TRUE); | |
6a488035 | 923 | |
8a4f27dc CW |
924 | $form->addYesNo('contribution_pay_later', ts('Contribution is Pay Later?'), TRUE); |
925 | $form->addYesNo('contribution_recurring', ts('Contribution is Recurring?'), TRUE); | |
b32bc653 CW |
926 | |
927 | // Recurring contribution fields | |
928 | foreach (self::getRecurringFields() as $key => $label) { | |
15cf9198 | 929 | CRM_Core_Form_Date::buildDateRange($form, $key, 1, '_low', '_high'); |
b32bc653 CW |
930 | // If data has been entered for a recurring field, tell the tpl layer to open the pane |
931 | if (!empty($form->_formValues[$key . '_relative']) || !empty($form->_formValues[$key . '_low']) || !empty($form->_formValues[$key . '_high'])) { | |
932 | $form->assign('contribution_recur_pane_open', TRUE); | |
933 | } | |
934 | } | |
a91e698a | 935 | |
8a4f27dc | 936 | $form->addYesNo('contribution_test', ts('Contribution is a Test?'), TRUE); |
6a488035 TO |
937 | |
938 | // Add field for transaction ID search | |
3c151c70 | 939 | $form->addElement('text', 'contribution_trxn_id', ts("Transaction ID")); |
6a488035 TO |
940 | |
941 | $form->addElement('text', 'contribution_check_number', ts('Check Number')); | |
942 | ||
943 | // Add field for pcp display in roll search | |
8a4f27dc | 944 | $form->addYesNo('contribution_pcp_display_in_roll', ts('Personal Campaign Page Honor Roll?'), TRUE); |
6a488035 | 945 | |
36bf52ef DS |
946 | // Soft credit related fields |
947 | $options = array( | |
3dbf477c | 948 | 'only_contribs' => ts('Contributions Only'), |
e0b774f0 | 949 | 'only_scredits' => ts('Soft Credits Only'), |
353ffa53 TO |
950 | 'both_related' => ts('Soft Credits with related Hard Credit'), |
951 | 'both' => ts('Both'), | |
36bf52ef DS |
952 | ); |
953 | $form->add('select', 'contribution_or_softcredits', ts('Contributions OR Soft Credits?'), $options, FALSE, array('class' => "crm-select2")); | |
e88186f4 | 954 | $form->addSelect( |
77b97be7 | 955 | 'contribution_soft_credit_type_id', |
e88186f4 | 956 | array( |
353ffa53 TO |
957 | 'entity' => 'contribution_soft', |
958 | 'field' => 'soft_credit_type_id', | |
959 | 'multiple' => TRUE, | |
960 | 'context' => 'search', | |
e88186f4 DS |
961 | ) |
962 | ); | |
36bf52ef | 963 | |
6a488035 TO |
964 | // Add all the custom searchable fields |
965 | $contribution = array('Contribution'); | |
966 | $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $contribution); | |
967 | if ($groupDetails) { | |
968 | $form->assign('contributeGroupTree', $groupDetails); | |
969 | foreach ($groupDetails as $group) { | |
970 | foreach ($group['fields'] as $field) { | |
971 | $fieldId = $field['id']; | |
972 | $elementName = 'custom_' . $fieldId; | |
973 | CRM_Core_BAO_CustomField::addQuickFormElement($form, | |
974 | $elementName, | |
975 | $fieldId, | |
976 | FALSE, FALSE, TRUE | |
977 | ); | |
978 | } | |
979 | } | |
980 | } | |
981 | ||
982 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'contribution_campaign_id'); | |
983 | ||
984 | // Add batch select | |
91aff94c | 985 | $batches = CRM_Contribute_PseudoConstant::batch(); |
39b795ba | 986 | |
481a74f4 | 987 | if (!empty($batches)) { |
6a488035 TO |
988 | $form->add('select', 'contribution_batch_id', |
989 | ts('Batch Name'), | |
ab345ca5 CW |
990 | array('' => ts('- any -')) + $batches, |
991 | FALSE, array('class' => 'crm-select2') | |
992 | ); | |
6a488035 TO |
993 | } |
994 | ||
995 | $form->assign('validCiviContribute', TRUE); | |
996 | $form->setDefaults(array('contribution_test' => 0)); | |
997 | } | |
998 | ||
186c9c17 EM |
999 | /** |
1000 | * @param $row | |
100fef9d | 1001 | * @param int $id |
186c9c17 | 1002 | */ |
00be9182 | 1003 | public static function searchAction(&$row, $id) { |
6a488035 TO |
1004 | } |
1005 | ||
186c9c17 EM |
1006 | /** |
1007 | * @param $tables | |
1008 | */ | |
00be9182 | 1009 | public static function tableNames(&$tables) { |
6a488035 | 1010 | // Add contribution table |
a7488080 | 1011 | if (!empty($tables['civicrm_product'])) { |
6a488035 TO |
1012 | $tables = array_merge(array('civicrm_contribution' => 1), $tables); |
1013 | } | |
1014 | ||
8cc574cf | 1015 | if (!empty($tables['civicrm_contribution_product']) && empty($tables['civicrm_product'])) { |
6a488035 TO |
1016 | $tables['civicrm_product'] = 1; |
1017 | } | |
1018 | } | |
a91e698a | 1019 | |
1020 | /** | |
fe482240 | 1021 | * Add the where for dates. |
77b97be7 | 1022 | * |
014c4014 TO |
1023 | * @param array $values |
1024 | * Array of query values. | |
1025 | * @param object $query | |
1026 | * The query object. | |
1027 | * @param string $name | |
1028 | * Query field that is set. | |
1029 | * @param string $field | |
1030 | * Name of field to be set. | |
1031 | * @param string $title | |
1032 | * Title of the field. | |
77b97be7 EM |
1033 | * |
1034 | * @return bool | |
a91e698a | 1035 | */ |
00be9182 | 1036 | public static function buildDateWhere(&$values, $query, $name, $field, $title) { |
a91e698a | 1037 | $fieldPart = strpos($name, $field); |
22e263ad | 1038 | if ($fieldPart === FALSE) { |
7c550ca0 | 1039 | return NULL; |
a91e698a | 1040 | } |
1041 | // we only have recurring dates using this ATM so lets' short cut to find the table name | |
1042 | $table = 'contribution_recur'; | |
d3e86119 | 1043 | $fieldName = explode($table . '_', $field); |
a91e698a | 1044 | $query->dateQueryBuilder($values, |
1045 | 'civicrm_' . $table, $field, $fieldName[1], $title | |
1046 | ); | |
1047 | return TRUE; | |
1048 | } | |
b32bc653 | 1049 | |
186c9c17 EM |
1050 | /** |
1051 | * @return array | |
1052 | */ | |
00be9182 | 1053 | public static function getRecurringFields() { |
b32bc653 CW |
1054 | return array( |
1055 | 'contribution_recur_start_date' => ts('Recurring Contribution Start Date'), | |
1056 | 'contribution_recur_next_sched_contribution_date' => ts('Next Scheduled Recurring Contribution'), | |
1057 | 'contribution_recur_cancel_date' => ts('Recurring Contribution Cancel Date'), | |
1058 | 'contribution_recur_end_date' => ts('Recurring Contribution End Date'), | |
1059 | 'contribution_recur_create_date' => ('Recurring Contribution Create Date'), | |
1060 | 'contribution_recur_modified_date' => ('Recurring Contribution Modified Date'), | |
1061 | 'contribution_recur_failure_retry_date' => ts('Failed Recurring Contribution Retry Date'), | |
1062 | ); | |
1063 | } | |
96025800 | 1064 | |
6a488035 | 1065 | } |