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