Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | /** |
100fef9d | 46 | * The params that are sent to the query |
6a488035 TO |
47 | * |
48 | * @var array | |
6a488035 TO |
49 | */ |
50 | protected $_queryParams; | |
51 | ||
6a488035 | 52 | /** |
100fef9d | 53 | * Are we restricting ourselves to a single contact |
6a488035 | 54 | * |
6a488035 TO |
55 | * @var boolean |
56 | */ | |
57 | protected $_single = FALSE; | |
58 | ||
59 | /** | |
100fef9d | 60 | * Are we restricting ourselves to a single contact |
6a488035 | 61 | * |
6a488035 TO |
62 | * @var boolean |
63 | */ | |
64 | protected $_limit = NULL; | |
65 | ||
6a488035 | 66 | /** |
100fef9d | 67 | * Prefix for the controller |
6a488035 TO |
68 | */ |
69 | protected $_prefix = "event_"; | |
70 | ||
71 | protected $_defaults; | |
72 | ||
73 | /** | |
100fef9d | 74 | * The saved search ID retrieved from the GET vars |
6a488035 TO |
75 | * |
76 | * @var int | |
6a488035 TO |
77 | */ |
78 | protected $_ssID; | |
79 | ||
80 | /** | |
100fef9d | 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 | /** | |
c490a46a | 166 | * Build the form object |
6a488035 | 167 | * |
6a488035 TO |
168 | * |
169 | * @return void | |
170 | */ | |
00be9182 | 171 | public function buildQuickForm() { |
3efb5b86 | 172 | parent::buildQuickForm(); |
6a488035 TO |
173 | $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
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(); | |
7c75c017 | 196 | // CRM-15379 |
3a6eb174 | 197 | $clause = NULL; |
7c75c017 | 198 | if (!empty($this->_formValues['participant_fee_id'])) { |
199 | $participant_fee_id = $this->_formValues['participant_fee_id']; | |
200 | $feeLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $participant_fee_id, 'label'); | |
201 | $feeLabel = CRM_Core_DAO::escapeString(trim($feeLabel)); | |
3a6eb174 | 202 | $clause = " ( participant.fee_level LIKE '%$feeLabel%' ) "; |
6a488035 | 203 | } |
59adcdd9 | 204 | |
6a488035 TO |
205 | $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $clause); |
206 | } | |
207 | $this->assign('participantCount', $participantCount); | |
208 | $this->assign('lineItems', $lineItems); | |
209 | ||
6a488035 TO |
210 | $permission = CRM_Core_Permission::getPermission(); |
211 | ||
34197a55 | 212 | $tasks = CRM_Event_Task::permissionedTaskTitles($permission); |
6a488035 TO |
213 | if (isset($this->_ssID)) { |
214 | if ($permission == CRM_Core_Permission::EDIT) { | |
215 | $tasks = $tasks + CRM_Event_Task::optionalTaskTitle(); | |
216 | } | |
217 | ||
218 | $savedSearchValues = array( | |
219 | 'id' => $this->_ssID, | |
220 | 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'), | |
221 | ); | |
222 | $this->assign_by_ref('savedSearch', $savedSearchValues); | |
223 | $this->assign('ssID', $this->_ssID); | |
224 | } | |
225 | ||
34197a55 | 226 | $this->addTaskMenu($tasks); |
6a488035 TO |
227 | } |
228 | ||
6a488035 TO |
229 | } |
230 | ||
231 | /** | |
232 | * The post processing of the form gets done here. | |
233 | * | |
234 | * Key things done during post processing are | |
235 | * - check for reset or next request. if present, skip post procesing. | |
236 | * - now check if user requested running a saved search, if so, then | |
237 | * the form values associated with the saved search are used for searching. | |
238 | * - if user has done a submit with new values the regular post submissing is | |
239 | * done. | |
240 | * The processing consists of using a Selector / Controller framework for getting the | |
241 | * search results. | |
242 | * | |
243 | * @param | |
244 | * | |
245 | * @return void | |
6a488035 | 246 | */ |
00be9182 | 247 | public function postProcess() { |
6a488035 TO |
248 | if ($this->_done) { |
249 | return; | |
250 | } | |
251 | ||
252 | $this->_done = TRUE; | |
253 | ||
254 | if (!empty($_POST)) { | |
255 | $this->_formValues = $this->controller->exportValues($this->_name); | |
e51b7372 | 256 | foreach (array('participant_status_id', 'participant_role_id') as $element) { |
257 | $value = CRM_Utils_Array::value($element, $this->_formValues); | |
258 | if ($value && is_array($value)) { | |
259 | $this->_formValues[$element] = array('IN' => $value); | |
260 | } | |
261 | } | |
6a488035 TO |
262 | } |
263 | ||
264 | if (empty($this->_formValues)) { | |
265 | $this->_formValues = $this->controller->exportValues($this->_name); | |
266 | } | |
267 | ||
268 | $this->fixFormValues(); | |
269 | ||
270 | if (isset($this->_ssID) && empty($_POST)) { | |
271 | // if we are editing / running a saved search and the form has not been posted | |
272 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
273 | } | |
274 | ||
d43b88cc CW |
275 | // We don't show test records in summaries or dashboards |
276 | if (empty($this->_formValues['participant_test']) && $this->_force) { | |
6a488035 TO |
277 | $this->_formValues["participant_test"] = 0; |
278 | } | |
279 | ||
280 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); | |
281 | ||
282 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
283 | ||
284 | $this->set('formValues', $this->_formValues); | |
285 | $this->set('queryParams', $this->_queryParams); | |
286 | ||
287 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 288 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
289 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
290 | ||
291 | // hack, make sure we reset the task values | |
a5611c8e | 292 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
293 | $formName = $stateMachine->getTaskFormName(); |
294 | $this->controller->resetPage($formName); | |
295 | return; | |
296 | } | |
297 | ||
298 | $sortID = NULL; | |
299 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
300 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
301 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
302 | ); | |
303 | } | |
304 | ||
305 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
306 | ||
307 | $selector = new CRM_Event_Selector_Search($this->_queryParams, | |
308 | $this->_action, | |
309 | NULL, | |
310 | $this->_single, | |
311 | $this->_limit, | |
312 | $this->_context | |
313 | ); | |
314 | ||
315 | $selector->setKey($this->controller->_key); | |
316 | ||
317 | $prefix = NULL; | |
318 | if ($this->_context == 'user') { | |
319 | $prefix = $this->_prefix; | |
320 | } | |
321 | ||
322 | $this->assign("{$prefix}limit", $this->_limit); | |
323 | $this->assign("{$prefix}single", $this->_single); | |
324 | ||
325 | $controller = new CRM_Core_Selector_Controller($selector, | |
326 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
327 | $sortID, | |
328 | CRM_Core_Action::VIEW, | |
329 | $this, | |
330 | CRM_Core_Selector_Controller::SESSION, | |
331 | $prefix | |
332 | ); | |
333 | $controller->setEmbedded(TRUE); | |
334 | ||
a5611c8e | 335 | $query = $selector->getQuery(); |
6a488035 TO |
336 | if ($this->_context == 'user') { |
337 | $query->setSkipPermission(TRUE); | |
338 | } | |
339 | $controller->run(); | |
340 | } | |
341 | ||
342 | /** | |
dc195289 | 343 | * add the rules (mainly global rules) for form. |
6a488035 TO |
344 | * All local rules are added near the element |
345 | * | |
355ba699 | 346 | * @return void |
6a488035 TO |
347 | * @see valid_date |
348 | */ | |
6ea503d4 TO |
349 | public function addRules() { |
350 | } | |
6a488035 TO |
351 | |
352 | /** | |
353 | * Set the default form values | |
354 | * | |
6a488035 | 355 | * |
a6c01b45 CW |
356 | * @return array |
357 | * the default array reference | |
6a488035 | 358 | */ |
00be9182 | 359 | public function setDefaultValues() { |
6a488035 TO |
360 | $defaults = array(); |
361 | $defaults = $this->_formValues; | |
362 | return $defaults; | |
363 | } | |
364 | ||
00be9182 | 365 | public function fixFormValues() { |
6a488035 TO |
366 | // if this search has been forced |
367 | // then see if there are any get values, and if so over-ride the post values | |
368 | // note that this means that GET over-rides POST :) | |
369 | $event = CRM_Utils_Request::retrieve('event', 'Positive', | |
370 | CRM_Core_DAO::$_nullObject | |
371 | ); | |
372 | if ($event) { | |
373 | $this->_formValues['event_id'] = $event; | |
374 | $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE); | |
375 | } | |
376 | ||
377 | $status = CRM_Utils_Request::retrieve('status', 'String', | |
378 | CRM_Core_DAO::$_nullObject | |
379 | ); | |
380 | ||
381 | if (isset($status)) { | |
382 | if ($status === 'true') { | |
383 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1"); | |
384 | } | |
385 | elseif ($status === 'false') { | |
386 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0"); | |
387 | } | |
388 | elseif (is_numeric($status)) { | |
389 | $status = (int) $status; | |
390 | $statusTypes = array($status => CRM_Event_PseudoConstant::participantStatus($status)); | |
391 | } | |
392 | $status = array(); | |
393 | foreach ($statusTypes as $key => $value) { | |
394 | $status[$key] = 1; | |
395 | } | |
396 | $this->_formValues['participant_status_id'] = $status; | |
397 | } | |
398 | ||
399 | $role = CRM_Utils_Request::retrieve('role', 'String', | |
400 | CRM_Core_DAO::$_nullObject | |
401 | ); | |
402 | ||
403 | if (isset($role)) { | |
404 | if ($role === 'true') { | |
405 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1"); | |
406 | } | |
407 | elseif ($role === 'false') { | |
408 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0"); | |
409 | } | |
410 | elseif (is_numeric($role)) { | |
411 | $role = (int) $role; | |
412 | $roleTypes = array($role => CRM_Event_PseudoConstant::participantRole($role)); | |
413 | } | |
414 | $role = array(); | |
415 | foreach ($roleTypes as $key => $value) { | |
416 | $role[$key] = 1; | |
417 | } | |
418 | $this->_formValues['participant_role_id'] = $role; | |
419 | } | |
420 | ||
421 | $type = CRM_Utils_Request::retrieve('type', 'Positive', | |
422 | CRM_Core_DAO::$_nullObject | |
423 | ); | |
424 | if ($type) { | |
425 | $this->_formValues['event_type'] = $type; | |
426 | } | |
427 | ||
428 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
429 | ||
430 | if ($cid) { | |
431 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
432 | if ($cid > 0) { | |
433 | $this->_formValues['contact_id'] = $cid; | |
434 | ||
435 | // also assign individual mode to the template | |
436 | $this->_single = TRUE; | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
0cf587a7 EM |
441 | /** |
442 | * @return null | |
443 | */ | |
00be9182 | 444 | public function getFormValues() { |
6a488035 TO |
445 | return NULL; |
446 | } | |
447 | ||
448 | /** | |
449 | * Return a descriptive name for the page, used in wizard header | |
450 | * | |
451 | * @return string | |
6a488035 TO |
452 | */ |
453 | public function getTitle() { | |
454 | return ts('Find Participants'); | |
455 | } | |
96025800 | 456 | |
6a488035 | 457 | } |