Merge pull request #23902 from civicrm/5.51
[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() {
2d09a0c3 82 // SearchFormName is deprecated & to be removed - the replacement is for the task to
83 // call $this->form->getSearchFormValues()
84 // A couple of extensions use it.
6a488035
TO
85 $this->set('searchFormName', 'Search');
86
87 /**
88 * set the button names
89 */
6a488035
TO
90 $this->_actionButtonName = $this->getButtonName('next', 'action');
91
92 $this->_done = FALSE;
6a488035 93
1d81be90 94 parent::preProcess();
6a488035 95
be2fb01f 96 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
6a488035
TO
97 $selector = new CRM_Event_Selector_Search($this->_queryParams,
98 $this->_action,
99 NULL,
100 $this->_single,
101 $this->_limit,
102 $this->_context
103 );
104 $prefix = NULL;
86f20cc1 105 if ($this->_context === 'user') {
6a488035
TO
106 $prefix = $this->_prefix;
107 }
108
109 $this->assign("{$prefix}limit", $this->_limit);
110 $this->assign("{$prefix}single", $this->_single);
111
112 $controller = new CRM_Core_Selector_Controller($selector,
113 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 114 $this->getSortID(),
6a488035
TO
115 CRM_Core_Action::VIEW,
116 $this,
117 CRM_Core_Selector_Controller::TRANSFER,
118 $prefix
119 );
120 $controller->setEmbedded(TRUE);
121 $controller->moveFromSessionToTemplate();
122
123 $this->assign('summary', $this->get('summary'));
124 }
125
126 /**
66f9e52b 127 * Build the form object.
6a488035 128 *
6a488035 129 * @return void
61b4d091 130 *
131 * @throws \CRM_Core_Exception
86f20cc1 132 * @throws \CiviCRM_API3_Exception
6a488035 133 */
00be9182 134 public function buildQuickForm() {
3efb5b86 135 parent::buildQuickForm();
0573fd28 136 $this->addContactSearchFields();
48242ab6 137
6a488035
TO
138 CRM_Event_BAO_Query::buildSearchForm($this);
139
6a488035
TO
140 $rows = $this->get('rows');
141 if (is_array($rows)) {
be2fb01f 142 $lineItems = $eventIds = [];
6a488035 143 if (!$this->_single) {
8d36b801 144 $this->addRowSelectors($rows);
6a488035
TO
145 }
146 foreach ($rows as $row) {
147 $eventIds[$row['event_id']] = $row['event_id'];
6a488035
TO
148 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
149 // add line item details if applicable
150 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
151 }
152 }
153
154 //get actual count only when we are dealing w/ single event.
155 $participantCount = 0;
156 if (count($eventIds) == 1) {
157 //convert form values to clause.
be2fb01f 158 $seatClause = [];
884fdc63 159 if (CRM_Utils_Array::value('participant_test', $this->_formValues) == '1' || CRM_Utils_Array::value('participant_test', $this->_formValues) == '0') {
160 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
161 }
162 if (!empty($this->_formValues['participant_status_id'])) {
f14010f5 163 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", 'IN', $this->_formValues['participant_status_id'], 'Int');
ef36db8b 164 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
165 $this->_formValues['participant_status_id'] = $status;
166 }
884fdc63 167 }
168 if (!empty($this->_formValues['participant_role_id'])) {
be2fb01f 169 $escapedRoles = [];
0906de17
MM
170 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
171 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
172 }
aba574d4 173 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
884fdc63 174 }
175
7c75c017 176 // CRM-15379
177 if (!empty($this->_formValues['participant_fee_id'])) {
178 $participant_fee_id = $this->_formValues['participant_fee_id'];
6bc9b1f6 179 $val_regexp = [];
f179424e 180 foreach ($participant_fee_id as $k => &$val) {
181 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
6bc9b1f6 182 $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val)));
f179424e 183 $val = CRM_Core_DAO::escapeString(trim($val));
184 }
6bc9b1f6 185 $feeLabel = implode('|', $val_regexp);
f179424e 186 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
6a488035 187 }
59adcdd9 188
884fdc63 189 $seatClause = implode(' AND ', $seatClause);
190 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
6a488035
TO
191 }
192 $this->assign('participantCount', $participantCount);
193 $this->assign('lineItems', $lineItems);
194
2e1f50d6 195 $taskParams['ssID'] = $this->_ssID ?? NULL;
8fd26836 196 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
6a488035 197
6a488035 198 if (isset($this->_ssID)) {
be2fb01f 199 $savedSearchValues = [
6a488035
TO
200 'id' => $this->_ssID,
201 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
be2fb01f 202 ];
6a488035 203 }
461c8a93
EM
204 $this->assign('savedSearch', $savedSearchValues ?? NULL);
205 $this->assign('ssID', $this->_ssID);
6a488035 206
34197a55 207 $this->addTaskMenu($tasks);
6a488035
TO
208 }
209
6a488035
TO
210 }
211
e597fc33
DG
212 /**
213 * Get the label for the sortName field if email searching is on.
214 *
215 * (email searching is a setting under search preferences).
216 *
217 * @return string
218 */
219 protected function getSortNameLabelWithEmail() {
220 return ts('Participant Name or Email');
221 }
222
223 /**
224 * Get the label for the sortName field if email searching is off.
225 *
226 * (email searching is a setting under search preferences).
227 *
228 * @return string
229 */
230 protected function getSortNameLabelWithOutEmail() {
231 return ts('Participant Name');
232 }
233
0573fd28 234 /**
235 * Get the label for the tag field.
236 *
237 * We do this in a function so the 'ts' wraps the whole string to allow
238 * better translation.
239 *
240 * @return string
241 */
242 protected function getTagLabel() {
243 return ts('Participant Tag(s)');
244 }
245
246 /**
247 * Get the label for the group field.
248 *
249 * @return string
250 */
251 protected function getGroupLabel() {
252 return ts('Participant Group(s)');
253 }
254
255 /**
256 * Get the label for the group field.
257 *
258 * @return string
259 */
260 protected function getContactTypeLabel() {
261 return ts('Participant Contact Type');
262 }
263
6a488035 264 /**
12cda498
AP
265 * Test submit the form.
266 * @param $formValues
6a488035 267 */
12cda498
AP
268 public function testSubmit($formValues) {
269 $this->submit($formValues);
270 }
6a488035 271
12cda498
AP
272 /**
273 * Submit the search form with given values.
274 * @param $formValues
275 */
276 private function submit($formValues) {
277 $this->_formValues = $formValues;
6a488035
TO
278
279 $this->fixFormValues();
280
281 if (isset($this->_ssID) && empty($_POST)) {
282 // if we are editing / running a saved search and the form has not been posted
283 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
284 }
285
d43b88cc
CW
286 // We don't show test records in summaries or dashboards
287 if (empty($this->_formValues['participant_test']) && $this->_force) {
6a488035
TO
288 $this->_formValues["participant_test"] = 0;
289 }
290
be2fb01f 291 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
6a488035 292
6a488035
TO
293 $this->set('queryParams', $this->_queryParams);
294
295 $buttonName = $this->controller->getButtonName();
e341bbee 296 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
297 // check actionName and if next, then do not repeat a search, since we are going to the next page
298
299 // hack, make sure we reset the task values
a5611c8e 300 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
301 $formName = $stateMachine->getTaskFormName();
302 $this->controller->resetPage($formName);
303 return;
304 }
305
6a488035
TO
306 $selector = new CRM_Event_Selector_Search($this->_queryParams,
307 $this->_action,
308 NULL,
309 $this->_single,
310 $this->_limit,
311 $this->_context
312 );
313
314 $selector->setKey($this->controller->_key);
315
316 $prefix = NULL;
317 if ($this->_context == 'user') {
318 $prefix = $this->_prefix;
319 }
320
321 $this->assign("{$prefix}limit", $this->_limit);
322 $this->assign("{$prefix}single", $this->_single);
323
324 $controller = new CRM_Core_Selector_Controller($selector,
325 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 326 $this->getSortID(),
6a488035
TO
327 CRM_Core_Action::VIEW,
328 $this,
329 CRM_Core_Selector_Controller::SESSION,
330 $prefix
331 );
332 $controller->setEmbedded(TRUE);
333
a5611c8e 334 $query = $selector->getQuery();
6a488035
TO
335 if ($this->_context == 'user') {
336 $query->setSkipPermission(TRUE);
337 }
338 $controller->run();
339 }
340
12cda498
AP
341 /**
342 * The post processing of the form gets done here.
343 *
344 * Key things done during post processing are
345 * - check for reset or next request. if present, skip post procesing.
346 * - now check if user requested running a saved search, if so, then
347 * the form values associated with the saved search are used for searching.
348 * - if user has done a submit with new values the regular post submissing is
349 * done.
350 * The processing consists of using a Selector / Controller framework for getting the
351 * search results.
352 *
353 * @param
354 *
355 * @return void
1d81be90 356 * @throws \CRM_Core_Exception
12cda498
AP
357 */
358 public function postProcess() {
359 if ($this->_done) {
360 return;
361 }
362
363 $this->_done = TRUE;
56d513f2 364 $this->setFormValues();
12cda498 365
56d513f2 366 $this->submit($this->_formValues);
12cda498
AP
367 }
368
6a488035 369 /**
dc195289 370 * add the rules (mainly global rules) for form.
6a488035
TO
371 * All local rules are added near the element
372 *
355ba699 373 * @return void
6a488035
TO
374 * @see valid_date
375 */
6ea503d4
TO
376 public function addRules() {
377 }
6a488035 378
00be9182 379 public function fixFormValues() {
6a488035
TO
380 // if this search has been forced
381 // then see if there are any get values, and if so over-ride the post values
382 // note that this means that GET over-rides POST :)
6a488035 383
1273d77c 384 $role = CRM_Utils_Request::retrieve('role', 'String');
6a488035
TO
385
386 if (isset($role)) {
387 if ($role === 'true') {
4b191b48 388 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
6a488035
TO
389 }
390 elseif ($role === 'false') {
4b191b48 391 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
6a488035
TO
392 }
393 elseif (is_numeric($role)) {
53133bb0 394 $roleTypes = (int) $role;
6a488035 395 }
d5bd1f7e 396 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
53133bb0 397 }
398
1273d77c 399 $type = CRM_Utils_Request::retrieve('type', 'Positive');
6a488035
TO
400 if ($type) {
401 $this->_formValues['event_type'] = $type;
402 }
403
404 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
405
406 if ($cid) {
407 $cid = CRM_Utils_Type::escape($cid, 'Integer');
408 if ($cid > 0) {
409 $this->_formValues['contact_id'] = $cid;
410
411 // also assign individual mode to the template
412 $this->_single = TRUE;
413 }
414 }
415 }
416
6a488035
TO
417 /**
418 * Return a descriptive name for the page, used in wizard header
419 *
420 * @return string
6a488035
TO
421 */
422 public function getTitle() {
423 return ts('Find Participants');
424 }
96025800 425
18db97df 426 /**
427 * Set the metadata for the form.
428 *
429 * @throws \CiviCRM_API3_Exception
430 */
431 protected function setSearchMetadata() {
432 $this->addSearchFieldMetadata(['Participant' => CRM_Event_BAO_Query::getSearchFieldMetadata()]);
433 }
434
1126bdc5
SL
435 /**
436 * Set the default form values.
437 *
438 *
439 * @return array
440 * the default array reference
86f20cc1 441 * @throws \CRM_Core_Exception
1126bdc5
SL
442 */
443 public function setDefaultValues() {
56d513f2 444 $this->_defaults = array_merge(parent::setDefaultValues(), (array) $this->_formValues);
5f3ff0ef
SL
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}