Merge pull request #3391 from eileenmcnaughton/CRM-14768
[civicrm-core.git] / CRM / Event / 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, |
a5611c8e 24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
6a488035
TO
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 * This file is for civievent search
42 */
3efb5b86 43class CRM_Event_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
73 * @access protected
74 */
75 protected $_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 /**
118 * prefix for the controller
119 *
120 */
121 protected $_prefix = "event_";
122
123 protected $_defaults;
124
125 /**
126 * the saved search ID retrieved from the GET vars
127 *
128 * @var int
129 * @access protected
130 */
131 protected $_ssID;
132
133 /**
134 * processing needed for buildForm and later
135 *
136 * @return void
137 * @access public
138 */
139 function preProcess() {
140 $this->set('searchFormName', 'Search');
141
142 /**
143 * set the button names
144 */
145 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
146 $this->_actionButtonName = $this->getButtonName('next', 'action');
147
148 $this->_done = FALSE;
149 $this->defaults = array();
150
a5611c8e
DL
151 /*
152 * we allow the controller to set force/reset externally, useful when we are being
153 * driven by the wizard framework
6a488035
TO
154 */
155 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
156 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
157 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
158 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
159 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
160 $this->assign("context", $this->_context);
161
162 // get user submitted values
163 // get it from controller only if form has been submitted, else preProcess has set this
164 if (!empty($_POST) && !$this->controller->isModal()) {
165 $this->_formValues = $this->controller->exportValues($this->_name);
166 }
167 else {
168 $this->_formValues = $this->get('formValues');
169 }
170
171 if (empty($this->_formValues)) {
172 if (isset($this->_ssID)) {
173 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
174 }
175 }
176
177 if ($this->_force) {
178 $this->postProcess();
179 $this->set('force', 0);
180 }
181
182 $sortID = NULL;
183 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
184 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
185 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
186 );
187 }
188
189 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
190 $selector = new CRM_Event_Selector_Search($this->_queryParams,
191 $this->_action,
192 NULL,
193 $this->_single,
194 $this->_limit,
195 $this->_context
196 );
197 $prefix = NULL;
198 if ($this->_context == 'user') {
199 $prefix = $this->_prefix;
200 }
201
202 $this->assign("{$prefix}limit", $this->_limit);
203 $this->assign("{$prefix}single", $this->_single);
204
205 $controller = new CRM_Core_Selector_Controller($selector,
206 $this->get(CRM_Utils_Pager::PAGE_ID),
207 $sortID,
208 CRM_Core_Action::VIEW,
209 $this,
210 CRM_Core_Selector_Controller::TRANSFER,
211 $prefix
212 );
213 $controller->setEmbedded(TRUE);
214 $controller->moveFromSessionToTemplate();
215
216 $this->assign('summary', $this->get('summary'));
217 }
218
219 /**
220 * Build the form
221 *
222 * @access public
223 *
224 * @return void
225 */
226 function buildQuickForm() {
3efb5b86 227 parent::buildQuickForm();
6a488035
TO
228 $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
229
230 CRM_Event_BAO_Query::buildSearchForm($this);
231
a5611c8e
DL
232 /*
233 * add form checkboxes for each row. This is needed out here to conform to QF protocol
234 * of all elements being declared in builQuickForm
6a488035
TO
235 */
236 $rows = $this->get('rows');
237 if (is_array($rows)) {
238 $lineItems = $eventIds = array();
239 if (!$this->_single) {
240 $this->addElement('checkbox',
241 'toggleSelect',
242 NULL,
243 NULL,
d664f648 244 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')
6a488035
TO
245 );
246 }
247 foreach ($rows as $row) {
248 $eventIds[$row['event_id']] = $row['event_id'];
249 if (!$this->_single) {
250 $this->addElement('checkbox', $row['checkbox'],
251 NULL, NULL,
d664f648 252 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
6a488035
TO
253 );
254 }
255 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
256 // add line item details if applicable
257 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
258 }
259 }
260
261 //get actual count only when we are dealing w/ single event.
262 $participantCount = 0;
263 if (count($eventIds) == 1) {
264 //convert form values to clause.
265 $seatClause = array();
59adcdd9
DG
266 // Filter on is_test if specified in search form
267 if (CRM_Utils_Array::value('participant_test', $this->_formValues) == '1' || CRM_Utils_Array::value('participant_test', $this->_formValues) == '0' ) {
268 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
269 }
a7488080 270 if (!empty($this->_formValues['participant_status_id'])) {
6a488035 271 $statuses = array_keys($this->_formValues['participant_status_id']);
6a488035
TO
272 $seatClause[] = '( participant.status_id IN ( ' . implode(' , ', $statuses) . ' ) )';
273 }
a7488080 274 if (!empty($this->_formValues['participant_role_id'])) {
6a488035 275 $roles = array_keys($this->_formValues['participant_role_id']);
59adcdd9 276 $seatClause[] = '( participant.role_id IN ( ' . implode(' , ', $roles) . ' ) )';
6a488035
TO
277 }
278 $clause = NULL;
279 if (!empty($seatClause)) {
280 $clause = implode(' AND ', $seatClause);
281 }
59adcdd9 282
6a488035
TO
283 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $clause);
284 }
285 $this->assign('participantCount', $participantCount);
286 $this->assign('lineItems', $lineItems);
287
288 $total = $cancel = 0;
289
290 $permission = CRM_Core_Permission::getPermission();
291
292 $tasks = array('' => ts('- actions -')) + CRM_Event_Task::permissionedTaskTitles($permission);
293 if (isset($this->_ssID)) {
294 if ($permission == CRM_Core_Permission::EDIT) {
295 $tasks = $tasks + CRM_Event_Task::optionalTaskTitle();
296 }
297
298 $savedSearchValues = array(
299 'id' => $this->_ssID,
300 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
301 );
302 $this->assign_by_ref('savedSearch', $savedSearchValues);
303 $this->assign('ssID', $this->_ssID);
304 }
305
306 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
307 $this->add('submit', $this->_actionButtonName, ts('Go'),
308 array(
309 'class' => 'form-submit',
310 'id' => 'Go',
311 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
312 )
313 );
314
6a488035
TO
315 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
316 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel',
317 array('checked' => 'checked')
318 );
319 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all',
d664f648 320 array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")
6a488035
TO
321 );
322 }
323
6a488035
TO
324 }
325
326 /**
327 * The post processing of the form gets done here.
328 *
329 * Key things done during post processing are
330 * - check for reset or next request. if present, skip post procesing.
331 * - now check if user requested running a saved search, if so, then
332 * the form values associated with the saved search are used for searching.
333 * - if user has done a submit with new values the regular post submissing is
334 * done.
335 * The processing consists of using a Selector / Controller framework for getting the
336 * search results.
337 *
338 * @param
339 *
340 * @return void
341 * @access public
342 */
343 function postProcess() {
344 if ($this->_done) {
345 return;
346 }
347
348 $this->_done = TRUE;
349
350 if (!empty($_POST)) {
351 $this->_formValues = $this->controller->exportValues($this->_name);
352 }
353
354 if (empty($this->_formValues)) {
355 $this->_formValues = $this->controller->exportValues($this->_name);
356 }
357
358 $this->fixFormValues();
359
360 if (isset($this->_ssID) && empty($_POST)) {
361 // if we are editing / running a saved search and the form has not been posted
362 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
363 }
364
d43b88cc
CW
365 // We don't show test records in summaries or dashboards
366 if (empty($this->_formValues['participant_test']) && $this->_force) {
6a488035
TO
367 $this->_formValues["participant_test"] = 0;
368 }
369
370 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
371
372 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
373
374 $this->set('formValues', $this->_formValues);
375 $this->set('queryParams', $this->_queryParams);
376
377 $buttonName = $this->controller->getButtonName();
e341bbee 378 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
379 // check actionName and if next, then do not repeat a search, since we are going to the next page
380
381 // hack, make sure we reset the task values
a5611c8e 382 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
383 $formName = $stateMachine->getTaskFormName();
384 $this->controller->resetPage($formName);
385 return;
386 }
387
388 $sortID = NULL;
389 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
390 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
391 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
392 );
393 }
394
395 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
396
397 $selector = new CRM_Event_Selector_Search($this->_queryParams,
398 $this->_action,
399 NULL,
400 $this->_single,
401 $this->_limit,
402 $this->_context
403 );
404
405 $selector->setKey($this->controller->_key);
406
407 $prefix = NULL;
408 if ($this->_context == 'user') {
409 $prefix = $this->_prefix;
410 }
411
412 $this->assign("{$prefix}limit", $this->_limit);
413 $this->assign("{$prefix}single", $this->_single);
414
415 $controller = new CRM_Core_Selector_Controller($selector,
416 $this->get(CRM_Utils_Pager::PAGE_ID),
417 $sortID,
418 CRM_Core_Action::VIEW,
419 $this,
420 CRM_Core_Selector_Controller::SESSION,
421 $prefix
422 );
423 $controller->setEmbedded(TRUE);
424
a5611c8e 425 $query = $selector->getQuery();
6a488035
TO
426 if ($this->_context == 'user') {
427 $query->setSkipPermission(TRUE);
428 }
429 $controller->run();
430 }
431
432 /**
433 * This function is used to add the rules (mainly global rules) for form.
434 * All local rules are added near the element
435 *
355ba699 436 * @return void
6a488035
TO
437 * @access public
438 * @see valid_date
439 */
6aac2db8 440 function addRules() {}
6a488035
TO
441
442 /**
443 * Set the default form values
444 *
445 * @access protected
446 *
447 * @return array the default array reference
448 */
449 function setDefaultValues() {
450 $defaults = array();
451 $defaults = $this->_formValues;
452 return $defaults;
453 }
454
455 function fixFormValues() {
456 // if this search has been forced
457 // then see if there are any get values, and if so over-ride the post values
458 // note that this means that GET over-rides POST :)
459 $event = CRM_Utils_Request::retrieve('event', 'Positive',
460 CRM_Core_DAO::$_nullObject
461 );
462 if ($event) {
463 $this->_formValues['event_id'] = $event;
464 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
465 }
466
467 $status = CRM_Utils_Request::retrieve('status', 'String',
468 CRM_Core_DAO::$_nullObject
469 );
470
471 if (isset($status)) {
472 if ($status === 'true') {
473 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
474 }
475 elseif ($status === 'false') {
476 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
477 }
478 elseif (is_numeric($status)) {
479 $status = (int) $status;
480 $statusTypes = array($status => CRM_Event_PseudoConstant::participantStatus($status));
481 }
482 $status = array();
483 foreach ($statusTypes as $key => $value) {
484 $status[$key] = 1;
485 }
486 $this->_formValues['participant_status_id'] = $status;
487 }
488
489 $role = CRM_Utils_Request::retrieve('role', 'String',
490 CRM_Core_DAO::$_nullObject
491 );
492
493 if (isset($role)) {
494 if ($role === 'true') {
495 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
496 }
497 elseif ($role === 'false') {
498 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
499 }
500 elseif (is_numeric($role)) {
501 $role = (int) $role;
502 $roleTypes = array($role => CRM_Event_PseudoConstant::participantRole($role));
503 }
504 $role = array();
505 foreach ($roleTypes as $key => $value) {
506 $role[$key] = 1;
507 }
508 $this->_formValues['participant_role_id'] = $role;
509 }
510
511 $type = CRM_Utils_Request::retrieve('type', 'Positive',
512 CRM_Core_DAO::$_nullObject
513 );
514 if ($type) {
515 $this->_formValues['event_type'] = $type;
516 }
517
518 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
519
520 if ($cid) {
521 $cid = CRM_Utils_Type::escape($cid, 'Integer');
522 if ($cid > 0) {
523 $this->_formValues['contact_id'] = $cid;
524
525 // also assign individual mode to the template
526 $this->_single = TRUE;
527 }
528 }
529 }
530
0cf587a7
EM
531 /**
532 * @return null
533 */
6a488035
TO
534 function getFormValues() {
535 return NULL;
536 }
537
538 /**
539 * Return a descriptive name for the page, used in wizard header
540 *
541 * @return string
542 * @access public
543 */
544 public function getTitle() {
545 return ts('Find Participants');
546 }
547}
548