abf22b7a25c58d2947ba87a6bb23d9cea581e4f6
[civicrm-core.git] / CRM / Event / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 $sortID = NULL;
114 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
115 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
116 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
117 );
118 }
119
120 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
121 $selector = new CRM_Event_Selector_Search($this->_queryParams,
122 $this->_action,
123 NULL,
124 $this->_single,
125 $this->_limit,
126 $this->_context
127 );
128 $prefix = NULL;
129 if ($this->_context == 'user') {
130 $prefix = $this->_prefix;
131 }
132
133 $this->assign("{$prefix}limit", $this->_limit);
134 $this->assign("{$prefix}single", $this->_single);
135
136 $controller = new CRM_Core_Selector_Controller($selector,
137 $this->get(CRM_Utils_Pager::PAGE_ID),
138 $sortID,
139 CRM_Core_Action::VIEW,
140 $this,
141 CRM_Core_Selector_Controller::TRANSFER,
142 $prefix
143 );
144 $controller->setEmbedded(TRUE);
145 $controller->moveFromSessionToTemplate();
146
147 $this->assign('summary', $this->get('summary'));
148 }
149
150 /**
151 * Build the form object.
152 *
153 * @return void
154 *
155 * @throws \CRM_Core_Exception
156 */
157 public function buildQuickForm() {
158 parent::buildQuickForm();
159 $this->addContactSearchFields();
160
161 CRM_Event_BAO_Query::buildSearchForm($this);
162
163 $rows = $this->get('rows');
164 if (is_array($rows)) {
165 $lineItems = $eventIds = [];
166 if (!$this->_single) {
167 $this->addRowSelectors($rows);
168 }
169 foreach ($rows as $row) {
170 $eventIds[$row['event_id']] = $row['event_id'];
171 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
172 // add line item details if applicable
173 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
174 }
175 }
176
177 //get actual count only when we are dealing w/ single event.
178 $participantCount = 0;
179 if (count($eventIds) == 1) {
180 //convert form values to clause.
181 $seatClause = [];
182 if (CRM_Utils_Array::value('participant_test', $this->_formValues) == '1' || CRM_Utils_Array::value('participant_test', $this->_formValues) == '0') {
183 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
184 }
185 if (!empty($this->_formValues['participant_status_id'])) {
186 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", 'IN', $this->_formValues['participant_status_id'], 'Int');
187 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
188 $this->_formValues['participant_status_id'] = $status;
189 }
190 }
191 if (!empty($this->_formValues['participant_role_id'])) {
192 $escapedRoles = [];
193 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
194 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
195 }
196 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
197 }
198
199 // CRM-15379
200 if (!empty($this->_formValues['participant_fee_id'])) {
201 $participant_fee_id = $this->_formValues['participant_fee_id'];
202 $val_regexp = [];
203 foreach ($participant_fee_id as $k => &$val) {
204 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
205 $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val)));
206 $val = CRM_Core_DAO::escapeString(trim($val));
207 }
208 $feeLabel = implode('|', $val_regexp);
209 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
210 }
211
212 $seatClause = implode(' AND ', $seatClause);
213 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
214 }
215 $this->assign('participantCount', $participantCount);
216 $this->assign('lineItems', $lineItems);
217
218 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
219 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
220
221 if (isset($this->_ssID)) {
222 $savedSearchValues = [
223 'id' => $this->_ssID,
224 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
225 ];
226 $this->assign_by_ref('savedSearch', $savedSearchValues);
227 $this->assign('ssID', $this->_ssID);
228 }
229
230 $this->addTaskMenu($tasks);
231 }
232
233 }
234
235 /**
236 * Get the label for the sortName field if email searching is on.
237 *
238 * (email searching is a setting under search preferences).
239 *
240 * @return string
241 */
242 protected function getSortNameLabelWithEmail() {
243 return ts('Participant Name or Email');
244 }
245
246 /**
247 * Get the label for the sortName field if email searching is off.
248 *
249 * (email searching is a setting under search preferences).
250 *
251 * @return string
252 */
253 protected function getSortNameLabelWithOutEmail() {
254 return ts('Participant Name');
255 }
256
257 /**
258 * Get the label for the tag field.
259 *
260 * We do this in a function so the 'ts' wraps the whole string to allow
261 * better translation.
262 *
263 * @return string
264 */
265 protected function getTagLabel() {
266 return ts('Participant Tag(s)');
267 }
268
269 /**
270 * Get the label for the group field.
271 *
272 * @return string
273 */
274 protected function getGroupLabel() {
275 return ts('Participant Group(s)');
276 }
277
278 /**
279 * Get the label for the group field.
280 *
281 * @return string
282 */
283 protected function getContactTypeLabel() {
284 return ts('Participant Contact Type');
285 }
286
287 /**
288 * Test submit the form.
289 * @param $formValues
290 */
291 public function testSubmit($formValues) {
292 $this->submit($formValues);
293 }
294
295 /**
296 * Submit the search form with given values.
297 * @param $formValues
298 */
299 private function submit($formValues) {
300 $this->_formValues = $formValues;
301
302 $this->fixFormValues();
303
304 if (isset($this->_ssID) && empty($_POST)) {
305 // if we are editing / running a saved search and the form has not been posted
306 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
307 }
308
309 // We don't show test records in summaries or dashboards
310 if (empty($this->_formValues['participant_test']) && $this->_force) {
311 $this->_formValues["participant_test"] = 0;
312 }
313
314 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
315
316 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
317
318 $this->set('formValues', $this->_formValues);
319 $this->set('queryParams', $this->_queryParams);
320
321 $buttonName = $this->controller->getButtonName();
322 if ($buttonName == $this->_actionButtonName) {
323 // check actionName and if next, then do not repeat a search, since we are going to the next page
324
325 // hack, make sure we reset the task values
326 $stateMachine = $this->controller->getStateMachine();
327 $formName = $stateMachine->getTaskFormName();
328 $this->controller->resetPage($formName);
329 return;
330 }
331
332 $sortID = NULL;
333 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
334 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
335 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
336 );
337 }
338
339 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
340
341 $selector = new CRM_Event_Selector_Search($this->_queryParams,
342 $this->_action,
343 NULL,
344 $this->_single,
345 $this->_limit,
346 $this->_context
347 );
348
349 $selector->setKey($this->controller->_key);
350
351 $prefix = NULL;
352 if ($this->_context == 'user') {
353 $prefix = $this->_prefix;
354 }
355
356 $this->assign("{$prefix}limit", $this->_limit);
357 $this->assign("{$prefix}single", $this->_single);
358
359 $controller = new CRM_Core_Selector_Controller($selector,
360 $this->get(CRM_Utils_Pager::PAGE_ID),
361 $sortID,
362 CRM_Core_Action::VIEW,
363 $this,
364 CRM_Core_Selector_Controller::SESSION,
365 $prefix
366 );
367 $controller->setEmbedded(TRUE);
368
369 $query = $selector->getQuery();
370 if ($this->_context == 'user') {
371 $query->setSkipPermission(TRUE);
372 }
373 $controller->run();
374 }
375
376 /**
377 * The post processing of the form gets done here.
378 *
379 * Key things done during post processing are
380 * - check for reset or next request. if present, skip post procesing.
381 * - now check if user requested running a saved search, if so, then
382 * the form values associated with the saved search are used for searching.
383 * - if user has done a submit with new values the regular post submissing is
384 * done.
385 * The processing consists of using a Selector / Controller framework for getting the
386 * search results.
387 *
388 * @param
389 *
390 * @return void
391 */
392 public function postProcess() {
393 if ($this->_done) {
394 return;
395 }
396
397 $this->_done = TRUE;
398 $formValues = [];
399
400 if (!empty($_POST)) {
401 $formValues = $this->controller->exportValues($this->_name);
402 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, ['participant_status_id']);
403 }
404
405 if (empty($formValues)) {
406 $formValues = $this->controller->exportValues($this->_name);
407 }
408
409 $this->submit($formValues);
410 }
411
412 /**
413 * add the rules (mainly global rules) for form.
414 * All local rules are added near the element
415 *
416 * @return void
417 * @see valid_date
418 */
419 public function addRules() {
420 }
421
422 public function fixFormValues() {
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 :)
426 $event = CRM_Utils_Request::retrieve('event', 'Positive');
427 if ($event) {
428 $this->_formValues['event_id'] = $event;
429 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
430 }
431
432 $status = CRM_Utils_Request::retrieve('status', 'String');
433
434 if (isset($status)) {
435 if ($status === 'true') {
436 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
437 }
438 elseif ($status === 'false') {
439 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
440 }
441 elseif (is_numeric($status)) {
442 $statusTypes = (int) $status;
443 }
444 elseif (is_array($status) && !array_key_exists('IN', $status)) {
445 $statusTypes = array_keys($status);
446 }
447 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? ['IN' => array_keys($statusTypes)] : $statusTypes;
448 }
449
450 $role = CRM_Utils_Request::retrieve('role', 'String');
451
452 if (isset($role)) {
453 if ($role === 'true') {
454 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
455 }
456 elseif ($role === 'false') {
457 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
458 }
459 elseif (is_numeric($role)) {
460 $roleTypes = (int) $role;
461 }
462 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
463 }
464
465 $type = CRM_Utils_Request::retrieve('type', 'Positive');
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
483 /**
484 * @return null
485 */
486 public function getFormValues() {
487 return NULL;
488 }
489
490 /**
491 * Return a descriptive name for the page, used in wizard header
492 *
493 * @return string
494 */
495 public function getTitle() {
496 return ts('Find Participants');
497 }
498
499 /**
500 * Set the default form values.
501 *
502 *
503 * @return array
504 * the default array reference
505 */
506 public function setDefaultValues() {
507 parent::setDefaultValues();
508 return $this->_formValues;
509 }
510
511 }