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