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 * @throws \CRM_Core_Exception
95 * @throws \CiviCRM_API3_Exception
97 public function preProcess() {
98 $this->set('searchFormName', 'Search');
101 * set the button names
103 $this->_searchButtonName
= $this->getButtonName('refresh');
104 $this->_actionButtonName
= $this->getButtonName('next', 'action');
106 $this->_done
= FALSE;
108 parent
::preProcess();
110 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
111 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
119 if ($this->_context
== 'user') {
120 $prefix = $this->_prefix
;
123 $this->assign("{$prefix}limit", $this->_limit
);
124 $this->assign("{$prefix}single", $this->_single
);
126 $controller = new CRM_Core_Selector_Controller($selector,
127 $this->get(CRM_Utils_Pager
::PAGE_ID
),
129 CRM_Core_Action
::VIEW
,
131 CRM_Core_Selector_Controller
::TRANSFER
,
134 $controller->setEmbedded(TRUE);
135 $controller->moveFromSessionToTemplate();
137 $this->assign('summary', $this->get('summary'));
141 * Build the form object.
145 * @throws \CRM_Core_Exception
147 public function buildQuickForm() {
148 parent
::buildQuickForm();
149 $this->addContactSearchFields();
151 CRM_Event_BAO_Query
::buildSearchForm($this);
153 $rows = $this->get('rows');
154 if (is_array($rows)) {
155 $lineItems = $eventIds = [];
156 if (!$this->_single
) {
157 $this->addRowSelectors($rows);
159 foreach ($rows as $row) {
160 $eventIds[$row['event_id']] = $row['event_id'];
161 if (CRM_Event_BAO_Event
::usesPriceSet($row['event_id'])) {
162 // add line item details if applicable
163 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem
::getLineItems($row['participant_id']);
167 //get actual count only when we are dealing w/ single event.
168 $participantCount = 0;
169 if (count($eventIds) == 1) {
170 //convert form values to clause.
172 if (CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '1' || CRM_Utils_Array
::value('participant_test', $this->_formValues
) == '0') {
173 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
175 if (!empty($this->_formValues
['participant_status_id'])) {
176 $seatClause[] = CRM_Contact_BAO_Query
::buildClause("participant.status_id", 'IN', $this->_formValues
['participant_status_id'], 'Int');
177 if ($status = CRM_Utils_Array
::value('IN', $this->_formValues
['participant_status_id'])) {
178 $this->_formValues
['participant_status_id'] = $status;
181 if (!empty($this->_formValues
['participant_role_id'])) {
183 foreach ((array) $this->_formValues
['participant_role_id'] as $participantRole) {
184 $escapedRoles[] = CRM_Utils_Type
::escape($participantRole, 'String');
186 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
190 if (!empty($this->_formValues
['participant_fee_id'])) {
191 $participant_fee_id = $this->_formValues
['participant_fee_id'];
193 foreach ($participant_fee_id as $k => &$val) {
194 $val = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
195 $val_regexp[$k] = CRM_Core_DAO
::escapeString(preg_quote(trim($val)));
196 $val = CRM_Core_DAO
::escapeString(trim($val));
198 $feeLabel = implode('|', $val_regexp);
199 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
202 $seatClause = implode(' AND ', $seatClause);
203 $participantCount = CRM_Event_BAO_Event
::eventTotalSeats(array_pop($eventIds), $seatClause);
205 $this->assign('participantCount', $participantCount);
206 $this->assign('lineItems', $lineItems);
208 $taskParams['ssID'] = isset($this->_ssID
) ?
$this->_ssID
: NULL;
209 $tasks = CRM_Event_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission(), $taskParams);
211 if (isset($this->_ssID
)) {
212 $savedSearchValues = [
213 'id' => $this->_ssID
,
214 'name' => CRM_Contact_BAO_SavedSearch
::getName($this->_ssID
, 'title'),
216 $this->assign_by_ref('savedSearch', $savedSearchValues);
217 $this->assign('ssID', $this->_ssID
);
220 $this->addTaskMenu($tasks);
226 * Get the label for the sortName field if email searching is on.
228 * (email searching is a setting under search preferences).
232 protected function getSortNameLabelWithEmail() {
233 return ts('Participant Name or Email');
237 * Get the label for the sortName field if email searching is off.
239 * (email searching is a setting under search preferences).
243 protected function getSortNameLabelWithOutEmail() {
244 return ts('Participant Name');
248 * Get the label for the tag field.
250 * We do this in a function so the 'ts' wraps the whole string to allow
251 * better translation.
255 protected function getTagLabel() {
256 return ts('Participant Tag(s)');
260 * Get the label for the group field.
264 protected function getGroupLabel() {
265 return ts('Participant Group(s)');
269 * Get the label for the group field.
273 protected function getContactTypeLabel() {
274 return ts('Participant Contact Type');
278 * Test submit the form.
281 public function testSubmit($formValues) {
282 $this->submit($formValues);
286 * Submit the search form with given values.
289 private function submit($formValues) {
290 $this->_formValues
= $formValues;
292 $this->fixFormValues();
294 if (isset($this->_ssID
) && empty($_POST)) {
295 // if we are editing / running a saved search and the form has not been posted
296 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
299 // We don't show test records in summaries or dashboards
300 if (empty($this->_formValues
['participant_test']) && $this->_force
) {
301 $this->_formValues
["participant_test"] = 0;
304 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
306 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
308 $this->set('formValues', $this->_formValues
);
309 $this->set('queryParams', $this->_queryParams
);
311 $buttonName = $this->controller
->getButtonName();
312 if ($buttonName == $this->_actionButtonName
) {
313 // check actionName and if next, then do not repeat a search, since we are going to the next page
315 // hack, make sure we reset the task values
316 $stateMachine = $this->controller
->getStateMachine();
317 $formName = $stateMachine->getTaskFormName();
318 $this->controller
->resetPage($formName);
322 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, ['event_id']);
324 $selector = new CRM_Event_Selector_Search($this->_queryParams
,
332 $selector->setKey($this->controller
->_key
);
335 if ($this->_context
== 'user') {
336 $prefix = $this->_prefix
;
339 $this->assign("{$prefix}limit", $this->_limit
);
340 $this->assign("{$prefix}single", $this->_single
);
342 $controller = new CRM_Core_Selector_Controller($selector,
343 $this->get(CRM_Utils_Pager
::PAGE_ID
),
345 CRM_Core_Action
::VIEW
,
347 CRM_Core_Selector_Controller
::SESSION
,
350 $controller->setEmbedded(TRUE);
352 $query = $selector->getQuery();
353 if ($this->_context
== 'user') {
354 $query->setSkipPermission(TRUE);
360 * The post processing of the form gets done here.
362 * Key things done during post processing are
363 * - check for reset or next request. if present, skip post procesing.
364 * - now check if user requested running a saved search, if so, then
365 * the form values associated with the saved search are used for searching.
366 * - if user has done a submit with new values the regular post submissing is
368 * The processing consists of using a Selector / Controller framework for getting the
374 * @throws \CRM_Core_Exception
376 public function postProcess() {
382 $formValues = $this->getFormValues();
384 $this->submit($formValues);
388 * add the rules (mainly global rules) for form.
389 * All local rules are added near the element
394 public function addRules() {
397 public function fixFormValues() {
398 // if this search has been forced
399 // then see if there are any get values, and if so over-ride the post values
400 // note that this means that GET over-rides POST :)
401 $event = CRM_Utils_Request
::retrieve('event', 'Positive');
403 $this->_formValues
['event_id'] = $event;
404 $this->_formValues
['event_name'] = CRM_Event_PseudoConstant
::event($event, TRUE);
407 $status = CRM_Utils_Request
::retrieve('status', 'String');
409 if (isset($status)) {
410 if ($status === 'true') {
411 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 1");
413 elseif ($status === 'false') {
414 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, "is_counted = 0");
416 elseif (is_numeric($status)) {
417 $statusTypes = (int) $status;
419 elseif (is_array($status) && !array_key_exists('IN', $status)) {
420 $statusTypes = array_keys($status);
422 $this->_formValues
['participant_status_id'] = is_array($statusTypes) ?
['IN' => array_keys($statusTypes)] : $statusTypes;
425 $role = CRM_Utils_Request
::retrieve('role', 'String');
428 if ($role === 'true') {
429 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 1");
431 elseif ($role === 'false') {
432 $roleTypes = CRM_Event_PseudoConstant
::participantRole(NULL, "filter = 0");
434 elseif (is_numeric($role)) {
435 $roleTypes = (int) $role;
437 $this->_formValues
['participant_role_id'] = is_array($roleTypes) ?
array_keys($roleTypes) : $roleTypes;
440 $type = CRM_Utils_Request
::retrieve('type', 'Positive');
442 $this->_formValues
['event_type'] = $type;
445 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
448 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
450 $this->_formValues
['contact_id'] = $cid;
452 // also assign individual mode to the template
453 $this->_single
= TRUE;
459 * Return a descriptive name for the page, used in wizard header
463 public function getTitle() {
464 return ts('Find Participants');
468 * Set the metadata for the form.
470 * @throws \CiviCRM_API3_Exception
472 protected function setSearchMetadata() {
473 $this->addSearchFieldMetadata(['Participant' => CRM_Event_BAO_Query
::getSearchFieldMetadata()]);