Merge pull request #13145 from civicrm/5.8
[civicrm-core.git] / CRM / Event / Page / ManageEvent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 * $Id$
33 *
34 */
35
36 /**
37 * Page for displaying list of events
38 */
39 class CRM_Event_Page_ManageEvent extends CRM_Core_Page {
40
41 /**
42 * The action links that we need to display for the browse screen.
43 *
44 * @var array
45 */
46 static $_actionLinks = NULL;
47
48 /**
49 * The event links to display for the browse screen.
50 * @var array
51 */
52 static $_eventLinks = NULL;
53
54 static $_links = NULL;
55
56 static $_tabLinks = NULL;
57
58 protected $_pager = NULL;
59
60 protected $_sortByCharacter;
61
62 protected $_isTemplate = FALSE;
63
64 /**
65 * Get action Links.
66 *
67 * @return array
68 * (reference) of action links
69 */
70 public function &links() {
71 if (!(self::$_actionLinks)) {
72 // helper variable for nicer formatting
73 $copyExtra = ts('Are you sure you want to make a copy of this Event?');
74 $deleteExtra = ts('Are you sure you want to delete this Event?');
75
76 self::$_actionLinks = array(
77 CRM_Core_Action::DISABLE => array(
78 'name' => ts('Disable'),
79 'ref' => 'crm-enable-disable',
80 'title' => ts('Disable Event'),
81 ),
82 CRM_Core_Action::ENABLE => array(
83 'name' => ts('Enable'),
84 'ref' => 'crm-enable-disable',
85 'title' => ts('Enable Event'),
86 ),
87 CRM_Core_Action::DELETE => array(
88 'name' => ts('Delete'),
89 'url' => CRM_Utils_System::currentPath(),
90 'qs' => 'action=delete&id=%%id%%',
91 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
92 'title' => ts('Delete Event'),
93 ),
94 CRM_Core_Action::COPY => array(
95 'name' => ts('Copy'),
96 'url' => CRM_Utils_System::currentPath(),
97 'qs' => 'reset=1&action=copy&id=%%id%%',
98 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
99 'title' => ts('Copy Event'),
100 ),
101 );
102 }
103 return self::$_actionLinks;
104 }
105
106 public function eventLinks() {
107 if (!(self::$_eventLinks)) {
108 self::$_eventLinks = [
109 'register_participant' => [
110 'name' => ts('Register Participant'),
111 'title' => ts('Register Participant'),
112 'url' => 'civicrm/participant/add',
113 'qs' => 'reset=1&action=add&context=standalone&eid=%%id%%',
114 ],
115 'event_info' => [
116 'name' => ts('Event Info'),
117 'title' => ts('Event Info'),
118 'url' => 'civicrm/event/info',
119 'qs' => 'reset=1&id=%%id%%',
120 'fe' => TRUE,
121 ],
122 'online_registration_test' => [
123 'name' => ts('Registration (Test-drive)'),
124 'title' => ts('Online Registration (Test-drive)'),
125 'url' => 'civicrm/event/register',
126 'qs' => 'reset=1&action=preview&id=%%id%%',
127 'fe' => TRUE,
128 ],
129 'online_registration_live' => [
130 'name' => ts('Registration (Live)'),
131 'title' => ts('Online Registration (Live)'),
132 'url' => 'civicrm/event/register',
133 'qs' => 'reset=1&id=%%id%%',
134 'fe' => TRUE,
135 ],
136 ];
137 }
138 return self::$_eventLinks;
139 }
140
141 /**
142 * Get tab Links for events.
143 *
144 * @param $enableCart
145 *
146 * @return array
147 * (reference) of tab links
148 */
149 public static function &tabs($enableCart) {
150 $cacheKey = $enableCart ? 1 : 0;
151 if (!(self::$_tabLinks)) {
152 self::$_tabLinks = array();
153 }
154 if (!isset(self::$_tabLinks[$cacheKey])) {
155 self::$_tabLinks[$cacheKey]['settings']
156 = array(
157 'title' => ts('Info and Settings'),
158 'url' => 'civicrm/event/manage/settings',
159 'field' => 'id',
160 );
161 self::$_tabLinks[$cacheKey]['location']
162 = array(
163 'title' => ts('Location'),
164 'url' => 'civicrm/event/manage/location',
165 'field' => 'loc_block_id',
166 );
167
168 self::$_tabLinks[$cacheKey]['fee']
169 = array(
170 'title' => ts('Fees'),
171 'url' => 'civicrm/event/manage/fee',
172 'field' => 'is_monetary',
173 );
174 self::$_tabLinks[$cacheKey]['registration']
175 = array(
176 'title' => ts('Online Registration'),
177 'url' => 'civicrm/event/manage/registration',
178 'field' => 'is_online_registration',
179 );
180
181 if (CRM_Core_Permission::check('administer CiviCRM') || CRM_Event_BAO_Event::checkPermission(NULL, CRM_Core_Permission::EDIT)) {
182 self::$_tabLinks[$cacheKey]['reminder']
183 = array(
184 'title' => ts('Schedule Reminders'),
185 'url' => 'civicrm/event/manage/reminder',
186 'field' => 'reminder',
187 );
188 }
189 self::$_tabLinks[$cacheKey]['conference']
190 = array(
191 'title' => ts('Conference Slots'),
192 'url' => 'civicrm/event/manage/conference',
193 'field' => 'slot_label_id',
194 );
195 self::$_tabLinks[$cacheKey]['friend']
196 = array(
197 'title' => ts('Tell a Friend'),
198 'url' => 'civicrm/event/manage/friend',
199 'field' => 'friend',
200 );
201 self::$_tabLinks[$cacheKey]['pcp']
202 = array(
203 'title' => ts('Personal Campaign Pages'),
204 'url' => 'civicrm/event/manage/pcp',
205 'field' => 'is_pcp_enabled',
206 );
207 self::$_tabLinks[$cacheKey]['repeat']
208 = array(
209 'title' => ts('Repeat'),
210 'url' => 'civicrm/event/manage/repeat',
211 'field' => 'is_repeating_event',
212 );
213 }
214
215 if (!$enableCart) {
216 unset(self::$_tabLinks[$cacheKey]['conference']);
217 }
218
219 CRM_Utils_Hook::tabset('civicrm/event/manage', self::$_tabLinks[$cacheKey], array());
220 return self::$_tabLinks[$cacheKey];
221 }
222
223 /**
224 * Run the page.
225 *
226 * This method is called after the page is created. It checks for the
227 * type of action and executes that action.
228 * Finally it calls the parent's run method.
229 *
230 * @return void
231 */
232 public function run() {
233 // get the requested action
234 $action = CRM_Utils_Request::retrieve('action', 'String',
235 // default to 'browse'
236 $this, FALSE, 'browse'
237 );
238
239 // assign vars to templates
240 $this->assign('action', $action);
241 $id = CRM_Utils_Request::retrieve('id', 'Positive',
242 $this, FALSE, 0, 'REQUEST'
243 );
244
245 // figure out whether we’re handling an event or an event template
246 if ($id) {
247 $this->_isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $id, 'is_template');
248 }
249 elseif ($action & CRM_Core_Action::ADD) {
250 $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
251 }
252
253 if (!$this->_isTemplate && $id) {
254 $breadCrumb = array(
255 array(
256 'title' => ts('Manage Events'),
257 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'),
258 ),
259 );
260 CRM_Utils_System::appendBreadCrumb($breadCrumb);
261 }
262
263 // what action to take ?
264 if ($action & CRM_Core_Action::DELETE) {
265 $session = CRM_Core_Session::singleton();
266 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse'));
267 $controller = new CRM_Core_Controller_Simple('CRM_Event_Form_ManageEvent_Delete',
268 'Delete Event',
269 $action
270 );
271 $controller->set('id', $id);
272 $controller->process();
273 return $controller->run();
274 }
275 elseif ($action & CRM_Core_Action::COPY) {
276 $this->copy();
277 }
278
279 // finally browse the custom groups
280 $this->browse();
281
282 // parent run
283 return parent::run();
284 }
285
286 /**
287 * Browse all events.
288 *
289 * @return void
290 */
291 public function browse() {
292 Civi::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
293
294 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
295 'String',
296 $this
297 );
298 $createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
299 if (strtolower($this->_sortByCharacter) == 'all' ||
300 !empty($_POST)
301 ) {
302 $this->_sortByCharacter = '';
303 $this->set('sortByCharacter', '');
304 }
305
306 $this->_force = $this->_searchResult = NULL;
307
308 $this->search();
309
310 $params = array();
311 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean',
312 $this, FALSE
313 );
314 $this->_searchResult = CRM_Utils_Request::retrieve('searchResult', 'Boolean', $this);
315
316 $whereClause = $this->whereClause($params, FALSE, $this->_force);
317 $this->pagerAToZ($whereClause, $params);
318
319 $params = array();
320 $whereClause = $this->whereClause($params, TRUE, $this->_force);
321 // because is_template != 1 would be to simple
322 $whereClause .= ' AND (is_template = 0 OR is_template IS NULL)';
323
324 $this->pager($whereClause, $params);
325
326 list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount();
327
328 // get all custom groups sorted by weight
329 $manageEvent = array();
330
331 $query = "
332 SELECT *
333 FROM civicrm_event
334 WHERE $whereClause
335 ORDER BY start_date desc
336 LIMIT $offset, $rowCount";
337
338 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Event_DAO_Event');
339 $permittedEventsByAction = CRM_Event_BAO_Event::getAllPermissions();
340
341 //get all campaigns.
342 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
343
344 // get the list of active event pcps
345 $eventPCPS = array();
346
347 $pcpDao = new CRM_PCP_DAO_PCPBlock();
348 $pcpDao->entity_table = 'civicrm_event';
349 $pcpDao->find();
350
351 while ($pcpDao->fetch()) {
352 $eventPCPS[$pcpDao->entity_id] = $pcpDao->entity_id;
353 }
354 // check if we're in shopping cart mode for events
355 $enableCart = Civi::settings()->get('enable_cart');
356 $this->assign('eventCartEnabled', $enableCart);
357 $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
358 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
359 )));
360 $eventType = CRM_Core_OptionGroup::values('event_type');
361 while ($dao->fetch()) {
362 if (in_array($dao->id, $permittedEventsByAction[CRM_Core_Permission::VIEW])) {
363 $manageEvent[$dao->id] = array();
364 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($dao->id, 'civicrm_event');
365 $manageEvent[$dao->id]['repeat'] = '';
366 if ($repeat) {
367 $manageEvent[$dao->id]['repeat'] = ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
368 }
369 CRM_Core_DAO::storeValues($dao, $manageEvent[$dao->id]);
370
371 // form all action links
372 $action = array_sum(array_keys($this->links()));
373
374 if ($dao->is_active) {
375 $action -= CRM_Core_Action::ENABLE;
376 }
377 else {
378 $action -= CRM_Core_Action::DISABLE;
379 }
380
381 if (!in_array($dao->id, $permittedEventsByAction[CRM_Core_Permission::DELETE])) {
382 $action -= CRM_Core_Action::DELETE;
383 }
384 if (!in_array($dao->id, $permittedEventsByAction[CRM_Core_Permission::EDIT])) {
385 $action -= CRM_Core_Action::UPDATE;
386 }
387
388 $eventLinks = self::eventLinks();
389 if (!CRM_Core_Permission::check('edit event participants')) {
390 unset($eventLinks['register_participant']);
391 }
392
393 $manageEvent[$dao->id]['eventlinks'] = CRM_Core_Action::formLink($eventLinks,
394 NULL,
395 array('id' => $dao->id),
396 ts('Event Links'),
397 TRUE,
398 'event.manage.eventlinks',
399 'Event',
400 $dao->id
401 );
402 $manageEvent[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(),
403 $action,
404 array('id' => $dao->id),
405 ts('more'),
406 TRUE,
407 'event.manage.list',
408 'Event',
409 $dao->id
410 );
411
412 $params = array(
413 'entity_id' => $dao->id,
414 'entity_table' => 'civicrm_event',
415 'is_active' => 1,
416 );
417
418 $defaults['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
419
420 $manageEvent[$dao->id]['friend'] = CRM_Friend_BAO_Friend::getValues($params);
421
422 if (isset($defaults['location']['address'][1]['city'])) {
423 $manageEvent[$dao->id]['city'] = $defaults['location']['address'][1]['city'];
424 }
425 if (isset($defaults['location']['address'][1]['state_province_id'])) {
426 $manageEvent[$dao->id]['state_province'] = CRM_Core_PseudoConstant::stateProvince($defaults['location']['address'][1]['state_province_id']);
427 }
428
429 //show campaigns on selector.
430 $manageEvent[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
431 $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->getId());
432 $manageEvent[$dao->id]['is_pcp_enabled'] = CRM_Utils_Array::value($dao->id, $eventPCPS);
433 $manageEvent[$dao->id]['event_type'] = CRM_Utils_Array::value($manageEvent[$dao->id]['event_type_id'], $eventType);
434 $manageEvent[$dao->id]['is_repeating_event'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_RecurringEntity', $dao->id, 'parent_id', 'entity_id');
435 // allow hooks to set 'field' value which allows configuration pop-up to show a tab as enabled/disabled
436 CRM_Utils_Hook::tabset('civicrm/event/manage/rows', $manageEvent, array('event_id' => $dao->id));
437 }
438 }
439
440 $manageEvent['tab'] = self::tabs($enableCart);
441 $this->assign('rows', $manageEvent);
442
443 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
444 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
445 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
446 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
447 $this->assign('findParticipants', $findParticipants);
448 }
449
450 /**
451 * make a copy of a Event, including
452 * all the fields in the event wizard
453 *
454 * @return void
455 * @throws \CRM_Core_Exception
456 */
457 public function copy() {
458 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE, 0, 'GET');
459
460 $urlString = 'civicrm/event/manage';
461 $copyEvent = CRM_Event_BAO_Event::copy($id);
462 $urlParams = 'reset=1';
463 // Redirect to Copied Event Configuration
464 if ($copyEvent->id) {
465 $urlString = 'civicrm/event/manage/settings';
466 $urlParams .= '&action=update&id=' . $copyEvent->id;
467 }
468
469 return CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams));
470 }
471
472 public function search() {
473 if (isset($this->_action) & (CRM_Core_Action::ADD |
474 CRM_Core_Action::UPDATE |
475 CRM_Core_Action::DELETE
476 )
477 ) {
478 return;
479 }
480
481 $form = new CRM_Core_Controller_Simple('CRM_Event_Form_SearchEvent', ts('Search Events'), CRM_Core_Action::ADD);
482 $form->setEmbedded(TRUE);
483 $form->setParent($this);
484 $form->process();
485 $form->run();
486 }
487
488 /**
489 * @param array $params
490 * @param bool $sortBy
491 * @param $force
492 *
493 * @return string
494 */
495 public function whereClause(&$params, $sortBy = TRUE, $force) {
496 $values = array();
497 $clauses = array();
498 $title = $this->get('title');
499 $createdId = $this->get('cid');
500
501 if ($createdId) {
502 $clauses[] = "(created_id = {$createdId})";
503 }
504
505 if ($title) {
506 $clauses[] = "title LIKE %1";
507 if (strpos($title, '%') !== FALSE) {
508 $params[1] = array(trim($title), 'String', FALSE);
509 }
510 else {
511 $params[1] = array(trim($title), 'String', TRUE);
512 }
513 }
514
515 $value = $this->get('event_type_id');
516 if ($value) {
517 if (is_array($value)) {
518 $type = implode(',', $value);
519 }
520 $clauses[] = "event_type_id IN ({$type})";
521 }
522
523 $eventsByDates = $this->get('eventsByDates');
524 if ($this->_searchResult) {
525 if ($eventsByDates) {
526
527 $from = $this->get('start_date');
528 if (!CRM_Utils_System::isNull($from)) {
529 $clauses[] = '( end_date >= %3 OR end_date IS NULL )';
530 $params[3] = array($from, 'String');
531 }
532
533 $to = $this->get('end_date');
534 if (!CRM_Utils_System::isNull($to)) {
535 $clauses[] = '( start_date <= %4 OR start_date IS NULL )';
536 $params[4] = array($to, 'String');
537 }
538 }
539 else {
540 $curDate = date('YmdHis');
541 $clauses[] = "(end_date >= {$curDate} OR end_date IS NULL)";
542 }
543 }
544 else {
545 $curDate = date('YmdHis');
546 $clauses[] = "(end_date >= {$curDate} OR end_date IS NULL)";
547 }
548
549 if ($sortBy &&
550 $this->_sortByCharacter !== NULL
551 ) {
552 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
553 }
554
555 $campaignIds = $this->get('campaign_id');
556 if (!CRM_Utils_System::isNull($campaignIds)) {
557 if (!is_array($campaignIds)) {
558 $campaignIds = array($campaignIds);
559 }
560 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campaignIds)) . ' ) )';
561 }
562
563 // don't do a the below assignment when doing a
564 // AtoZ pager clause
565 if ($sortBy) {
566 if (count($clauses) > 1 || $eventsByDates) {
567 $this->assign('isSearch', 1);
568 }
569 else {
570 $this->assign('isSearch', 0);
571 }
572 }
573
574 return !empty($clauses) ? implode(' AND ', $clauses) : '(1)';
575 }
576
577 /**
578 * @param $whereClause
579 * @param array $whereParams
580 */
581 public function pager($whereClause, $whereParams) {
582
583 $params['status'] = ts('Event %%StatusMessage%%');
584 $params['csvString'] = NULL;
585 $params['buttonTop'] = 'PagerTopButton';
586 $params['buttonBottom'] = 'PagerBottomButton';
587 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
588 if (!$params['rowCount']) {
589 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
590 }
591
592 $query = "
593 SELECT count(id)
594 FROM civicrm_event
595 WHERE $whereClause";
596
597 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
598
599 $this->_pager = new CRM_Utils_Pager($params);
600 $this->assign_by_ref('pager', $this->_pager);
601 }
602
603 /**
604 * @param $whereClause
605 * @param array $whereParams
606 */
607 public function pagerAtoZ($whereClause, $whereParams) {
608
609 $query = "
610 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
611 FROM civicrm_event
612 WHERE $whereClause
613 ORDER BY UPPER(LEFT(title, 1))
614 ";
615 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
616
617 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
618 $this->assign('aToZ', $aToZBar);
619 }
620
621 }