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