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