Merge pull request #15793 from seamuslee001/depreacete_dao_temp_table_name
[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',
6ffab5b7 267 'contribution_product_id',
b5a37491 268 'invoice_id',
8f3dc989 269 'payment_instrument_id',
3086e282 270 'contribution_batch_id',
be2fb01f 271 ];
e9f51713 272 // @todo - stop changing formValues - respect submitted form values, change a working array.
0b38e8f1 273 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
1f0d8c92 274
e9f51713 275 // @todo - stop changing formValues - respect submitted form values, change a working array.
a2be2b19
PN
276 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
277 if ($tags && !is_array($tags)) {
e9f51713 278 // @todo - stop changing formValues - respect submitted form values, change a working array.
a2be2b19
PN
279 unset($this->_formValues['contact_tags']);
280 $this->_formValues['contact_tags'][$tags] = 1;
281 }
ed106721 282
a2be2b19
PN
283 if ($tags && is_array($tags)) {
284 unset($this->_formValues['contact_tags']);
22e263ad 285 foreach ($tags as $notImportant => $tagID) {
e9f51713 286 // @todo - stop changing formValues - respect submitted form values, change a working array.
a2be2b19
PN
287 $this->_formValues['contact_tags'][$tagID] = 1;
288 }
6a488035 289 }
ed106721 290
ac7b09fc 291 $group = CRM_Utils_Array::value('group', $this->_formValues);
292 if ($group && !is_array($group)) {
e9f51713 293 // @todo - stop changing formValues - respect submitted form values, change a working array.
ac7b09fc 294 unset($this->_formValues['group']);
295 $this->_formValues['group'][$group] = 1;
296 }
ed106721 297
ac7b09fc 298 if ($group && is_array($group)) {
299 unset($this->_formValues['group']);
300 foreach ($group as $groupID) {
e9f51713 301 // @todo - stop changing formValues - respect submitted form values, change a working array.
ac7b09fc 302 $this->_formValues['group'][$groupID] = 1;
6a488035
TO
303 }
304 }
6a488035
TO
305 }
306
e9f51713 307 // @todo - stop changing formValues - respect submitted form values, change a working array.
c94d39fd 308 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035 309
e9f51713 310 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
311 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
312
6a488035
TO
313 $this->set('queryParams', $this->_queryParams);
314
315 $buttonName = $this->controller->getButtonName();
e341bbee 316 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
317 // check actionName and if next, then do not repeat a search, since we are going to the next page
318
319 // hack, make sure we reset the task values
ed106721 320 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
321 $formName = $stateMachine->getTaskFormName();
322 $this->controller->resetPage($formName);
323 return;
324 }
325
6a488035
TO
326 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
327 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
328 $this->_action,
329 NULL,
330 $this->_single,
331 $this->_limit,
332 $this->_context
333 );
334 $selector->setKey($this->controller->_key);
335
336 $prefix = NULL;
337 if ($this->_context == 'basic' || $this->_context == 'user') {
338 $prefix = $this->_prefix;
339 }
340
341 $controller = new CRM_Core_Selector_Controller($selector,
342 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 343 $this->getSortID(),
6a488035
TO
344 CRM_Core_Action::VIEW,
345 $this,
346 CRM_Core_Selector_Controller::SESSION,
347 $prefix
348 );
349 $controller->setEmbedded(TRUE);
350
351 $query = &$selector->getQuery();
352 if ($this->_context == 'user') {
353 $query->setSkipPermission(TRUE);
354 }
18f383f8 355
6a488035
TO
356 $controller->run();
357 }
358
c5a11b83
EM
359 /**
360 * Use values from $_GET if force is set to TRUE.
361 *
362 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
363 */
00be9182 364 public function fixFormValues() {
6a488035
TO
365 if (!$this->_force) {
366 return;
367 }
368
1273d77c 369 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035 370 if ($status) {
be2fb01f
CW
371 $this->_formValues['contribution_status_id'] = [$status => 1];
372 $this->_defaults['contribution_status_id'] = [$status => 1];
6a488035
TO
373 }
374
29d72d92
A
375 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
376 if ($pcpid) {
377 // Add new pcpid to the tail of the array...
378 foreach ($pcpid as $pcpIdList) {
379 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
380 }
381 // and avoid any duplicate
382 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
383 }
384
6a488035
TO
385 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
386
387 if ($cid) {
388 $cid = CRM_Utils_Type::escape($cid, 'Integer');
389 if ($cid > 0) {
390 $this->_formValues['contact_id'] = $cid;
c5a11b83 391 // @todo - why do we retrieve these when they are not used?
6a488035
TO
392 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
393 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
394 'sort_name'
395 );
396 // also assign individual mode to the template
397 $this->_single = TRUE;
398 }
399 }
400
6a488035
TO
401 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
402 $this
403 );
404
1273d77c 405 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
6a488035
TO
406 if (isset($test)) {
407 $test = CRM_Utils_Type::escape($test, 'Boolean');
408 $this->_formValues['contribution_test'] = $test;
409 }
410 //Recurring id
411 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
412 if ($recur) {
413 $this->_formValues['contribution_recur_id'] = $recur;
414 $this->_formValues['contribution_recurring'] = 1;
415 }
416
417 //check for contribution page id.
418 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
419 if ($contribPageId) {
420 $this->_formValues['contribution_page_id'] = $contribPageId;
421 }
6a488035
TO
422 }
423
424 /**
c5a11b83 425 * Return a descriptive name for the page, used in wizard header.
6a488035
TO
426 *
427 * @return string
6a488035
TO
428 */
429 public function getTitle() {
430 return ts('Find Contributions');
431 }
96025800 432
3a27e13e 433 /**
434 * Set the metadata for the form.
435 *
436 * @throws \CiviCRM_API3_Exception
437 */
438 protected function setSearchMetadata() {
439 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query::getSearchFieldMetadata()]);
440 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur::getContributionRecurSearchFieldMetadata()]);
441 }
442
77e533a5 443 /**
444 * Handling for url params that are deprecated.
445 *
446 * @throws \CRM_Core_Exception
447 */
448 protected function setDeprecatedDefaults() {
449 $lowReceiveDate = CRM_Utils_Request::retrieve('start', 'Timestamp');
450 if (!empty($lowReceiveDate)) {
451 $this->_formValues['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
452 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_low not start');
453 }
454 $highReceiveDate = CRM_Utils_Request::retrieve('end', 'Timestamp');
455 if (!empty($highReceiveDate)) {
456 $this->_formValues['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
457 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_high not end');
458 }
cd8ae813 459 //check for contribution page id.
460 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
461 if ($contribPageId) {
462 CRM_Core_Error::deprecatedFunctionWarning('pass contribution_page_id');
463 $this->_formValues['contribution_page_id'] = $contribPageId;
464 }
77e533a5 465 }
466
6a488035 467}