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