Merge in 5.20
[civicrm-core.git] / CRM / Event / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This file is for civievent search
20 */
21 class CRM_Event_Form_Search extends CRM_Core_Form_Search {
22
23 /**
24 * The params that are sent to the query.
25 *
26 * @var array
27 */
28 protected $_queryParams;
29
30 /**
31 * Are we restricting ourselves to a single contact.
32 *
33 * @var bool
34 */
35 protected $_single = FALSE;
36
37 /**
38 * Are we restricting ourselves to a single contact.
39 *
40 * @var bool
41 */
42 protected $_limit = NULL;
43
44 /**
45 * Prefix for the controller.
46 * @var string
47 */
48 protected $_prefix = "event_";
49
50 /**
51 * The saved search ID retrieved from the GET vars.
52 *
53 * @var int
54 */
55 protected $_ssID;
56
57 /**
58 * Metadata of all fields to include on the form.
59 *
60 * @var array
61 */
62 protected $searchFieldMetadata = [];
63
64 /**
65 * Get the default entity for the form.
66 *
67 * @return string
68 */
69 public function getDefaultEntity() {
70 return 'Participant';
71 }
72
73 /**
74 * Processing needed for buildForm and later.
75 *
76 * @return void
77 *
78 * @throws \CRM_Core_Exception
79 * @throws \CiviCRM_API3_Exception
80 */
81 public function preProcess() {
82 $this->set('searchFormName', 'Search');
83
84 /**
85 * set the button names
86 */
87 $this->_searchButtonName = $this->getButtonName('refresh');
88 $this->_actionButtonName = $this->getButtonName('next', 'action');
89
90 $this->_done = FALSE;
91
92 parent::preProcess();
93
94 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
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),
112 $this->getSortID(),
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 /**
125 * Build the form object.
126 *
127 * @return void
128 *
129 * @throws \CRM_Core_Exception
130 */
131 public function buildQuickForm() {
132 parent::buildQuickForm();
133 $this->addContactSearchFields();
134
135 CRM_Event_BAO_Query::buildSearchForm($this);
136
137 $rows = $this->get('rows');
138 if (is_array($rows)) {
139 $lineItems = $eventIds = [];
140 if (!$this->_single) {
141 $this->addRowSelectors($rows);
142 }
143 foreach ($rows as $row) {
144 $eventIds[$row['event_id']] = $row['event_id'];
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.
155 $seatClause = [];
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'])) {
160 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", 'IN', $this->_formValues['participant_status_id'], 'Int');
161 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
162 $this->_formValues['participant_status_id'] = $status;
163 }
164 }
165 if (!empty($this->_formValues['participant_role_id'])) {
166 $escapedRoles = [];
167 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
168 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
169 }
170 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
171 }
172
173 // CRM-15379
174 if (!empty($this->_formValues['participant_fee_id'])) {
175 $participant_fee_id = $this->_formValues['participant_fee_id'];
176 $val_regexp = [];
177 foreach ($participant_fee_id as $k => &$val) {
178 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
179 $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val)));
180 $val = CRM_Core_DAO::escapeString(trim($val));
181 }
182 $feeLabel = implode('|', $val_regexp);
183 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
184 }
185
186 $seatClause = implode(' AND ', $seatClause);
187 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
188 }
189 $this->assign('participantCount', $participantCount);
190 $this->assign('lineItems', $lineItems);
191
192 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
193 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
194
195 if (isset($this->_ssID)) {
196 $savedSearchValues = [
197 'id' => $this->_ssID,
198 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
199 ];
200 $this->assign_by_ref('savedSearch', $savedSearchValues);
201 $this->assign('ssID', $this->_ssID);
202 }
203
204 $this->addTaskMenu($tasks);
205 }
206
207 }
208
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
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
261 /**
262 * Test submit the form.
263 * @param $formValues
264 */
265 public function testSubmit($formValues) {
266 $this->submit($formValues);
267 }
268
269 /**
270 * Submit the search form with given values.
271 * @param $formValues
272 */
273 private function submit($formValues) {
274 $this->_formValues = $formValues;
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
283 // We don't show test records in summaries or dashboards
284 if (empty($this->_formValues['participant_test']) && $this->_force) {
285 $this->_formValues["participant_test"] = 0;
286 }
287
288 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
289
290 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
291
292 $this->set('formValues', $this->_formValues);
293 $this->set('queryParams', $this->_queryParams);
294
295 $buttonName = $this->controller->getButtonName();
296 if ($buttonName == $this->_actionButtonName) {
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
300 $stateMachine = $this->controller->getStateMachine();
301 $formName = $stateMachine->getTaskFormName();
302 $this->controller->resetPage($formName);
303 return;
304 }
305
306 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
307
308 $selector = new CRM_Event_Selector_Search($this->_queryParams,
309 $this->_action,
310 NULL,
311 $this->_single,
312 $this->_limit,
313 $this->_context
314 );
315
316 $selector->setKey($this->controller->_key);
317
318 $prefix = NULL;
319 if ($this->_context == 'user') {
320 $prefix = $this->_prefix;
321 }
322
323 $this->assign("{$prefix}limit", $this->_limit);
324 $this->assign("{$prefix}single", $this->_single);
325
326 $controller = new CRM_Core_Selector_Controller($selector,
327 $this->get(CRM_Utils_Pager::PAGE_ID),
328 $this->getSortID(),
329 CRM_Core_Action::VIEW,
330 $this,
331 CRM_Core_Selector_Controller::SESSION,
332 $prefix
333 );
334 $controller->setEmbedded(TRUE);
335
336 $query = $selector->getQuery();
337 if ($this->_context == 'user') {
338 $query->setSkipPermission(TRUE);
339 }
340 $controller->run();
341 }
342
343 /**
344 * The post processing of the form gets done here.
345 *
346 * Key things done during post processing are
347 * - check for reset or next request. if present, skip post procesing.
348 * - now check if user requested running a saved search, if so, then
349 * the form values associated with the saved search are used for searching.
350 * - if user has done a submit with new values the regular post submissing is
351 * done.
352 * The processing consists of using a Selector / Controller framework for getting the
353 * search results.
354 *
355 * @param
356 *
357 * @return void
358 * @throws \CRM_Core_Exception
359 */
360 public function postProcess() {
361 if ($this->_done) {
362 return;
363 }
364
365 $this->_done = TRUE;
366 $formValues = $this->getFormValues();
367
368 $this->submit($formValues);
369 }
370
371 /**
372 * add the rules (mainly global rules) for form.
373 * All local rules are added near the element
374 *
375 * @return void
376 * @see valid_date
377 */
378 public function addRules() {
379 }
380
381 public function fixFormValues() {
382 // if this search has been forced
383 // then see if there are any get values, and if so over-ride the post values
384 // note that this means that GET over-rides POST :)
385 $event = CRM_Utils_Request::retrieve('event', 'Positive');
386 if ($event) {
387 $this->_formValues['event_id'] = $event;
388 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
389 }
390
391 $status = CRM_Utils_Request::retrieve('status', 'String');
392
393 if (isset($status)) {
394 if ($status === 'true') {
395 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
396 }
397 elseif ($status === 'false') {
398 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
399 }
400 elseif (is_numeric($status)) {
401 $statusTypes = (int) $status;
402 }
403 elseif (is_array($status) && !array_key_exists('IN', $status)) {
404 $statusTypes = array_keys($status);
405 }
406 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? ['IN' => array_keys($statusTypes)] : $statusTypes;
407 }
408
409 $role = CRM_Utils_Request::retrieve('role', 'String');
410
411 if (isset($role)) {
412 if ($role === 'true') {
413 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
414 }
415 elseif ($role === 'false') {
416 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
417 }
418 elseif (is_numeric($role)) {
419 $roleTypes = (int) $role;
420 }
421 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
422 }
423
424 $type = CRM_Utils_Request::retrieve('type', 'Positive');
425 if ($type) {
426 $this->_formValues['event_type'] = $type;
427 }
428
429 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
430
431 if ($cid) {
432 $cid = CRM_Utils_Type::escape($cid, 'Integer');
433 if ($cid > 0) {
434 $this->_formValues['contact_id'] = $cid;
435
436 // also assign individual mode to the template
437 $this->_single = TRUE;
438 }
439 }
440 }
441
442 /**
443 * Return a descriptive name for the page, used in wizard header
444 *
445 * @return string
446 */
447 public function getTitle() {
448 return ts('Find Participants');
449 }
450
451 /**
452 * Set the metadata for the form.
453 *
454 * @throws \CiviCRM_API3_Exception
455 */
456 protected function setSearchMetadata() {
457 $this->addSearchFieldMetadata(['Participant' => CRM_Event_BAO_Query::getSearchFieldMetadata()]);
458 }
459
460 /**
461 * Set the default form values.
462 *
463 *
464 * @return array
465 * the default array reference
466 */
467 public function setDefaultValues() {
468 parent::setDefaultValues();
469 return $this->_formValues;
470 }
471
472 }