3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
35 * This file is for civievent search
37 class CRM_Event_Form_Search
extends CRM_Core_Form_Search
{
40 * The params that are sent to the query.
44 protected $_queryParams;
47 * Are we restricting ourselves to a single contact.
51 protected $_single = FALSE;
54 * Are we restricting ourselves to a single contact.
58 protected $_limit = NULL;
61 * Prefix for the controller.
64 protected $_prefix = "event_";
67 * The saved search ID retrieved from the GET vars.
74 * Metadata of all fields to include on the form.
78 protected $searchFieldMetadata = [];
81 * Get the default entity for the form.
85 public function getDefaultEntity() {
90 * Processing needed for buildForm and later.
94 public function preProcess() {
95 $this->set('searchFormName', 'Search');
98 * set the button names
100 $this->_searchButtonName
= $this->getButtonName('refresh');
101 $this->_actionButtonName
= $this->getButtonName('next', 'action');
103 $this->_done
= FALSE;
105 $this->loadStandardSearchOptionsFromUrl();
106 $this->loadFormValues();
109 $this->postProcess();
110 $this->set('force', 0);
113 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
114 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
122 if ($this->_context
== 'user') {
123 $prefix = $this->_prefix
;
126 $this->assign("{$prefix}limit", $this->_limit
);
127 $this->assign("{$prefix}single", $this->_single
);
129 $controller = new CRM_Core_Selector_Controller($selector,
130 $this->get(CRM_Utils_Pager
::PAGE_ID
),
132 CRM_Core_Action
::VIEW
,
134 CRM_Core_Selector_Controller
::TRANSFER
,
137 $controller->setEmbedded(TRUE);
138 $controller->moveFromSessionToTemplate();
140 $this->assign('summary', $this->get('summary'));
144 * Build the form object.
148 * @throws \CRM_Core_Exception
150 public function buildQuickForm() {
151 parent
::buildQuickForm();
152 $this->addContactSearchFields();
154 CRM_Event_BAO_Query
::buildSearchForm($this);
156 $rows = $this->get('rows');
157 if (is_array($rows)) {
158 $lineItems = $eventIds = [];
159 if (!$this->_single
) {
160 $this->addRowSelectors($rows);
162 foreach ($rows as $row) {
163 $eventIds[$row['event_id']] = $row['event_id'];
164 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
165 // add line item details if applicable
166 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
170 //get actual count only when we are dealing w/ single event.
171 $participantCount = 0;
172 if (count($eventIds) == 1) {
173 //convert form values to clause.
175 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0') {
176 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
178 if (!empty($this->_formValues
['participant_status_id'])) {
179 $seatClause[] = CRM_Contact_BAO_Query
::buildClause("participant.status_id", 'IN', $this->_formValues
['participant_status_id'], 'Int');
180 if ($status = CRM_Utils_Array
::value('IN', $this->_formValues
['participant_status_id'])) {
181 $this->_formValues
['participant_status_id'] = $status;
184 if (!empty($this->_formValues
['participant_role_id'])) {
186 foreach ((array) $this->_formValues
['participant_role_id'] as $participantRole) {
187 $escapedRoles[] = CRM_Utils_Type
::escape($participantRole, 'String');
189 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
193 if (!empty($this->_formValues
['participant_fee_id'])) {
194 $participant_fee_id = $this->_formValues
['participant_fee_id'];
196 foreach ($participant_fee_id as $k => &$val) {
197 $val = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
198 $val_regexp[$k] = CRM_Core_DAO
::escapeString(preg_quote(trim($val)));
199 $val = CRM_Core_DAO
::escapeString(trim($val));
201 $feeLabel = implode('|', $val_regexp);
202 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
205 $seatClause = implode(' AND ', $seatClause);
206 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $seatClause);
208 $this->assign('participantCount', $participantCount);
209 $this->assign('lineItems', $lineItems);
211 $taskParams['ssID'] = isset($this->_ssID
) ?
$this->_ssID
: NULL;
212 $tasks = CRM_Event_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission(), $taskParams);
214 if (isset($this->_ssID
)) {
215 $savedSearchValues = [
216 'id' => $this->_ssID
,
217 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
219 $this->assign_by_ref('savedSearch', $savedSearchValues);
220 $this->assign('ssID', $this->_ssID
);
223 $this->addTaskMenu($tasks);
229 * Get the label for the sortName field if email searching is on.
231 * (email searching is a setting under search preferences).
235 protected function getSortNameLabelWithEmail() {
236 return ts('Participant Name or Email');
240 * Get the label for the sortName field if email searching is off.
242 * (email searching is a setting under search preferences).
246 protected function getSortNameLabelWithOutEmail() {
247 return ts('Participant Name');
251 * Get the label for the tag field.
253 * We do this in a function so the 'ts' wraps the whole string to allow
254 * better translation.
258 protected function getTagLabel() {
259 return ts('Participant Tag(s)');
263 * Get the label for the group field.
267 protected function getGroupLabel() {
268 return ts('Participant Group(s)');
272 * Get the label for the group field.
276 protected function getContactTypeLabel() {
277 return ts('Participant Contact Type');
281 * Test submit the form.
284 public function testSubmit($formValues) {
285 $this->submit($formValues);
289 * Submit the search form with given values.
292 private function submit($formValues) {
293 $this->_formValues
= $formValues;
295 $this->fixFormValues();
297 if (isset($this->_ssID
) && empty($_POST)) {
298 // if we are editing / running a saved search and the form has not been posted
299 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
302 // We don't show test records in summaries or dashboards
303 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
304 $this->_formValues
["participant_test"] = 0;
307 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
309 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
311 $this->set('formValues', $this->_formValues
);
312 $this->set('queryParams', $this->_queryParams
);
314 $buttonName = $this->controller
->getButtonName();
315 if ($buttonName == $this->_actionButtonName
) {
316 // check actionName and if next, then do not repeat a search, since we are going to the next page
318 // hack, make sure we reset the task values
319 $stateMachine = $this->controller
->getStateMachine();
320 $formName = $stateMachine->getTaskFormName();
321 $this->controller
->resetPage($formName);
325 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
327 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
335 $selector->setKey($this->controller
->_key
);
338 if ($this->_context
== 'user') {
339 $prefix = $this->_prefix
;
342 $this->assign("{$prefix}limit", $this->_limit
);
343 $this->assign("{$prefix}single", $this->_single
);
345 $controller = new CRM_Core_Selector_Controller($selector,
346 $this->get(CRM_Utils_Pager
::PAGE_ID
),
348 CRM_Core_Action
::VIEW
,
350 CRM_Core_Selector_Controller
::SESSION
,
353 $controller->setEmbedded(TRUE);
355 $query = $selector->getQuery();
356 if ($this->_context
== 'user') {
357 $query->setSkipPermission(TRUE);
363 * The post processing of the form gets done here.
365 * Key things done during post processing are
366 * - check for reset or next request. if present, skip post procesing.
367 * - now check if user requested running a saved search, if so, then
368 * the form values associated with the saved search are used for searching.
369 * - if user has done a submit with new values the regular post submissing is
371 * The processing consists of using a Selector / Controller framework for getting the
378 public function postProcess() {
386 if (!empty($_POST)) {
387 $formValues = $this->controller
->exportValues($this->_name
);
388 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, ['participant_status_id']);
391 if (empty($formValues)) {
392 $formValues = $this->controller
->exportValues($this->_name
);
395 $this->submit($formValues);
399 * add the rules (mainly global rules) for form.
400 * All local rules are added near the element
405 public function addRules() {
408 public function fixFormValues() {
409 // if this search has been forced
410 // then see if there are any get values, and if so over-ride the post values
411 // note that this means that GET over-rides POST :)
412 $event = CRM_Utils_Request
::retrieve('event', 'Positive');
414 $this->_formValues
['event_id'] = $event;
415 $this->_formValues
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
418 $status = CRM_Utils_Request
::retrieve('status', 'String');
420 if (isset($status)) {
421 if ($status === 'true') {
422 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
424 elseif ($status === 'false') {
425 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
427 elseif (is_numeric($status)) {
428 $statusTypes = (int) $status;
430 elseif (is_array($status) && !array_key_exists('IN', $status)) {
431 $statusTypes = array_keys($status);
433 $this->_formValues
['participant_status_id'] = is_array($statusTypes) ?
['IN' => array_keys($statusTypes)] : $statusTypes;
436 $role = CRM_Utils_Request
::retrieve('role', 'String');
439 if ($role === 'true') {
440 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
442 elseif ($role === 'false') {
443 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
445 elseif (is_numeric($role)) {
446 $roleTypes = (int) $role;
448 $this->_formValues
['participant_role_id'] = is_array($roleTypes) ?
array_keys($roleTypes) : $roleTypes;
451 $type = CRM_Utils_Request
::retrieve('type', 'Positive');
453 $this->_formValues
['event_type'] = $type;
456 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
459 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
461 $this->_formValues
['contact_id'] = $cid;
463 // also assign individual mode to the template
464 $this->_single
= TRUE;
472 public function getFormValues() {
477 * Return a descriptive name for the page, used in wizard header
481 public function getTitle() {
482 return ts('Find Participants');