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