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