CRM-12872 - Centralize search task menu code
[civicrm-core.git] / CRM / Contribute / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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
TO
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
6a488035
TO
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
f1751139 73 * @access public
6a488035 74 */
f1751139 75 public $_formValues;
6a488035
TO
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');
6a488035
TO
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 */
ed106721 146
6a488035
TO
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() {
a7488080
CW
217 if (empty($this->_defaults
218['contribution_status'])) {
6a488035
TO
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() {
3efb5b86 232 parent::buildQuickForm();
6a488035
TO
233 // text for sort_name
234 $this->addElement('text',
235 'sort_name',
236 ts('Contributor Name or Email'),
237 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
238 'sort_name'
239 )
240 );
241
242 $this->_group = CRM_Core_PseudoConstant::group();
243
244 // multiselect for groups
245 if ($this->_group) {
246 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
ab345ca5 247 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
248 );
249 }
250
251 // multiselect for tags
6a488035
TO
252 $contactTags = CRM_Core_BAO_Tag::getTags();
253
254 if ($contactTags) {
255 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
ab345ca5 256 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
257 );
258 }
259
260 CRM_Contribute_BAO_Query::buildSearchForm($this);
261
262 /*
263 * add form checkboxes for each row. This is needed out here to conform to QF protocol
264 * of all elements being declared in builQuickForm
265 */
ed106721 266
6a488035
TO
267 $rows = $this->get('rows');
268 if (is_array($rows)) {
269 if (!$this->_single) {
270 $this->addElement('checkbox',
271 'toggleSelect',
272 NULL,
273 NULL,
d664f648 274 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')
6a488035
TO
275 );
276 foreach ($rows as $row) {
277 $this->addElement('checkbox', $row['checkbox'],
278 NULL, NULL,
d664f648 279 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
6a488035
TO
280 );
281 }
282 }
283
284 $total = $cancel = 0;
285
286 $permission = CRM_Core_Permission::getPermission();
287
c410490a
DS
288 $queryParams = $this->get('queryParams');
289 $softCreditFiltering = FALSE;
290 if (!empty($queryParams)) {
291 $softCreditFiltering = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
292 }
34197a55
CW
293 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering);
294 $this->addTaskMenu($tasks);
6a488035 295
6a488035
TO
296 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
297 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
d664f648 298 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
6a488035
TO
299 }
300
6a488035
TO
301 }
302
303 /**
304 * The post processing of the form gets done here.
305 *
306 * Key things done during post processing are
307 * - check for reset or next request. if present, skip post procesing.
308 * - now check if user requested running a saved search, if so, then
309 * the form values associated with the saved search are used for searching.
310 * - if user has done a submit with new values the regular post submissing is
311 * done.
312 * The processing consists of using a Selector / Controller framework for getting the
313 * search results.
314 *
315 * @param
316 *
317 * @return void
318 * @access public
319 */
320 function postProcess() {
321 if ($this->_done) {
322 return;
323 }
324
325 $this->_done = TRUE;
326
327 if (!empty($_POST)) {
328 $this->_formValues = $this->controller->exportValues($this->_name);
329 }
330
331 $this->fixFormValues();
332
d43b88cc
CW
333 // We don't show test records in summaries or dashboards
334 if (empty($this->_formValues['contribution_test']) && $this->_force) {
6a488035
TO
335 $this->_formValues["contribution_test"] = 0;
336 }
337
338 foreach (array(
339 'contribution_amount_low', 'contribution_amount_high') as $f) {
340 if (isset($this->_formValues[$f])) {
341 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
342 }
343 }
ed106721 344
6a488035 345 $config = CRM_Core_Config::singleton();
a2be2b19
PN
346 if (!empty($_POST)) {
347 // CRM-13848
348 $financialType = CRM_Utils_Array::value('financial_type_id', $this->_formValues);
349 if ($financialType && is_array($financialType)) {
350 unset($this->_formValues['financial_type_id']);
351 foreach($financialType as $notImportant => $typeID) {
352 $this->_formValues['financial_type_id'][$typeID] = 1;
353 }
1f0d8c92 354 }
1f0d8c92 355
a2be2b19
PN
356 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
357 if ($tags && !is_array($tags)) {
358 unset($this->_formValues['contact_tags']);
359 $this->_formValues['contact_tags'][$tags] = 1;
360 }
ed106721 361
a2be2b19
PN
362 if ($tags && is_array($tags)) {
363 unset($this->_formValues['contact_tags']);
364 foreach($tags as $notImportant => $tagID) {
365 $this->_formValues['contact_tags'][$tagID] = 1;
366 }
6a488035 367 }
ed106721
DL
368
369
a2be2b19
PN
370 if (!$config->groupTree) {
371 $group = CRM_Utils_Array::value('group', $this->_formValues);
372 if ($group && !is_array($group)) {
373 unset($this->_formValues['group']);
374 $this->_formValues['group'][$group] = 1;
375 }
ed106721 376
a2be2b19
PN
377 if ($group && is_array($group)) {
378 unset($this->_formValues['group']);
379 foreach($group as $notImportant => $groupID) {
380 $this->_formValues['group'][$groupID] = 1;
381 }
6a488035
TO
382 }
383 }
6a488035
TO
384 }
385
386 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
387
388 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
389
390 $this->set('formValues', $this->_formValues);
391 $this->set('queryParams', $this->_queryParams);
392
393 $buttonName = $this->controller->getButtonName();
e341bbee 394 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
395 // check actionName and if next, then do not repeat a search, since we are going to the next page
396
397 // hack, make sure we reset the task values
ed106721 398 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
399 $formName = $stateMachine->getTaskFormName();
400 $this->controller->resetPage($formName);
401 return;
402 }
403
404
405 $sortID = NULL;
406 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
407 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
408 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
409 );
410 }
411
412 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
413 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
414 $this->_action,
415 NULL,
416 $this->_single,
417 $this->_limit,
418 $this->_context
419 );
420 $selector->setKey($this->controller->_key);
421
422 $prefix = NULL;
423 if ($this->_context == 'basic' || $this->_context == 'user') {
424 $prefix = $this->_prefix;
425 }
426
427 $controller = new CRM_Core_Selector_Controller($selector,
428 $this->get(CRM_Utils_Pager::PAGE_ID),
429 $sortID,
430 CRM_Core_Action::VIEW,
431 $this,
432 CRM_Core_Selector_Controller::SESSION,
433 $prefix
434 );
435 $controller->setEmbedded(TRUE);
436
437 $query = &$selector->getQuery();
438 if ($this->_context == 'user') {
439 $query->setSkipPermission(TRUE);
440 }
441 $summary = &$query->summaryContribution($this->_context);
442 $this->set('summary', $summary);
443 $this->assign('contributionSummary', $summary);
444 $controller->run();
445 }
446
447 function fixFormValues() {
448 // if this search has been forced
449 // then see if there are any get values, and if so over-ride the post values
450 // note that this means that GET over-rides POST :)
6a488035
TO
451 if (!$this->_force) {
452 return;
453 }
454
455 $status = CRM_Utils_Request::retrieve('status', 'String',
456 CRM_Core_DAO::$_nullObject
457 );
458 if ($status) {
459 $this->_formValues['contribution_status_id'] = array($status => 1);
460 $this->_defaults['contribution_status_id'] = array($status => 1);
461 }
462
463 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
464
465 if ($cid) {
466 $cid = CRM_Utils_Type::escape($cid, 'Integer');
467 if ($cid > 0) {
468 $this->_formValues['contact_id'] = $cid;
469 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
470 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
471 'sort_name'
472 );
473 // also assign individual mode to the template
474 $this->_single = TRUE;
475 }
476 }
477
478 $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp',
479 CRM_Core_DAO::$_nullObject
480 );
481 if ($lowDate) {
482 $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp');
483 $date = CRM_Utils_Date::setDateDefaults($lowDate);
484 $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0];
485 }
486
487 $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp',
488 CRM_Core_DAO::$_nullObject
489 );
490 if ($highDate) {
491 $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp');
492 $date = CRM_Utils_Date::setDateDefaults($highDate);
493 $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0];
494 }
495
496 if ($highDate || $lowDate) {
497 //set the Choose Date Range value
498 $this->_formValues['contribution_date_relative'] = 0;
499 }
500
501 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
502 $this
503 );
504
505 $test = CRM_Utils_Request::retrieve('test', 'Boolean',
506 CRM_Core_DAO::$_nullObject
507 );
508 if (isset($test)) {
509 $test = CRM_Utils_Type::escape($test, 'Boolean');
510 $this->_formValues['contribution_test'] = $test;
511 }
512 //Recurring id
513 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
514 if ($recur) {
515 $this->_formValues['contribution_recur_id'] = $recur;
516 $this->_formValues['contribution_recurring'] = 1;
517 }
518
519 //check for contribution page id.
520 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
521 if ($contribPageId) {
522 $this->_formValues['contribution_page_id'] = $contribPageId;
523 }
524
525 //give values to default.
526 $this->_defaults = $this->_formValues;
527 }
528
529 /**
530 * Return a descriptive name for the page, used in wizard header
531 *
532 * @return string
533 * @access public
534 */
535 public function getTitle() {
536 return ts('Find Contributions');
537 }
538}
539