Merge pull request #13432 from francescbassas/patch-16
[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 * 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 $this->loadStandardSearchOptionsFromUrl();
94 $this->loadFormValues();
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
108 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id'));
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 /**
139 * Build the form object.
140 *
141 *
142 * @return void
143 */
144 public function buildQuickForm() {
145 parent::buildQuickForm();
146 $this->addContactSearchFields();
147
148 CRM_Event_BAO_Query::buildSearchForm($this);
149
150 $rows = $this->get('rows');
151 if (is_array($rows)) {
152 $lineItems = $eventIds = array();
153 if (!$this->_single) {
154 $this->addRowSelectors($rows);
155 }
156 foreach ($rows as $row) {
157 $eventIds[$row['event_id']] = $row['event_id'];
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.
168 $seatClause = array();
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'])) {
173 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", '=', $this->_formValues['participant_status_id'], 'Int');
174 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
175 $this->_formValues['participant_status_id'] = $status;
176 }
177 }
178 if (!empty($this->_formValues['participant_role_id'])) {
179 $escapedRoles = array();
180 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
181 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
182 }
183 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
184 }
185
186 // CRM-15379
187 if (!empty($this->_formValues['participant_fee_id'])) {
188 $participant_fee_id = $this->_formValues['participant_fee_id'];
189 foreach ($participant_fee_id as $k => &$val) {
190 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
191 $val = CRM_Core_DAO::escapeString(trim($val));
192 }
193 $feeLabel = implode('|', $participant_fee_id);
194 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
195 }
196
197 $seatClause = implode(' AND ', $seatClause);
198 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
199 }
200 $this->assign('participantCount', $participantCount);
201 $this->assign('lineItems', $lineItems);
202
203 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
204 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
205
206 if (isset($this->_ssID)) {
207 $savedSearchValues = array(
208 'id' => $this->_ssID,
209 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
210 );
211 $this->assign_by_ref('savedSearch', $savedSearchValues);
212 $this->assign('ssID', $this->_ssID);
213 }
214
215 $this->addTaskMenu($tasks);
216 }
217
218 }
219
220 /**
221 * Get the label for the sortName field if email searching is on.
222 *
223 * (email searching is a setting under search preferences).
224 *
225 * @return string
226 */
227 protected function getSortNameLabelWithEmail() {
228 return ts('Participant Name or Email');
229 }
230
231 /**
232 * Get the label for the sortName field if email searching is off.
233 *
234 * (email searching is a setting under search preferences).
235 *
236 * @return string
237 */
238 protected function getSortNameLabelWithOutEmail() {
239 return ts('Participant Name');
240 }
241
242 /**
243 * Get the label for the tag field.
244 *
245 * We do this in a function so the 'ts' wraps the whole string to allow
246 * better translation.
247 *
248 * @return string
249 */
250 protected function getTagLabel() {
251 return ts('Participant Tag(s)');
252 }
253
254 /**
255 * Get the label for the group field.
256 *
257 * @return string
258 */
259 protected function getGroupLabel() {
260 return ts('Participant Group(s)');
261 }
262
263 /**
264 * Get the label for the group field.
265 *
266 * @return string
267 */
268 protected function getContactTypeLabel() {
269 return ts('Participant Contact Type');
270 }
271
272 /**
273 * The post processing of the form gets done here.
274 *
275 * Key things done during post processing are
276 * - check for reset or next request. if present, skip post procesing.
277 * - now check if user requested running a saved search, if so, then
278 * the form values associated with the saved search are used for searching.
279 * - if user has done a submit with new values the regular post submissing is
280 * done.
281 * The processing consists of using a Selector / Controller framework for getting the
282 * search results.
283 *
284 * @param
285 *
286 * @return void
287 */
288 public function postProcess() {
289 if ($this->_done) {
290 return;
291 }
292
293 $this->_done = TRUE;
294
295 if (!empty($_POST)) {
296 $this->_formValues = $this->controller->exportValues($this->_name);
297 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, array('participant_status_id'));
298 }
299
300 if (empty($this->_formValues)) {
301 $this->_formValues = $this->controller->exportValues($this->_name);
302 }
303
304 $this->fixFormValues();
305
306 if (isset($this->_ssID) && empty($_POST)) {
307 // if we are editing / running a saved search and the form has not been posted
308 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
309 }
310
311 // We don't show test records in summaries or dashboards
312 if (empty($this->_formValues['participant_test']) && $this->_force) {
313 $this->_formValues["participant_test"] = 0;
314 }
315
316 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
317
318 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id'));
319
320 $this->set('formValues', $this->_formValues);
321 $this->set('queryParams', $this->_queryParams);
322
323 $buttonName = $this->controller->getButtonName();
324 if ($buttonName == $this->_actionButtonName) {
325 // check actionName and if next, then do not repeat a search, since we are going to the next page
326
327 // hack, make sure we reset the task values
328 $stateMachine = $this->controller->getStateMachine();
329 $formName = $stateMachine->getTaskFormName();
330 $this->controller->resetPage($formName);
331 return;
332 }
333
334 $sortID = NULL;
335 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
336 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
337 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
338 );
339 }
340
341 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id'));
342
343 $selector = new CRM_Event_Selector_Search($this->_queryParams,
344 $this->_action,
345 NULL,
346 $this->_single,
347 $this->_limit,
348 $this->_context
349 );
350
351 $selector->setKey($this->controller->_key);
352
353 $prefix = NULL;
354 if ($this->_context == 'user') {
355 $prefix = $this->_prefix;
356 }
357
358 $this->assign("{$prefix}limit", $this->_limit);
359 $this->assign("{$prefix}single", $this->_single);
360
361 $controller = new CRM_Core_Selector_Controller($selector,
362 $this->get(CRM_Utils_Pager::PAGE_ID),
363 $sortID,
364 CRM_Core_Action::VIEW,
365 $this,
366 CRM_Core_Selector_Controller::SESSION,
367 $prefix
368 );
369 $controller->setEmbedded(TRUE);
370
371 $query = $selector->getQuery();
372 if ($this->_context == 'user') {
373 $query->setSkipPermission(TRUE);
374 }
375 $controller->run();
376 }
377
378 /**
379 * add the rules (mainly global rules) for form.
380 * All local rules are added near the element
381 *
382 * @return void
383 * @see valid_date
384 */
385 public function addRules() {
386 }
387
388 /**
389 * Set the default form values.
390 *
391 *
392 * @return array
393 * the default array reference
394 */
395 public function setDefaultValues() {
396 $defaults = array();
397 $defaults = $this->_formValues;
398 return $defaults;
399 }
400
401 public function fixFormValues() {
402 // if this search has been forced
403 // then see if there are any get values, and if so over-ride the post values
404 // note that this means that GET over-rides POST :)
405 $event = CRM_Utils_Request::retrieve('event', 'Positive');
406 if ($event) {
407 $this->_formValues['event_id'] = $event;
408 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
409 }
410
411 $status = CRM_Utils_Request::retrieve('status', 'String');
412
413 if (isset($status)) {
414 if ($status === 'true') {
415 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
416 }
417 elseif ($status === 'false') {
418 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
419 }
420 elseif (is_numeric($status)) {
421 $statusTypes = (int) $status;
422 }
423 elseif (is_array($status) && !array_key_exists('IN', $status)) {
424 $statusTypes = array_keys($status);
425 }
426 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? array('IN' => array_keys($statusTypes)) : $statusTypes;
427 }
428
429 $role = CRM_Utils_Request::retrieve('role', 'String');
430
431 if (isset($role)) {
432 if ($role === 'true') {
433 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
434 }
435 elseif ($role === 'false') {
436 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
437 }
438 elseif (is_numeric($role)) {
439 $roleTypes = (int) $role;
440 }
441 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
442 }
443
444 $type = CRM_Utils_Request::retrieve('type', 'Positive');
445 if ($type) {
446 $this->_formValues['event_type'] = $type;
447 }
448
449 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
450
451 if ($cid) {
452 $cid = CRM_Utils_Type::escape($cid, 'Integer');
453 if ($cid > 0) {
454 $this->_formValues['contact_id'] = $cid;
455
456 // also assign individual mode to the template
457 $this->_single = TRUE;
458 }
459 }
460 }
461
462 /**
463 * @return null
464 */
465 public function getFormValues() {
466 return NULL;
467 }
468
469 /**
470 * Return a descriptive name for the page, used in wizard header
471 *
472 * @return string
473 */
474 public function getTitle() {
475 return ts('Find Participants');
476 }
477
478 }