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