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