Merge pull request #23311 from colemanw/unusedEvent
[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 // 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 }
266
267 foreach ([
268 'contribution_amount_low',
269 'contribution_amount_high',
270 ] as $f) {
271 if (isset($this->_formValues[$f])) {
272 // @todo - stop changing formValues - respect submitted form values, change a working array.
273 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
274 }
275 }
276
277 if (!empty($_POST)) {
278 $specialParams = [
279 'financial_type_id',
280 'contribution_soft_credit_type_id',
281 'contribution_status_id',
282 'contribution_trxn_id',
283 'contribution_product_id',
284 'invoice_id',
285 'payment_instrument_id',
286 'contribution_batch_id',
287 ];
288 // @todo - stop changing formValues - respect submitted form values, change a working array.
289 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
290
291 // @todo - stop changing formValues - respect submitted form values, change a working array.
292 $tags = $this->_formValues['contact_tags'] ?? NULL;
293 if ($tags && !is_array($tags)) {
294 // @todo - stop changing formValues - respect submitted form values, change a working array.
295 unset($this->_formValues['contact_tags']);
296 $this->_formValues['contact_tags'][$tags] = 1;
297 }
298
299 if ($tags && is_array($tags)) {
300 unset($this->_formValues['contact_tags']);
301 foreach ($tags as $notImportant => $tagID) {
302 // @todo - stop changing formValues - respect submitted form values, change a working array.
303 $this->_formValues['contact_tags'][$tagID] = 1;
304 }
305 }
306
307 $group = $this->_formValues['group'] ?? NULL;
308 if ($group && !is_array($group)) {
309 // @todo - stop changing formValues - respect submitted form values, change a working array.
310 unset($this->_formValues['group']);
311 $this->_formValues['group'][$group] = 1;
312 }
313
314 if ($group && is_array($group)) {
315 unset($this->_formValues['group']);
316 foreach ($group as $groupID) {
317 // @todo - stop changing formValues - respect submitted form values, change a working array.
318 $this->_formValues['group'][$groupID] = 1;
319 }
320 }
321 }
322
323 // @todo - stop changing formValues - respect submitted form values, change a working array.
324 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
325
326 $this->set('queryParams', $this->_queryParams);
327
328 $buttonName = $this->controller->getButtonName();
329 if ($buttonName == $this->_actionButtonName) {
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
333 $stateMachine = $this->controller->getStateMachine();
334 $formName = $stateMachine->getTaskFormName();
335 $this->controller->resetPage($formName);
336 return;
337 }
338
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;
350 if ($this->_context === 'basic' || $this->_context === 'user') {
351 $prefix = $this->_prefix;
352 }
353
354 $controller = new CRM_Core_Selector_Controller($selector,
355 $this->get(CRM_Utils_Pager::PAGE_ID),
356 $this->getSortID(),
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();
365 if ($this->_context === 'user') {
366 $query->setSkipPermission(TRUE);
367 }
368
369 $controller->run();
370 }
371
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.
376 *
377 * @throws \CRM_Core_Exception
378 */
379 public function fixFormValues() {
380 if (!$this->_force) {
381 return;
382 }
383
384 $status = CRM_Utils_Request::retrieve('status', 'String');
385 if ($status) {
386 $this->_formValues['contribution_status_id'] = [$status => 1];
387 $this->_defaults['contribution_status_id'] = [$status => 1];
388 }
389
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
400 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
401
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) {
407 $cid = CRM_Utils_Type::escape($cid, 'Integer');
408 if ($cid > 0) {
409 $this->_formValues['contact_id'] = $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
418 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
419 $this
420 );
421
422 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
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 }
439 }
440
441 /**
442 * Return a descriptive name for the page, used in wizard header.
443 *
444 * @return string
445 */
446 public function getTitle() {
447 return ts('Find Contributions');
448 }
449
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
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 //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 }
482 }
483
484 }