CRM-11610 - Search js - Abstract table row highlight/select
[civicrm-core.git] / CRM / Event / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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, |
a5611c8e 24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
6a488035
TO
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 */
43class 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
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() {
227 $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
228
229 CRM_Event_BAO_Query::buildSearchForm($this);
230
a5611c8e
DL
231 /*
232 * add form checkboxes for each row. This is needed out here to conform to QF protocol
233 * of all elements being declared in builQuickForm
6a488035
TO
234 */
235 $rows = $this->get('rows');
236 if (is_array($rows)) {
c1b06d18 237 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js');
6a488035
TO
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
324 // add buttons
325 $this->addButtons(array(
326 array(
327 'type' => 'refresh',
328 'name' => ts('Search'),
329 'isDefault' => TRUE,
330 ),
331 ));
332 }
333
334 /**
335 * The post processing of the form gets done here.
336 *
337 * Key things done during post processing are
338 * - check for reset or next request. if present, skip post procesing.
339 * - now check if user requested running a saved search, if so, then
340 * the form values associated with the saved search are used for searching.
341 * - if user has done a submit with new values the regular post submissing is
342 * done.
343 * The processing consists of using a Selector / Controller framework for getting the
344 * search results.
345 *
346 * @param
347 *
348 * @return void
349 * @access public
350 */
351 function postProcess() {
352 if ($this->_done) {
353 return;
354 }
355
356 $this->_done = TRUE;
357
358 if (!empty($_POST)) {
359 $this->_formValues = $this->controller->exportValues($this->_name);
360 }
361
362 if (empty($this->_formValues)) {
363 $this->_formValues = $this->controller->exportValues($this->_name);
364 }
365
366 $this->fixFormValues();
367
368 if (isset($this->_ssID) && empty($_POST)) {
369 // if we are editing / running a saved search and the form has not been posted
370 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
371 }
372
d43b88cc
CW
373 // We don't show test records in summaries or dashboards
374 if (empty($this->_formValues['participant_test']) && $this->_force) {
6a488035
TO
375 $this->_formValues["participant_test"] = 0;
376 }
377
378 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
379
380 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
381
382 $this->set('formValues', $this->_formValues);
383 $this->set('queryParams', $this->_queryParams);
384
385 $buttonName = $this->controller->getButtonName();
e341bbee 386 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
387 // check actionName and if next, then do not repeat a search, since we are going to the next page
388
389 // hack, make sure we reset the task values
a5611c8e 390 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
391 $formName = $stateMachine->getTaskFormName();
392 $this->controller->resetPage($formName);
393 return;
394 }
395
396 $sortID = NULL;
397 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
398 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
399 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
400 );
401 }
402
403 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
404
405 $selector = new CRM_Event_Selector_Search($this->_queryParams,
406 $this->_action,
407 NULL,
408 $this->_single,
409 $this->_limit,
410 $this->_context
411 );
412
413 $selector->setKey($this->controller->_key);
414
415 $prefix = NULL;
416 if ($this->_context == 'user') {
417 $prefix = $this->_prefix;
418 }
419
420 $this->assign("{$prefix}limit", $this->_limit);
421 $this->assign("{$prefix}single", $this->_single);
422
423 $controller = new CRM_Core_Selector_Controller($selector,
424 $this->get(CRM_Utils_Pager::PAGE_ID),
425 $sortID,
426 CRM_Core_Action::VIEW,
427 $this,
428 CRM_Core_Selector_Controller::SESSION,
429 $prefix
430 );
431 $controller->setEmbedded(TRUE);
432
a5611c8e 433 $query = $selector->getQuery();
6a488035
TO
434 if ($this->_context == 'user') {
435 $query->setSkipPermission(TRUE);
436 }
437 $controller->run();
438 }
439
440 /**
441 * This function is used to add the rules (mainly global rules) for form.
442 * All local rules are added near the element
443 *
355ba699 444 * @return void
6a488035
TO
445 * @access public
446 * @see valid_date
447 */
448 function addRules() {
449 $this->addFormRule(array('CRM_Event_Form_Search', 'formRule'));
450 }
451
452 /**
453 * global validation rules for the form
454 *
455 * @param array $fields posted values of the form
456 * @param array $errors list of errors to be posted back to the form
457 *
458 * @return void
459 * @static
460 * @access public
461 */
462 static function formRule($fields) {
463 $errors = array();
464
465 if ($fields['event_name'] && !is_numeric($fields['event_id'])) {
466 $errors['event_id'] = ts('Please select valid event.');
467 }
468
469 if ($fields['event_type'] && !is_numeric($fields['event_type_id'])) {
470 $errors['event_type'] = ts('Please select valid event type.');
471 }
472 if (!empty($errors)) {
473 return $errors;
474 }
475
476 return TRUE;
477 }
478
479 /**
480 * Set the default form values
481 *
482 * @access protected
483 *
484 * @return array the default array reference
485 */
486 function setDefaultValues() {
487 $defaults = array();
488 $defaults = $this->_formValues;
489 return $defaults;
490 }
491
492 function fixFormValues() {
493 // if this search has been forced
494 // then see if there are any get values, and if so over-ride the post values
495 // note that this means that GET over-rides POST :)
496 $event = CRM_Utils_Request::retrieve('event', 'Positive',
497 CRM_Core_DAO::$_nullObject
498 );
499 if ($event) {
500 $this->_formValues['event_id'] = $event;
501 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
502 }
503
504 $status = CRM_Utils_Request::retrieve('status', 'String',
505 CRM_Core_DAO::$_nullObject
506 );
507
508 if (isset($status)) {
509 if ($status === 'true') {
510 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
511 }
512 elseif ($status === 'false') {
513 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
514 }
515 elseif (is_numeric($status)) {
516 $status = (int) $status;
517 $statusTypes = array($status => CRM_Event_PseudoConstant::participantStatus($status));
518 }
519 $status = array();
520 foreach ($statusTypes as $key => $value) {
521 $status[$key] = 1;
522 }
523 $this->_formValues['participant_status_id'] = $status;
524 }
525
526 $role = CRM_Utils_Request::retrieve('role', 'String',
527 CRM_Core_DAO::$_nullObject
528 );
529
530 if (isset($role)) {
531 if ($role === 'true') {
532 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
533 }
534 elseif ($role === 'false') {
535 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
536 }
537 elseif (is_numeric($role)) {
538 $role = (int) $role;
539 $roleTypes = array($role => CRM_Event_PseudoConstant::participantRole($role));
540 }
541 $role = array();
542 foreach ($roleTypes as $key => $value) {
543 $role[$key] = 1;
544 }
545 $this->_formValues['participant_role_id'] = $role;
546 }
547
548 $type = CRM_Utils_Request::retrieve('type', 'Positive',
549 CRM_Core_DAO::$_nullObject
550 );
551 if ($type) {
552 $this->_formValues['event_type'] = $type;
553 }
554
555 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
556
557 if ($cid) {
558 $cid = CRM_Utils_Type::escape($cid, 'Integer');
559 if ($cid > 0) {
560 $this->_formValues['contact_id'] = $cid;
561
562 // also assign individual mode to the template
563 $this->_single = TRUE;
564 }
565 }
566 }
567
568 function getFormValues() {
569 return NULL;
570 }
571
572 /**
573 * Return a descriptive name for the page, used in wizard header
574 *
575 * @return string
576 * @access public
577 */
578 public function getTitle() {
579 return ts('Find Participants');
580 }
581}
582