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