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