[REF] replace copy & paste with shared getSortID function.
[civicrm-core.git] / CRM / Event / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2020
32 */
33
34 /**
35 * This file is for civievent search
36 */
37 class CRM_Event_Form_Search extends CRM_Core_Form_Search {
38
39 /**
40 * The params that are sent to the query.
41 *
42 * @var array
43 */
44 protected $_queryParams;
45
46 /**
47 * Are we restricting ourselves to a single contact.
48 *
49 * @var bool
50 */
51 protected $_single = FALSE;
52
53 /**
54 * Are we restricting ourselves to a single contact.
55 *
56 * @var bool
57 */
58 protected $_limit = NULL;
59
60 /**
61 * Prefix for the controller.
62 * @var string
63 */
64 protected $_prefix = "event_";
65
66 /**
67 * The saved search ID retrieved from the GET vars.
68 *
69 * @var int
70 */
71 protected $_ssID;
72
73 /**
74 * Metadata of all fields to include on the form.
75 *
76 * @var array
77 */
78 protected $searchFieldMetadata = [];
79
80 /**
81 * Get the default entity for the form.
82 *
83 * @return string
84 */
85 public function getDefaultEntity() {
86 return 'Participant';
87 }
88
89 /**
90 * Processing needed for buildForm and later.
91 *
92 * @return void
93 */
94 public function preProcess() {
95 $this->set('searchFormName', 'Search');
96
97 /**
98 * set the button names
99 */
100 $this->_searchButtonName = $this->getButtonName('refresh');
101 $this->_actionButtonName = $this->getButtonName('next', 'action');
102
103 $this->_done = FALSE;
104
105 $this->loadStandardSearchOptionsFromUrl();
106 $this->loadFormValues();
107
108 if ($this->_force) {
109 $this->postProcess();
110 $this->set('force', 0);
111 }
112
113 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
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),
131 $this->getSortID(),
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 /**
144 * Build the form object.
145 *
146 * @return void
147 *
148 * @throws \CRM_Core_Exception
149 */
150 public function buildQuickForm() {
151 parent::buildQuickForm();
152 $this->addContactSearchFields();
153
154 CRM_Event_BAO_Query::buildSearchForm($this);
155
156 $rows = $this->get('rows');
157 if (is_array($rows)) {
158 $lineItems = $eventIds = [];
159 if (!$this->_single) {
160 $this->addRowSelectors($rows);
161 }
162 foreach ($rows as $row) {
163 $eventIds[$row['event_id']] = $row['event_id'];
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.
174 $seatClause = [];
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'])) {
179 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", 'IN', $this->_formValues['participant_status_id'], 'Int');
180 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
181 $this->_formValues['participant_status_id'] = $status;
182 }
183 }
184 if (!empty($this->_formValues['participant_role_id'])) {
185 $escapedRoles = [];
186 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
187 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
188 }
189 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
190 }
191
192 // CRM-15379
193 if (!empty($this->_formValues['participant_fee_id'])) {
194 $participant_fee_id = $this->_formValues['participant_fee_id'];
195 $val_regexp = [];
196 foreach ($participant_fee_id as $k => &$val) {
197 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
198 $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val)));
199 $val = CRM_Core_DAO::escapeString(trim($val));
200 }
201 $feeLabel = implode('|', $val_regexp);
202 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
203 }
204
205 $seatClause = implode(' AND ', $seatClause);
206 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
207 }
208 $this->assign('participantCount', $participantCount);
209 $this->assign('lineItems', $lineItems);
210
211 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
212 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
213
214 if (isset($this->_ssID)) {
215 $savedSearchValues = [
216 'id' => $this->_ssID,
217 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
218 ];
219 $this->assign_by_ref('savedSearch', $savedSearchValues);
220 $this->assign('ssID', $this->_ssID);
221 }
222
223 $this->addTaskMenu($tasks);
224 }
225
226 }
227
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
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
280 /**
281 * Test submit the form.
282 * @param $formValues
283 */
284 public function testSubmit($formValues) {
285 $this->submit($formValues);
286 }
287
288 /**
289 * Submit the search form with given values.
290 * @param $formValues
291 */
292 private function submit($formValues) {
293 $this->_formValues = $formValues;
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
302 // We don't show test records in summaries or dashboards
303 if (empty($this->_formValues['participant_test']) && $this->_force) {
304 $this->_formValues["participant_test"] = 0;
305 }
306
307 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
308
309 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
310
311 $this->set('formValues', $this->_formValues);
312 $this->set('queryParams', $this->_queryParams);
313
314 $buttonName = $this->controller->getButtonName();
315 if ($buttonName == $this->_actionButtonName) {
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
319 $stateMachine = $this->controller->getStateMachine();
320 $formName = $stateMachine->getTaskFormName();
321 $this->controller->resetPage($formName);
322 return;
323 }
324
325 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
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),
347 $this->getSortID(),
348 CRM_Core_Action::VIEW,
349 $this,
350 CRM_Core_Selector_Controller::SESSION,
351 $prefix
352 );
353 $controller->setEmbedded(TRUE);
354
355 $query = $selector->getQuery();
356 if ($this->_context == 'user') {
357 $query->setSkipPermission(TRUE);
358 }
359 $controller->run();
360 }
361
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;
384 $formValues = [];
385
386 if (!empty($_POST)) {
387 $formValues = $this->controller->exportValues($this->_name);
388 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, ['participant_status_id']);
389 }
390
391 if (empty($formValues)) {
392 $formValues = $this->controller->exportValues($this->_name);
393 }
394
395 $this->submit($formValues);
396 }
397
398 /**
399 * add the rules (mainly global rules) for form.
400 * All local rules are added near the element
401 *
402 * @return void
403 * @see valid_date
404 */
405 public function addRules() {
406 }
407
408 public function fixFormValues() {
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 :)
412 $event = CRM_Utils_Request::retrieve('event', 'Positive');
413 if ($event) {
414 $this->_formValues['event_id'] = $event;
415 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
416 }
417
418 $status = CRM_Utils_Request::retrieve('status', 'String');
419
420 if (isset($status)) {
421 if ($status === 'true') {
422 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
423 }
424 elseif ($status === 'false') {
425 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
426 }
427 elseif (is_numeric($status)) {
428 $statusTypes = (int) $status;
429 }
430 elseif (is_array($status) && !array_key_exists('IN', $status)) {
431 $statusTypes = array_keys($status);
432 }
433 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? ['IN' => array_keys($statusTypes)] : $statusTypes;
434 }
435
436 $role = CRM_Utils_Request::retrieve('role', 'String');
437
438 if (isset($role)) {
439 if ($role === 'true') {
440 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
441 }
442 elseif ($role === 'false') {
443 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
444 }
445 elseif (is_numeric($role)) {
446 $roleTypes = (int) $role;
447 }
448 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
449 }
450
451 $type = CRM_Utils_Request::retrieve('type', 'Positive');
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
469 /**
470 * @return null
471 */
472 public function getFormValues() {
473 return NULL;
474 }
475
476 /**
477 * Return a descriptive name for the page, used in wizard header
478 *
479 * @return string
480 */
481 public function getTitle() {
482 return ts('Find Participants');
483 }
484
485 }