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);
114 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
115 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
116 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
120 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
121 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
129 if ($this->_context
== 'user') {
130 $prefix = $this->_prefix
;
133 $this->assign("{$prefix}limit", $this->_limit
);
134 $this->assign("{$prefix}single", $this->_single
);
136 $controller = new CRM_Core_Selector_Controller($selector,
137 $this->get(CRM_Utils_Pager
::PAGE_ID
),
139 CRM_Core_Action
::VIEW
,
141 CRM_Core_Selector_Controller
::TRANSFER
,
144 $controller->setEmbedded(TRUE);
145 $controller->moveFromSessionToTemplate();
147 $this->assign('summary', $this->get('summary'));
151 * Build the form object.
155 * @throws \CRM_Core_Exception
157 public function buildQuickForm() {
158 parent
::buildQuickForm();
159 $this->addContactSearchFields();
161 CRM_Event_BAO_Query
::buildSearchForm($this);
163 $rows = $this->get('rows');
164 if (is_array($rows)) {
165 $lineItems = $eventIds = [];
166 if (!$this->_single
) {
167 $this->addRowSelectors($rows);
169 foreach ($rows as $row) {
170 $eventIds[$row['event_id']] = $row['event_id'];
171 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
172 // add line item details if applicable
173 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
177 //get actual count only when we are dealing w/ single event.
178 $participantCount = 0;
179 if (count($eventIds) == 1) {
180 //convert form values to clause.
182 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0') {
183 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
185 if (!empty($this->_formValues
['participant_status_id'])) {
186 $seatClause[] = CRM_Contact_BAO_Query
::buildClause("participant.status_id", 'IN', $this->_formValues
['participant_status_id'], 'Int');
187 if ($status = CRM_Utils_Array
::value('IN', $this->_formValues
['participant_status_id'])) {
188 $this->_formValues
['participant_status_id'] = $status;
191 if (!empty($this->_formValues
['participant_role_id'])) {
193 foreach ((array) $this->_formValues
['participant_role_id'] as $participantRole) {
194 $escapedRoles[] = CRM_Utils_Type
::escape($participantRole, 'String');
196 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
200 if (!empty($this->_formValues
['participant_fee_id'])) {
201 $participant_fee_id = $this->_formValues
['participant_fee_id'];
203 foreach ($participant_fee_id as $k => &$val) {
204 $val = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
205 $val_regexp[$k] = CRM_Core_DAO
::escapeString(preg_quote(trim($val)));
206 $val = CRM_Core_DAO
::escapeString(trim($val));
208 $feeLabel = implode('|', $val_regexp);
209 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
212 $seatClause = implode(' AND ', $seatClause);
213 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $seatClause);
215 $this->assign('participantCount', $participantCount);
216 $this->assign('lineItems', $lineItems);
218 $taskParams['ssID'] = isset($this->_ssID
) ?
$this->_ssID
: NULL;
219 $tasks = CRM_Event_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission(), $taskParams);
221 if (isset($this->_ssID
)) {
222 $savedSearchValues = [
223 'id' => $this->_ssID
,
224 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
226 $this->assign_by_ref('savedSearch', $savedSearchValues);
227 $this->assign('ssID', $this->_ssID
);
230 $this->addTaskMenu($tasks);
236 * Get the label for the sortName field if email searching is on.
238 * (email searching is a setting under search preferences).
242 protected function getSortNameLabelWithEmail() {
243 return ts('Participant Name or Email');
247 * Get the label for the sortName field if email searching is off.
249 * (email searching is a setting under search preferences).
253 protected function getSortNameLabelWithOutEmail() {
254 return ts('Participant Name');
258 * Get the label for the tag field.
260 * We do this in a function so the 'ts' wraps the whole string to allow
261 * better translation.
265 protected function getTagLabel() {
266 return ts('Participant Tag(s)');
270 * Get the label for the group field.
274 protected function getGroupLabel() {
275 return ts('Participant Group(s)');
279 * Get the label for the group field.
283 protected function getContactTypeLabel() {
284 return ts('Participant Contact Type');
288 * Test submit the form.
291 public function testSubmit($formValues) {
292 $this->submit($formValues);
296 * Submit the search form with given values.
299 private function submit($formValues) {
300 $this->_formValues
= $formValues;
302 $this->fixFormValues();
304 if (isset($this->_ssID
) && empty($_POST)) {
305 // if we are editing / running a saved search and the form has not been posted
306 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
309 // We don't show test records in summaries or dashboards
310 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
311 $this->_formValues
["participant_test"] = 0;
314 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
316 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
318 $this->set('formValues', $this->_formValues
);
319 $this->set('queryParams', $this->_queryParams
);
321 $buttonName = $this->controller
->getButtonName();
322 if ($buttonName == $this->_actionButtonName
) {
323 // check actionName and if next, then do not repeat a search, since we are going to the next page
325 // hack, make sure we reset the task values
326 $stateMachine = $this->controller
->getStateMachine();
327 $formName = $stateMachine->getTaskFormName();
328 $this->controller
->resetPage($formName);
333 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
334 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
335 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
339 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
341 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
349 $selector->setKey($this->controller
->_key
);
352 if ($this->_context
== 'user') {
353 $prefix = $this->_prefix
;
356 $this->assign("{$prefix}limit", $this->_limit
);
357 $this->assign("{$prefix}single", $this->_single
);
359 $controller = new CRM_Core_Selector_Controller($selector,
360 $this->get(CRM_Utils_Pager
::PAGE_ID
),
362 CRM_Core_Action
::VIEW
,
364 CRM_Core_Selector_Controller
::SESSION
,
367 $controller->setEmbedded(TRUE);
369 $query = $selector->getQuery();
370 if ($this->_context
== 'user') {
371 $query->setSkipPermission(TRUE);
377 * The post processing of the form gets done here.
379 * Key things done during post processing are
380 * - check for reset or next request. if present, skip post procesing.
381 * - now check if user requested running a saved search, if so, then
382 * the form values associated with the saved search are used for searching.
383 * - if user has done a submit with new values the regular post submissing is
385 * The processing consists of using a Selector / Controller framework for getting the
392 public function postProcess() {
400 if (!empty($_POST)) {
401 $formValues = $this->controller
->exportValues($this->_name
);
402 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, ['participant_status_id']);
405 if (empty($formValues)) {
406 $formValues = $this->controller
->exportValues($this->_name
);
409 $this->submit($formValues);
413 * add the rules (mainly global rules) for form.
414 * All local rules are added near the element
419 public function addRules() {
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');