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