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