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