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