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