3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
41 * This file is for civievent search
43 class CRM_Event_Form_Search
extends CRM_Core_Form
{
46 * Are we forced to run a search
54 * name of search button
59 protected $_searchButtonName;
62 * name of action button
67 protected $_actionButtonName;
70 * form values that we will be using
75 protected $_formValues;
78 * the params that are sent to the query
83 protected $_queryParams;
86 * have we already done this search
94 * are we restricting ourselves to a single contact
99 protected $_single = FALSE;
102 * are we restricting ourselves to a single contact
107 protected $_limit = NULL;
110 * what context are we being invoked from
115 protected $_context = NULL;
118 * prefix for the controller
121 protected $_prefix = "event_";
123 protected $_defaults;
126 * the saved search ID retrieved from the GET vars
134 * processing needed for buildForm and later
139 function preProcess() {
140 $this->set('searchFormName', 'Search');
143 * set the button names
145 $this->_searchButtonName
= $this->getButtonName('refresh');
146 $this->_actionButtonName
= $this->getButtonName('next', 'action');
148 $this->_done
= FALSE;
149 $this->defaults
= array();
152 * we allow the controller to set force/reset externally, useful when we are being
153 * driven by the wizard framework
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
);
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
);
168 $this->_formValues
= $this->get('formValues');
171 if (empty($this->_formValues
)) {
172 if (isset($this->_ssID
)) {
173 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
178 $this->postProcess();
179 $this->set('force', 0);
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
)
189 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
190 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
198 if ($this->_context
== 'user') {
199 $prefix = $this->_prefix
;
202 $this->assign("{$prefix}limit", $this->_limit
);
203 $this->assign("{$prefix}single", $this->_single
);
205 $controller = new CRM_Core_Selector_Controller($selector,
206 $this->get(CRM_Utils_Pager
::PAGE_ID
),
208 CRM_Core_Action
::VIEW
,
210 CRM_Core_Selector_Controller
::TRANSFER
,
213 $controller->setEmbedded(TRUE);
214 $controller->moveFromSessionToTemplate();
216 $this->assign('summary', $this->get('summary'));
226 function buildQuickForm() {
227 $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
229 CRM_Event_BAO_Query
::buildSearchForm($this);
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
235 $rows = $this->get('rows');
236 if (is_array($rows)) {
237 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js');
238 $lineItems = $eventIds = array();
239 if (!$this->_single
) {
240 $this->addElement('checkbox',
244 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')
247 foreach ($rows as $row) {
248 $eventIds[$row['event_id']] = $row['event_id'];
249 if (!$this->_single
) {
250 $this->addElement('checkbox', $row['checkbox'],
252 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
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']);
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();
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']} )";
270 if (!empty($this->_formValues
['participant_status_id'])) {
271 $statuses = array_keys($this->_formValues
['participant_status_id']);
272 $seatClause[] = '( participant.status_id IN ( ' . implode(' , ', $statuses) . ' ) )';
274 if (!empty($this->_formValues
['participant_role_id'])) {
275 $roles = array_keys($this->_formValues
['participant_role_id']);
276 $seatClause[] = '( participant.role_id IN ( ' . implode(' , ', $roles) . ' ) )';
279 if (!empty($seatClause)) {
280 $clause = implode(' AND ', $seatClause);
283 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $clause);
285 $this->assign('participantCount', $participantCount);
286 $this->assign('lineItems', $lineItems);
288 $total = $cancel = 0;
290 $permission = CRM_Core_Permission
::getPermission();
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();
298 $savedSearchValues = array(
299 'id' => $this->_ssID
,
300 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
302 $this->assign_by_ref('savedSearch', $savedSearchValues);
303 $this->assign('ssID', $this->_ssID
);
306 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
307 $this->add('submit', $this->_actionButtonName
, ts('Go'),
309 'class' => 'form-submit',
311 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
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')
319 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all',
320 array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")
325 $this->addButtons(array(
328 'name' => ts('Search'),
335 * The post processing of the form gets done here.
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
343 * The processing consists of using a Selector / Controller framework for getting the
351 function postProcess() {
358 if (!empty($_POST)) {
359 $this->_formValues
= $this->controller
->exportValues($this->_name
);
362 if (empty($this->_formValues
)) {
363 $this->_formValues
= $this->controller
->exportValues($this->_name
);
366 $this->fixFormValues();
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
);
373 // We don't show test records in summaries or dashboards
374 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
375 $this->_formValues
["participant_test"] = 0;
378 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
380 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
382 $this->set('formValues', $this->_formValues
);
383 $this->set('queryParams', $this->_queryParams
);
385 $buttonName = $this->controller
->getButtonName();
386 if ($buttonName == $this->_actionButtonName
) {
387 // check actionName and if next, then do not repeat a search, since we are going to the next page
389 // hack, make sure we reset the task values
390 $stateMachine = $this->controller
->getStateMachine();
391 $formName = $stateMachine->getTaskFormName();
392 $this->controller
->resetPage($formName);
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
)
403 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
405 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
413 $selector->setKey($this->controller
->_key
);
416 if ($this->_context
== 'user') {
417 $prefix = $this->_prefix
;
420 $this->assign("{$prefix}limit", $this->_limit
);
421 $this->assign("{$prefix}single", $this->_single
);
423 $controller = new CRM_Core_Selector_Controller($selector,
424 $this->get(CRM_Utils_Pager
::PAGE_ID
),
426 CRM_Core_Action
::VIEW
,
428 CRM_Core_Selector_Controller
::SESSION
,
431 $controller->setEmbedded(TRUE);
433 $query = $selector->getQuery();
434 if ($this->_context
== 'user') {
435 $query->setSkipPermission(TRUE);
441 * This function is used to add the rules (mainly global rules) for form.
442 * All local rules are added near the element
448 function addRules() {
449 $this->addFormRule(array('CRM_Event_Form_Search', 'formRule'));
453 * global validation rules for the form
455 * @param array $fields posted values of the form
456 * @param array $errors list of errors to be posted back to the form
462 static function formRule($fields) {
465 if ($fields['event_name'] && !is_numeric($fields['event_id'])) {
466 $errors['event_id'] = ts('Please select valid event.');
469 if ($fields['event_type'] && !is_numeric($fields['event_type_id'])) {
470 $errors['event_type'] = ts('Please select valid event type.');
472 if (!empty($errors)) {
480 * Set the default form values
484 * @return array the default array reference
486 function setDefaultValues() {
488 $defaults = $this->_formValues
;
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
500 $this->_formValues
['event_id'] = $event;
501 $this->_formValues
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
504 $status = CRM_Utils_Request
::retrieve('status', 'String',
505 CRM_Core_DAO
::$_nullObject
508 if (isset($status)) {
509 if ($status === 'true') {
510 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
512 elseif ($status === 'false') {
513 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
515 elseif (is_numeric($status)) {
516 $status = (int) $status;
517 $statusTypes = array($status => CRM_Event_PseudoConstant
::participantStatus($status));
520 foreach ($statusTypes as $key => $value) {
523 $this->_formValues
['participant_status_id'] = $status;
526 $role = CRM_Utils_Request
::retrieve('role', 'String',
527 CRM_Core_DAO
::$_nullObject
531 if ($role === 'true') {
532 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
534 elseif ($role === 'false') {
535 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
537 elseif (is_numeric($role)) {
539 $roleTypes = array($role => CRM_Event_PseudoConstant
::participantRole($role));
542 foreach ($roleTypes as $key => $value) {
545 $this->_formValues
['participant_role_id'] = $role;
548 $type = CRM_Utils_Request
::retrieve('type', 'Positive',
549 CRM_Core_DAO
::$_nullObject
552 $this->_formValues
['event_type'] = $type;
555 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
558 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
560 $this->_formValues
['contact_id'] = $cid;
562 // also assign individual mode to the template
563 $this->_single
= TRUE;
568 function getFormValues() {
573 * Return a descriptive name for the page, used in wizard header
578 public function getTitle() {
579 return ts('Find Participants');