3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file is for civievent search
21 class CRM_Event_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query.
28 protected $_queryParams;
31 * Are we restricting ourselves to a single contact.
35 protected $_single = FALSE;
38 * Are we restricting ourselves to a single contact.
42 protected $_limit = NULL;
45 * Prefix for the controller.
48 protected $_prefix = "event_";
51 * The saved search ID retrieved from the GET vars.
58 * Metadata of all fields to include on the form.
62 protected $searchFieldMetadata = [];
65 * Get the default entity for the form.
69 public function getDefaultEntity() {
74 * Processing needed for buildForm and later.
78 * @throws \CRM_Core_Exception
79 * @throws \CiviCRM_API3_Exception
81 public function preProcess() {
82 $this->set('searchFormName', 'Search');
85 * set the button names
87 $this->_actionButtonName
= $this->getButtonName('next', 'action');
93 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
94 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
102 if ($this->_context
=== 'user') {
103 $prefix = $this->_prefix
;
106 $this->assign("{$prefix}limit", $this->_limit
);
107 $this->assign("{$prefix}single", $this->_single
);
109 $controller = new CRM_Core_Selector_Controller($selector,
110 $this->get(CRM_Utils_Pager
::PAGE_ID
),
112 CRM_Core_Action
::VIEW
,
114 CRM_Core_Selector_Controller
::TRANSFER
,
117 $controller->setEmbedded(TRUE);
118 $controller->moveFromSessionToTemplate();
120 $this->assign('summary', $this->get('summary'));
124 * Build the form object.
128 * @throws \CRM_Core_Exception
129 * @throws \CiviCRM_API3_Exception
131 public function buildQuickForm() {
132 parent
::buildQuickForm();
133 $this->addContactSearchFields();
135 CRM_Event_BAO_Query
::buildSearchForm($this);
137 $rows = $this->get('rows');
138 if (is_array($rows)) {
139 $lineItems = $eventIds = [];
140 if (!$this->_single
) {
141 $this->addRowSelectors($rows);
143 foreach ($rows as $row) {
144 $eventIds[$row['event_id']] = $row['event_id'];
145 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
146 // add line item details if applicable
147 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
151 //get actual count only when we are dealing w/ single event.
152 $participantCount = 0;
153 if (count($eventIds) == 1) {
154 //convert form values to clause.
156 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0') {
157 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
159 if (!empty($this->_formValues
['participant_status_id'])) {
160 $seatClause[] = CRM_Contact_BAO_Query
::buildClause("participant.status_id", 'IN', $this->_formValues
['participant_status_id'], 'Int');
161 if ($status = CRM_Utils_Array
::value('IN', $this->_formValues
['participant_status_id'])) {
162 $this->_formValues
['participant_status_id'] = $status;
165 if (!empty($this->_formValues
['participant_role_id'])) {
167 foreach ((array) $this->_formValues
['participant_role_id'] as $participantRole) {
168 $escapedRoles[] = CRM_Utils_Type
::escape($participantRole, 'String');
170 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
174 if (!empty($this->_formValues
['participant_fee_id'])) {
175 $participant_fee_id = $this->_formValues
['participant_fee_id'];
177 foreach ($participant_fee_id as $k => &$val) {
178 $val = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
179 $val_regexp[$k] = CRM_Core_DAO
::escapeString(preg_quote(trim($val)));
180 $val = CRM_Core_DAO
::escapeString(trim($val));
182 $feeLabel = implode('|', $val_regexp);
183 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
186 $seatClause = implode(' AND ', $seatClause);
187 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $seatClause);
189 $this->assign('participantCount', $participantCount);
190 $this->assign('lineItems', $lineItems);
192 $taskParams['ssID'] = $this->_ssID ??
NULL;
193 $tasks = CRM_Event_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission(), $taskParams);
195 if (isset($this->_ssID
)) {
196 $savedSearchValues = [
197 'id' => $this->_ssID
,
198 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
200 $this->assign_by_ref('savedSearch', $savedSearchValues);
201 $this->assign('ssID', $this->_ssID
);
204 $this->addTaskMenu($tasks);
210 * Get the label for the sortName field if email searching is on.
212 * (email searching is a setting under search preferences).
216 protected function getSortNameLabelWithEmail() {
217 return ts('Participant Name or Email');
221 * Get the label for the sortName field if email searching is off.
223 * (email searching is a setting under search preferences).
227 protected function getSortNameLabelWithOutEmail() {
228 return ts('Participant Name');
232 * Get the label for the tag field.
234 * We do this in a function so the 'ts' wraps the whole string to allow
235 * better translation.
239 protected function getTagLabel() {
240 return ts('Participant Tag(s)');
244 * Get the label for the group field.
248 protected function getGroupLabel() {
249 return ts('Participant Group(s)');
253 * Get the label for the group field.
257 protected function getContactTypeLabel() {
258 return ts('Participant Contact Type');
262 * Test submit the form.
265 public function testSubmit($formValues) {
266 $this->submit($formValues);
270 * Submit the search form with given values.
273 private function submit($formValues) {
274 $this->_formValues
= $formValues;
276 $this->fixFormValues();
278 if (isset($this->_ssID
) && empty($_POST)) {
279 // if we are editing / running a saved search and the form has not been posted
280 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
283 // We don't show test records in summaries or dashboards
284 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
285 $this->_formValues
["participant_test"] = 0;
288 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
290 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
292 $this->set('queryParams', $this->_queryParams
);
294 $buttonName = $this->controller
->getButtonName();
295 if ($buttonName == $this->_actionButtonName
) {
296 // check actionName and if next, then do not repeat a search, since we are going to the next page
298 // hack, make sure we reset the task values
299 $stateMachine = $this->controller
->getStateMachine();
300 $formName = $stateMachine->getTaskFormName();
301 $this->controller
->resetPage($formName);
305 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
313 $selector->setKey($this->controller
->_key
);
316 if ($this->_context
== 'user') {
317 $prefix = $this->_prefix
;
320 $this->assign("{$prefix}limit", $this->_limit
);
321 $this->assign("{$prefix}single", $this->_single
);
323 $controller = new CRM_Core_Selector_Controller($selector,
324 $this->get(CRM_Utils_Pager
::PAGE_ID
),
326 CRM_Core_Action
::VIEW
,
328 CRM_Core_Selector_Controller
::SESSION
,
331 $controller->setEmbedded(TRUE);
333 $query = $selector->getQuery();
334 if ($this->_context
== 'user') {
335 $query->setSkipPermission(TRUE);
341 * The post processing of the form gets done here.
343 * Key things done during post processing are
344 * - check for reset or next request. if present, skip post procesing.
345 * - now check if user requested running a saved search, if so, then
346 * the form values associated with the saved search are used for searching.
347 * - if user has done a submit with new values the regular post submissing is
349 * The processing consists of using a Selector / Controller framework for getting the
355 * @throws \CRM_Core_Exception
357 public function postProcess() {
363 $this->setFormValues();
365 $this->submit($this->_formValues
);
369 * add the rules (mainly global rules) for form.
370 * All local rules are added near the element
375 public function addRules() {
378 public function fixFormValues() {
379 // if this search has been forced
380 // then see if there are any get values, and if so over-ride the post values
381 // note that this means that GET over-rides POST :)
383 $role = CRM_Utils_Request
::retrieve('role', 'String');
386 if ($role === 'true') {
387 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
389 elseif ($role === 'false') {
390 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
392 elseif (is_numeric($role)) {
393 $roleTypes = (int) $role;
395 $this->_formValues
['participant_role_id'] = is_array($roleTypes) ?
array_keys($roleTypes) : $roleTypes;
398 $type = CRM_Utils_Request
::retrieve('type', 'Positive');
400 $this->_formValues
['event_type'] = $type;
403 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
406 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
408 $this->_formValues
['contact_id'] = $cid;
410 // also assign individual mode to the template
411 $this->_single
= TRUE;
417 * Return a descriptive name for the page, used in wizard header
421 public function getTitle() {
422 return ts('Find Participants');
426 * Set the metadata for the form.
428 * @throws \CiviCRM_API3_Exception
430 protected function setSearchMetadata() {
431 $this->addSearchFieldMetadata(['Participant' => CRM_Event_BAO_Query
::getSearchFieldMetadata()]);
435 * Set the default form values.
439 * the default array reference
440 * @throws \CRM_Core_Exception
442 public function setDefaultValues() {
443 $this->_defaults
= array_merge(parent
::setDefaultValues(), (array) $this->_formValues
);
444 $event = CRM_Utils_Request
::retrieve('event', 'Positive');
446 $this->_defaults
['event_id'] = $event;
447 $this->_defaults
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
450 $status = CRM_Utils_Request
::retrieve('status', 'String');
451 if (isset($status)) {
452 if ($status === 'true') {
453 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
455 elseif ($status === 'false') {
456 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
458 elseif (is_numeric($status)) {
459 $statusTypes = (int) $status;
461 elseif (is_array($status) && !array_key_exists('IN', $status)) {
462 $statusTypes = array_keys($status);
464 $this->_defaults
['participant_status_id'] = is_array($statusTypes) ?
array_keys($statusTypes) : $statusTypes;
466 return $this->_defaults
;