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