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