Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
c5a11b83 | 35 | * Advanced search, extends basic search. |
6a488035 | 36 | */ |
3efb5b86 | 37 | class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { |
6a488035 | 38 | |
6a488035 | 39 | /** |
fe482240 | 40 | * The params that are sent to the query. |
6a488035 TO |
41 | * |
42 | * @var array | |
6a488035 TO |
43 | */ |
44 | protected $_queryParams; | |
45 | ||
6a488035 | 46 | /** |
fe482240 | 47 | * Are we restricting ourselves to a single contact. |
6a488035 | 48 | * |
6a488035 TO |
49 | * @var boolean |
50 | */ | |
51 | protected $_single = FALSE; | |
52 | ||
53 | /** | |
fe482240 | 54 | * Are we restricting ourselves to a single contact. |
6a488035 | 55 | * |
6a488035 TO |
56 | * @var boolean |
57 | */ | |
58 | protected $_limit = NULL; | |
59 | ||
6a488035 | 60 | /** |
fe482240 | 61 | * Prefix for the controller. |
6a488035 TO |
62 | */ |
63 | protected $_prefix = "contribute_"; | |
64 | ||
65 | /** | |
fe482240 | 66 | * Processing needed for buildForm and later. |
95ea96be | 67 | */ |
acb1052e | 68 | public function preProcess() { |
6a488035 TO |
69 | $this->set('searchFormName', 'Search'); |
70 | ||
6a488035 | 71 | $this->_searchButtonName = $this->getButtonName('refresh'); |
6a488035 TO |
72 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
73 | ||
74 | $this->_done = FALSE; | |
c5a11b83 | 75 | // @todo - is this an error - $this->_defaults is used. |
6a488035 TO |
76 | $this->defaults = array(); |
77 | ||
78 | /* | |
79 | * we allow the controller to set force/reset externally, useful when we are being | |
80 | * driven by the wizard framework | |
81 | */ | |
ed106721 | 82 | |
a3d827a7 | 83 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); |
353ffa53 TO |
84 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
85 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
edc80cda | 86 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search'); |
6a488035 TO |
87 | |
88 | $this->assign("context", $this->_context); | |
89 | ||
90 | // get user submitted values | |
91 | // get it from controller only if form has been submitted, else preProcess has set this | |
92 | if (!empty($_POST)) { | |
93 | $this->_formValues = $this->controller->exportValues($this->_name); | |
94 | } | |
95 | else { | |
96 | $this->_formValues = $this->get('formValues'); | |
97 | } | |
98 | ||
99 | //membership ID | |
100 | $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this); | |
101 | if (isset($memberShipId)) { | |
102 | $this->_formValues['contribution_membership_id'] = $memberShipId; | |
103 | } | |
104 | $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this); | |
105 | if (isset($participantId)) { | |
106 | $this->_formValues['contribution_participant_id'] = $participantId; | |
107 | } | |
108 | ||
109 | if ($this->_force) { | |
110 | $this->postProcess(); | |
111 | $this->set('force', 0); | |
112 | } | |
113 | ||
114 | $sortID = NULL; | |
115 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
116 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
117 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
118 | ); | |
119 | } | |
120 | ||
c94d39fd | 121 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
6a488035 TO |
122 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, |
123 | $this->_action, | |
124 | NULL, | |
125 | $this->_single, | |
126 | $this->_limit, | |
127 | $this->_context | |
128 | ); | |
129 | $prefix = NULL; | |
130 | if ($this->_context == 'user') { | |
131 | $prefix = $this->_prefix; | |
132 | } | |
133 | ||
134 | $this->assign("{$prefix}limit", $this->_limit); | |
135 | $this->assign("{$prefix}single", $this->_single); | |
136 | ||
137 | $controller = new CRM_Core_Selector_Controller($selector, | |
138 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
139 | $sortID, | |
140 | CRM_Core_Action::VIEW, | |
141 | $this, | |
142 | CRM_Core_Selector_Controller::TRANSFER, | |
143 | $prefix | |
144 | ); | |
145 | ||
146 | $controller->setEmbedded(TRUE); | |
147 | $controller->moveFromSessionToTemplate(); | |
148 | ||
149 | $this->assign('contributionSummary', $this->get('summary')); | |
150 | } | |
151 | ||
c5a11b83 EM |
152 | /** |
153 | * Set defaults. | |
154 | * | |
155 | * @return array | |
156 | */ | |
00be9182 | 157 | public function setDefaultValues() { |
874c9be7 | 158 | if (empty($this->_defaults['contribution_status'])) { |
6a488035 TO |
159 | $this->_defaults['contribution_status'][1] = 1; |
160 | } | |
161 | return $this->_defaults; | |
162 | } | |
163 | ||
164 | /** | |
fe482240 | 165 | * Build the form object. |
6a488035 | 166 | */ |
00be9182 | 167 | public function buildQuickForm() { |
3efb5b86 | 168 | parent::buildQuickForm(); |
0573fd28 | 169 | $this->addContactSearchFields(); |
6a488035 TO |
170 | |
171 | CRM_Contribute_BAO_Query::buildSearchForm($this); | |
172 | ||
6a488035 TO |
173 | $rows = $this->get('rows'); |
174 | if (is_array($rows)) { | |
175 | if (!$this->_single) { | |
8d36b801 | 176 | $this->addRowSelectors($rows); |
6a488035 TO |
177 | } |
178 | ||
6a488035 TO |
179 | $permission = CRM_Core_Permission::getPermission(); |
180 | ||
c410490a | 181 | $queryParams = $this->get('queryParams'); |
1a72712d | 182 | $taskParams['softCreditFiltering'] = FALSE; |
c410490a | 183 | if (!empty($queryParams)) { |
1a72712d | 184 | $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams); |
c410490a | 185 | } |
1a72712d | 186 | $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams); |
34197a55 | 187 | $this->addTaskMenu($tasks); |
6a488035 TO |
188 | } |
189 | ||
6a488035 TO |
190 | } |
191 | ||
e597fc33 DG |
192 | /** |
193 | * Get the label for the sortName field if email searching is on. | |
194 | * | |
195 | * (email searching is a setting under search preferences). | |
196 | * | |
197 | * @return string | |
198 | */ | |
199 | protected function getSortNameLabelWithEmail() { | |
200 | return ts('Contributor Name or Email'); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Get the label for the sortName field if email searching is off. | |
205 | * | |
206 | * (email searching is a setting under search preferences). | |
207 | * | |
208 | * @return string | |
209 | */ | |
210 | protected function getSortNameLabelWithOutEmail() { | |
211 | return ts('Contributor Name'); | |
212 | } | |
213 | ||
0573fd28 | 214 | /** |
215 | * Get the label for the tag field. | |
216 | * | |
217 | * We do this in a function so the 'ts' wraps the whole string to allow | |
218 | * better translation. | |
219 | * | |
220 | * @return string | |
221 | */ | |
222 | protected function getTagLabel() { | |
223 | return ts('Contributor Tag(s)'); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Get the label for the group field. | |
228 | * | |
229 | * @return string | |
230 | */ | |
231 | protected function getGroupLabel() { | |
232 | return ts('Contributor Group(s)'); | |
233 | } | |
234 | ||
235 | /** | |
236 | * Get the label for the group field. | |
237 | * | |
238 | * @return string | |
239 | */ | |
240 | protected function getContactTypeLabel() { | |
241 | return ts('Contributor Contact Type'); | |
242 | } | |
243 | ||
6a488035 TO |
244 | /** |
245 | * The post processing of the form gets done here. | |
246 | * | |
247 | * Key things done during post processing are | |
c5a11b83 | 248 | * - check for reset or next request. if present, skip post processing. |
6a488035 TO |
249 | * - now check if user requested running a saved search, if so, then |
250 | * the form values associated with the saved search are used for searching. | |
c5a11b83 | 251 | * - if user has done a submit with new values the regular post submission is |
6a488035 TO |
252 | * done. |
253 | * The processing consists of using a Selector / Controller framework for getting the | |
254 | * search results. | |
6a488035 | 255 | */ |
00be9182 | 256 | public function postProcess() { |
6a488035 TO |
257 | if ($this->_done) { |
258 | return; | |
259 | } | |
260 | ||
261 | $this->_done = TRUE; | |
262 | ||
01efed67 | 263 | if (!empty($_POST) && !$this->_force) { |
6a488035 TO |
264 | $this->_formValues = $this->controller->exportValues($this->_name); |
265 | } | |
266 | ||
267 | $this->fixFormValues(); | |
268 | ||
d43b88cc | 269 | // We don't show test records in summaries or dashboards |
b899db8f | 270 | if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') { |
6a488035 TO |
271 | $this->_formValues["contribution_test"] = 0; |
272 | } | |
273 | ||
274 | foreach (array( | |
353ffa53 | 275 | 'contribution_amount_low', |
acb1052e | 276 | 'contribution_amount_high', |
353ffa53 | 277 | ) as $f) { |
6a488035 TO |
278 | if (isset($this->_formValues[$f])) { |
279 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); | |
280 | } | |
281 | } | |
ed106721 | 282 | |
6a488035 | 283 | $config = CRM_Core_Config::singleton(); |
a2be2b19 | 284 | if (!empty($_POST)) { |
afa0b07c | 285 | $specialParams = array( |
286 | 'financial_type_id', | |
287 | 'contribution_soft_credit_type_id', | |
288 | 'contribution_status_id', | |
289 | 'contribution_source', | |
acb1052e | 290 | 'contribution_trxn_id', |
8039a148 | 291 | 'contribution_page_id', |
6ffab5b7 | 292 | 'contribution_product_id', |
b5a37491 | 293 | 'invoice_id', |
8f3dc989 | 294 | 'payment_instrument_id', |
3086e282 | 295 | 'contribution_batch_id', |
afa0b07c | 296 | ); |
0b38e8f1 | 297 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams); |
1f0d8c92 | 298 | |
a2be2b19 PN |
299 | $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues); |
300 | if ($tags && !is_array($tags)) { | |
301 | unset($this->_formValues['contact_tags']); | |
302 | $this->_formValues['contact_tags'][$tags] = 1; | |
303 | } | |
ed106721 | 304 | |
a2be2b19 PN |
305 | if ($tags && is_array($tags)) { |
306 | unset($this->_formValues['contact_tags']); | |
22e263ad | 307 | foreach ($tags as $notImportant => $tagID) { |
a2be2b19 PN |
308 | $this->_formValues['contact_tags'][$tagID] = 1; |
309 | } | |
6a488035 | 310 | } |
ed106721 | 311 | |
ac7b09fc | 312 | $group = CRM_Utils_Array::value('group', $this->_formValues); |
313 | if ($group && !is_array($group)) { | |
314 | unset($this->_formValues['group']); | |
315 | $this->_formValues['group'][$group] = 1; | |
316 | } | |
ed106721 | 317 | |
ac7b09fc | 318 | if ($group && is_array($group)) { |
319 | unset($this->_formValues['group']); | |
320 | foreach ($group as $groupID) { | |
321 | $this->_formValues['group'][$groupID] = 1; | |
6a488035 TO |
322 | } |
323 | } | |
6a488035 TO |
324 | } |
325 | ||
c94d39fd | 326 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
6a488035 TO |
327 | |
328 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
329 | ||
330 | $this->set('formValues', $this->_formValues); | |
331 | $this->set('queryParams', $this->_queryParams); | |
332 | ||
333 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 334 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
335 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
336 | ||
337 | // hack, make sure we reset the task values | |
ed106721 | 338 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
339 | $formName = $stateMachine->getTaskFormName(); |
340 | $this->controller->resetPage($formName); | |
341 | return; | |
342 | } | |
343 | ||
6a488035 TO |
344 | $sortID = NULL; |
345 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
346 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
347 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
348 | ); | |
349 | } | |
350 | ||
351 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
352 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, | |
353 | $this->_action, | |
354 | NULL, | |
355 | $this->_single, | |
356 | $this->_limit, | |
357 | $this->_context | |
358 | ); | |
359 | $selector->setKey($this->controller->_key); | |
360 | ||
361 | $prefix = NULL; | |
362 | if ($this->_context == 'basic' || $this->_context == 'user') { | |
363 | $prefix = $this->_prefix; | |
364 | } | |
365 | ||
366 | $controller = new CRM_Core_Selector_Controller($selector, | |
367 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
368 | $sortID, | |
369 | CRM_Core_Action::VIEW, | |
370 | $this, | |
371 | CRM_Core_Selector_Controller::SESSION, | |
372 | $prefix | |
373 | ); | |
374 | $controller->setEmbedded(TRUE); | |
375 | ||
376 | $query = &$selector->getQuery(); | |
377 | if ($this->_context == 'user') { | |
378 | $query->setSkipPermission(TRUE); | |
379 | } | |
380 | $summary = &$query->summaryContribution($this->_context); | |
381 | $this->set('summary', $summary); | |
382 | $this->assign('contributionSummary', $summary); | |
383 | $controller->run(); | |
384 | } | |
385 | ||
c5a11b83 EM |
386 | /** |
387 | * Use values from $_GET if force is set to TRUE. | |
388 | * | |
389 | * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained. | |
390 | */ | |
00be9182 | 391 | public function fixFormValues() { |
6a488035 TO |
392 | if (!$this->_force) { |
393 | return; | |
394 | } | |
395 | ||
1273d77c | 396 | $status = CRM_Utils_Request::retrieve('status', 'String'); |
6a488035 TO |
397 | if ($status) { |
398 | $this->_formValues['contribution_status_id'] = array($status => 1); | |
399 | $this->_defaults['contribution_status_id'] = array($status => 1); | |
400 | } | |
401 | ||
29d72d92 A |
402 | $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this); |
403 | if ($pcpid) { | |
404 | // Add new pcpid to the tail of the array... | |
405 | foreach ($pcpid as $pcpIdList) { | |
406 | $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList; | |
407 | } | |
408 | // and avoid any duplicate | |
409 | $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']); | |
410 | } | |
411 | ||
6a488035 TO |
412 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
413 | ||
414 | if ($cid) { | |
415 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
416 | if ($cid > 0) { | |
417 | $this->_formValues['contact_id'] = $cid; | |
c5a11b83 | 418 | // @todo - why do we retrieve these when they are not used? |
6a488035 TO |
419 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); |
420 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, | |
421 | 'sort_name' | |
422 | ); | |
423 | // also assign individual mode to the template | |
424 | $this->_single = TRUE; | |
425 | } | |
426 | } | |
427 | ||
1273d77c | 428 | $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp'); |
6a488035 TO |
429 | if ($lowDate) { |
430 | $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp'); | |
431 | $date = CRM_Utils_Date::setDateDefaults($lowDate); | |
432 | $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0]; | |
433 | } | |
434 | ||
1273d77c | 435 | $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp'); |
6a488035 TO |
436 | if ($highDate) { |
437 | $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp'); | |
438 | $date = CRM_Utils_Date::setDateDefaults($highDate); | |
439 | $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0]; | |
440 | } | |
441 | ||
442 | if ($highDate || $lowDate) { | |
443 | //set the Choose Date Range value | |
444 | $this->_formValues['contribution_date_relative'] = 0; | |
445 | } | |
446 | ||
447 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', | |
448 | $this | |
449 | ); | |
450 | ||
1273d77c | 451 | $test = CRM_Utils_Request::retrieve('test', 'Boolean'); |
6a488035 TO |
452 | if (isset($test)) { |
453 | $test = CRM_Utils_Type::escape($test, 'Boolean'); | |
454 | $this->_formValues['contribution_test'] = $test; | |
455 | } | |
456 | //Recurring id | |
457 | $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE); | |
458 | if ($recur) { | |
459 | $this->_formValues['contribution_recur_id'] = $recur; | |
460 | $this->_formValues['contribution_recurring'] = 1; | |
461 | } | |
462 | ||
463 | //check for contribution page id. | |
464 | $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this); | |
465 | if ($contribPageId) { | |
466 | $this->_formValues['contribution_page_id'] = $contribPageId; | |
467 | } | |
468 | ||
469 | //give values to default. | |
470 | $this->_defaults = $this->_formValues; | |
471 | } | |
472 | ||
473 | /** | |
c5a11b83 | 474 | * Return a descriptive name for the page, used in wizard header. |
6a488035 TO |
475 | * |
476 | * @return string | |
6a488035 TO |
477 | */ |
478 | public function getTitle() { | |
479 | return ts('Find Contributions'); | |
480 | } | |
96025800 | 481 | |
6a488035 | 482 | } |