Merge pull request #14503 from pradpnayak/ReportFix
[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 * @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 $this->loadStandardSearchOptionsFromUrl();
88
89 // get user submitted values
90 // get it from controller only if form has been submitted, else preProcess has set this
91 if (!empty($_POST)) {
92 $this->_formValues = $this->controller->exportValues($this->_name);
93 }
94 else {
95 $this->_formValues = $this->get('formValues');
96 }
97
98 //membership ID
99 $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
100 if (isset($memberShipId)) {
101 $this->_formValues['contribution_membership_id'] = $memberShipId;
102 }
103 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
104 if (isset($participantId)) {
105 $this->_formValues['contribution_participant_id'] = $participantId;
106 }
107
108 if ($this->_force) {
109 // Search field metadata is normally added in buildForm but we are bypassing that in this flow
110 // (I've always found the flow kinda confusing & perhaps that is the problem but this mitigates)
111 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query::getSearchFieldMetadata()]);
112 $this->postProcess();
113 $this->set('force', 0);
114 }
115
116 $sortID = NULL;
117 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
118 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
119 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
120 );
121 }
122
123 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
124 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
125 $this->_action,
126 NULL,
127 $this->_single,
128 $this->_limit,
129 $this->_context
130 );
131 $prefix = NULL;
132 if ($this->_context == 'user') {
133 $prefix = $this->_prefix;
134 }
135
136 $this->assign("{$prefix}limit", $this->_limit);
137 $this->assign("{$prefix}single", $this->_single);
138
139 $controller = new CRM_Core_Selector_Controller($selector,
140 $this->get(CRM_Utils_Pager::PAGE_ID),
141 $sortID,
142 CRM_Core_Action::VIEW,
143 $this,
144 CRM_Core_Selector_Controller::TRANSFER,
145 $prefix
146 );
147
148 $controller->setEmbedded(TRUE);
149 $controller->moveFromSessionToTemplate();
150
151 $this->assign('contributionSummary', $this->get('summary'));
152 }
153
154 /**
155 * Set defaults.
156 *
157 * @return array
158 * @throws \Exception
159 */
160 public function setDefaultValues() {
161 $lowReceiveDate = CRM_Utils_Request::retrieve('start', 'Timestamp');
162 if (!empty($lowReceiveDate)) {
163 $this->_formValues['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
164 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_low not start');
165 }
166 $highReceiveDate = CRM_Utils_Request::retrieve('end', 'Timestamp');
167 if (!empty($highReceiveDate)) {
168 $this->_formValues['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
169 CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_high not end');
170 }
171 $this->_defaults = parent::setDefaultValues();
172 if (empty($this->_defaults['contribution_status_id']) && !$this->_force) {
173 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
174 $this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey(
175 'CRM_Contribute_BAO_Contribution',
176 'contribution_status_id',
177 'Completed'
178 );
179 }
180 return $this->_defaults;
181 }
182
183 /**
184 * Build the form object.
185 *
186 * @throws \CRM_Core_Exception
187 * @throws \CiviCRM_API3_Exception
188 */
189 public function buildQuickForm() {
190 if ($this->isFormInViewOrEditMode()) {
191 parent::buildQuickForm();
192 $this->addContactSearchFields();
193
194 CRM_Contribute_BAO_Query::buildSearchForm($this);
195 }
196
197 $rows = $this->get('rows');
198 if (is_array($rows)) {
199 if (!$this->_single) {
200 $this->addRowSelectors($rows);
201 }
202
203 $permission = CRM_Core_Permission::getPermission();
204
205 $queryParams = $this->get('queryParams');
206 $taskParams['softCreditFiltering'] = FALSE;
207 if (!empty($queryParams)) {
208 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
209 }
210 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams);
211 $this->addTaskMenu($tasks);
212 }
213
214 }
215
216 /**
217 * Get the label for the sortName field if email searching is on.
218 *
219 * (email searching is a setting under search preferences).
220 *
221 * @return string
222 */
223 protected function getSortNameLabelWithEmail() {
224 return ts('Contributor Name or Email');
225 }
226
227 /**
228 * Get the label for the sortName field if email searching is off.
229 *
230 * (email searching is a setting under search preferences).
231 *
232 * @return string
233 */
234 protected function getSortNameLabelWithOutEmail() {
235 return ts('Contributor Name');
236 }
237
238 /**
239 * Get the label for the tag field.
240 *
241 * We do this in a function so the 'ts' wraps the whole string to allow
242 * better translation.
243 *
244 * @return string
245 */
246 protected function getTagLabel() {
247 return ts('Contributor Tag(s)');
248 }
249
250 /**
251 * Get the label for the group field.
252 *
253 * @return string
254 */
255 protected function getGroupLabel() {
256 return ts('Contributor Group(s)');
257 }
258
259 /**
260 * Get the label for the group field.
261 *
262 * @return string
263 */
264 protected function getContactTypeLabel() {
265 return ts('Contributor Contact Type');
266 }
267
268 /**
269 * The post processing of the form gets done here.
270 *
271 * Key things done during post processing are
272 * - check for reset or next request. if present, skip post processing.
273 * - now check if user requested running a saved search, if so, then
274 * the form values associated with the saved search are used for searching.
275 * - if user has done a submit with new values the regular post submission is
276 * done.
277 * The processing consists of using a Selector / Controller framework for getting the
278 * search results.
279 */
280 public function postProcess() {
281 if ($this->_done) {
282 return;
283 }
284
285 $this->_done = TRUE;
286
287 if (!empty($_POST) && !$this->_force) {
288 $this->_formValues = $this->controller->exportValues($this->_name);
289 }
290 $this->convertTextStringsToUseLikeOperator();
291 $this->fixFormValues();
292
293 // We don't show test records in summaries or dashboards
294 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') {
295 $this->_formValues["contribution_test"] = 0;
296 }
297
298 foreach ([
299 'contribution_amount_low',
300 'contribution_amount_high',
301 ] as $f) {
302 if (isset($this->_formValues[$f])) {
303 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
304 }
305 }
306
307 $config = CRM_Core_Config::singleton();
308 if (!empty($_POST)) {
309 $specialParams = [
310 'financial_type_id',
311 'contribution_soft_credit_type_id',
312 'contribution_status_id',
313 'contribution_trxn_id',
314 'contribution_page_id',
315 'contribution_product_id',
316 'invoice_id',
317 'payment_instrument_id',
318 'contribution_batch_id',
319 ];
320 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
321
322 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
323 if ($tags && !is_array($tags)) {
324 unset($this->_formValues['contact_tags']);
325 $this->_formValues['contact_tags'][$tags] = 1;
326 }
327
328 if ($tags && is_array($tags)) {
329 unset($this->_formValues['contact_tags']);
330 foreach ($tags as $notImportant => $tagID) {
331 $this->_formValues['contact_tags'][$tagID] = 1;
332 }
333 }
334
335 $group = CRM_Utils_Array::value('group', $this->_formValues);
336 if ($group && !is_array($group)) {
337 unset($this->_formValues['group']);
338 $this->_formValues['group'][$group] = 1;
339 }
340
341 if ($group && is_array($group)) {
342 unset($this->_formValues['group']);
343 foreach ($group as $groupID) {
344 $this->_formValues['group'][$groupID] = 1;
345 }
346 }
347 }
348
349 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
350
351 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
352
353 $this->set('formValues', $this->_formValues);
354 $this->set('queryParams', $this->_queryParams);
355
356 $buttonName = $this->controller->getButtonName();
357 if ($buttonName == $this->_actionButtonName) {
358 // check actionName and if next, then do not repeat a search, since we are going to the next page
359
360 // hack, make sure we reset the task values
361 $stateMachine = $this->controller->getStateMachine();
362 $formName = $stateMachine->getTaskFormName();
363 $this->controller->resetPage($formName);
364 return;
365 }
366
367 $sortID = NULL;
368 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
369 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
370 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
371 );
372 }
373
374 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
375 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
376 $this->_action,
377 NULL,
378 $this->_single,
379 $this->_limit,
380 $this->_context
381 );
382 $selector->setKey($this->controller->_key);
383
384 $prefix = NULL;
385 if ($this->_context == 'basic' || $this->_context == 'user') {
386 $prefix = $this->_prefix;
387 }
388
389 $controller = new CRM_Core_Selector_Controller($selector,
390 $this->get(CRM_Utils_Pager::PAGE_ID),
391 $sortID,
392 CRM_Core_Action::VIEW,
393 $this,
394 CRM_Core_Selector_Controller::SESSION,
395 $prefix
396 );
397 $controller->setEmbedded(TRUE);
398
399 $query = &$selector->getQuery();
400 if ($this->_context == 'user') {
401 $query->setSkipPermission(TRUE);
402 }
403
404 $controller->run();
405 }
406
407 /**
408 * Use values from $_GET if force is set to TRUE.
409 *
410 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
411 */
412 public function fixFormValues() {
413 if (!$this->_force) {
414 return;
415 }
416 // Start by loading url defaults.
417 $this->_formValues = $this->setDefaultValues();
418
419 $status = CRM_Utils_Request::retrieve('status', 'String');
420 if ($status) {
421 $this->_formValues['contribution_status_id'] = [$status => 1];
422 $this->_defaults['contribution_status_id'] = [$status => 1];
423 }
424
425 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
426 if ($pcpid) {
427 // Add new pcpid to the tail of the array...
428 foreach ($pcpid as $pcpIdList) {
429 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
430 }
431 // and avoid any duplicate
432 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
433 }
434
435 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
436
437 if ($cid) {
438 $cid = CRM_Utils_Type::escape($cid, 'Integer');
439 if ($cid > 0) {
440 $this->_formValues['contact_id'] = $cid;
441 // @todo - why do we retrieve these when they are not used?
442 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
443 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
444 'sort_name'
445 );
446 // also assign individual mode to the template
447 $this->_single = TRUE;
448 }
449 }
450
451 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
452 $this
453 );
454
455 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
456 if (isset($test)) {
457 $test = CRM_Utils_Type::escape($test, 'Boolean');
458 $this->_formValues['contribution_test'] = $test;
459 }
460 //Recurring id
461 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
462 if ($recur) {
463 $this->_formValues['contribution_recur_id'] = $recur;
464 $this->_formValues['contribution_recurring'] = 1;
465 }
466
467 //check for contribution page id.
468 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
469 if ($contribPageId) {
470 $this->_formValues['contribution_page_id'] = $contribPageId;
471 }
472
473 //give values to default.
474 $this->_defaults = $this->_formValues;
475 }
476
477 /**
478 * Return a descriptive name for the page, used in wizard header.
479 *
480 * @return string
481 */
482 public function getTitle() {
483 return ts('Find Contributions');
484 }
485
486 }