CRM-19649 - Multiple wysiwyg presets
[civicrm-core.git] / CRM / Event / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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');
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, 0, FALSE, NULL, array('event_id'));
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 if (CRM_Core_Permission::check('access deleted contacts') and Civi::settings()->get('contact_undelete')) {
174 $this->addElement('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)'));
175 }
176
177 CRM_Event_BAO_Query::buildSearchForm($this);
178
179 $rows = $this->get('rows');
180 if (is_array($rows)) {
181 $lineItems = $eventIds = array();
182 if (!$this->_single) {
183 $this->addRowSelectors($rows);
184 }
185 foreach ($rows as $row) {
186 $eventIds[$row['event_id']] = $row['event_id'];
187 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
188 // add line item details if applicable
189 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
190 }
191 }
192
193 //get actual count only when we are dealing w/ single event.
194 $participantCount = 0;
195 if (count($eventIds) == 1) {
196 //convert form values to clause.
197 $seatClause = array();
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 $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", '=', $this->_formValues['participant_status_id'], 'Int');
203 if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
204 $this->_formValues['participant_status_id'] = $status;
205 }
206 }
207 if (!empty($this->_formValues['participant_role_id'])) {
208 $escapedRoles = array();
209 foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
210 $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
211 }
212 $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
213 }
214
215 // CRM-15379
216 if (!empty($this->_formValues['participant_fee_id'])) {
217 $participant_fee_id = $this->_formValues['participant_fee_id'];
218 foreach ($participant_fee_id as $k => &$val) {
219 $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
220 $val = CRM_Core_DAO::escapeString(trim($val));
221 }
222 $feeLabel = implode('|', $participant_fee_id);
223 $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
224 }
225
226 $seatClause = implode(' AND ', $seatClause);
227 $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
228 }
229 $this->assign('participantCount', $participantCount);
230 $this->assign('lineItems', $lineItems);
231
232 $permission = CRM_Core_Permission::getPermission();
233
234 $tasks = CRM_Event_Task::permissionedTaskTitles($permission);
235 if (isset($this->_ssID)) {
236 if ($permission == CRM_Core_Permission::EDIT) {
237 $tasks = $tasks + CRM_Event_Task::optionalTaskTitle();
238 }
239
240 $savedSearchValues = array(
241 'id' => $this->_ssID,
242 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
243 );
244 $this->assign_by_ref('savedSearch', $savedSearchValues);
245 $this->assign('ssID', $this->_ssID);
246 }
247
248 $this->addTaskMenu($tasks);
249 }
250
251 }
252
253 /**
254 * Get the label for the sortName field if email searching is on.
255 *
256 * (email searching is a setting under search preferences).
257 *
258 * @return string
259 */
260 protected function getSortNameLabelWithEmail() {
261 return ts('Participant Name or Email');
262 }
263
264 /**
265 * Get the label for the sortName field if email searching is off.
266 *
267 * (email searching is a setting under search preferences).
268 *
269 * @return string
270 */
271 protected function getSortNameLabelWithOutEmail() {
272 return ts('Participant Name');
273 }
274
275 /**
276 * The post processing of the form gets done here.
277 *
278 * Key things done during post processing are
279 * - check for reset or next request. if present, skip post procesing.
280 * - now check if user requested running a saved search, if so, then
281 * the form values associated with the saved search are used for searching.
282 * - if user has done a submit with new values the regular post submissing is
283 * done.
284 * The processing consists of using a Selector / Controller framework for getting the
285 * search results.
286 *
287 * @param
288 *
289 * @return void
290 */
291 public function postProcess() {
292 if ($this->_done) {
293 return;
294 }
295
296 $this->_done = TRUE;
297
298 if (!empty($_POST)) {
299 $this->_formValues = $this->controller->exportValues($this->_name);
300 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, array('participant_status_id'));
301 }
302
303 if (empty($this->_formValues)) {
304 $this->_formValues = $this->controller->exportValues($this->_name);
305 }
306
307 $this->fixFormValues();
308
309 if (isset($this->_ssID) && empty($_POST)) {
310 // if we are editing / running a saved search and the form has not been posted
311 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
312 }
313
314 // We don't show test records in summaries or dashboards
315 if (empty($this->_formValues['participant_test']) && $this->_force) {
316 $this->_formValues["participant_test"] = 0;
317 }
318
319 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
320
321 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id'));
322
323 $this->set('formValues', $this->_formValues);
324 $this->set('queryParams', $this->_queryParams);
325
326 $buttonName = $this->controller->getButtonName();
327 if ($buttonName == $this->_actionButtonName) {
328 // check actionName and if next, then do not repeat a search, since we are going to the next page
329
330 // hack, make sure we reset the task values
331 $stateMachine = $this->controller->getStateMachine();
332 $formName = $stateMachine->getTaskFormName();
333 $this->controller->resetPage($formName);
334 return;
335 }
336
337 $sortID = NULL;
338 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
339 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
340 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
341 );
342 }
343
344 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id'));
345
346 $selector = new CRM_Event_Selector_Search($this->_queryParams,
347 $this->_action,
348 NULL,
349 $this->_single,
350 $this->_limit,
351 $this->_context
352 );
353
354 $selector->setKey($this->controller->_key);
355
356 $prefix = NULL;
357 if ($this->_context == 'user') {
358 $prefix = $this->_prefix;
359 }
360
361 $this->assign("{$prefix}limit", $this->_limit);
362 $this->assign("{$prefix}single", $this->_single);
363
364 $controller = new CRM_Core_Selector_Controller($selector,
365 $this->get(CRM_Utils_Pager::PAGE_ID),
366 $sortID,
367 CRM_Core_Action::VIEW,
368 $this,
369 CRM_Core_Selector_Controller::SESSION,
370 $prefix
371 );
372 $controller->setEmbedded(TRUE);
373
374 $query = $selector->getQuery();
375 if ($this->_context == 'user') {
376 $query->setSkipPermission(TRUE);
377 }
378 $controller->run();
379 }
380
381 /**
382 * add the rules (mainly global rules) for form.
383 * All local rules are added near the element
384 *
385 * @return void
386 * @see valid_date
387 */
388 public function addRules() {
389 }
390
391 /**
392 * Set the default form values.
393 *
394 *
395 * @return array
396 * the default array reference
397 */
398 public function setDefaultValues() {
399 $defaults = array();
400 $defaults = $this->_formValues;
401 return $defaults;
402 }
403
404 public function fixFormValues() {
405 // if this search has been forced
406 // then see if there are any get values, and if so over-ride the post values
407 // note that this means that GET over-rides POST :)
408 $event = CRM_Utils_Request::retrieve('event', 'Positive');
409 if ($event) {
410 $this->_formValues['event_id'] = $event;
411 $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
412 }
413
414 $status = CRM_Utils_Request::retrieve('status', 'String');
415
416 if (isset($status)) {
417 if ($status === 'true') {
418 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
419 }
420 elseif ($status === 'false') {
421 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
422 }
423 elseif (is_numeric($status)) {
424 $statusTypes = (int) $status;
425 }
426 elseif (is_array($status) && !array_key_exists('IN', $status)) {
427 $statusTypes = array_keys($status);
428 }
429 $this->_formValues['participant_status_id'] = is_array($statusTypes) ? array('IN' => array_keys($statusTypes)) : $statusTypes;
430 }
431
432 $role = CRM_Utils_Request::retrieve('role', 'String');
433
434 if (isset($role)) {
435 if ($role === 'true') {
436 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
437 }
438 elseif ($role === 'false') {
439 $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
440 }
441 elseif (is_numeric($role)) {
442 $roleTypes = (int) $role;
443 }
444 $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
445 }
446
447 $type = CRM_Utils_Request::retrieve('type', 'Positive');
448 if ($type) {
449 $this->_formValues['event_type'] = $type;
450 }
451
452 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
453
454 if ($cid) {
455 $cid = CRM_Utils_Type::escape($cid, 'Integer');
456 if ($cid > 0) {
457 $this->_formValues['contact_id'] = $cid;
458
459 // also assign individual mode to the template
460 $this->_single = TRUE;
461 }
462 }
463 }
464
465 /**
466 * @return null
467 */
468 public function getFormValues() {
469 return NULL;
470 }
471
472 /**
473 * Return a descriptive name for the page, used in wizard header
474 *
475 * @return string
476 */
477 public function getTitle() {
478 return ts('Find Participants');
479 }
480
481 }