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