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