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