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