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