Merge pull request #7211 from agh1/fixStatusAlert
[civicrm-core.git] / CRM / Event / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 protected $_prefix = "event_";
70
71 /**
72 * The saved search ID retrieved from the GET vars.
73 *
74 * @var int
75 */
76 protected $_ssID;
77
78 /**
79 * Processing needed for buildForm and later.
80 *
81 * @return void
82 */
83 public function preProcess() {
84 $this->set('searchFormName', 'Search');
85
86 /**
87 * set the button names
88 */
89 $this->_searchButtonName = $this->getButtonName('refresh');
90 $this->_actionButtonName = $this->getButtonName('next', 'action');
91
92 $this->_done = FALSE;
93 $this->defaults = array();
94
95 /*
96 * we allow the controller to set force/reset externally, useful when we are being
97 * driven by the wizard framework
98 */
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);
102 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
103 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
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 /**
164 * Build the form object.
165 *
166 *
167 * @return void
168 */
169 public function buildQuickForm() {
170 parent::buildQuickForm();
171 $this->addSortNameField();
172
173 CRM_Event_BAO_Query::buildSearchForm($this);
174
175 $rows = $this->get('rows');
176 if (is_array($rows)) {
177 $lineItems = $eventIds = array();
178 if (!$this->_single) {
179 $this->addRowSelectors($rows);
180 }
181 foreach ($rows as $row) {
182 $eventIds[$row['event_id']] = $row['event_id'];
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();
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'])) {
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 }
202 }
203 if (!empty($this->_formValues['participant_role_id'])) {
204 $seatClause[] = '( participant.role_id IN ( ' . implode(' , ', (array) $this->_formValues['participant_role_id']) . ' ) )';
205 }
206
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));
212 $seatClause[] = "( participant.fee_level LIKE '%$feeLabel%' )";
213 }
214
215 $seatClause = implode(' AND ', $seatClause);
216 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
217 }
218 $this->assign('participantCount', $participantCount);
219 $this->assign('lineItems', $lineItems);
220
221 $permission = CRM_Core_Permission::getPermission();
222
223 $tasks = CRM_Event_Task::permissionedTaskTitles($permission);
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
237 $this->addTaskMenu($tasks);
238 }
239
240 }
241
242 /**
243 * Get the label for the sortName field if email searching is on.
244 *
245 * (email searching is a setting under search preferences).
246 *
247 * @return string
248 */
249 protected function getSortNameLabelWithEmail() {
250 return ts('Participant Name or Email');
251 }
252
253 /**
254 * Get the label for the sortName field if email searching is off.
255 *
256 * (email searching is a setting under search preferences).
257 *
258 * @return string
259 */
260 protected function getSortNameLabelWithOutEmail() {
261 return ts('Participant Name');
262 }
263
264 /**
265 * The post processing of the form gets done here.
266 *
267 * Key things done during post processing are
268 * - check for reset or next request. if present, skip post procesing.
269 * - now check if user requested running a saved search, if so, then
270 * the form values associated with the saved search are used for searching.
271 * - if user has done a submit with new values the regular post submissing is
272 * done.
273 * The processing consists of using a Selector / Controller framework for getting the
274 * search results.
275 *
276 * @param
277 *
278 * @return void
279 */
280 public function postProcess() {
281 if ($this->_done) {
282 return;
283 }
284
285 $this->_done = TRUE;
286
287 if (!empty($_POST)) {
288 $this->_formValues = $this->controller->exportValues($this->_name);
289 }
290
291 if (empty($this->_formValues)) {
292 $this->_formValues = $this->controller->exportValues($this->_name);
293 }
294
295 $this->fixFormValues();
296
297 if (isset($this->_ssID) && empty($_POST)) {
298 // if we are editing / running a saved search and the form has not been posted
299 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
300 }
301
302 // We don't show test records in summaries or dashboards
303 if (empty($this->_formValues['participant_test']) && $this->_force) {
304 $this->_formValues["participant_test"] = 0;
305 }
306
307 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
308
309 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
310
311 $this->set('formValues', $this->_formValues);
312 $this->set('queryParams', $this->_queryParams);
313
314 $buttonName = $this->controller->getButtonName();
315 if ($buttonName == $this->_actionButtonName) {
316 // check actionName and if next, then do not repeat a search, since we are going to the next page
317
318 // hack, make sure we reset the task values
319 $stateMachine = $this->controller->getStateMachine();
320 $formName = $stateMachine->getTaskFormName();
321 $this->controller->resetPage($formName);
322 return;
323 }
324
325 $sortID = NULL;
326 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
327 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
328 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
329 );
330 }
331
332 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
333
334 $selector = new CRM_Event_Selector_Search($this->_queryParams,
335 $this->_action,
336 NULL,
337 $this->_single,
338 $this->_limit,
339 $this->_context
340 );
341
342 $selector->setKey($this->controller->_key);
343
344 $prefix = NULL;
345 if ($this->_context == 'user') {
346 $prefix = $this->_prefix;
347 }
348
349 $this->assign("{$prefix}limit", $this->_limit);
350 $this->assign("{$prefix}single", $this->_single);
351
352 $controller = new CRM_Core_Selector_Controller($selector,
353 $this->get(CRM_Utils_Pager::PAGE_ID),
354 $sortID,
355 CRM_Core_Action::VIEW,
356 $this,
357 CRM_Core_Selector_Controller::SESSION,
358 $prefix
359 );
360 $controller->setEmbedded(TRUE);
361
362 $query = $selector->getQuery();
363 if ($this->_context == 'user') {
364 $query->setSkipPermission(TRUE);
365 }
366 $controller->run();
367 }
368
369 /**
370 * add the rules (mainly global rules) for form.
371 * All local rules are added near the element
372 *
373 * @return void
374 * @see valid_date
375 */
376 public function addRules() {
377 }
378
379 /**
380 * Set the default form values.
381 *
382 *
383 * @return array
384 * the default array reference
385 */
386 public function setDefaultValues() {
387 $defaults = array();
388 $defaults = $this->_formValues;
389 return $defaults;
390 }
391
392 public function fixFormValues() {
393 // if this search has been forced
394 // then see if there are any get values, and if so over-ride the post values
395 // note that this means that GET over-rides POST :)
396 $event = CRM_Utils_Request::retrieve('event', 'Positive',
397 CRM_Core_DAO::$_nullObject
398 );
399 if ($event) {
400 $this->_formValues['event_id'] = $event;
401 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
402 }
403
404 $status = CRM_Utils_Request::retrieve('status', 'String',
405 CRM_Core_DAO::$_nullObject
406 );
407
408 if (isset($status)) {
409 if ($status === 'true') {
410 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
411 }
412 elseif ($status === 'false') {
413 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
414 }
415 elseif (is_numeric($status)) {
416 $statusTypes = (int) $status;
417 }
418
419 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? array('IN' => array_keys($statusTypes)) : $statusTypes;
420 }
421 elseif ($statusTypes = CRM_Utils_Array::value('participant_status_id', $this->_formValues)) {
422 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? array('IN' => $statusTypes) : $statusTypes;
423 }
424
425 $role = CRM_Utils_Request::retrieve('role', 'String',
426 CRM_Core_DAO::$_nullObject
427 );
428
429 if (isset($role)) {
430 if ($role === 'true') {
431 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
432 }
433 elseif ($role === 'false') {
434 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
435 }
436 elseif (is_numeric($role)) {
437 $roleTypes = (int) $role;
438 }
439 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
440 }
441
442 $type = CRM_Utils_Request::retrieve('type', 'Positive',
443 CRM_Core_DAO::$_nullObject
444 );
445 if ($type) {
446 $this->_formValues['event_type'] = $type;
447 }
448
449 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
450
451 if ($cid) {
452 $cid = CRM_Utils_Type::escape($cid, 'Integer');
453 if ($cid > 0) {
454 $this->_formValues['contact_id'] = $cid;
455
456 // also assign individual mode to the template
457 $this->_single = TRUE;
458 }
459 }
460 }
461
462 /**
463 * @return null
464 */
465 public function getFormValues() {
466 return NULL;
467 }
468
469 /**
470 * Return a descriptive name for the page, used in wizard header
471 *
472 * @return string
473 */
474 public function getTitle() {
475 return ts('Find Participants');
476 }
477
478 }