Merge remote branch 'canonical/master' into merge-20140930
[civicrm-core.git] / CRM / Event / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @access protected
50 */
51 protected $_queryParams;
52
53 /**
54 * are we restricting ourselves to a single contact
55 *
56 * @access protected
57 * @var boolean
58 */
59 protected $_single = FALSE;
60
61 /**
62 * are we restricting ourselves to a single contact
63 *
64 * @access protected
65 * @var boolean
66 */
67 protected $_limit = NULL;
68
69 /**
70 * prefix for the controller
71 *
72 */
73 protected $_prefix = "event_";
74
75 protected $_defaults;
76
77 /**
78 * the saved search ID retrieved from the GET vars
79 *
80 * @var int
81 * @access protected
82 */
83 protected $_ssID;
84
85 /**
86 * processing needed for buildForm and later
87 *
88 * @return void
89 * @access public
90 */
91 function preProcess() {
92 $this->set('searchFormName', 'Search');
93
94 /**
95 * set the button names
96 */
97 $this->_searchButtonName = $this->getButtonName('refresh');
98 $this->_actionButtonName = $this->getButtonName('next', 'action');
99
100 $this->_done = FALSE;
101 $this->defaults = array();
102
103 /*
104 * we allow the controller to set force/reset externally, useful when we are being
105 * driven by the wizard framework
106 */
107 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
108 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
109 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
110 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
111 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
112 $this->assign("context", $this->_context);
113
114 // get user submitted values
115 // get it from controller only if form has been submitted, else preProcess has set this
116 if (!empty($_POST) && !$this->controller->isModal()) {
117 $this->_formValues = $this->controller->exportValues($this->_name);
118 }
119 else {
120 $this->_formValues = $this->get('formValues');
121 }
122
123 if (empty($this->_formValues)) {
124 if (isset($this->_ssID)) {
125 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
126 }
127 }
128
129 if ($this->_force) {
130 $this->postProcess();
131 $this->set('force', 0);
132 }
133
134 $sortID = NULL;
135 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
136 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
137 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
138 );
139 }
140
141 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
142 $selector = new CRM_Event_Selector_Search($this->_queryParams,
143 $this->_action,
144 NULL,
145 $this->_single,
146 $this->_limit,
147 $this->_context
148 );
149 $prefix = NULL;
150 if ($this->_context == 'user') {
151 $prefix = $this->_prefix;
152 }
153
154 $this->assign("{$prefix}limit", $this->_limit);
155 $this->assign("{$prefix}single", $this->_single);
156
157 $controller = new CRM_Core_Selector_Controller($selector,
158 $this->get(CRM_Utils_Pager::PAGE_ID),
159 $sortID,
160 CRM_Core_Action::VIEW,
161 $this,
162 CRM_Core_Selector_Controller::TRANSFER,
163 $prefix
164 );
165 $controller->setEmbedded(TRUE);
166 $controller->moveFromSessionToTemplate();
167
168 $this->assign('summary', $this->get('summary'));
169 }
170
171 /**
172 * Build the form
173 *
174 * @access public
175 *
176 * @return void
177 */
178 function buildQuickForm() {
179 parent::buildQuickForm();
180 $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
181
182 CRM_Event_BAO_Query::buildSearchForm($this);
183
184 $rows = $this->get('rows');
185 if (is_array($rows)) {
186 $lineItems = $eventIds = array();
187 if (!$this->_single) {
188 $this->addRowSelectors($rows);
189 }
190 foreach ($rows as $row) {
191 $eventIds[$row['event_id']] = $row['event_id'];
192 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
193 // add line item details if applicable
194 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
195 }
196 }
197
198 //get actual count only when we are dealing w/ single event.
199 $participantCount = 0;
200 if (count($eventIds) == 1) {
201 //convert form values to clause.
202 $seatClause = array();
203 // Filter on is_test if specified in search form
204 if (CRM_Utils_Array::value('participant_test', $this->_formValues) == '1' || CRM_Utils_Array::value('participant_test', $this->_formValues) == '0' ) {
205 $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
206 }
207 if (!empty($this->_formValues['participant_status_id'])) {
208 $statuses = array_keys($this->_formValues['participant_status_id']);
209 $seatClause[] = '( participant.status_id IN ( ' . implode(' , ', $statuses) . ' ) )';
210 }
211 if (!empty($this->_formValues['participant_role_id'])) {
212 $roles = array_keys($this->_formValues['participant_role_id']);
213 $seatClause[] = '( participant.role_id IN ( ' . implode(' , ', $roles) . ' ) )';
214 }
215 $clause = NULL;
216 if (!empty($seatClause)) {
217 $clause = implode(' AND ', $seatClause);
218 }
219
220 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $clause);
221 }
222 $this->assign('participantCount', $participantCount);
223 $this->assign('lineItems', $lineItems);
224
225 $permission = CRM_Core_Permission::getPermission();
226
227 $tasks = CRM_Event_Task::permissionedTaskTitles($permission);
228 if (isset($this->_ssID)) {
229 if ($permission == CRM_Core_Permission::EDIT) {
230 $tasks = $tasks + CRM_Event_Task::optionalTaskTitle();
231 }
232
233 $savedSearchValues = array(
234 'id' => $this->_ssID,
235 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
236 );
237 $this->assign_by_ref('savedSearch', $savedSearchValues);
238 $this->assign('ssID', $this->_ssID);
239 }
240
241 $this->addTaskMenu($tasks);
242 }
243
244 }
245
246 /**
247 * The post processing of the form gets done here.
248 *
249 * Key things done during post processing are
250 * - check for reset or next request. if present, skip post procesing.
251 * - now check if user requested running a saved search, if so, then
252 * the form values associated with the saved search are used for searching.
253 * - if user has done a submit with new values the regular post submissing is
254 * done.
255 * The processing consists of using a Selector / Controller framework for getting the
256 * search results.
257 *
258 * @param
259 *
260 * @return void
261 * @access public
262 */
263 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 * @access public
358 * @see valid_date
359 */
360 function addRules() {}
361
362 /**
363 * Set the default form values
364 *
365 * @access protected
366 *
367 * @return array the default array reference
368 */
369 function setDefaultValues() {
370 $defaults = array();
371 $defaults = $this->_formValues;
372 return $defaults;
373 }
374
375 function fixFormValues() {
376 // if this search has been forced
377 // then see if there are any get values, and if so over-ride the post values
378 // note that this means that GET over-rides POST :)
379 $event = CRM_Utils_Request::retrieve('event', 'Positive',
380 CRM_Core_DAO::$_nullObject
381 );
382 if ($event) {
383 $this->_formValues['event_id'] = $event;
384 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
385 }
386
387 $status = CRM_Utils_Request::retrieve('status', 'String',
388 CRM_Core_DAO::$_nullObject
389 );
390
391 if (isset($status)) {
392 if ($status === 'true') {
393 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
394 }
395 elseif ($status === 'false') {
396 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
397 }
398 elseif (is_numeric($status)) {
399 $status = (int) $status;
400 $statusTypes = array($status => CRM_Event_PseudoConstant::participantStatus($status));
401 }
402 $status = array();
403 foreach ($statusTypes as $key => $value) {
404 $status[$key] = 1;
405 }
406 $this->_formValues['participant_status_id'] = $status;
407 }
408
409 $role = CRM_Utils_Request::retrieve('role', 'String',
410 CRM_Core_DAO::$_nullObject
411 );
412
413 if (isset($role)) {
414 if ($role === 'true') {
415 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
416 }
417 elseif ($role === 'false') {
418 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
419 }
420 elseif (is_numeric($role)) {
421 $role = (int) $role;
422 $roleTypes = array($role => CRM_Event_PseudoConstant::participantRole($role));
423 }
424 $role = array();
425 foreach ($roleTypes as $key => $value) {
426 $role[$key] = 1;
427 }
428 $this->_formValues['participant_role_id'] = $role;
429 }
430
431 $type = CRM_Utils_Request::retrieve('type', 'Positive',
432 CRM_Core_DAO::$_nullObject
433 );
434 if ($type) {
435 $this->_formValues['event_type'] = $type;
436 }
437
438 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
439
440 if ($cid) {
441 $cid = CRM_Utils_Type::escape($cid, 'Integer');
442 if ($cid > 0) {
443 $this->_formValues['contact_id'] = $cid;
444
445 // also assign individual mode to the template
446 $this->_single = TRUE;
447 }
448 }
449 }
450
451 /**
452 * @return null
453 */
454 function getFormValues() {
455 return NULL;
456 }
457
458 /**
459 * Return a descriptive name for the page, used in wizard header
460 *
461 * @return string
462 * @access public
463 */
464 public function getTitle() {
465 return ts('Find Participants');
466 }
467 }
468