3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
39 * This file is for civievent search
41 class CRM_Event_Form_Search
extends CRM_Core_Form_Search
{
44 * The params that are sent to the query.
48 protected $_queryParams;
51 * Are we restricting ourselves to a single contact.
55 protected $_single = FALSE;
58 * Are we restricting ourselves to a single contact.
62 protected $_limit = NULL;
65 * Prefix for the controller.
67 protected $_prefix = "event_";
70 * The saved search ID retrieved from the GET vars.
77 * Processing needed for buildForm and later.
81 public function preProcess() {
82 $this->set('searchFormName', 'Search');
85 * set the button names
87 $this->_searchButtonName
= $this->getButtonName('refresh');
88 $this->_actionButtonName
= $this->getButtonName('next', 'action');
91 $this->defaults
= array();
94 * we allow the controller to set force/reset externally, useful when we are being
95 * driven by the wizard framework
97 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean');
98 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
99 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
100 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
101 $this->_ssID
= CRM_Utils_Request
::retrieve('ssID', 'Positive', $this);
102 $this->assign("context", $this->_context
);
104 // get user submitted values
105 // get it from controller only if form has been submitted, else preProcess has set this
106 if (!empty($_POST) && !$this->controller
->isModal()) {
107 $this->_formValues
= $this->controller
->exportValues($this->_name
);
110 $this->_formValues
= $this->get('formValues');
113 if (empty($this->_formValues
)) {
114 if (isset($this->_ssID
)) {
115 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
120 $this->postProcess();
121 $this->set('force', 0);
125 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
126 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
127 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
131 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, array('event_id'));
132 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
140 if ($this->_context
== 'user') {
141 $prefix = $this->_prefix
;
144 $this->assign("{$prefix}limit", $this->_limit
);
145 $this->assign("{$prefix}single", $this->_single
);
147 $controller = new CRM_Core_Selector_Controller($selector,
148 $this->get(CRM_Utils_Pager
::PAGE_ID
),
150 CRM_Core_Action
::VIEW
,
152 CRM_Core_Selector_Controller
::TRANSFER
,
155 $controller->setEmbedded(TRUE);
156 $controller->moveFromSessionToTemplate();
158 $this->assign('summary', $this->get('summary'));
162 * Build the form object.
167 public function buildQuickForm() {
168 parent
::buildQuickForm();
169 $this->addContactSearchFields();
171 CRM_Event_BAO_Query
::buildSearchForm($this);
173 $rows = $this->get('rows');
174 if (is_array($rows)) {
175 $lineItems = $eventIds = array();
176 if (!$this->_single
) {
177 $this->addRowSelectors($rows);
179 foreach ($rows as $row) {
180 $eventIds[$row['event_id']] = $row['event_id'];
181 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
182 // add line item details if applicable
183 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
187 //get actual count only when we are dealing w/ single event.
188 $participantCount = 0;
189 if (count($eventIds) == 1) {
190 //convert form values to clause.
191 $seatClause = array();
192 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0') {
193 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
195 if (!empty($this->_formValues
['participant_status_id'])) {
196 $seatClause[] = CRM_Contact_BAO_Query
::buildClause("participant.status_id", '=', $this->_formValues
['participant_status_id'], 'Int');
197 if ($status = CRM_Utils_Array
::value('IN', $this->_formValues
['participant_status_id'])) {
198 $this->_formValues
['participant_status_id'] = $status;
201 if (!empty($this->_formValues
['participant_role_id'])) {
202 $escapedRoles = array();
203 foreach ((array) $this->_formValues
['participant_role_id'] as $participantRole) {
204 $escapedRoles[] = CRM_Utils_Type
::escape($participantRole, 'String');
206 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
210 if (!empty($this->_formValues
['participant_fee_id'])) {
211 $participant_fee_id = $this->_formValues
['participant_fee_id'];
212 foreach ($participant_fee_id as $k => &$val) {
213 $val = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
214 $val = CRM_Core_DAO
::escapeString(trim($val));
216 $feeLabel = implode('|', $participant_fee_id);
217 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
220 $seatClause = implode(' AND ', $seatClause);
221 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $seatClause);
223 $this->assign('participantCount', $participantCount);
224 $this->assign('lineItems', $lineItems);
226 $taskParams['ssID'] = isset($this->_ssID
) ?
$this->_ssID
: NULL;
227 $tasks = CRM_Event_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission(), $taskParams);
229 if (isset($this->_ssID
)) {
230 $savedSearchValues = array(
231 'id' => $this->_ssID
,
232 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
234 $this->assign_by_ref('savedSearch', $savedSearchValues);
235 $this->assign('ssID', $this->_ssID
);
238 $this->addTaskMenu($tasks);
244 * Get the label for the sortName field if email searching is on.
246 * (email searching is a setting under search preferences).
250 protected function getSortNameLabelWithEmail() {
251 return ts('Participant Name or Email');
255 * Get the label for the sortName field if email searching is off.
257 * (email searching is a setting under search preferences).
261 protected function getSortNameLabelWithOutEmail() {
262 return ts('Participant Name');
266 * Get the label for the tag field.
268 * We do this in a function so the 'ts' wraps the whole string to allow
269 * better translation.
273 protected function getTagLabel() {
274 return ts('Participant Tag(s)');
278 * Get the label for the group field.
282 protected function getGroupLabel() {
283 return ts('Participant Group(s)');
287 * Get the label for the group field.
291 protected function getContactTypeLabel() {
292 return ts('Participant Contact Type');
296 * The post processing of the form gets done here.
298 * Key things done during post processing are
299 * - check for reset or next request. if present, skip post procesing.
300 * - now check if user requested running a saved search, if so, then
301 * the form values associated with the saved search are used for searching.
302 * - if user has done a submit with new values the regular post submissing is
304 * The processing consists of using a Selector / Controller framework for getting the
311 public function postProcess() {
318 if (!empty($_POST)) {
319 $this->_formValues
= $this->controller
->exportValues($this->_name
);
320 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, array('participant_status_id'));
323 if (empty($this->_formValues
)) {
324 $this->_formValues
= $this->controller
->exportValues($this->_name
);
327 $this->fixFormValues();
329 if (isset($this->_ssID
) && empty($_POST)) {
330 // if we are editing / running a saved search and the form has not been posted
331 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
334 // We don't show test records in summaries or dashboards
335 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
336 $this->_formValues
["participant_test"] = 0;
339 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
341 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, array('event_id'));
343 $this->set('formValues', $this->_formValues
);
344 $this->set('queryParams', $this->_queryParams
);
346 $buttonName = $this->controller
->getButtonName();
347 if ($buttonName == $this->_actionButtonName
) {
348 // check actionName and if next, then do not repeat a search, since we are going to the next page
350 // hack, make sure we reset the task values
351 $stateMachine = $this->controller
->getStateMachine();
352 $formName = $stateMachine->getTaskFormName();
353 $this->controller
->resetPage($formName);
358 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
359 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
360 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
364 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, array('event_id'));
366 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
374 $selector->setKey($this->controller
->_key
);
377 if ($this->_context
== 'user') {
378 $prefix = $this->_prefix
;
381 $this->assign("{$prefix}limit", $this->_limit
);
382 $this->assign("{$prefix}single", $this->_single
);
384 $controller = new CRM_Core_Selector_Controller($selector,
385 $this->get(CRM_Utils_Pager
::PAGE_ID
),
387 CRM_Core_Action
::VIEW
,
389 CRM_Core_Selector_Controller
::SESSION
,
392 $controller->setEmbedded(TRUE);
394 $query = $selector->getQuery();
395 if ($this->_context
== 'user') {
396 $query->setSkipPermission(TRUE);
402 * add the rules (mainly global rules) for form.
403 * All local rules are added near the element
408 public function addRules() {
412 * Set the default form values.
416 * the default array reference
418 public function setDefaultValues() {
420 $defaults = $this->_formValues
;
424 public function fixFormValues() {
425 // if this search has been forced
426 // then see if there are any get values, and if so over-ride the post values
427 // note that this means that GET over-rides POST :)
428 $event = CRM_Utils_Request
::retrieve('event', 'Positive');
430 $this->_formValues
['event_id'] = $event;
431 $this->_formValues
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
434 $status = CRM_Utils_Request
::retrieve('status', 'String');
436 if (isset($status)) {
437 if ($status === 'true') {
438 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
440 elseif ($status === 'false') {
441 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
443 elseif (is_numeric($status)) {
444 $statusTypes = (int) $status;
446 elseif (is_array($status) && !array_key_exists('IN', $status)) {
447 $statusTypes = array_keys($status);
449 $this->_formValues
['participant_status_id'] = is_array($statusTypes) ?
array('IN' => array_keys($statusTypes)) : $statusTypes;
452 $role = CRM_Utils_Request
::retrieve('role', 'String');
455 if ($role === 'true') {
456 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
458 elseif ($role === 'false') {
459 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
461 elseif (is_numeric($role)) {
462 $roleTypes = (int) $role;
464 $this->_formValues
['participant_role_id'] = is_array($roleTypes) ?
array_keys($roleTypes) : $roleTypes;
467 $type = CRM_Utils_Request
::retrieve('type', 'Positive');
469 $this->_formValues
['event_type'] = $type;
472 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
475 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
477 $this->_formValues
['contact_id'] = $cid;
479 // also assign individual mode to the template
480 $this->_single
= TRUE;
488 public function getFormValues() {
493 * Return a descriptive name for the page, used in wizard header
497 public function getTitle() {
498 return ts('Find Participants');