3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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.
68 protected $_prefix = "event_";
71 * The saved search ID retrieved from the GET vars.
78 * Processing needed for buildForm and later.
82 public function preProcess() {
83 $this->set('searchFormName', 'Search');
86 * set the button names
88 $this->_searchButtonName
= $this->getButtonName('refresh');
89 $this->_actionButtonName
= $this->getButtonName('next', 'action');
93 $this->loadStandardSearchOptionsFromUrl();
94 $this->loadFormValues();
98 $this->set('force', 0);
102 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
103 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
104 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
108 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
109 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
117 if ($this->_context
== 'user') {
118 $prefix = $this->_prefix
;
121 $this->assign("{$prefix}limit", $this->_limit
);
122 $this->assign("{$prefix}single", $this->_single
);
124 $controller = new CRM_Core_Selector_Controller($selector,
125 $this->get(CRM_Utils_Pager
::PAGE_ID
),
127 CRM_Core_Action
::VIEW
,
129 CRM_Core_Selector_Controller
::TRANSFER
,
132 $controller->setEmbedded(TRUE);
133 $controller->moveFromSessionToTemplate();
135 $this->assign('summary', $this->get('summary'));
139 * Build the form object.
144 public function buildQuickForm() {
145 parent
::buildQuickForm();
146 $this->addContactSearchFields();
148 CRM_Event_BAO_Query
::buildSearchForm($this);
150 $rows = $this->get('rows');
151 if (is_array($rows)) {
152 $lineItems = $eventIds = [];
153 if (!$this->_single
) {
154 $this->addRowSelectors($rows);
156 foreach ($rows as $row) {
157 $eventIds[$row['event_id']] = $row['event_id'];
158 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
159 // add line item details if applicable
160 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
164 //get actual count only when we are dealing w/ single event.
165 $participantCount = 0;
166 if (count($eventIds) == 1) {
167 //convert form values to clause.
169 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0') {
170 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
172 if (!empty($this->_formValues
['participant_status_id'])) {
173 $seatClause[] = CRM_Contact_BAO_Query
::buildClause("participant.status_id", 'IN', $this->_formValues
['participant_status_id'], 'Int');
174 if ($status = CRM_Utils_Array
::value('IN', $this->_formValues
['participant_status_id'])) {
175 $this->_formValues
['participant_status_id'] = $status;
178 if (!empty($this->_formValues
['participant_role_id'])) {
180 foreach ((array) $this->_formValues
['participant_role_id'] as $participantRole) {
181 $escapedRoles[] = CRM_Utils_Type
::escape($participantRole, 'String');
183 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
187 if (!empty($this->_formValues
['participant_fee_id'])) {
188 $participant_fee_id = $this->_formValues
['participant_fee_id'];
190 foreach ($participant_fee_id as $k => &$val) {
191 $val = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
192 $val_regexp[$k] = CRM_Core_DAO
::escapeString(preg_quote(trim($val)));
193 $val = CRM_Core_DAO
::escapeString(trim($val));
195 $feeLabel = implode('|', $val_regexp);
196 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
199 $seatClause = implode(' AND ', $seatClause);
200 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $seatClause);
202 $this->assign('participantCount', $participantCount);
203 $this->assign('lineItems', $lineItems);
205 $taskParams['ssID'] = isset($this->_ssID
) ?
$this->_ssID
: NULL;
206 $tasks = CRM_Event_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission(), $taskParams);
208 if (isset($this->_ssID
)) {
209 $savedSearchValues = [
210 'id' => $this->_ssID
,
211 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
213 $this->assign_by_ref('savedSearch', $savedSearchValues);
214 $this->assign('ssID', $this->_ssID
);
217 $this->addTaskMenu($tasks);
223 * Get the label for the sortName field if email searching is on.
225 * (email searching is a setting under search preferences).
229 protected function getSortNameLabelWithEmail() {
230 return ts('Participant Name or Email');
234 * Get the label for the sortName field if email searching is off.
236 * (email searching is a setting under search preferences).
240 protected function getSortNameLabelWithOutEmail() {
241 return ts('Participant Name');
245 * Get the label for the tag field.
247 * We do this in a function so the 'ts' wraps the whole string to allow
248 * better translation.
252 protected function getTagLabel() {
253 return ts('Participant Tag(s)');
257 * Get the label for the group field.
261 protected function getGroupLabel() {
262 return ts('Participant Group(s)');
266 * Get the label for the group field.
270 protected function getContactTypeLabel() {
271 return ts('Participant Contact Type');
275 * Test submit the form.
278 public function testSubmit($formValues) {
279 $this->submit($formValues);
283 * Submit the search form with given values.
286 private function submit($formValues) {
287 $this->_formValues
= $formValues;
289 $this->fixFormValues();
291 if (isset($this->_ssID
) && empty($_POST)) {
292 // if we are editing / running a saved search and the form has not been posted
293 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
296 // We don't show test records in summaries or dashboards
297 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
298 $this->_formValues
["participant_test"] = 0;
301 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
303 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
305 $this->set('formValues', $this->_formValues
);
306 $this->set('queryParams', $this->_queryParams
);
308 $buttonName = $this->controller
->getButtonName();
309 if ($buttonName == $this->_actionButtonName
) {
310 // check actionName and if next, then do not repeat a search, since we are going to the next page
312 // hack, make sure we reset the task values
313 $stateMachine = $this->controller
->getStateMachine();
314 $formName = $stateMachine->getTaskFormName();
315 $this->controller
->resetPage($formName);
320 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
321 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
322 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
326 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
328 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
336 $selector->setKey($this->controller
->_key
);
339 if ($this->_context
== 'user') {
340 $prefix = $this->_prefix
;
343 $this->assign("{$prefix}limit", $this->_limit
);
344 $this->assign("{$prefix}single", $this->_single
);
346 $controller = new CRM_Core_Selector_Controller($selector,
347 $this->get(CRM_Utils_Pager
::PAGE_ID
),
349 CRM_Core_Action
::VIEW
,
351 CRM_Core_Selector_Controller
::SESSION
,
354 $controller->setEmbedded(TRUE);
356 $query = $selector->getQuery();
357 if ($this->_context
== 'user') {
358 $query->setSkipPermission(TRUE);
364 * The post processing of the form gets done here.
366 * Key things done during post processing are
367 * - check for reset or next request. if present, skip post procesing.
368 * - now check if user requested running a saved search, if so, then
369 * the form values associated with the saved search are used for searching.
370 * - if user has done a submit with new values the regular post submissing is
372 * The processing consists of using a Selector / Controller framework for getting the
379 public function postProcess() {
387 if (!empty($_POST)) {
388 $formValues = $this->controller
->exportValues($this->_name
);
389 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, ['participant_status_id']);
392 if (empty($this->_formValues
)) {
393 $formValues = $this->controller
->exportValues($this->_name
);
396 $this->submit($formValues);
400 * add the rules (mainly global rules) for form.
401 * All local rules are added near the element
406 public function addRules() {
410 * Set the default form values.
414 * the default array reference
416 public function setDefaultValues() {
418 $defaults = $this->_formValues
;
422 public function fixFormValues() {
423 // if this search has been forced
424 // then see if there are any get values, and if so over-ride the post values
425 // note that this means that GET over-rides POST :)
426 $event = CRM_Utils_Request
::retrieve('event', 'Positive');
428 $this->_formValues
['event_id'] = $event;
429 $this->_formValues
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
432 $status = CRM_Utils_Request
::retrieve('status', 'String');
434 if (isset($status)) {
435 if ($status === 'true') {
436 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
438 elseif ($status === 'false') {
439 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
441 elseif (is_numeric($status)) {
442 $statusTypes = (int) $status;
444 elseif (is_array($status) && !array_key_exists('IN', $status)) {
445 $statusTypes = array_keys($status);
447 $this->_formValues
['participant_status_id'] = is_array($statusTypes) ?
['IN' => array_keys($statusTypes)] : $statusTypes;
450 $role = CRM_Utils_Request
::retrieve('role', 'String');
453 if ($role === 'true') {
454 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
456 elseif ($role === 'false') {
457 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
459 elseif (is_numeric($role)) {
460 $roleTypes = (int) $role;
462 $this->_formValues
['participant_role_id'] = is_array($roleTypes) ?
array_keys($roleTypes) : $roleTypes;
465 $type = CRM_Utils_Request
::retrieve('type', 'Positive');
467 $this->_formValues
['event_type'] = $type;
470 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
473 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
475 $this->_formValues
['contact_id'] = $cid;
477 // also assign individual mode to the template
478 $this->_single
= TRUE;
486 public function getFormValues() {
491 * Return a descriptive name for the page, used in wizard header
495 public function getTitle() {
496 return ts('Find Participants');