Merge pull request #15830 from eileenmcnaughton/dedupe4
[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 * Explicitly declare the entity api name.
52 */
53 public function getDefaultEntity() {
54 return 'Contribution';
55 }
56
57 /**
58 * Processing needed for buildForm and later.
59 *
60 * @throws \CiviCRM_API3_Exception
61 * @throws \CRM_Core_Exception
62 */
63 public function preProcess() {
64 $this->set('searchFormName', 'Search');
65
66 $this->_searchButtonName = $this->getButtonName('refresh');
67 $this->_actionButtonName = $this->getButtonName('next', 'action');
68
69 $this->_done = FALSE;
70
71 parent::preProcess();
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
83 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
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),
101 $this->getSortID(),
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
114 /**
115 * Set defaults.
116 *
117 * @return array
118 * @throws \Exception
119 */
120 public function setDefaultValues() {
121 $this->setDeprecatedDefaults();
122 $this->_defaults = parent::setDefaultValues();
123
124 $this->_defaults = array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults);
125
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.
128 $this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey(
129 'CRM_Contribute_BAO_Contribution',
130 'contribution_status_id',
131 'Completed'
132 );
133 }
134 return $this->_defaults;
135 }
136
137 /**
138 * Build the form object.
139 *
140 * @throws \CRM_Core_Exception
141 * @throws \CiviCRM_API3_Exception
142 */
143 public function buildQuickForm() {
144 if ($this->isFormInViewOrEditMode()) {
145 parent::buildQuickForm();
146 $this->addContactSearchFields();
147
148 CRM_Contribute_BAO_Query::buildSearchForm($this);
149 }
150
151 $rows = $this->get('rows');
152 if (is_array($rows)) {
153 if (!$this->_single) {
154 $this->addRowSelectors($rows);
155 }
156
157 $permission = CRM_Core_Permission::getPermission();
158
159 $queryParams = $this->get('queryParams');
160 $taskParams['softCreditFiltering'] = FALSE;
161 if (!empty($queryParams)) {
162 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
163 }
164 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams);
165 $this->addTaskMenu($tasks);
166 }
167
168 }
169
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
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
222 /**
223 * The post processing of the form gets done here.
224 *
225 * Key things done during post processing are
226 * - check for reset or next request. if present, skip post processing.
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.
229 * - if user has done a submit with new values the regular post submission is
230 * done.
231 * The processing consists of using a Selector / Controller framework for getting the
232 * search results.
233 */
234 public function postProcess() {
235 if ($this->_done) {
236 return;
237 }
238
239 $this->_done = TRUE;
240
241 $this->setFormValues();
242 // @todo - stop changing formValues - respect submitted form values, change a working array.
243 $this->fixFormValues();
244
245 // We don't show test records in summaries or dashboards
246 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') {
247 // @todo - stop changing formValues - respect submitted form values, change a working array.
248 $this->_formValues["contribution_test"] = 0;
249 }
250
251 foreach ([
252 'contribution_amount_low',
253 'contribution_amount_high',
254 ] as $f) {
255 if (isset($this->_formValues[$f])) {
256 // @todo - stop changing formValues - respect submitted form values, change a working array.
257 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
258 }
259 }
260
261 if (!empty($_POST)) {
262 $specialParams = [
263 'financial_type_id',
264 'contribution_soft_credit_type_id',
265 'contribution_status_id',
266 'contribution_trxn_id',
267 'contribution_page_id',
268 'contribution_product_id',
269 'invoice_id',
270 'payment_instrument_id',
271 'contribution_batch_id',
272 ];
273 // @todo - stop changing formValues - respect submitted form values, change a working array.
274 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
275
276 // @todo - stop changing formValues - respect submitted form values, change a working array.
277 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
278 if ($tags && !is_array($tags)) {
279 // @todo - stop changing formValues - respect submitted form values, change a working array.
280 unset($this->_formValues['contact_tags']);
281 $this->_formValues['contact_tags'][$tags] = 1;
282 }
283
284 if ($tags && is_array($tags)) {
285 unset($this->_formValues['contact_tags']);
286 foreach ($tags as $notImportant => $tagID) {
287 // @todo - stop changing formValues - respect submitted form values, change a working array.
288 $this->_formValues['contact_tags'][$tagID] = 1;
289 }
290 }
291
292 $group = CRM_Utils_Array::value('group', $this->_formValues);
293 if ($group && !is_array($group)) {
294 // @todo - stop changing formValues - respect submitted form values, change a working array.
295 unset($this->_formValues['group']);
296 $this->_formValues['group'][$group] = 1;
297 }
298
299 if ($group && is_array($group)) {
300 unset($this->_formValues['group']);
301 foreach ($group as $groupID) {
302 // @todo - stop changing formValues - respect submitted form values, change a working array.
303 $this->_formValues['group'][$groupID] = 1;
304 }
305 }
306 }
307
308 // @todo - stop changing formValues - respect submitted form values, change a working array.
309 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
310
311 // @todo - stop changing formValues - respect submitted form values, change a working array.
312 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
313
314 $this->set('queryParams', $this->_queryParams);
315
316 $buttonName = $this->controller->getButtonName();
317 if ($buttonName == $this->_actionButtonName) {
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
321 $stateMachine = $this->controller->getStateMachine();
322 $formName = $stateMachine->getTaskFormName();
323 $this->controller->resetPage($formName);
324 return;
325 }
326
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),
344 $this->getSortID(),
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 }
356
357 $controller->run();
358 }
359
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 */
365 public function fixFormValues() {
366 if (!$this->_force) {
367 return;
368 }
369
370 $status = CRM_Utils_Request::retrieve('status', 'String');
371 if ($status) {
372 $this->_formValues['contribution_status_id'] = [$status => 1];
373 $this->_defaults['contribution_status_id'] = [$status => 1];
374 }
375
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
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;
392 // @todo - why do we retrieve these when they are not used?
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
402 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
403 $this
404 );
405
406 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
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 }
423 }
424
425 /**
426 * Return a descriptive name for the page, used in wizard header.
427 *
428 * @return string
429 */
430 public function getTitle() {
431 return ts('Find Contributions');
432 }
433
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
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
462 }