Merge pull request #17949 from eileenmcnaughton/export_im
[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->_actionButtonName = $this->getButtonName('next', 'action');
67
68 $this->_done = FALSE;
69
70 parent::preProcess();
71
72 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
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;
81 if ($this->_context === 'user') {
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),
90 $this->getSortID(),
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
103 /**
104 * Set defaults.
105 *
106 * @return array
107 * @throws \Exception
108 */
109 public function setDefaultValues() {
110 $this->setDeprecatedDefaults();
111 $this->_defaults = parent::setDefaultValues();
112
113 $this->_defaults = array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults);
114
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.
117 $this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey(
118 'CRM_Contribute_BAO_Contribution',
119 'contribution_status_id',
120 'Completed'
121 );
122 }
123
124 // The membership or contribution id could be set on the form if viewing
125 // an embedded block on ParticipantView or MembershipView.
126 $membershipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
127 if (isset($membershipId)) {
128 $this->_defaults['contribution_membership_id'] = $membershipId;
129 }
130 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
131 if (isset($participantId)) {
132 $this->_defaults['contribution_participant_id'] = $participantId;
133 }
134
135 return $this->_defaults;
136 }
137
138 /**
139 * Build the form object.
140 *
141 * @throws \CRM_Core_Exception
142 * @throws \CiviCRM_API3_Exception
143 */
144 public function buildQuickForm() {
145 if ($this->isFormInViewOrEditMode()) {
146 parent::buildQuickForm();
147 $this->addContactSearchFields();
148
149 CRM_Contribute_BAO_Query::buildSearchForm($this);
150 }
151
152 $rows = $this->get('rows');
153 if (is_array($rows)) {
154 if (!$this->_single) {
155 $this->addRowSelectors($rows);
156 }
157
158 $permission = CRM_Core_Permission::getPermission();
159
160 $queryParams = $this->get('queryParams');
161 $taskParams['softCreditFiltering'] = FALSE;
162 if (!empty($queryParams)) {
163 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
164 }
165 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams);
166 $this->addTaskMenu($tasks);
167 }
168
169 }
170
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
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
223 /**
224 * The post processing of the form gets done here.
225 *
226 * Key things done during post processing are
227 * - check for reset or next request. if present, skip post processing.
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.
230 * - if user has done a submit with new values the regular post submission is
231 * done.
232 * The processing consists of using a Selector / Controller framework for getting the
233 * search results.
234 *
235 * @throws \CRM_Core_Exception
236 */
237 public function postProcess() {
238 if ($this->_done) {
239 return;
240 }
241
242 $this->_done = TRUE;
243
244 $this->setFormValues();
245 // @todo - stop changing formValues - respect submitted form values, change a working array.
246 $this->fixFormValues();
247
248 // We don't show test records in summaries or dashboards
249 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context === 'dashboard') {
250 // @todo - stop changing formValues - respect submitted form values, change a working array.
251 $this->_formValues['contribution_test'] = 0;
252 }
253
254 foreach ([
255 'contribution_amount_low',
256 'contribution_amount_high',
257 ] as $f) {
258 if (isset($this->_formValues[$f])) {
259 // @todo - stop changing formValues - respect submitted form values, change a working array.
260 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
261 }
262 }
263
264 if (!empty($_POST)) {
265 $specialParams = [
266 'financial_type_id',
267 'contribution_soft_credit_type_id',
268 'contribution_status_id',
269 'contribution_trxn_id',
270 'contribution_product_id',
271 'invoice_id',
272 'payment_instrument_id',
273 'contribution_batch_id',
274 ];
275 // @todo - stop changing formValues - respect submitted form values, change a working array.
276 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
277
278 // @todo - stop changing formValues - respect submitted form values, change a working array.
279 $tags = $this->_formValues['contact_tags'] ?? NULL;
280 if ($tags && !is_array($tags)) {
281 // @todo - stop changing formValues - respect submitted form values, change a working array.
282 unset($this->_formValues['contact_tags']);
283 $this->_formValues['contact_tags'][$tags] = 1;
284 }
285
286 if ($tags && is_array($tags)) {
287 unset($this->_formValues['contact_tags']);
288 foreach ($tags as $notImportant => $tagID) {
289 // @todo - stop changing formValues - respect submitted form values, change a working array.
290 $this->_formValues['contact_tags'][$tagID] = 1;
291 }
292 }
293
294 $group = $this->_formValues['group'] ?? NULL;
295 if ($group && !is_array($group)) {
296 // @todo - stop changing formValues - respect submitted form values, change a working array.
297 unset($this->_formValues['group']);
298 $this->_formValues['group'][$group] = 1;
299 }
300
301 if ($group && is_array($group)) {
302 unset($this->_formValues['group']);
303 foreach ($group as $groupID) {
304 // @todo - stop changing formValues - respect submitted form values, change a working array.
305 $this->_formValues['group'][$groupID] = 1;
306 }
307 }
308 }
309
310 // @todo - stop changing formValues - respect submitted form values, change a working array.
311 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
312
313 $this->set('queryParams', $this->_queryParams);
314
315 $buttonName = $this->controller->getButtonName();
316 if ($buttonName == $this->_actionButtonName) {
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
320 $stateMachine = $this->controller->getStateMachine();
321 $formName = $stateMachine->getTaskFormName();
322 $this->controller->resetPage($formName);
323 return;
324 }
325
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),
343 $this->getSortID(),
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 }
355
356 $controller->run();
357 }
358
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 *
364 * @throws \CRM_Core_Exception
365 */
366 public function fixFormValues() {
367 if (!$this->_force) {
368 return;
369 }
370
371 $status = CRM_Utils_Request::retrieve('status', 'String');
372 if ($status) {
373 $this->_formValues['contribution_status_id'] = [$status => 1];
374 $this->_defaults['contribution_status_id'] = [$status => 1];
375 }
376
377 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
378 if ($pcpid) {
379 // Add new pcpid to the tail of the array...
380 foreach ($pcpid as $pcpIdList) {
381 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
382 }
383 // and avoid any duplicate
384 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
385 }
386
387 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
388
389 if ($cid) {
390 $cid = CRM_Utils_Type::escape($cid, 'Integer');
391 if ($cid > 0) {
392 $this->_formValues['contact_id'] = $cid;
393 // @todo - why do we retrieve these when they are not used?
394 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
395 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
396 'sort_name'
397 );
398 // also assign individual mode to the template
399 $this->_single = TRUE;
400 }
401 }
402
403 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
404 $this
405 );
406
407 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
408 if (isset($test)) {
409 $test = CRM_Utils_Type::escape($test, 'Boolean');
410 $this->_formValues['contribution_test'] = $test;
411 }
412 //Recurring id
413 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
414 if ($recur) {
415 $this->_formValues['contribution_recur_id'] = $recur;
416 $this->_formValues['contribution_recurring'] = 1;
417 }
418
419 //check for contribution page id.
420 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
421 if ($contribPageId) {
422 $this->_formValues['contribution_page_id'] = $contribPageId;
423 }
424 }
425
426 /**
427 * Return a descriptive name for the page, used in wizard header.
428 *
429 * @return string
430 */
431 public function getTitle() {
432 return ts('Find Contributions');
433 }
434
435 /**
436 * Set the metadata for the form.
437 *
438 * @throws \CiviCRM_API3_Exception
439 */
440 protected function setSearchMetadata() {
441 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query::getSearchFieldMetadata()]);
442 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur::getContributionRecurSearchFieldMetadata()]);
443 }
444
445 /**
446 * Handling for url params that are deprecated.
447 *
448 * @throws \CRM_Core_Exception
449 */
450 protected function setDeprecatedDefaults() {
451 $lowReceiveDate = CRM_Utils_Request::retrieve('start', 'Timestamp');
452 if (!empty($lowReceiveDate)) {
453 $this->_formValues['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
454 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_low not start');
455 }
456 $highReceiveDate = CRM_Utils_Request::retrieve('end', 'Timestamp');
457 if (!empty($highReceiveDate)) {
458 $this->_formValues['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
459 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_high not end');
460 }
461 //check for contribution page id.
462 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
463 if ($contribPageId) {
464 CRM_Core_Error::deprecatedFunctionWarning('pass contribution_page_id');
465 $this->_formValues['contribution_page_id'] = $contribPageId;
466 }
467 }
468
469 }