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