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 print button
67 protected $_printButtonName;
70 * name of action button
75 protected $_actionButtonName;
78 * form values that we will be using
83 protected $_formValues;
86 * the params that are sent to the query
91 protected $_queryParams;
94 * have we already done this search
102 * are we restricting ourselves to a single contact
107 protected $_single = FALSE;
110 * are we restricting ourselves to a single contact
115 protected $_limit = NULL;
118 * what context are we being invoked from
123 protected $_context = NULL;
126 * prefix for the controller
129 protected $_prefix = "event_";
131 protected $_defaults;
134 * the saved search ID retrieved from the GET vars
142 * processing needed for buildForm and later
147 function preProcess() {
148 $this->set('searchFormName', 'Search');
151 * set the button names
153 $this->_searchButtonName
= $this->getButtonName('refresh');
154 $this->_printButtonName
= $this->getButtonName('next', 'print');
155 $this->_actionButtonName
= $this->getButtonName('next', 'action');
157 $this->_done
= FALSE;
158 $this->defaults
= array();
161 * we allow the controller to set force/reset externally, useful when we are being
162 * driven by the wizard framework
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
);
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
);
177 $this->_formValues
= $this->get('formValues');
180 if (empty($this->_formValues
)) {
181 if (isset($this->_ssID
)) {
182 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
187 $this->postProcess();
188 $this->set('force', 0);
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
)
198 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
199 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
207 if ($this->_context
== 'user') {
208 $prefix = $this->_prefix
;
211 $this->assign("{$prefix}limit", $this->_limit
);
212 $this->assign("{$prefix}single", $this->_single
);
214 $controller = new CRM_Core_Selector_Controller($selector,
215 $this->get(CRM_Utils_Pager
::PAGE_ID
),
217 CRM_Core_Action
::VIEW
,
219 CRM_Core_Selector_Controller
::TRANSFER
,
222 $controller->setEmbedded(TRUE);
223 $controller->moveFromSessionToTemplate();
225 $this->assign('summary', $this->get('summary'));
235 function buildQuickForm() {
236 $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
238 CRM_Event_BAO_Query
::buildSearchForm($this);
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
244 $rows = $this->get('rows');
245 if (is_array($rows)) {
246 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
247 $lineItems = $eventIds = array();
248 if (!$this->_single
) {
249 $this->addElement('checkbox',
253 array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);")
256 foreach ($rows as $row) {
257 $eventIds[$row['event_id']] = $row['event_id'];
258 if (!$this->_single
) {
259 $this->addElement('checkbox', $row['checkbox'],
261 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
264 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
265 // add line item details if applicable
266 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
270 //get actual count only when we are dealing w/ single event.
271 $participantCount = 0;
272 if (count($eventIds) == 1) {
273 //convert form values to clause.
274 $seatClause = array();
275 // Filter on is_test if specified in search form
276 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0' ) {
277 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
279 if (!empty($this->_formValues
['participant_status_id'])) {
280 $statuses = array_keys($this->_formValues
['participant_status_id']);
281 $seatClause[] = '( participant.status_id IN ( ' . implode(' , ', $statuses) . ' ) )';
283 if (!empty($this->_formValues
['participant_role_id'])) {
284 $roles = array_keys($this->_formValues
['participant_role_id']);
285 $seatClause[] = '( participant.role_id IN ( ' . implode(' , ', $roles) . ' ) )';
288 if (!empty($seatClause)) {
289 $clause = implode(' AND ', $seatClause);
292 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $clause);
294 $this->assign('participantCount', $participantCount);
295 $this->assign('lineItems', $lineItems);
297 $total = $cancel = 0;
299 $permission = CRM_Core_Permission
::getPermission();
301 $tasks = array('' => ts('- actions -')) + CRM_Event_Task
::permissionedTaskTitles($permission);
302 if (isset($this->_ssID
)) {
303 if ($permission == CRM_Core_Permission
::EDIT
) {
304 $tasks = $tasks + CRM_Event_Task
::optionalTaskTitle();
307 $savedSearchValues = array(
308 'id' => $this->_ssID
,
309 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
311 $this->assign_by_ref('savedSearch', $savedSearchValues);
312 $this->assign('ssID', $this->_ssID
);
315 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
316 $this->add('submit', $this->_actionButtonName
, ts('Go'),
318 'class' => 'form-submit',
320 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
324 $this->add('submit', $this->_printButtonName
, ts('Print'),
326 'class' => 'form-submit',
327 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
331 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
332 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel',
333 array('checked' => 'checked')
335 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all',
336 array('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );")
341 $this->addButtons(array(
344 'name' => ts('Search'),
351 * The post processing of the form gets done here.
353 * Key things done during post processing are
354 * - check for reset or next request. if present, skip post procesing.
355 * - now check if user requested running a saved search, if so, then
356 * the form values associated with the saved search are used for searching.
357 * - if user has done a submit with new values the regular post submissing is
359 * The processing consists of using a Selector / Controller framework for getting the
367 function postProcess() {
374 if (!empty($_POST)) {
375 $this->_formValues
= $this->controller
->exportValues($this->_name
);
378 if (empty($this->_formValues
)) {
379 $this->_formValues
= $this->controller
->exportValues($this->_name
);
382 $this->fixFormValues();
384 if (isset($this->_ssID
) && empty($_POST)) {
385 // if we are editing / running a saved search and the form has not been posted
386 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
389 // We don't show test records in summaries or dashboards
390 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
391 $this->_formValues
["participant_test"] = 0;
394 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
396 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
398 $this->set('formValues', $this->_formValues
);
399 $this->set('queryParams', $this->_queryParams
);
401 $buttonName = $this->controller
->getButtonName();
402 if ($buttonName == $this->_actionButtonName ||
$buttonName == $this->_printButtonName
) {
403 // check actionName and if next, then do not repeat a search, since we are going to the next page
405 // hack, make sure we reset the task values
406 $stateMachine = $this->controller
->getStateMachine();
407 $formName = $stateMachine->getTaskFormName();
408 $this->controller
->resetPage($formName);
413 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
414 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
415 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
419 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
421 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
429 $selector->setKey($this->controller
->_key
);
432 if ($this->_context
== 'user') {
433 $prefix = $this->_prefix
;
436 $this->assign("{$prefix}limit", $this->_limit
);
437 $this->assign("{$prefix}single", $this->_single
);
439 $controller = new CRM_Core_Selector_Controller($selector,
440 $this->get(CRM_Utils_Pager
::PAGE_ID
),
442 CRM_Core_Action
::VIEW
,
444 CRM_Core_Selector_Controller
::SESSION
,
447 $controller->setEmbedded(TRUE);
449 $query = $selector->getQuery();
450 if ($this->_context
== 'user') {
451 $query->setSkipPermission(TRUE);
457 * This function is used to add the rules (mainly global rules) for form.
458 * All local rules are added near the element
464 function addRules() {
465 $this->addFormRule(array('CRM_Event_Form_Search', 'formRule'));
469 * global validation rules for the form
471 * @param array $fields posted values of the form
472 * @param array $errors list of errors to be posted back to the form
478 static function formRule($fields) {
481 if ($fields['event_name'] && !is_numeric($fields['event_id'])) {
482 $errors['event_id'] = ts('Please select valid event.');
485 if ($fields['event_type'] && !is_numeric($fields['event_type_id'])) {
486 $errors['event_type'] = ts('Please select valid event type.');
488 if (!empty($errors)) {
496 * Set the default form values
500 * @return array the default array reference
502 function setDefaultValues() {
504 $defaults = $this->_formValues
;
508 function fixFormValues() {
509 // if this search has been forced
510 // then see if there are any get values, and if so over-ride the post values
511 // note that this means that GET over-rides POST :)
512 $event = CRM_Utils_Request
::retrieve('event', 'Positive',
513 CRM_Core_DAO
::$_nullObject
516 $this->_formValues
['event_id'] = $event;
517 $this->_formValues
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
520 $status = CRM_Utils_Request
::retrieve('status', 'String',
521 CRM_Core_DAO
::$_nullObject
524 if (isset($status)) {
525 if ($status === 'true') {
526 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
528 elseif ($status === 'false') {
529 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
531 elseif (is_numeric($status)) {
532 $status = (int) $status;
533 $statusTypes = array($status => CRM_Event_PseudoConstant
::participantStatus($status));
536 foreach ($statusTypes as $key => $value) {
539 $this->_formValues
['participant_status_id'] = $status;
542 $role = CRM_Utils_Request
::retrieve('role', 'String',
543 CRM_Core_DAO
::$_nullObject
547 if ($role === 'true') {
548 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
550 elseif ($role === 'false') {
551 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
553 elseif (is_numeric($role)) {
555 $roleTypes = array($role => CRM_Event_PseudoConstant
::participantRole($role));
558 foreach ($roleTypes as $key => $value) {
561 $this->_formValues
['participant_role_id'] = $role;
564 $type = CRM_Utils_Request
::retrieve('type', 'Positive',
565 CRM_Core_DAO
::$_nullObject
568 $this->_formValues
['event_type'] = $type;
571 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
574 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
576 $this->_formValues
['contact_id'] = $cid;
578 // also assign individual mode to the template
579 $this->_single
= TRUE;
584 function getFormValues() {
589 * Return a descriptive name for the page, used in wizard header
594 public function getTitle() {
595 return ts('Find Participants');