Merge pull request #14619 from mattwire/pseudoconstant_types
[civicrm-core.git] / CRM / Contribute / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 public function preProcess() {
77 $this->set('searchFormName', 'Search');
78
79 $this->_searchButtonName = $this->getButtonName('refresh');
80 $this->_actionButtonName = $this->getButtonName('next', 'action');
81
82 $this->_done = FALSE;
83
84 $this->loadStandardSearchOptionsFromUrl();
85
86 // get user submitted values
87 // get it from controller only if form has been submitted, else preProcess has set this
88 if (!empty($_POST)) {
89 $this->_formValues = $this->controller->exportValues($this->_name);
90 }
91 else {
92 $this->_formValues = $this->get('formValues');
93 }
94
95 //membership ID
96 $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
97 if (isset($memberShipId)) {
98 $this->_formValues['contribution_membership_id'] = $memberShipId;
99 }
100 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
101 if (isset($participantId)) {
102 $this->_formValues['contribution_participant_id'] = $participantId;
103 }
104
105 if ($this->_force) {
106 $this->postProcess();
107 $this->set('force', 0);
108 }
109
110 $sortID = NULL;
111 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
112 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
113 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
114 );
115 }
116
117 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
118 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
119 $this->_action,
120 NULL,
121 $this->_single,
122 $this->_limit,
123 $this->_context
124 );
125 $prefix = NULL;
126 if ($this->_context == 'user') {
127 $prefix = $this->_prefix;
128 }
129
130 $this->assign("{$prefix}limit", $this->_limit);
131 $this->assign("{$prefix}single", $this->_single);
132
133 $controller = new CRM_Core_Selector_Controller($selector,
134 $this->get(CRM_Utils_Pager::PAGE_ID),
135 $sortID,
136 CRM_Core_Action::VIEW,
137 $this,
138 CRM_Core_Selector_Controller::TRANSFER,
139 $prefix
140 );
141
142 $controller->setEmbedded(TRUE);
143 $controller->moveFromSessionToTemplate();
144
145 $this->assign('contributionSummary', $this->get('summary'));
146 }
147
148 /**
149 * Set defaults.
150 *
151 * @return array
152 * @throws \Exception
153 */
154 public function setDefaultValues() {
155 $this->_defaults = parent::setDefaultValues();
156 if (empty($this->_defaults['contribution_status'])) {
157 $this->_defaults['contribution_status'][1] = 1;
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 if (!empty($_POST) && !$this->_force) {
267 $this->_formValues = $this->controller->exportValues($this->_name);
268 }
269 $this->convertTextStringsToUseLikeOperator();
270 $this->fixFormValues();
271
272 // We don't show test records in summaries or dashboards
273 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') {
274 $this->_formValues["contribution_test"] = 0;
275 }
276
277 foreach ([
278 'contribution_amount_low',
279 'contribution_amount_high',
280 ] as $f) {
281 if (isset($this->_formValues[$f])) {
282 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
283 }
284 }
285
286 $config = CRM_Core_Config::singleton();
287 if (!empty($_POST)) {
288 $specialParams = [
289 'financial_type_id',
290 'contribution_soft_credit_type_id',
291 'contribution_status_id',
292 'contribution_trxn_id',
293 'contribution_page_id',
294 'contribution_product_id',
295 'invoice_id',
296 'payment_instrument_id',
297 'contribution_batch_id',
298 ];
299 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
300
301 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
302 if ($tags && !is_array($tags)) {
303 unset($this->_formValues['contact_tags']);
304 $this->_formValues['contact_tags'][$tags] = 1;
305 }
306
307 if ($tags && is_array($tags)) {
308 unset($this->_formValues['contact_tags']);
309 foreach ($tags as $notImportant => $tagID) {
310 $this->_formValues['contact_tags'][$tagID] = 1;
311 }
312 }
313
314 $group = CRM_Utils_Array::value('group', $this->_formValues);
315 if ($group && !is_array($group)) {
316 unset($this->_formValues['group']);
317 $this->_formValues['group'][$group] = 1;
318 }
319
320 if ($group && is_array($group)) {
321 unset($this->_formValues['group']);
322 foreach ($group as $groupID) {
323 $this->_formValues['group'][$groupID] = 1;
324 }
325 }
326 }
327
328 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
329
330 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
331
332 $this->set('formValues', $this->_formValues);
333 $this->set('queryParams', $this->_queryParams);
334
335 $buttonName = $this->controller->getButtonName();
336 if ($buttonName == $this->_actionButtonName) {
337 // check actionName and if next, then do not repeat a search, since we are going to the next page
338
339 // hack, make sure we reset the task values
340 $stateMachine = $this->controller->getStateMachine();
341 $formName = $stateMachine->getTaskFormName();
342 $this->controller->resetPage($formName);
343 return;
344 }
345
346 $sortID = NULL;
347 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
348 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
349 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
350 );
351 }
352
353 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
354 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
355 $this->_action,
356 NULL,
357 $this->_single,
358 $this->_limit,
359 $this->_context
360 );
361 $selector->setKey($this->controller->_key);
362
363 $prefix = NULL;
364 if ($this->_context == 'basic' || $this->_context == 'user') {
365 $prefix = $this->_prefix;
366 }
367
368 $controller = new CRM_Core_Selector_Controller($selector,
369 $this->get(CRM_Utils_Pager::PAGE_ID),
370 $sortID,
371 CRM_Core_Action::VIEW,
372 $this,
373 CRM_Core_Selector_Controller::SESSION,
374 $prefix
375 );
376 $controller->setEmbedded(TRUE);
377
378 $query = &$selector->getQuery();
379 if ($this->_context == 'user') {
380 $query->setSkipPermission(TRUE);
381 }
382
383 $controller->run();
384 }
385
386 /**
387 * Use values from $_GET if force is set to TRUE.
388 *
389 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
390 */
391 public function fixFormValues() {
392 if (!$this->_force) {
393 return;
394 }
395
396 $status = CRM_Utils_Request::retrieve('status', 'String');
397 if ($status) {
398 $this->_formValues['contribution_status_id'] = [$status => 1];
399 $this->_defaults['contribution_status_id'] = [$status => 1];
400 }
401
402 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
403 if ($pcpid) {
404 // Add new pcpid to the tail of the array...
405 foreach ($pcpid as $pcpIdList) {
406 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
407 }
408 // and avoid any duplicate
409 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
410 }
411
412 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
413
414 if ($cid) {
415 $cid = CRM_Utils_Type::escape($cid, 'Integer');
416 if ($cid > 0) {
417 $this->_formValues['contact_id'] = $cid;
418 // @todo - why do we retrieve these when they are not used?
419 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
420 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
421 'sort_name'
422 );
423 // also assign individual mode to the template
424 $this->_single = TRUE;
425 }
426 }
427
428 $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp');
429 if ($lowDate) {
430 $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp');
431 $date = CRM_Utils_Date::setDateDefaults($lowDate);
432 $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0];
433 }
434
435 $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp');
436 if ($highDate) {
437 $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp');
438 $date = CRM_Utils_Date::setDateDefaults($highDate);
439 $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0];
440 }
441
442 if ($highDate || $lowDate) {
443 //set the Choose Date Range value
444 $this->_formValues['contribution_date_relative'] = 0;
445 }
446
447 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
448 $this
449 );
450
451 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
452 if (isset($test)) {
453 $test = CRM_Utils_Type::escape($test, 'Boolean');
454 $this->_formValues['contribution_test'] = $test;
455 }
456 //Recurring id
457 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
458 if ($recur) {
459 $this->_formValues['contribution_recur_id'] = $recur;
460 $this->_formValues['contribution_recurring'] = 1;
461 }
462
463 //check for contribution page id.
464 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
465 if ($contribPageId) {
466 $this->_formValues['contribution_page_id'] = $contribPageId;
467 }
468
469 //give values to default.
470 $this->_defaults = $this->_formValues;
471 }
472
473 /**
474 * Return a descriptive name for the page, used in wizard header.
475 *
476 * @return string
477 */
478 public function getTitle() {
479 return ts('Find Contributions');
480 }
481
482 }