Remove duplicate call to contribution summary
[civicrm-core.git] / CRM / Contribute / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
c5a11b83 35 * Advanced search, extends basic search.
6a488035 36 */
3efb5b86 37class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
6a488035 38
6a488035 39 /**
fe482240 40 * The params that are sent to the query.
6a488035
TO
41 *
42 * @var array
6a488035
TO
43 */
44 protected $_queryParams;
45
6a488035 46 /**
fe482240 47 * Are we restricting ourselves to a single contact.
6a488035 48 *
6a488035
TO
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
fe482240 54 * Are we restricting ourselves to a single contact.
6a488035 55 *
6a488035
TO
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
6a488035 60 /**
fe482240 61 * Prefix for the controller.
6a488035
TO
62 */
63 protected $_prefix = "contribute_";
64
927898c5 65
66 /**
67 * Explicitly declare the entity api name.
68 */
69 public function getDefaultEntity() {
70 return 'Contribution';
71 }
72
6a488035 73 /**
fe482240 74 * Processing needed for buildForm and later.
95ea96be 75 */
acb1052e 76 public function preProcess() {
6a488035
TO
77 $this->set('searchFormName', 'Search');
78
6a488035 79 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
80 $this->_actionButtonName = $this->getButtonName('next', 'action');
81
82 $this->_done = FALSE;
c5a11b83 83 // @todo - is this an error - $this->_defaults is used.
6a488035
TO
84 $this->defaults = array();
85
86 /*
87 * we allow the controller to set force/reset externally, useful when we are being
88 * driven by the wizard framework
89 */
ed106721 90
a3d827a7 91 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean');
353ffa53
TO
92 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
93 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
edc80cda 94 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search');
6a488035
TO
95
96 $this->assign("context", $this->_context);
97
98 // get user submitted values
99 // get it from controller only if form has been submitted, else preProcess has set this
100 if (!empty($_POST)) {
101 $this->_formValues = $this->controller->exportValues($this->_name);
102 }
103 else {
104 $this->_formValues = $this->get('formValues');
105 }
106
107 //membership ID
108 $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
109 if (isset($memberShipId)) {
110 $this->_formValues['contribution_membership_id'] = $memberShipId;
111 }
112 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
113 if (isset($participantId)) {
114 $this->_formValues['contribution_participant_id'] = $participantId;
115 }
116
117 if ($this->_force) {
118 $this->postProcess();
119 $this->set('force', 0);
120 }
121
122 $sortID = NULL;
123 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
124 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
125 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
126 );
127 }
128
c94d39fd 129 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
6a488035
TO
130 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
131 $this->_action,
132 NULL,
133 $this->_single,
134 $this->_limit,
135 $this->_context
136 );
137 $prefix = NULL;
138 if ($this->_context == 'user') {
139 $prefix = $this->_prefix;
140 }
141
142 $this->assign("{$prefix}limit", $this->_limit);
143 $this->assign("{$prefix}single", $this->_single);
144
145 $controller = new CRM_Core_Selector_Controller($selector,
146 $this->get(CRM_Utils_Pager::PAGE_ID),
147 $sortID,
148 CRM_Core_Action::VIEW,
149 $this,
150 CRM_Core_Selector_Controller::TRANSFER,
151 $prefix
152 );
153
154 $controller->setEmbedded(TRUE);
155 $controller->moveFromSessionToTemplate();
156
157 $this->assign('contributionSummary', $this->get('summary'));
158 }
159
c5a11b83
EM
160 /**
161 * Set defaults.
162 *
163 * @return array
164 */
00be9182 165 public function setDefaultValues() {
874c9be7 166 if (empty($this->_defaults['contribution_status'])) {
6a488035
TO
167 $this->_defaults['contribution_status'][1] = 1;
168 }
169 return $this->_defaults;
170 }
171
172 /**
fe482240 173 * Build the form object.
6a488035 174 */
00be9182 175 public function buildQuickForm() {
eb0010b8
JP
176 if ($this->isFormInViewOrEditMode()) {
177 parent::buildQuickForm();
178 $this->addContactSearchFields();
6a488035 179
eb0010b8
JP
180 CRM_Contribute_BAO_Query::buildSearchForm($this);
181 }
6a488035 182
6a488035
TO
183 $rows = $this->get('rows');
184 if (is_array($rows)) {
185 if (!$this->_single) {
8d36b801 186 $this->addRowSelectors($rows);
6a488035
TO
187 }
188
6a488035
TO
189 $permission = CRM_Core_Permission::getPermission();
190
c410490a 191 $queryParams = $this->get('queryParams');
1a72712d 192 $taskParams['softCreditFiltering'] = FALSE;
c410490a 193 if (!empty($queryParams)) {
1a72712d 194 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
c410490a 195 }
1a72712d 196 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams);
34197a55 197 $this->addTaskMenu($tasks);
6a488035
TO
198 }
199
6a488035
TO
200 }
201
e597fc33
DG
202 /**
203 * Get the label for the sortName field if email searching is on.
204 *
205 * (email searching is a setting under search preferences).
206 *
207 * @return string
208 */
209 protected function getSortNameLabelWithEmail() {
210 return ts('Contributor Name or Email');
211 }
212
213 /**
214 * Get the label for the sortName field if email searching is off.
215 *
216 * (email searching is a setting under search preferences).
217 *
218 * @return string
219 */
220 protected function getSortNameLabelWithOutEmail() {
221 return ts('Contributor Name');
222 }
223
0573fd28 224 /**
225 * Get the label for the tag field.
226 *
227 * We do this in a function so the 'ts' wraps the whole string to allow
228 * better translation.
229 *
230 * @return string
231 */
232 protected function getTagLabel() {
233 return ts('Contributor Tag(s)');
234 }
235
236 /**
237 * Get the label for the group field.
238 *
239 * @return string
240 */
241 protected function getGroupLabel() {
242 return ts('Contributor Group(s)');
243 }
244
245 /**
246 * Get the label for the group field.
247 *
248 * @return string
249 */
250 protected function getContactTypeLabel() {
251 return ts('Contributor Contact Type');
252 }
253
6a488035
TO
254 /**
255 * The post processing of the form gets done here.
256 *
257 * Key things done during post processing are
c5a11b83 258 * - check for reset or next request. if present, skip post processing.
6a488035
TO
259 * - now check if user requested running a saved search, if so, then
260 * the form values associated with the saved search are used for searching.
c5a11b83 261 * - if user has done a submit with new values the regular post submission is
6a488035
TO
262 * done.
263 * The processing consists of using a Selector / Controller framework for getting the
264 * search results.
6a488035 265 */
00be9182 266 public function postProcess() {
6a488035
TO
267 if ($this->_done) {
268 return;
269 }
270
271 $this->_done = TRUE;
272
01efed67 273 if (!empty($_POST) && !$this->_force) {
6a488035
TO
274 $this->_formValues = $this->controller->exportValues($this->_name);
275 }
276
277 $this->fixFormValues();
278
d43b88cc 279 // We don't show test records in summaries or dashboards
b899db8f 280 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') {
6a488035
TO
281 $this->_formValues["contribution_test"] = 0;
282 }
283
284 foreach (array(
353ffa53 285 'contribution_amount_low',
acb1052e 286 'contribution_amount_high',
353ffa53 287 ) as $f) {
6a488035
TO
288 if (isset($this->_formValues[$f])) {
289 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
290 }
291 }
ed106721 292
6a488035 293 $config = CRM_Core_Config::singleton();
a2be2b19 294 if (!empty($_POST)) {
afa0b07c 295 $specialParams = array(
296 'financial_type_id',
297 'contribution_soft_credit_type_id',
298 'contribution_status_id',
299 'contribution_source',
acb1052e 300 'contribution_trxn_id',
8039a148 301 'contribution_page_id',
6ffab5b7 302 'contribution_product_id',
b5a37491 303 'invoice_id',
8f3dc989 304 'payment_instrument_id',
3086e282 305 'contribution_batch_id',
afa0b07c 306 );
0b38e8f1 307 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams);
1f0d8c92 308
a2be2b19
PN
309 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
310 if ($tags && !is_array($tags)) {
311 unset($this->_formValues['contact_tags']);
312 $this->_formValues['contact_tags'][$tags] = 1;
313 }
ed106721 314
a2be2b19
PN
315 if ($tags && is_array($tags)) {
316 unset($this->_formValues['contact_tags']);
22e263ad 317 foreach ($tags as $notImportant => $tagID) {
a2be2b19
PN
318 $this->_formValues['contact_tags'][$tagID] = 1;
319 }
6a488035 320 }
ed106721 321
ac7b09fc 322 $group = CRM_Utils_Array::value('group', $this->_formValues);
323 if ($group && !is_array($group)) {
324 unset($this->_formValues['group']);
325 $this->_formValues['group'][$group] = 1;
326 }
ed106721 327
ac7b09fc 328 if ($group && is_array($group)) {
329 unset($this->_formValues['group']);
330 foreach ($group as $groupID) {
331 $this->_formValues['group'][$groupID] = 1;
6a488035
TO
332 }
333 }
6a488035
TO
334 }
335
c94d39fd 336 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
337
338 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
339
340 $this->set('formValues', $this->_formValues);
341 $this->set('queryParams', $this->_queryParams);
342
343 $buttonName = $this->controller->getButtonName();
e341bbee 344 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
345 // check actionName and if next, then do not repeat a search, since we are going to the next page
346
347 // hack, make sure we reset the task values
ed106721 348 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
349 $formName = $stateMachine->getTaskFormName();
350 $this->controller->resetPage($formName);
351 return;
352 }
353
6a488035
TO
354 $sortID = NULL;
355 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
356 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
357 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
358 );
359 }
360
361 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
362 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
363 $this->_action,
364 NULL,
365 $this->_single,
366 $this->_limit,
367 $this->_context
368 );
369 $selector->setKey($this->controller->_key);
370
371 $prefix = NULL;
372 if ($this->_context == 'basic' || $this->_context == 'user') {
373 $prefix = $this->_prefix;
374 }
375
376 $controller = new CRM_Core_Selector_Controller($selector,
377 $this->get(CRM_Utils_Pager::PAGE_ID),
378 $sortID,
379 CRM_Core_Action::VIEW,
380 $this,
381 CRM_Core_Selector_Controller::SESSION,
382 $prefix
383 );
384 $controller->setEmbedded(TRUE);
385
386 $query = &$selector->getQuery();
387 if ($this->_context == 'user') {
388 $query->setSkipPermission(TRUE);
389 }
18f383f8 390
6a488035
TO
391 $controller->run();
392 }
393
c5a11b83
EM
394 /**
395 * Use values from $_GET if force is set to TRUE.
396 *
397 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
398 */
00be9182 399 public function fixFormValues() {
6a488035
TO
400 if (!$this->_force) {
401 return;
402 }
403
1273d77c 404 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035
TO
405 if ($status) {
406 $this->_formValues['contribution_status_id'] = array($status => 1);
407 $this->_defaults['contribution_status_id'] = array($status => 1);
408 }
409
29d72d92
A
410 $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this);
411 if ($pcpid) {
412 // Add new pcpid to the tail of the array...
413 foreach ($pcpid as $pcpIdList) {
414 $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList;
415 }
416 // and avoid any duplicate
417 $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']);
418 }
419
6a488035
TO
420 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
421
422 if ($cid) {
423 $cid = CRM_Utils_Type::escape($cid, 'Integer');
424 if ($cid > 0) {
425 $this->_formValues['contact_id'] = $cid;
c5a11b83 426 // @todo - why do we retrieve these when they are not used?
6a488035
TO
427 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
428 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
429 'sort_name'
430 );
431 // also assign individual mode to the template
432 $this->_single = TRUE;
433 }
434 }
435
1273d77c 436 $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp');
6a488035
TO
437 if ($lowDate) {
438 $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp');
439 $date = CRM_Utils_Date::setDateDefaults($lowDate);
440 $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0];
441 }
442
1273d77c 443 $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp');
6a488035
TO
444 if ($highDate) {
445 $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp');
446 $date = CRM_Utils_Date::setDateDefaults($highDate);
447 $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0];
448 }
449
450 if ($highDate || $lowDate) {
451 //set the Choose Date Range value
452 $this->_formValues['contribution_date_relative'] = 0;
453 }
454
455 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
456 $this
457 );
458
1273d77c 459 $test = CRM_Utils_Request::retrieve('test', 'Boolean');
6a488035
TO
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 /**
c5a11b83 482 * Return a descriptive name for the page, used in wizard header.
6a488035
TO
483 *
484 * @return string
6a488035
TO
485 */
486 public function getTitle() {
487 return ts('Find Contributions');
488 }
96025800 489
6a488035 490}