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