Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Files required | |
36 | */ | |
37 | ||
38 | /** | |
39 | * This file is for civievent search | |
40 | */ | |
3efb5b86 | 41 | class 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 | * |
6a488035 TO |
53 | * @var boolean |
54 | */ | |
55 | protected $_single = FALSE; | |
56 | ||
57 | /** | |
66f9e52b | 58 | * Are we restricting ourselves to a single contact. |
6a488035 | 59 | * |
6a488035 TO |
60 | * @var boolean |
61 | */ | |
62 | protected $_limit = NULL; | |
63 | ||
6a488035 | 64 | /** |
66f9e52b | 65 | * Prefix for the controller. |
6a488035 TO |
66 | */ |
67 | protected $_prefix = "event_"; | |
68 | ||
6a488035 | 69 | /** |
66f9e52b | 70 | * The saved search ID retrieved from the GET vars. |
6a488035 TO |
71 | * |
72 | * @var int | |
6a488035 TO |
73 | */ |
74 | protected $_ssID; | |
75 | ||
76 | /** | |
66f9e52b | 77 | * Processing needed for buildForm and later. |
6a488035 TO |
78 | * |
79 | * @return void | |
6a488035 | 80 | */ |
00be9182 | 81 | public function preProcess() { |
6a488035 TO |
82 | $this->set('searchFormName', 'Search'); |
83 | ||
84 | /** | |
85 | * set the button names | |
86 | */ | |
87 | $this->_searchButtonName = $this->getButtonName('refresh'); | |
6a488035 TO |
88 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
89 | ||
90 | $this->_done = FALSE; | |
91 | $this->defaults = array(); | |
92 | ||
a5611c8e DL |
93 | /* |
94 | * we allow the controller to set force/reset externally, useful when we are being | |
95 | * driven by the wizard framework | |
6a488035 | 96 | */ |
a3d827a7 | 97 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); |
353ffa53 TO |
98 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
99 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
6a488035 | 100 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
353ffa53 | 101 | $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); |
6a488035 TO |
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 | ||
f179424e | 131 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id')); |
6a488035 TO |
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 | /** | |
66f9e52b | 162 | * Build the form object. |
6a488035 | 163 | * |
6a488035 TO |
164 | * |
165 | * @return void | |
166 | */ | |
00be9182 | 167 | public function buildQuickForm() { |
3efb5b86 | 168 | parent::buildQuickForm(); |
0573fd28 | 169 | $this->addContactSearchFields(); |
48242ab6 | 170 | |
6a488035 TO |
171 | CRM_Event_BAO_Query::buildSearchForm($this); |
172 | ||
6a488035 TO |
173 | $rows = $this->get('rows'); |
174 | if (is_array($rows)) { | |
175 | $lineItems = $eventIds = array(); | |
176 | if (!$this->_single) { | |
8d36b801 | 177 | $this->addRowSelectors($rows); |
6a488035 TO |
178 | } |
179 | foreach ($rows as $row) { | |
180 | $eventIds[$row['event_id']] = $row['event_id']; | |
6a488035 TO |
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(); | |
884fdc63 | 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'])) { | |
ef36db8b | 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 | } | |
884fdc63 | 200 | } |
201 | if (!empty($this->_formValues['participant_role_id'])) { | |
0906de17 MM |
202 | $escapedRoles = array(); |
203 | foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) { | |
204 | $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String'); | |
205 | } | |
aba574d4 | 206 | $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )"; |
884fdc63 | 207 | } |
208 | ||
7c75c017 | 209 | // CRM-15379 |
210 | if (!empty($this->_formValues['participant_fee_id'])) { | |
211 | $participant_fee_id = $this->_formValues['participant_fee_id']; | |
f179424e | 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}' )"; | |
6a488035 | 218 | } |
59adcdd9 | 219 | |
884fdc63 | 220 | $seatClause = implode(' AND ', $seatClause); |
221 | $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause); | |
6a488035 TO |
222 | } |
223 | $this->assign('participantCount', $participantCount); | |
224 | $this->assign('lineItems', $lineItems); | |
225 | ||
8fd26836 MW |
226 | $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL; |
227 | $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams); | |
6a488035 | 228 | |
6a488035 | 229 | if (isset($this->_ssID)) { |
6a488035 TO |
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 | ||
34197a55 | 238 | $this->addTaskMenu($tasks); |
6a488035 TO |
239 | } |
240 | ||
6a488035 TO |
241 | } |
242 | ||
e597fc33 DG |
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 | ||
0573fd28 | 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 | ||
6a488035 TO |
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 | |
6a488035 | 310 | */ |
00be9182 | 311 | public function postProcess() { |
6a488035 TO |
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); | |
8ccbdd92 | 320 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, array('participant_status_id')); |
6a488035 TO |
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 | ||
d43b88cc CW |
334 | // We don't show test records in summaries or dashboards |
335 | if (empty($this->_formValues['participant_test']) && $this->_force) { | |
6a488035 TO |
336 | $this->_formValues["participant_test"] = 0; |
337 | } | |
338 | ||
c94d39fd | 339 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
6a488035 | 340 | |
f179424e | 341 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id')); |
6a488035 TO |
342 | |
343 | $this->set('formValues', $this->_formValues); | |
344 | $this->set('queryParams', $this->_queryParams); | |
345 | ||
346 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 347 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
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 | |
a5611c8e | 351 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
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 | ||
f179424e | 364 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id')); |
6a488035 TO |
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 | ||
a5611c8e | 394 | $query = $selector->getQuery(); |
6a488035 TO |
395 | if ($this->_context == 'user') { |
396 | $query->setSkipPermission(TRUE); | |
397 | } | |
398 | $controller->run(); | |
399 | } | |
400 | ||
401 | /** | |
dc195289 | 402 | * add the rules (mainly global rules) for form. |
6a488035 TO |
403 | * All local rules are added near the element |
404 | * | |
355ba699 | 405 | * @return void |
6a488035 TO |
406 | * @see valid_date |
407 | */ | |
6ea503d4 TO |
408 | public function addRules() { |
409 | } | |
6a488035 TO |
410 | |
411 | /** | |
66f9e52b | 412 | * Set the default form values. |
6a488035 | 413 | * |
6a488035 | 414 | * |
a6c01b45 CW |
415 | * @return array |
416 | * the default array reference | |
6a488035 | 417 | */ |
00be9182 | 418 | public function setDefaultValues() { |
6a488035 TO |
419 | $defaults = array(); |
420 | $defaults = $this->_formValues; | |
421 | return $defaults; | |
422 | } | |
423 | ||
00be9182 | 424 | public function fixFormValues() { |
6a488035 TO |
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 :) | |
1273d77c | 428 | $event = CRM_Utils_Request::retrieve('event', 'Positive'); |
6a488035 TO |
429 | if ($event) { |
430 | $this->_formValues['event_id'] = $event; | |
431 | $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE); | |
432 | } | |
433 | ||
1273d77c | 434 | $status = CRM_Utils_Request::retrieve('status', 'String'); |
6a488035 TO |
435 | |
436 | if (isset($status)) { | |
437 | if ($status === 'true') { | |
d5bd1f7e | 438 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1"); |
6a488035 TO |
439 | } |
440 | elseif ($status === 'false') { | |
d5bd1f7e | 441 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0"); |
6a488035 TO |
442 | } |
443 | elseif (is_numeric($status)) { | |
53133bb0 | 444 | $statusTypes = (int) $status; |
6a488035 | 445 | } |
790b3777 | 446 | elseif (is_array($status) && !array_key_exists('IN', $status)) { |
447 | $statusTypes = array_keys($status); | |
448 | } | |
e5326076 | 449 | $this->_formValues['participant_status_id'] = is_array($statusTypes) ? array('IN' => array_keys($statusTypes)) : $statusTypes; |
6a488035 TO |
450 | } |
451 | ||
1273d77c | 452 | $role = CRM_Utils_Request::retrieve('role', 'String'); |
6a488035 TO |
453 | |
454 | if (isset($role)) { | |
455 | if ($role === 'true') { | |
4b191b48 | 456 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1"); |
6a488035 TO |
457 | } |
458 | elseif ($role === 'false') { | |
4b191b48 | 459 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0"); |
6a488035 TO |
460 | } |
461 | elseif (is_numeric($role)) { | |
53133bb0 | 462 | $roleTypes = (int) $role; |
6a488035 | 463 | } |
d5bd1f7e | 464 | $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes; |
53133bb0 | 465 | } |
466 | ||
1273d77c | 467 | $type = CRM_Utils_Request::retrieve('type', 'Positive'); |
6a488035 TO |
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 | ||
0cf587a7 EM |
485 | /** |
486 | * @return null | |
487 | */ | |
00be9182 | 488 | public function getFormValues() { |
6a488035 TO |
489 | return NULL; |
490 | } | |
491 | ||
492 | /** | |
493 | * Return a descriptive name for the page, used in wizard header | |
494 | * | |
495 | * @return string | |
6a488035 TO |
496 | */ |
497 | public function getTitle() { | |
498 | return ts('Find Participants'); | |
499 | } | |
96025800 | 500 | |
6a488035 | 501 | } |