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