Merge pull request #19695 from civicrm/5.35
[civicrm-core.git] / CRM / Contribute / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Advanced search, extends basic search.
20 */
21 class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
22
23 /**
24 * The params that are sent to the query.
25 *
26 * @var array
27 */
28 protected $_queryParams;
29
30 /**
31 * Are we restricting ourselves to a single contact.
32 *
33 * @var bool
34 */
35 protected $_single = FALSE;
36
37 /**
38 * Are we restricting ourselves to a single contact.
39 *
40 * @var bool
41 */
42 protected $_limit = NULL;
43
44 /**
45 * Prefix for the controller.
46 * @var string
47 */
48 protected $_prefix = 'contribute_';
49
50 /**
51 * @var bool
52 */
53 public $submitOnce = TRUE;
54
55 /**
56 * Explicitly declare the entity api name.
57 */
58 public function getDefaultEntity() {
59 return 'Contribution';
60 }
61
62 /**
63 * Processing needed for buildForm and later.
64 *
65 * @throws \CiviCRM_API3_Exception
66 * @throws \CRM_Core_Exception
67 */
68 public function preProcess() {
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.
72 $this->set('searchFormName', 'Search');
73
74 $this->_actionButtonName = $this->getButtonName('next', 'action');
75
76 $this->_done = FALSE;
77
78 parent::preProcess();
79
80 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
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;
89 if ($this->_context === 'user') {
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),
98 $this->getSortID(),
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
111 /**
112 * Set defaults.
113 *
114 * @return array
115 * @throws \Exception
116 */
117 public function setDefaultValues() {
118 $this->setDeprecatedDefaults();
119 $this->_defaults = parent::setDefaultValues();
120
121 $this->_defaults = array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults);
122
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.
125 $this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey(
126 'CRM_Contribute_BAO_Contribution',
127 'contribution_status_id',
128 'Completed'
129 );
130 }
131
132 // The membership or contribution id could be set on the form if viewing
133 // an embedded block on ParticipantView or MembershipView.
134 $membershipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
135 if (isset($membershipId)) {
136 $this->_defaults['contribution_membership_id'] = $membershipId;
137 }
138 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
139 if (isset($participantId)) {
140 $this->_defaults['contribution_participant_id'] = $participantId;
141 }
142
143 return $this->_defaults;
144 }
145
146 /**
147 * Build the form object.
148 *
149 * @throws \CRM_Core_Exception
150 * @throws \CiviCRM_API3_Exception
151 */
152 public function buildQuickForm() {
153 if ($this->isFormInViewOrEditMode()) {
154 parent::buildQuickForm();
155 $this->addContactSearchFields();
156
157 CRM_Contribute_BAO_Query::buildSearchForm($this);
158 }
159
160 $rows = $this->get('rows');
161 if (is_array($rows)) {
162 if (!$this->_single) {
163 $this->addRowSelectors($rows);
164 }
165
166 $permission = CRM_Core_Permission::getPermission();
167
168 $queryParams = $this->get('queryParams');
169 $taskParams['softCreditFiltering'] = FALSE;
170 if (!empty($queryParams)) {
171 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
172 }
173 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams);
174 $this->addTaskMenu($tasks);
175 }
176
177 }
178
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
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
231 /**
232 * The post processing of the form gets done here.
233 *
234 * Key things done during post processing are
235 * - check for reset or next request. if present, skip post processing.
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.
238 * - if user has done a submit with new values the regular post submission is
239 * done.
240 * The processing consists of using a Selector / Controller framework for getting the
241 * search results.
242 *
243 * @throws \CRM_Core_Exception
244 */
245 public function postProcess() {
246 if ($this->_done) {
247 return;
248 }
249
250 $this->_done = TRUE;
251
252 $this->setFormValues();
253 // @todo - stop changing formValues - respect submitted form values, change a working array.
254 $this->fixFormValues();
255
256 // We don't show test records in summaries or dashboards
257 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context === 'dashboard') {
258 // @todo - stop changing formValues - respect submitted form values, change a working array.
259 $this->_formValues['contribution_test'] = 0;
260 }
261
262 foreach ([
263 'contribution_amount_low',
264 'contribution_amount_high',
265 ] as $f) {
266 if (isset($this->_formValues[$f])) {
267 // @todo - stop changing formValues - respect submitted form values, change a working array.
268 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
269 }
270 }
271
272 if (!empty($_POST)) {
273 $specialParams = [
274 'financial_type_id',
275 'contribution_soft_credit_type_id',
276 'contribution_status_id',
277 'contribution_trxn_id',
278 'contribution_product_id',
279 'invoice_id',
280 'payment_instrument_id',
281 'contribution_batch_id',
282 ];
283 // @todo - stop changing formValues - respect submitted form values, change a working array.
284 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
285
286 // @todo - stop changing formValues - respect submitted form values, change a working array.
287 $tags = $this->_formValues['contact_tags'] ?? NULL;
288 if ($tags && !is_array($tags)) {
289 // @todo - stop changing formValues - respect submitted form values, change a working array.
290 unset($this->_formValues['contact_tags']);
291 $this->_formValues['contact_tags'][$tags] = 1;
292 }
293
294 if ($tags && is_array($tags)) {
295 unset($this->_formValues['contact_tags']);
296 foreach ($tags as $notImportant => $tagID) {
297 // @todo - stop changing formValues - respect submitted form values, change a working array.
298 $this->_formValues['contact_tags'][$tagID] = 1;
299 }
300 }
301
302 $group = $this->_formValues['group'] ?? NULL;
303 if ($group && !is_array($group)) {
304 // @todo - stop changing formValues - respect submitted form values, change a working array.
305 unset($this->_formValues['group']);
306 $this->_formValues['group'][$group] = 1;
307 }
308
309 if ($group && is_array($group)) {
310 unset($this->_formValues['group']);
311 foreach ($group as $groupID) {
312 // @todo - stop changing formValues - respect submitted form values, change a working array.
313 $this->_formValues['group'][$groupID] = 1;
314 }
315 }
316 }
317
318 // @todo - stop changing formValues - respect submitted form values, change a working array.
319 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
320
321 $this->set('queryParams', $this->_queryParams);
322
323 $buttonName = $this->controller->getButtonName();
324 if ($buttonName == $this->_actionButtonName) {
325 // check actionName and if next, then do not repeat a search, since we are going to the next page
326
327 // hack, make sure we reset the task values
328 $stateMachine = $this->controller->getStateMachine();
329 $formName = $stateMachine->getTaskFormName();
330 $this->controller->resetPage($formName);
331 return;
332 }
333
334 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
335 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
336 $this->_action,
337 NULL,
338 $this->_single,
339 $this->_limit,
340 $this->_context
341 );
342 $selector->setKey($this->controller->_key);
343
344 $prefix = NULL;
345 if ($this->_context === 'basic' || $this->_context === 'user') {
346 $prefix = $this->_prefix;
347 }
348
349 $controller = new CRM_Core_Selector_Controller($selector,
350 $this->get(CRM_Utils_Pager::PAGE_ID),
351 $this->getSortID(),
352 CRM_Core_Action::VIEW,
353 $this,
354 CRM_Core_Selector_Controller::SESSION,
355 $prefix
356 );
357 $controller->setEmbedded(TRUE);
358
359 $query = &$selector->getQuery();
360 if ($this->_context === 'user') {
361 $query->setSkipPermission(TRUE);
362 }
363
364 $controller->run();
365 }
366
367 /**
368 * Use values from $_GET if force is set to TRUE.
369 *
370 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
371 *
372 * @throws \CRM_Core_Exception
373 */
374 public function fixFormValues() {
375 if (!$this->_force) {
376 return;
377 }
378
379 $status = CRM_Utils_Request::retrieve('status', 'String');
380 if ($status) {
381 $this->_formValues['contribution_status_id'] = [$status => 1];
382 $this->_defaults['contribution_status_id'] = [$status => 1];
383 }
384
385 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
386 if ($pcpid) {
387 // Add new pcpid to the tail of the array...
388 foreach ($pcpid as $pcpIdList) {
389 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
390 }
391 // and avoid any duplicate
392 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
393 }
394
395 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
396
397 // skip cid (contact id of membership/participant record) to get associated payments for membership/participant record,
398 // contribution record may be on different contact id.
399 $skip_cid = CRM_Utils_Request::retrieve('skip_cid', 'Boolean', $this, FALSE, FALSE);
400
401 if ($cid && !$skip_cid) {
402 $cid = CRM_Utils_Type::escape($cid, 'Integer');
403 if ($cid > 0) {
404 $this->_formValues['contact_id'] = $cid;
405 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
406 'sort_name'
407 );
408 // also assign individual mode to the template
409 $this->_single = TRUE;
410 }
411 }
412
413 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
414 $this
415 );
416
417 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
418 if (isset($test)) {
419 $test = CRM_Utils_Type::escape($test, 'Boolean');
420 $this->_formValues['contribution_test'] = $test;
421 }
422 //Recurring id
423 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
424 if ($recur) {
425 $this->_formValues['contribution_recur_id'] = $recur;
426 $this->_formValues['contribution_recurring'] = 1;
427 }
428
429 //check for contribution page id.
430 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
431 if ($contribPageId) {
432 $this->_formValues['contribution_page_id'] = $contribPageId;
433 }
434 }
435
436 /**
437 * Return a descriptive name for the page, used in wizard header.
438 *
439 * @return string
440 */
441 public function getTitle() {
442 return ts('Find Contributions');
443 }
444
445 /**
446 * Set the metadata for the form.
447 *
448 * @throws \CiviCRM_API3_Exception
449 */
450 protected function setSearchMetadata() {
451 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query::getSearchFieldMetadata()]);
452 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur::getContributionRecurSearchFieldMetadata()]);
453 }
454
455 /**
456 * Handling for url params that are deprecated.
457 *
458 * @throws \CRM_Core_Exception
459 */
460 protected function setDeprecatedDefaults() {
461 $lowReceiveDate = CRM_Utils_Request::retrieve('start', 'Timestamp');
462 if (!empty($lowReceiveDate)) {
463 $this->_formValues['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
464 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_low not start');
465 }
466 $highReceiveDate = CRM_Utils_Request::retrieve('end', 'Timestamp');
467 if (!empty($highReceiveDate)) {
468 $this->_formValues['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
469 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_high not end');
470 }
471 //check for contribution page id.
472 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
473 if ($contribPageId) {
474 CRM_Core_Error::deprecatedFunctionWarning('pass contribution_page_id');
475 $this->_formValues['contribution_page_id'] = $contribPageId;
476 }
477 }
478
479 }