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