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