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