Merge pull request #3079 from mepps/fix-jquery-autocomplete-search
[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 'settings' => array('title' => ts('Info and Settings'), 'class' => 'ajaxForm livePage') + $default,
80 'location' => array('title' => ts('Event Location')) + $default,
81 'fee' => array('title' => ts('Fees')) + $default,
82 'registration' => array('title' => ts('Online Registration')) + $default,
83 'reminder' => array('title' => ts('Schedule Reminders'), 'class' => 'livePage') + $default,
84 'conference' => array('title' => ts('Conference Slots')) + $default,
85 'friend' => array('title' => ts('Tell a Friend')) + $default,
86 'pcp' => array('title' => ts('Personal Campaigns')) + $default,
87 );
88
89 // check if we're in shopping cart mode for events
90 $enableCart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME,
91 'enable_cart'
92 );
93 if (!$enableCart) {
94 unset($tabs['conference']);
95 }
96
97 $eventID = $form->getVar('_id');
98 if ($eventID) {
99 // disable tabs based on their configuration status
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
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_mapping map ON ( map.entity_value = 'civicrm_event' )
106 LEFT JOIN civicrm_action_schedule sch ON ( sch.mapping_id = map.id AND sch.entity_value = %1 )
107 WHERE e.id = %1
108 ";
109 $params = array(1 => array($eventID, 'Integer'));
110 $dao = CRM_Core_DAO::executeQuery($sql, $params);
111 if (!$dao->fetch()) {
112 CRM_Core_Error::fatal();
113 }
114 if (!$dao->is_location) {
115 $tabs['location']['valid'] = FALSE;
116 }
117 if (!$dao->is_online_registration) {
118 $tabs['registration']['valid'] = FALSE;
119 }
120 if (!$dao->is_monetary) {
121 $tabs['fee']['valid'] = FALSE;
122 }
123 if (!$dao->is_active) {
124 $tabs['friend']['valid'] = FALSE;
125 }
126 if (!$dao->is_pcp) {
127 $tabs['pcp']['valid'] = FALSE;
128 }
129 if (!$dao->is_reminder) {
130 $tabs['reminder']['valid'] = FALSE;
131 }
132 }
133
134 // see if any other modules want to add any tabs
135 // note: status of 'valid' flag of any injected tab, needs to be taken care in the hook implementation.
136 CRM_Utils_Hook::tabset('civicrm/event/manage', $tabs,
137 array('event_id' => $eventID));
138
139 $fullName = $form->getVar('_name');
140 $className = CRM_Utils_String::getClassName($fullName);
141 $new = '';
142 // hack for special cases.
143 switch ($className) {
144 case 'Event':
145 $attributes = $form->getVar('_attributes');
146 $class = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
147 break;
148
149 case 'EventInfo':
150 $class = 'settings';
151 break;
152
153 case 'ScheduleReminders':
154 $class = 'reminder';
155 $new = !empty($_GET['new']) ? '&new=1' : '';
156 break;
157
158 default:
159 $class = strtolower($className);
160 break;
161 }
162
163 if (array_key_exists($class, $tabs)) {
164 $tabs[$class]['current'] = TRUE;
165 $qfKey = $form->get('qfKey');
166 if ($qfKey) {
167 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
168 }
169 }
170
171 if ($eventID) {
172 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
173
174 foreach ($tabs as $key => $value) {
175 if (!isset($tabs[$key]['qfKey'])) {
176 $tabs[$key]['qfKey'] = NULL;
177 }
178
179 $tabs[$key]['link'] = CRM_Utils_System::url("civicrm/event/manage/{$key}",
180 "{$reset}action=update&id={$eventID}&component=event{$new}{$tabs[$key]['qfKey']}"
181 );
182 }
183 }
184
185 return $tabs;
186 }
187
188 static function reset(&$form) {
189 $tabs = self::process($form);
190 $form->set('tabHeader', $tabs);
191 }
192
193 static function getCurrentTab($tabs) {
194 static $current = FALSE;
195
196 if ($current) {
197 return $current;
198 }
199
200 if (is_array($tabs)) {
201 foreach ($tabs as $subPage => $pageVal) {
202 if ($pageVal['current'] === TRUE) {
203 $current = $subPage;
204 break;
205 }
206 }
207 }
208
209 $current = $current ? $current : 'settings';
210 return $current;
211 }
212 }
213