Merge pull request #16292 from eileenmcnaughton/522
[civicrm-core.git] / CRM / Event / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
6a488035
TO
18/**
19 * This file is for civievent search
20 */
3efb5b86 21class CRM_Event_Form_Search extends CRM_Core_Form_Search {
6a488035 22
6a488035 23 /**
66f9e52b 24 * The params that are sent to the query.
6a488035
TO
25 *
26 * @var array
6a488035
TO
27 */
28 protected $_queryParams;
29
6a488035 30 /**
66f9e52b 31 * Are we restricting ourselves to a single contact.
6a488035 32 *
d51c6add 33 * @var bool
6a488035
TO
34 */
35 protected $_single = FALSE;
36
37 /**
66f9e52b 38 * Are we restricting ourselves to a single contact.
6a488035 39 *
d51c6add 40 * @var bool
6a488035
TO
41 */
42 protected $_limit = NULL;
43
6a488035 44 /**
66f9e52b 45 * Prefix for the controller.
90b461f1 46 * @var string
6a488035
TO
47 */
48 protected $_prefix = "event_";
49
6a488035 50 /**
66f9e52b 51 * The saved search ID retrieved from the GET vars.
6a488035
TO
52 *
53 * @var int
6a488035
TO
54 */
55 protected $_ssID;
56
61b4d091 57 /**
58 * Metadata of all fields to include on the form.
59 *
60 * @var array
61 */
62 protected $searchFieldMetadata = [];
63
61b4d091 64 /**
8a6fde27 65 * Get the default entity for the form.
66 *
61b4d091 67 * @return string
68 */
69 public function getDefaultEntity() {
70 return 'Participant';
71 }
72
6a488035 73 /**
66f9e52b 74 * Processing needed for buildForm and later.
6a488035
TO
75 *
76 * @return void
1d81be90 77 *
78 * @throws \CRM_Core_Exception
79 * @throws \CiviCRM_API3_Exception
6a488035 80 */
00be9182 81 public function preProcess() {
6a488035
TO
82 $this->set('searchFormName', 'Search');
83
84 /**
85 * set the button names
86 */
87 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
88 $this->_actionButtonName = $this->getButtonName('next', 'action');
89
90 $this->_done = FALSE;
6a488035 91
1d81be90 92 parent::preProcess();
6a488035 93
be2fb01f 94 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
6a488035
TO
95 $selector = new CRM_Event_Selector_Search($this->_queryParams,
96 $this->_action,
97 NULL,
98 $this->_single,
99 $this->_limit,
100 $this->_context
101 );
102 $prefix = NULL;
103 if ($this->_context == 'user') {
104 $prefix = $this->_prefix;
105 }
106
107 $this->assign("{$prefix}limit", $this->_limit);
108 $this->assign("{$prefix}single", $this->_single);
109
110 $controller = new CRM_Core_Selector_Controller($selector,
111 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 112 $this->getSortID(),
6a488035
TO
113 CRM_Core_Action::VIEW,
114 $this,
115 CRM_Core_Selector_Controller::TRANSFER,
116 $prefix
117 );
118 $controller->setEmbedded(TRUE);
119 $controller->moveFromSessionToTemplate();
120
121 $this->assign('summary', $this->get('summary'));
122 }
123
124 /**
66f9e52b 125 * Build the form object.
6a488035 126 *
6a488035 127 * @return void
61b4d091 128 *
129 * @throws \CRM_Core_Exception
6a488035 130 */
00be9182 131 public function buildQuickForm() {
3efb5b86 132 parent::buildQuickForm();
0573fd28 133 $this->addContactSearchFields();
48242ab6 134
6a488035
TO
135 CRM_Event_BAO_Query::buildSearchForm($this);
136
6a488035
TO
137 $rows = $this->get('rows');
138 if (is_array($rows)) {
be2fb01f 139 $lineItems = $eventIds = [];
6a488035 140 if (!$this->_single) {
8d36b801 141 $this->addRowSelectors($rows);
6a488035
TO
142 }
143 foreach ($rows as $row) {
144 $eventIds[$row['event_id']] = $row['event_id'];
6a488035
TO
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']);
148 }
149 }
150
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.
be2fb01f 155 $seatClause = [];
884fdc63 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']} )";
158 }
159 if (!empty($this->_formValues['participant_status_id'])) {
f14010f5 160 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", 'IN', $this->_formValues['participant_status_id'], 'Int');
ef36db8b 161 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
162 $this->_formValues['participant_status_id'] = $status;
163 }
884fdc63 164 }
165 if (!empty($this->_formValues['participant_role_id'])) {
be2fb01f 166 $escapedRoles = [];
0906de17
MM
167 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
168 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
169 }
aba574d4 170 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
884fdc63 171 }
172
7c75c017 173 // CRM-15379
174 if (!empty($this->_formValues['participant_fee_id'])) {
175 $participant_fee_id = $this->_formValues['participant_fee_id'];
6bc9b1f6 176 $val_regexp = [];
f179424e 177 foreach ($participant_fee_id as $k => &$val) {
178 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
6bc9b1f6 179 $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val)));
f179424e 180 $val = CRM_Core_DAO::escapeString(trim($val));
181 }
6bc9b1f6 182 $feeLabel = implode('|', $val_regexp);
f179424e 183 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
6a488035 184 }
59adcdd9 185
884fdc63 186 $seatClause = implode(' AND ', $seatClause);
187 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
6a488035
TO
188 }
189 $this->assign('participantCount', $participantCount);
190 $this->assign('lineItems', $lineItems);
191
8fd26836
MW
192 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
193 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
6a488035 194
6a488035 195 if (isset($this->_ssID)) {
be2fb01f 196 $savedSearchValues = [
6a488035
TO
197 'id' => $this->_ssID,
198 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
be2fb01f 199 ];
6a488035
TO
200 $this->assign_by_ref('savedSearch', $savedSearchValues);
201 $this->assign('ssID', $this->_ssID);
202 }
203
34197a55 204 $this->addTaskMenu($tasks);
6a488035
TO
205 }
206
6a488035
TO
207 }
208
e597fc33
DG
209 /**
210 * Get the label for the sortName field if email searching is on.
211 *
212 * (email searching is a setting under search preferences).
213 *
214 * @return string
215 */
216 protected function getSortNameLabelWithEmail() {
217 return ts('Participant Name or Email');
218 }
219
220 /**
221 * Get the label for the sortName field if email searching is off.
222 *
223 * (email searching is a setting under search preferences).
224 *
225 * @return string
226 */
227 protected function getSortNameLabelWithOutEmail() {
228 return ts('Participant Name');
229 }
230
0573fd28 231 /**
232 * Get the label for the tag field.
233 *
234 * We do this in a function so the 'ts' wraps the whole string to allow
235 * better translation.
236 *
237 * @return string
238 */
239 protected function getTagLabel() {
240 return ts('Participant Tag(s)');
241 }
242
243 /**
244 * Get the label for the group field.
245 *
246 * @return string
247 */
248 protected function getGroupLabel() {
249 return ts('Participant Group(s)');
250 }
251
252 /**
253 * Get the label for the group field.
254 *
255 * @return string
256 */
257 protected function getContactTypeLabel() {
258 return ts('Participant Contact Type');
259 }
260
6a488035 261 /**
12cda498
AP
262 * Test submit the form.
263 * @param $formValues
6a488035 264 */
12cda498
AP
265 public function testSubmit($formValues) {
266 $this->submit($formValues);
267 }
6a488035 268
12cda498
AP
269 /**
270 * Submit the search form with given values.
271 * @param $formValues
272 */
273 private function submit($formValues) {
274 $this->_formValues = $formValues;
6a488035
TO
275
276 $this->fixFormValues();
277
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);
281 }
282
d43b88cc
CW
283 // We don't show test records in summaries or dashboards
284 if (empty($this->_formValues['participant_test']) && $this->_force) {
6a488035
TO
285 $this->_formValues["participant_test"] = 0;
286 }
287
c94d39fd 288 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035 289
be2fb01f 290 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
6a488035 291
6a488035
TO
292 $this->set('queryParams', $this->_queryParams);
293
294 $buttonName = $this->controller->getButtonName();
e341bbee 295 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
296 // check actionName and if next, then do not repeat a search, since we are going to the next page
297
298 // hack, make sure we reset the task values
a5611c8e 299 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
300 $formName = $stateMachine->getTaskFormName();
301 $this->controller->resetPage($formName);
302 return;
303 }
304
be2fb01f 305 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
6a488035
TO
306
307 $selector = new CRM_Event_Selector_Search($this->_queryParams,
308 $this->_action,
309 NULL,
310 $this->_single,
311 $this->_limit,
312 $this->_context
313 );
314
315 $selector->setKey($this->controller->_key);
316
317 $prefix = NULL;
318 if ($this->_context == 'user') {
319 $prefix = $this->_prefix;
320 }
321
322 $this->assign("{$prefix}limit", $this->_limit);
323 $this->assign("{$prefix}single", $this->_single);
324
325 $controller = new CRM_Core_Selector_Controller($selector,
326 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 327 $this->getSortID(),
6a488035
TO
328 CRM_Core_Action::VIEW,
329 $this,
330 CRM_Core_Selector_Controller::SESSION,
331 $prefix
332 );
333 $controller->setEmbedded(TRUE);
334
a5611c8e 335 $query = $selector->getQuery();
6a488035
TO
336 if ($this->_context == 'user') {
337 $query->setSkipPermission(TRUE);
338 }
339 $controller->run();
340 }
341
12cda498
AP
342 /**
343 * The post processing of the form gets done here.
344 *
345 * Key things done during post processing are
346 * - check for reset or next request. if present, skip post procesing.
347 * - now check if user requested running a saved search, if so, then
348 * the form values associated with the saved search are used for searching.
349 * - if user has done a submit with new values the regular post submissing is
350 * done.
351 * The processing consists of using a Selector / Controller framework for getting the
352 * search results.
353 *
354 * @param
355 *
356 * @return void
1d81be90 357 * @throws \CRM_Core_Exception
12cda498
AP
358 */
359 public function postProcess() {
360 if ($this->_done) {
361 return;
362 }
363
364 $this->_done = TRUE;
1d81be90 365 $formValues = $this->getFormValues();
12cda498
AP
366
367 $this->submit($formValues);
368 }
369
6a488035 370 /**
dc195289 371 * add the rules (mainly global rules) for form.
6a488035
TO
372 * All local rules are added near the element
373 *
355ba699 374 * @return void
6a488035
TO
375 * @see valid_date
376 */
6ea503d4
TO
377 public function addRules() {
378 }
6a488035 379
00be9182 380 public function fixFormValues() {
6a488035
TO
381 // if this search has been forced
382 // then see if there are any get values, and if so over-ride the post values
383 // note that this means that GET over-rides POST :)
6a488035 384
1273d77c 385 $role = CRM_Utils_Request::retrieve('role', 'String');
6a488035
TO
386
387 if (isset($role)) {
388 if ($role === 'true') {
4b191b48 389 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
6a488035
TO
390 }
391 elseif ($role === 'false') {
4b191b48 392 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
6a488035
TO
393 }
394 elseif (is_numeric($role)) {
53133bb0 395 $roleTypes = (int) $role;
6a488035 396 }
d5bd1f7e 397 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
53133bb0 398 }
399
1273d77c 400 $type = CRM_Utils_Request::retrieve('type', 'Positive');
6a488035
TO
401 if ($type) {
402 $this->_formValues['event_type'] = $type;
403 }
404
405 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
406
407 if ($cid) {
408 $cid = CRM_Utils_Type::escape($cid, 'Integer');
409 if ($cid > 0) {
410 $this->_formValues['contact_id'] = $cid;
411
412 // also assign individual mode to the template
413 $this->_single = TRUE;
414 }
415 }
416 }
417
6a488035
TO
418 /**
419 * Return a descriptive name for the page, used in wizard header
420 *
421 * @return string
6a488035
TO
422 */
423 public function getTitle() {
424 return ts('Find Participants');
425 }
96025800 426
18db97df 427 /**
428 * Set the metadata for the form.
429 *
430 * @throws \CiviCRM_API3_Exception
431 */
432 protected function setSearchMetadata() {
433 $this->addSearchFieldMetadata(['Participant' => CRM_Event_BAO_Query::getSearchFieldMetadata()]);
434 }
435
1126bdc5
SL
436 /**
437 * Set the default form values.
438 *
439 *
440 * @return array
441 * the default array reference
442 */
443 public function setDefaultValues() {
5f3ff0ef
SL
444 $this->_defaults = parent::setDefaultValues();
445 $event = CRM_Utils_Request::retrieve('event', 'Positive');
446 if ($event) {
447 $this->_defaults['event_id'] = $event;
448 $this->_defaults['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
449 }
450
451 $status = CRM_Utils_Request::retrieve('status', 'String');
452 if (isset($status)) {
453 if ($status === 'true') {
454 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
455 }
456 elseif ($status === 'false') {
457 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
458 }
459 elseif (is_numeric($status)) {
460 $statusTypes = (int) $status;
461 }
462 elseif (is_array($status) && !array_key_exists('IN', $status)) {
463 $statusTypes = array_keys($status);
464 }
465 $this->_defaults['participant_status_id'] = is_array($statusTypes) ? array_keys($statusTypes) : $statusTypes;
466 }
467 return $this->_defaults;
1126bdc5
SL
468 }
469
6a488035 470}