dev/core#560 Convert final CRM_Core_Error::fatal to statusBounces or Exceptions
[civicrm-core.git] / CRM / Event / Form / ManageEvent / TabHeader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * Helper class to build navigation links
22 */
23 class CRM_Event_Form_ManageEvent_TabHeader {
24
25 /**
26 * @param CRM_Event_Form_ManageEvent $form
27 *
28 * @return array
29 * @throws \CRM_Core_Exception
30 */
31 public static function build(&$form) {
32 $tabs = $form->get('tabHeader');
33 if (!$tabs || empty($_GET['reset'])) {
34 $tabs = self::process($form);
35 $form->set('tabHeader', $tabs);
36 }
37 $form->assign_by_ref('tabHeader', $tabs);
38 CRM_Core_Resources::singleton()
39 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
40 ->addSetting([
41 'tabSettings' => [
42 'active' => self::getCurrentTab($tabs),
43 ],
44 ]);
45 CRM_Event_Form_ManageEvent::addProfileEditScripts();
46 return $tabs;
47 }
48
49 /**
50 * @param CRM_Event_Form_ManageEvent $form
51 *
52 * @return array
53 * @throws Exception
54 */
55 public static function process(&$form) {
56 if ($form->getVar('_id') <= 0) {
57 return NULL;
58 }
59
60 $default = [
61 'link' => NULL,
62 'valid' => TRUE,
63 'active' => TRUE,
64 'current' => FALSE,
65 'class' => 'ajaxForm',
66 ];
67
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;
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])) {
76 $tabs['reminder'] = ['title' => ts('Schedule Reminders'), 'class' => 'livePage'] + $default;
77 }
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;
82
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
88 // check if we're in shopping cart mode for events
89 if (!(bool) Civi::settings()->get('enable_cart')) {
90 unset($tabs['conference']);
91 }
92
93 $eventID = $form->getVar('_id');
94 if ($eventID) {
95 // disable tabs based on their configuration status
96 $eventNameMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
97 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
98 ]));
99 $sql = "
100 SELECT 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
101 FROM civicrm_event e
102 LEFT JOIN civicrm_tell_friend taf ON ( taf.entity_table = 'civicrm_event' AND taf.entity_id = e.id )
103 LEFT JOIN civicrm_pcp_block pcp ON ( pcp.entity_table = 'civicrm_event' AND pcp.entity_id = e.id )
104 LEFT JOIN civicrm_action_schedule sch ON ( sch.mapping_id = %2 AND sch.entity_value = %1 )
105 LEFT JOIN civicrm_recurring_entity re ON ( e.id = re.entity_id AND re.entity_table = 'civicrm_event' )
106 WHERE e.id = %1
107 ";
108 //Check if repeat is configured
109 CRM_Core_BAO_RecurringEntity::getParentFor($eventID, 'civicrm_event');
110 $params = [
111 1 => [$eventID, 'Integer'],
112 2 => [$eventNameMapping->getId(), 'Integer'],
113 ];
114 $dao = CRM_Core_DAO::executeQuery($sql, $params);
115 if (!$dao->fetch()) {
116 throw new CRM_Core_Exception('Unable to determine Event information');;
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 }
136 if (!$dao->is_repeating_event) {
137 $tabs['repeat']['valid'] = FALSE;
138 }
139 }
140
141 // see if any other modules want to add any tabs
142 // note: status of 'valid' flag of any injected tab, needs to be taken care in the hook implementation.
143 CRM_Utils_Hook::tabset('civicrm/event/manage', $tabs,
144 ['event_id' => $eventID]);
145
146 $fullName = $form->getVar('_name');
147 $className = CRM_Utils_String::getClassName($fullName);
148 $new = '';
149
150 // hack for special cases.
151 switch ($className) {
152 case 'Event':
153 $attributes = $form->getVar('_attributes');
154 $class = CRM_Utils_Request::retrieveComponent($attributes);
155 break;
156
157 case 'EventInfo':
158 $class = 'settings';
159 break;
160
161 case 'ScheduleReminders':
162 $class = 'reminder';
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) {
179 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
180
181 foreach ($tabs as $key => $value) {
182 if (!isset($tabs[$key]['qfKey'])) {
183 $tabs[$key]['qfKey'] = NULL;
184 }
185
186 $action = 'update';
187 if ($key == 'reminder') {
188 $action = 'browse';
189 }
190
191 $link = "civicrm/event/manage/{$key}";
192 $query = "{$reset}action={$action}&id={$eventID}&component=event{$tabs[$key]['qfKey']}";
193
194 $tabs[$key]['link'] = (isset($value['link']) ? $value['link'] :
195 CRM_Utils_System::url($link, $query));
196 }
197 }
198
199 return $tabs;
200 }
201
202 /**
203 * @param CRM_Event_Form_ManageEvent $form
204 */
205 public static function reset(&$form) {
206 $tabs = self::process($form);
207 $form->set('tabHeader', $tabs);
208 }
209
210 /**
211 * @param $tabs
212 *
213 * @return int|string
214 */
215 public static function getCurrentTab($tabs) {
216 static $current = FALSE;
217
218 if ($current) {
219 return $current;
220 }
221
222 if (is_array($tabs)) {
223 foreach ($tabs as $subPage => $pageVal) {
224 if (CRM_Utils_Array::value('current', $pageVal) === TRUE) {
225 $current = $subPage;
226 break;
227 }
228 }
229 }
230
231 $current = $current ? $current : 'settings';
232 return $current;
233 }
234
235 }