5cafd0f2ebed9f09b2b782e7e53587a5064b6758
[civicrm-core.git] / CRM / Contribute / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * Files required
38 */
39
40 /**
41 * advanced search, extends basic search
42 */
43 class CRM_Contribute_Form_Search extends CRM_Core_Form {
44
45 /**
46 * Are we forced to run a search
47 *
48 * @var int
49 * @access protected
50 */
51 protected $_force;
52
53 /**
54 * name of search button
55 *
56 * @var string
57 * @access protected
58 */
59 protected $_searchButtonName;
60
61 /**
62 * name of action button
63 *
64 * @var string
65 * @access protected
66 */
67 protected $_actionButtonName;
68
69 /**
70 * form values that we will be using
71 *
72 * @var array
73 * @access public
74 */
75 public $_formValues;
76
77 /**
78 * the params that are sent to the query
79 *
80 * @var array
81 * @access protected
82 */
83 protected $_queryParams;
84
85 /**
86 * have we already done this search
87 *
88 * @access protected
89 * @var boolean
90 */
91 protected $_done;
92
93 /**
94 * are we restricting ourselves to a single contact
95 *
96 * @access protected
97 * @var boolean
98 */
99 protected $_single = FALSE;
100
101 /**
102 * are we restricting ourselves to a single contact
103 *
104 * @access protected
105 * @var boolean
106 */
107 protected $_limit = NULL;
108
109 /**
110 * what context are we being invoked from
111 *
112 * @access protected
113 * @var string
114 */
115 protected $_context = NULL;
116
117 protected $_defaults;
118
119 /**
120 * prefix for the controller
121 *
122 */
123 protected $_prefix = "contribute_";
124
125 /**
126 * processing needed for buildForm and later
127 *
128 * @return void
129 * @access public
130 */ function preProcess() {
131 $this->set('searchFormName', 'Search');
132
133 /**
134 * set the button names
135 */
136 $this->_searchButtonName = $this->getButtonName('refresh');
137 $this->_actionButtonName = $this->getButtonName('next', 'action');
138
139 $this->_done = FALSE;
140 $this->defaults = array();
141
142 /*
143 * we allow the controller to set force/reset externally, useful when we are being
144 * driven by the wizard framework
145 */
146
147 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
148 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
149 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
150 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
151
152 $this->assign("context", $this->_context);
153
154 // get user submitted values
155 // get it from controller only if form has been submitted, else preProcess has set this
156 if (!empty($_POST)) {
157 $this->_formValues = $this->controller->exportValues($this->_name);
158 }
159 else {
160 $this->_formValues = $this->get('formValues');
161 }
162
163 //membership ID
164 $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
165 if (isset($memberShipId)) {
166 $this->_formValues['contribution_membership_id'] = $memberShipId;
167 }
168 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
169 if (isset($participantId)) {
170 $this->_formValues['contribution_participant_id'] = $participantId;
171 }
172
173 if ($this->_force) {
174 $this->postProcess();
175 $this->set('force', 0);
176 }
177
178 $sortID = NULL;
179 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
180 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
181 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
182 );
183 }
184
185 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
186 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
187 $this->_action,
188 NULL,
189 $this->_single,
190 $this->_limit,
191 $this->_context
192 );
193 $prefix = NULL;
194 if ($this->_context == 'user') {
195 $prefix = $this->_prefix;
196 }
197
198 $this->assign("{$prefix}limit", $this->_limit);
199 $this->assign("{$prefix}single", $this->_single);
200
201 $controller = new CRM_Core_Selector_Controller($selector,
202 $this->get(CRM_Utils_Pager::PAGE_ID),
203 $sortID,
204 CRM_Core_Action::VIEW,
205 $this,
206 CRM_Core_Selector_Controller::TRANSFER,
207 $prefix
208 );
209
210 $controller->setEmbedded(TRUE);
211 $controller->moveFromSessionToTemplate();
212
213 $this->assign('contributionSummary', $this->get('summary'));
214 }
215
216 function setDefaultValues() {
217 if (empty($this->_defaults
218 ['contribution_status'])) {
219 $this->_defaults['contribution_status'][1] = 1;
220 }
221 return $this->_defaults;
222 }
223
224 /**
225 * Build the form
226 *
227 * @access public
228 *
229 * @return void
230 */
231 function buildQuickForm() {
232 // text for sort_name
233 $this->addElement('text',
234 'sort_name',
235 ts('Contributor Name or Email'),
236 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
237 'sort_name'
238 )
239 );
240
241 $this->_group = CRM_Core_PseudoConstant::group();
242
243 // multiselect for groups
244 if ($this->_group) {
245 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
246 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
247 );
248 }
249
250 // multiselect for tags
251 $contactTags = CRM_Core_BAO_Tag::getTags();
252
253 if ($contactTags) {
254 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
255 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
256 );
257 }
258
259 CRM_Contribute_BAO_Query::buildSearchForm($this);
260
261 /*
262 * add form checkboxes for each row. This is needed out here to conform to QF protocol
263 * of all elements being declared in builQuickForm
264 */
265
266 $rows = $this->get('rows');
267 if (is_array($rows)) {
268 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js');
269 if (!$this->_single) {
270 $this->addElement('checkbox',
271 'toggleSelect',
272 NULL,
273 NULL,
274 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')
275 );
276 foreach ($rows as $row) {
277 $this->addElement('checkbox', $row['checkbox'],
278 NULL, NULL,
279 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
280 );
281 }
282 }
283
284 $total = $cancel = 0;
285
286 $permission = CRM_Core_Permission::getPermission();
287
288 $queryParams = $this->get('queryParams');
289 $softCreditFiltering = FALSE;
290 if (!empty($queryParams)) {
291 $softCreditFiltering = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
292 }
293 $tasks = array('' => ts('- actions -')) + CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering);
294 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
295 $this->add('submit', $this->_actionButtonName, ts('Go'),
296 array(
297 'class' => 'form-submit',
298 'id' => 'Go',
299 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
300 )
301 );
302
303 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
304 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
305 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
306 }
307
308 // add buttons
309 $this->addButtons(array(
310 array(
311 'type' => 'refresh',
312 'name' => ts('Search'),
313 'isDefault' => TRUE,
314 ),
315 )
316 );
317 }
318
319 /**
320 * The post processing of the form gets done here.
321 *
322 * Key things done during post processing are
323 * - check for reset or next request. if present, skip post procesing.
324 * - now check if user requested running a saved search, if so, then
325 * the form values associated with the saved search are used for searching.
326 * - if user has done a submit with new values the regular post submissing is
327 * done.
328 * The processing consists of using a Selector / Controller framework for getting the
329 * search results.
330 *
331 * @param
332 *
333 * @return void
334 * @access public
335 */
336 function postProcess() {
337 if ($this->_done) {
338 return;
339 }
340
341 $this->_done = TRUE;
342
343 if (!empty($_POST)) {
344 $this->_formValues = $this->controller->exportValues($this->_name);
345 }
346
347 $this->fixFormValues();
348
349 // We don't show test records in summaries or dashboards
350 if (empty($this->_formValues['contribution_test']) && $this->_force) {
351 $this->_formValues["contribution_test"] = 0;
352 }
353
354 foreach (array(
355 'contribution_amount_low', 'contribution_amount_high') as $f) {
356 if (isset($this->_formValues[$f])) {
357 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
358 }
359 }
360
361 $config = CRM_Core_Config::singleton();
362 // CRM-13848
363 $financialType = CRM_Utils_Array::value('financial_type_id', $this->_formValues);
364 if ($financialType && is_array($financialType)) {
365 unset($this->_formValues['financial_type_id']);
366 foreach($financialType as $notImportant => $typeID) {
367 $this->_formValues['financial_type_id'][$typeID] = 1;
368 }
369 }
370
371 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
372 if ($tags && !is_array($tags)) {
373 unset($this->_formValues['contact_tags']);
374 $this->_formValues['contact_tags'][$tags] = 1;
375 }
376
377 if ($tags && is_array($tags)) {
378 unset($this->_formValues['contact_tags']);
379 foreach($tags as $notImportant => $tagID) {
380 $this->_formValues['contact_tags'][$tagID] = 1;
381 }
382 }
383
384
385 if (!$config->groupTree) {
386 $group = CRM_Utils_Array::value('group', $this->_formValues);
387 if ($group && !is_array($group)) {
388 unset($this->_formValues['group']);
389 $this->_formValues['group'][$group] = 1;
390 }
391
392 if ($group && is_array($group)) {
393 unset($this->_formValues['group']);
394 foreach($group as $notImportant => $groupID) {
395 $this->_formValues['group'][$groupID] = 1;
396 }
397 }
398
399 }
400
401 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
402
403 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
404
405 $this->set('formValues', $this->_formValues);
406 $this->set('queryParams', $this->_queryParams);
407
408 $buttonName = $this->controller->getButtonName();
409 if ($buttonName == $this->_actionButtonName) {
410 // check actionName and if next, then do not repeat a search, since we are going to the next page
411
412 // hack, make sure we reset the task values
413 $stateMachine = $this->controller->getStateMachine();
414 $formName = $stateMachine->getTaskFormName();
415 $this->controller->resetPage($formName);
416 return;
417 }
418
419
420 $sortID = NULL;
421 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
422 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
423 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
424 );
425 }
426
427 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
428 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
429 $this->_action,
430 NULL,
431 $this->_single,
432 $this->_limit,
433 $this->_context
434 );
435 $selector->setKey($this->controller->_key);
436
437 $prefix = NULL;
438 if ($this->_context == 'basic' || $this->_context == 'user') {
439 $prefix = $this->_prefix;
440 }
441
442 $controller = new CRM_Core_Selector_Controller($selector,
443 $this->get(CRM_Utils_Pager::PAGE_ID),
444 $sortID,
445 CRM_Core_Action::VIEW,
446 $this,
447 CRM_Core_Selector_Controller::SESSION,
448 $prefix
449 );
450 $controller->setEmbedded(TRUE);
451
452 $query = &$selector->getQuery();
453 if ($this->_context == 'user') {
454 $query->setSkipPermission(TRUE);
455 }
456 $summary = &$query->summaryContribution($this->_context);
457 $this->set('summary', $summary);
458 $this->assign('contributionSummary', $summary);
459 $controller->run();
460 }
461
462 function fixFormValues() {
463 // if this search has been forced
464 // then see if there are any get values, and if so over-ride the post values
465 // note that this means that GET over-rides POST :)
466
467 if (!$this->_force) {
468 return;
469 }
470
471 $status = CRM_Utils_Request::retrieve('status', 'String',
472 CRM_Core_DAO::$_nullObject
473 );
474 if ($status) {
475 $this->_formValues['contribution_status_id'] = array($status => 1);
476 $this->_defaults['contribution_status_id'] = array($status => 1);
477 }
478
479 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
480
481 if ($cid) {
482 $cid = CRM_Utils_Type::escape($cid, 'Integer');
483 if ($cid > 0) {
484 $this->_formValues['contact_id'] = $cid;
485 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
486 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
487 'sort_name'
488 );
489 // also assign individual mode to the template
490 $this->_single = TRUE;
491 }
492 }
493
494 $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp',
495 CRM_Core_DAO::$_nullObject
496 );
497 if ($lowDate) {
498 $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp');
499 $date = CRM_Utils_Date::setDateDefaults($lowDate);
500 $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0];
501 }
502
503 $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp',
504 CRM_Core_DAO::$_nullObject
505 );
506 if ($highDate) {
507 $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp');
508 $date = CRM_Utils_Date::setDateDefaults($highDate);
509 $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0];
510 }
511
512 if ($highDate || $lowDate) {
513 //set the Choose Date Range value
514 $this->_formValues['contribution_date_relative'] = 0;
515 }
516
517 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
518 $this
519 );
520
521 $test = CRM_Utils_Request::retrieve('test', 'Boolean',
522 CRM_Core_DAO::$_nullObject
523 );
524 if (isset($test)) {
525 $test = CRM_Utils_Type::escape($test, 'Boolean');
526 $this->_formValues['contribution_test'] = $test;
527 }
528 //Recurring id
529 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
530 if ($recur) {
531 $this->_formValues['contribution_recur_id'] = $recur;
532 $this->_formValues['contribution_recurring'] = 1;
533 }
534
535 //check for contribution page id.
536 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
537 if ($contribPageId) {
538 $this->_formValues['contribution_page_id'] = $contribPageId;
539 }
540
541 //give values to default.
542 $this->_defaults = $this->_formValues;
543 }
544
545 /**
546 * Return a descriptive name for the page, used in wizard header
547 *
548 * @return string
549 * @access public
550 */
551 public function getTitle() {
552 return ts('Find Contributions');
553 }
554 }
555