Merge pull request #16008 from civicrm/5.20
[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 $enableCart = Civi::settings()->get('enable_cart');
90 if (!$enableCart) {
91 unset($tabs['conference']);
92 }
93
94 $eventID = $form->getVar('_id');
95 if ($eventID) {
96 // disable tabs based on their configuration status
97 $eventNameMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
98 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
99 ]));
100 $sql = "
101 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
102 FROM civicrm_event e
103 LEFT JOIN civicrm_tell_friend taf ON ( taf.entity_table = 'civicrm_event' AND taf.entity_id = e.id )
104 LEFT JOIN civicrm_pcp_block pcp ON ( pcp.entity_table = 'civicrm_event' AND pcp.entity_id = e.id )
105 LEFT JOIN civicrm_action_schedule sch ON ( sch.mapping_id = %2 AND sch.entity_value = %1 )
106 LEFT JOIN civicrm_recurring_entity re ON ( e.id = re.entity_id AND re.entity_table = 'civicrm_event' )
107 WHERE e.id = %1
108 ";
109 //Check if repeat is configured
110 $eventHasParent = CRM_Core_BAO_RecurringEntity::getParentFor($eventID, 'civicrm_event');
111 $params = [
112 1 => [$eventID, 'Integer'],
113 2 => [$eventNameMapping->getId(), 'Integer'],
114 ];
115 $dao = CRM_Core_DAO::executeQuery($sql, $params);
116 if (!$dao->fetch()) {
117 CRM_Core_Error::fatal();
118 }
119 if (!$dao->is_location) {
120 $tabs['location']['valid'] = FALSE;
121 }
122 if (!$dao->is_online_registration) {
123 $tabs['registration']['valid'] = FALSE;
124 }
125 if (!$dao->is_monetary) {
126 $tabs['fee']['valid'] = FALSE;
127 }
128 if (!$dao->is_active) {
129 $tabs['friend']['valid'] = FALSE;
130 }
131 if (!$dao->is_pcp) {
132 $tabs['pcp']['valid'] = FALSE;
133 }
134 if (!$dao->is_reminder) {
135 $tabs['reminder']['valid'] = FALSE;
136 }
137 if (!$dao->is_repeating_event) {
138 $tabs['repeat']['valid'] = FALSE;
139 }
140 }
141
142 // see if any other modules want to add any tabs
143 // note: status of 'valid' flag of any injected tab, needs to be taken care in the hook implementation.
144 CRM_Utils_Hook::tabset('civicrm/event/manage', $tabs,
145 ['event_id' => $eventID]);
146
147 $fullName = $form->getVar('_name');
148 $className = CRM_Utils_String::getClassName($fullName);
149 $new = '';
150
151 // hack for special cases.
152 switch ($className) {
153 case 'Event':
154 $attributes = $form->getVar('_attributes');
155 $class = CRM_Utils_Request::retrieveComponent($attributes);
156 break;
157
158 case 'EventInfo':
159 $class = 'settings';
160 break;
161
162 case 'ScheduleReminders':
163 $class = 'reminder';
164 break;
165
166 default:
167 $class = strtolower($className);
168 break;
169 }
170
171 if (array_key_exists($class, $tabs)) {
172 $tabs[$class]['current'] = TRUE;
173 $qfKey = $form->get('qfKey');
174 if ($qfKey) {
175 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
176 }
177 }
178
179 if ($eventID) {
180 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
181
182 foreach ($tabs as $key => $value) {
183 if (!isset($tabs[$key]['qfKey'])) {
184 $tabs[$key]['qfKey'] = NULL;
185 }
186
187 $action = 'update';
188 if ($key == 'reminder') {
189 $action = 'browse';
190 }
191
192 $link = "civicrm/event/manage/{$key}";
193 $query = "{$reset}action={$action}&id={$eventID}&component=event{$tabs[$key]['qfKey']}";
194
195 $tabs[$key]['link'] = (isset($value['link']) ? $value['link'] :
196 CRM_Utils_System::url($link, $query));
197 }
198 }
199
200 return $tabs;
201 }
202
203 /**
204 * @param CRM_Event_Form_ManageEvent $form
205 */
206 public static function reset(&$form) {
207 $tabs = self::process($form);
208 $form->set('tabHeader', $tabs);
209 }
210
211 /**
212 * @param $tabs
213 *
214 * @return int|string
215 */
216 public static function getCurrentTab($tabs) {
217 static $current = FALSE;
218
219 if ($current) {
220 return $current;
221 }
222
223 if (is_array($tabs)) {
224 foreach ($tabs as $subPage => $pageVal) {
225 if (CRM_Utils_Array::value('current', $pageVal) === TRUE) {
226 $current = $subPage;
227 break;
228 }
229 }
230 }
231
232 $current = $current ? $current : 'settings';
233 return $current;
234 }
235
236 }