Merge pull request #23474 from eileenmcnaughton/import_dep
[civicrm-core.git] / CRM / Contribute / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
c5a11b83 19 * Advanced search, extends basic search.
6a488035 20 */
3efb5b86 21class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
6a488035 22
6a488035 23 /**
fe482240 24 * The params that are sent to the query.
6a488035
TO
25 *
26 * @var array
6a488035
TO
27 */
28 protected $_queryParams;
29
6a488035 30 /**
fe482240 31 * Are we restricting ourselves to a single contact.
6a488035 32 *
b67daa72 33 * @var bool
6a488035
TO
34 */
35 protected $_single = FALSE;
36
37 /**
fe482240 38 * Are we restricting ourselves to a single contact.
6a488035 39 *
b67daa72 40 * @var bool
6a488035
TO
41 */
42 protected $_limit = NULL;
43
6a488035 44 /**
fe482240 45 * Prefix for the controller.
1330f57a 46 * @var string
6a488035 47 */
7473db7f 48 protected $_prefix = 'contribute_';
6a488035 49
88aae6d4
A
50 /**
51 * @var bool
52 */
53 public $submitOnce = TRUE;
54
927898c5 55 /**
56 * Explicitly declare the entity api name.
57 */
58 public function getDefaultEntity() {
59 return 'Contribution';
60 }
61
6a488035 62 /**
fe482240 63 * Processing needed for buildForm and later.
a1c22f90 64 *
65 * @throws \CiviCRM_API3_Exception
66 * @throws \CRM_Core_Exception
95ea96be 67 */
acb1052e 68 public function preProcess() {
2d09a0c3 69 // SearchFormName is deprecated & to be removed - the replacement is for the task to
70 // call $this->form->getSearchFormValues()
71 // A couple of extensions use it.
6a488035
TO
72 $this->set('searchFormName', 'Search');
73
6a488035
TO
74 $this->_actionButtonName = $this->getButtonName('next', 'action');
75
76 $this->_done = FALSE;
6a488035 77
e9f51713 78 parent::preProcess();
6a488035 79
c94d39fd 80 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
6a488035
TO
81 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
82 $this->_action,
83 NULL,
84 $this->_single,
85 $this->_limit,
86 $this->_context
87 );
88 $prefix = NULL;
7473db7f 89 if ($this->_context === 'user') {
6a488035
TO
90 $prefix = $this->_prefix;
91 }
92
93 $this->assign("{$prefix}limit", $this->_limit);
94 $this->assign("{$prefix}single", $this->_single);
95
96 $controller = new CRM_Core_Selector_Controller($selector,
97 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 98 $this->getSortID(),
6a488035
TO
99 CRM_Core_Action::VIEW,
100 $this,
101 CRM_Core_Selector_Controller::TRANSFER,
102 $prefix
103 );
104
105 $controller->setEmbedded(TRUE);
106 $controller->moveFromSessionToTemplate();
107
108 $this->assign('contributionSummary', $this->get('summary'));
109 }
110
c5a11b83
EM
111 /**
112 * Set defaults.
113 *
114 * @return array
c5c17034 115 * @throws \Exception
c5a11b83 116 */
00be9182 117 public function setDefaultValues() {
77e533a5 118 $this->setDeprecatedDefaults();
c5c17034 119 $this->_defaults = parent::setDefaultValues();
1157f03f
SL
120
121 $this->_defaults = array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults);
122
a1c22f90 123 if (empty($this->_defaults['contribution_status_id']) && !$this->_force) {
124 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
8f8147e8
SL
125 $this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey(
126 'CRM_Contribute_BAO_Contribution',
127 'contribution_status_id',
128 'Completed'
129 );
6a488035 130 }
c419b32d 131
132 // The membership or contribution id could be set on the form if viewing
133 // an embedded block on ParticipantView or MembershipView.
0b541cd6
SL
134 $membershipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
135 if (isset($membershipId)) {
136 $this->_defaults['contribution_membership_id'] = $membershipId;
c419b32d 137 }
138 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
139 if (isset($participantId)) {
140 $this->_defaults['contribution_participant_id'] = $participantId;
141 }
142
6a488035
TO
143 return $this->_defaults;
144 }
145
146 /**
fe482240 147 * Build the form object.
7123f88c 148 *
149 * @throws \CRM_Core_Exception
150 * @throws \CiviCRM_API3_Exception
6a488035 151 */
00be9182 152 public function buildQuickForm() {
eb0010b8
JP
153 if ($this->isFormInViewOrEditMode()) {
154 parent::buildQuickForm();
155 $this->addContactSearchFields();
6a488035 156
eb0010b8
JP
157 CRM_Contribute_BAO_Query::buildSearchForm($this);
158 }
6a488035 159
6a488035
TO
160 $rows = $this->get('rows');
161 if (is_array($rows)) {
162 if (!$this->_single) {
8d36b801 163 $this->addRowSelectors($rows);
6a488035
TO
164 }
165
6a488035
TO
166 $permission = CRM_Core_Permission::getPermission();
167
c410490a 168 $queryParams = $this->get('queryParams');
1a72712d 169 $taskParams['softCreditFiltering'] = FALSE;
c410490a 170 if (!empty($queryParams)) {
1a72712d 171 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
c410490a 172 }
1a72712d 173 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams);
34197a55 174 $this->addTaskMenu($tasks);
6a488035
TO
175 }
176
6a488035
TO
177 }
178
e597fc33
DG
179 /**
180 * Get the label for the sortName field if email searching is on.
181 *
182 * (email searching is a setting under search preferences).
183 *
184 * @return string
185 */
186 protected function getSortNameLabelWithEmail() {
187 return ts('Contributor Name or Email');
188 }
189
190 /**
191 * Get the label for the sortName field if email searching is off.
192 *
193 * (email searching is a setting under search preferences).
194 *
195 * @return string
196 */
197 protected function getSortNameLabelWithOutEmail() {
198 return ts('Contributor Name');
199 }
200
0573fd28 201 /**
202 * Get the label for the tag field.
203 *
204 * We do this in a function so the 'ts' wraps the whole string to allow
205 * better translation.
206 *
207 * @return string
208 */
209 protected function getTagLabel() {
210 return ts('Contributor Tag(s)');
211 }
212
213 /**
214 * Get the label for the group field.
215 *
216 * @return string
217 */
218 protected function getGroupLabel() {
219 return ts('Contributor Group(s)');
220 }
221
222 /**
223 * Get the label for the group field.
224 *
225 * @return string
226 */
227 protected function getContactTypeLabel() {
228 return ts('Contributor Contact Type');
229 }
230
6a488035
TO
231 /**
232 * The post processing of the form gets done here.
233 *
234 * Key things done during post processing are
c5a11b83 235 * - check for reset or next request. if present, skip post processing.
6a488035
TO
236 * - now check if user requested running a saved search, if so, then
237 * the form values associated with the saved search are used for searching.
c5a11b83 238 * - if user has done a submit with new values the regular post submission is
6a488035
TO
239 * done.
240 * The processing consists of using a Selector / Controller framework for getting the
241 * search results.
7473db7f 242 *
243 * @throws \CRM_Core_Exception
6a488035 244 */
00be9182 245 public function postProcess() {
6a488035
TO
246 if ($this->_done) {
247 return;
248 }
249
250 $this->_done = TRUE;
251
359fdb6f 252 $this->setFormValues();
e9f51713 253 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
254 $this->fixFormValues();
255
d43b88cc 256 // We don't show test records in summaries or dashboards
7473db7f 257 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context === 'dashboard') {
e9f51713 258 // @todo - stop changing formValues - respect submitted form values, change a working array.
7473db7f 259 $this->_formValues['contribution_test'] = 0;
6a488035 260 }
f35ca929
JJ
261 // We don't show template records in summaries or dashboards
262 if (empty($this->_formValues['is_template']) && $this->_force && !empty($this->_context) && ($this->_context === 'dashboard' || $this->_context === 'contribution')) {
263 // @todo - stop changing formValues - respect submitted form values, change a working array.
264 $this->_formValues['is_template'] = 0;
265 }
6a488035 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',
6ffab5b7 283 'contribution_product_id',
b5a37491 284 'invoice_id',
8f3dc989 285 'payment_instrument_id',
3086e282 286 'contribution_batch_id',
be2fb01f 287 ];
e9f51713 288 // @todo - stop changing formValues - respect submitted form values, change a working array.
0b38e8f1 289 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
1f0d8c92 290
e9f51713 291 // @todo - stop changing formValues - respect submitted form values, change a working array.
9c1bc317 292 $tags = $this->_formValues['contact_tags'] ?? NULL;
a2be2b19 293 if ($tags && !is_array($tags)) {
e9f51713 294 // @todo - stop changing formValues - respect submitted form values, change a working array.
a2be2b19
PN
295 unset($this->_formValues['contact_tags']);
296 $this->_formValues['contact_tags'][$tags] = 1;
297 }
ed106721 298
a2be2b19
PN
299 if ($tags && is_array($tags)) {
300 unset($this->_formValues['contact_tags']);
22e263ad 301 foreach ($tags as $notImportant => $tagID) {
e9f51713 302 // @todo - stop changing formValues - respect submitted form values, change a working array.
a2be2b19
PN
303 $this->_formValues['contact_tags'][$tagID] = 1;
304 }
6a488035 305 }
ed106721 306
9c1bc317 307 $group = $this->_formValues['group'] ?? NULL;
ac7b09fc 308 if ($group && !is_array($group)) {
e9f51713 309 // @todo - stop changing formValues - respect submitted form values, change a working array.
ac7b09fc 310 unset($this->_formValues['group']);
311 $this->_formValues['group'][$group] = 1;
312 }
ed106721 313
ac7b09fc 314 if ($group && is_array($group)) {
315 unset($this->_formValues['group']);
316 foreach ($group as $groupID) {
e9f51713 317 // @todo - stop changing formValues - respect submitted form values, change a working array.
ac7b09fc 318 $this->_formValues['group'][$groupID] = 1;
6a488035
TO
319 }
320 }
6a488035
TO
321 }
322
e9f51713 323 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
324 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
325
6a488035
TO
326 $this->set('queryParams', $this->_queryParams);
327
328 $buttonName = $this->controller->getButtonName();
e341bbee 329 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
330 // check actionName and if next, then do not repeat a search, since we are going to the next page
331
332 // hack, make sure we reset the task values
ed106721 333 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
334 $formName = $stateMachine->getTaskFormName();
335 $this->controller->resetPage($formName);
336 return;
337 }
338
6a488035
TO
339 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
340 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
341 $this->_action,
342 NULL,
343 $this->_single,
344 $this->_limit,
345 $this->_context
346 );
347 $selector->setKey($this->controller->_key);
348
349 $prefix = NULL;
7473db7f 350 if ($this->_context === 'basic' || $this->_context === 'user') {
6a488035
TO
351 $prefix = $this->_prefix;
352 }
353
354 $controller = new CRM_Core_Selector_Controller($selector,
355 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 356 $this->getSortID(),
6a488035
TO
357 CRM_Core_Action::VIEW,
358 $this,
359 CRM_Core_Selector_Controller::SESSION,
360 $prefix
361 );
362 $controller->setEmbedded(TRUE);
363
364 $query = &$selector->getQuery();
7473db7f 365 if ($this->_context === 'user') {
6a488035
TO
366 $query->setSkipPermission(TRUE);
367 }
18f383f8 368
6a488035
TO
369 $controller->run();
370 }
371
c5a11b83
EM
372 /**
373 * Use values from $_GET if force is set to TRUE.
374 *
375 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
7473db7f 376 *
377 * @throws \CRM_Core_Exception
c5a11b83 378 */
00be9182 379 public function fixFormValues() {
6a488035
TO
380 if (!$this->_force) {
381 return;
382 }
383
1273d77c 384 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035 385 if ($status) {
be2fb01f
CW
386 $this->_formValues['contribution_status_id'] = [$status => 1];
387 $this->_defaults['contribution_status_id'] = [$status => 1];
6a488035
TO
388 }
389
29d72d92
A
390 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
391 if ($pcpid) {
392 // Add new pcpid to the tail of the array...
393 foreach ($pcpid as $pcpIdList) {
394 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
395 }
396 // and avoid any duplicate
397 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
398 }
399
6a488035
TO
400 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
401
e7afe635
SP
402 // skip cid (contact id of membership/participant record) to get associated payments for membership/participant record,
403 // contribution record may be on different contact id.
404 $skip_cid = CRM_Utils_Request::retrieve('skip_cid', 'Boolean', $this, FALSE, FALSE);
405
406 if ($cid && !$skip_cid) {
6a488035
TO
407 $cid = CRM_Utils_Type::escape($cid, 'Integer');
408 if ($cid > 0) {
409 $this->_formValues['contact_id'] = $cid;
6a488035
TO
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 }
cd8ae813 476 //check for contribution page id.
477 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
478 if ($contribPageId) {
479 CRM_Core_Error::deprecatedFunctionWarning('pass contribution_page_id');
480 $this->_formValues['contribution_page_id'] = $contribPageId;
481 }
77e533a5 482 }
483
6a488035 484}