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