dev/core#560 Convert final CRM_Core_Error::fatal to statusBounces or Exceptions
[civicrm-core.git] / CRM / Event / Form / ManageEvent / TabHeader.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * Helper class to build navigation links
22 */
23class CRM_Event_Form_ManageEvent_TabHeader {
24
0cf587a7 25 /**
c65ce939 26 * @param CRM_Event_Form_ManageEvent $form
0cf587a7
EM
27 *
28 * @return array
c1bd8375 29 * @throws \CRM_Core_Exception
0cf587a7 30 */
00be9182 31 public static function build(&$form) {
6a488035 32 $tabs = $form->get('tabHeader');
8cc574cf 33 if (!$tabs || empty($_GET['reset'])) {
6a488035
TO
34 $tabs = self::process($form);
35 $form->set('tabHeader', $tabs);
36 }
37 $form->assign_by_ref('tabHeader', $tabs);
4165b7e5 38 CRM_Core_Resources::singleton()
96ed17aa 39 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
be2fb01f
CW
40 ->addSetting([
41 'tabSettings' => [
353ffa53 42 'active' => self::getCurrentTab($tabs),
be2fb01f
CW
43 ],
44 ]);
bfd83a87 45 CRM_Event_Form_ManageEvent::addProfileEditScripts();
6a488035
TO
46 return $tabs;
47 }
48
0cf587a7 49 /**
c65ce939 50 * @param CRM_Event_Form_ManageEvent $form
0cf587a7
EM
51 *
52 * @return array
53 * @throws Exception
54 */
00be9182 55 public static function process(&$form) {
6a488035
TO
56 if ($form->getVar('_id') <= 0) {
57 return NULL;
58 }
59
be2fb01f 60 $default = [
4b628e67
CW
61 'link' => NULL,
62 'valid' => TRUE,
63 'active' => TRUE,
64 'current' => FALSE,
65 'class' => 'ajaxForm',
be2fb01f 66 ];
4b628e67 67
be2fb01f
CW
68 $tabs = [];
69 $tabs['settings'] = ['title' => ts('Info and Settings'), 'class' => 'ajaxForm livePage'] + $default;
70 $tabs['location'] = ['title' => ts('Event Location')] + $default;
71 $tabs['fee'] = ['title' => ts('Fees')] + $default;
72 $tabs['registration'] = ['title' => ts('Online Registration')] + $default;
cfbfd406
MWMC
73 // @fixme I don't understand the event permissions check here - can we just get rid of it?
74 $permissions = CRM_Event_BAO_Event::getAllPermissions();
75 if (CRM_Core_Permission::check('administer CiviCRM') || !empty($permissions[CRM_Core_Permission::EDIT])) {
be2fb01f 76 $tabs['reminder'] = ['title' => ts('Schedule Reminders'), 'class' => 'livePage'] + $default;
209989e3 77 }
be2fb01f
CW
78 $tabs['conference'] = ['title' => ts('Conference Slots')] + $default;
79 $tabs['friend'] = ['title' => ts('Tell a Friend')] + $default;
80 $tabs['pcp'] = ['title' => ts('Personal Campaigns')] + $default;
81 $tabs['repeat'] = ['title' => ts('Repeat')] + $default;
209989e3 82
c65ce939
CW
83 // Repeat tab must refresh page when switching repeat mode so js & vars will get set-up
84 if (!$form->_isRepeatingEvent) {
85 unset($tabs['repeat']['class']);
86 }
87
6a488035 88 // check if we're in shopping cart mode for events
7fffa5fa 89 if (!(bool) Civi::settings()->get('enable_cart')) {
6a488035
TO
90 unset($tabs['conference']);
91 }
92
93 $eventID = $form->getVar('_id');
a215a5b0 94 if ($eventID) {
0cf587a7 95 // disable tabs based on their configuration status
be2fb01f 96 $eventNameMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
46f5566c 97 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
be2fb01f 98 ]));
a215a5b0 99 $sql = "
6063a2f1 100SELECT e.loc_block_id as is_location, e.is_online_registration, e.is_monetary, taf.is_active, pcp.is_active as is_pcp, sch.id as is_reminder, re.id as is_repeating_event
a215a5b0
DS
101FROM civicrm_event e
102LEFT JOIN civicrm_tell_friend taf ON ( taf.entity_table = 'civicrm_event' AND taf.entity_id = e.id )
103LEFT JOIN civicrm_pcp_block pcp ON ( pcp.entity_table = 'civicrm_event' AND pcp.entity_id = e.id )
50a23755 104LEFT JOIN civicrm_action_schedule sch ON ( sch.mapping_id = %2 AND sch.entity_value = %1 )
6063a2f1 105LEFT JOIN civicrm_recurring_entity re ON ( e.id = re.entity_id AND re.entity_table = 'civicrm_event' )
a215a5b0
DS
106WHERE e.id = %1
107";
62933949 108 //Check if repeat is configured
7fffa5fa 109 CRM_Core_BAO_RecurringEntity::getParentFor($eventID, 'civicrm_event');
be2fb01f
CW
110 $params = [
111 1 => [$eventID, 'Integer'],
112 2 => [$eventNameMapping->getId(), 'Integer'],
113 ];
a215a5b0
DS
114 $dao = CRM_Core_DAO::executeQuery($sql, $params);
115 if (!$dao->fetch()) {
79e11805 116 throw new CRM_Core_Exception('Unable to determine Event information');;
a215a5b0
DS
117 }
118 if (!$dao->is_location) {
119 $tabs['location']['valid'] = FALSE;
120 }
121 if (!$dao->is_online_registration) {
122 $tabs['registration']['valid'] = FALSE;
123 }
124 if (!$dao->is_monetary) {
125 $tabs['fee']['valid'] = FALSE;
126 }
127 if (!$dao->is_active) {
128 $tabs['friend']['valid'] = FALSE;
129 }
130 if (!$dao->is_pcp) {
131 $tabs['pcp']['valid'] = FALSE;
132 }
133 if (!$dao->is_reminder) {
134 $tabs['reminder']['valid'] = FALSE;
135 }
a50a97b8 136 if (!$dao->is_repeating_event) {
62933949 137 $tabs['repeat']['valid'] = FALSE;
138 }
a215a5b0 139 }
6a488035 140
c1ad592f 141 // see if any other modules want to add any tabs
a215a5b0 142 // note: status of 'valid' flag of any injected tab, needs to be taken care in the hook implementation.
c1ad592f 143 CRM_Utils_Hook::tabset('civicrm/event/manage', $tabs,
be2fb01f 144 ['event_id' => $eventID]);
c1ad592f 145
353ffa53 146 $fullName = $form->getVar('_name');
6a488035 147 $className = CRM_Utils_String::getClassName($fullName);
353ffa53 148 $new = '';
4d3e4dbe 149
6a488035
TO
150 // hack for special cases.
151 switch ($className) {
152 case 'Event':
153 $attributes = $form->getVar('_attributes');
b90552b7 154 $class = CRM_Utils_Request::retrieveComponent($attributes);
6a488035
TO
155 break;
156
c13a1cff
AN
157 case 'EventInfo':
158 $class = 'settings';
159 break;
160
6a488035
TO
161 case 'ScheduleReminders':
162 $class = 'reminder';
6a488035
TO
163 break;
164
165 default:
166 $class = strtolower($className);
167 break;
168 }
169
170 if (array_key_exists($class, $tabs)) {
171 $tabs[$class]['current'] = TRUE;
172 $qfKey = $form->get('qfKey');
173 if ($qfKey) {
174 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
175 }
176 }
177
178 if ($eventID) {
0d8afee2 179 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
6a488035
TO
180
181 foreach ($tabs as $key => $value) {
182 if (!isset($tabs[$key]['qfKey'])) {
183 $tabs[$key]['qfKey'] = NULL;
184 }
185
e8e90f7e 186 $action = 'update';
4d3e4dbe 187 if ($key == 'reminder') {
188 $action = 'browse';
189 }
190
2f3732a9
FG
191 $link = "civicrm/event/manage/{$key}";
192 $query = "{$reset}action={$action}&id={$eventID}&component=event{$tabs[$key]['qfKey']}";
e84a94d2 193
32f93260 194 $tabs[$key]['link'] = (isset($value['link']) ? $value['link'] :
2f3732a9 195 CRM_Utils_System::url($link, $query));
74affe5c 196 }
6a488035 197 }
fa3dbfbd 198
6a488035
TO
199 return $tabs;
200 }
201
0cf587a7 202 /**
c65ce939 203 * @param CRM_Event_Form_ManageEvent $form
0cf587a7 204 */
00be9182 205 public static function reset(&$form) {
6a488035
TO
206 $tabs = self::process($form);
207 $form->set('tabHeader', $tabs);
208 }
209
0cf587a7
EM
210 /**
211 * @param $tabs
212 *
213 * @return int|string
214 */
00be9182 215 public static function getCurrentTab($tabs) {
6a488035
TO
216 static $current = FALSE;
217
218 if ($current) {
219 return $current;
220 }
221
222 if (is_array($tabs)) {
223 foreach ($tabs as $subPage => $pageVal) {
c1bd8375 224 if (CRM_Utils_Array::value('current', $pageVal) === TRUE) {
6a488035
TO
225 $current = $subPage;
226 break;
227 }
228 }
229 }
230
231 $current = $current ? $current : 'settings';
232 return $current;
233 }
96025800 234
6a488035 235}