133d4e9460345522e8fc982d8c6bcee1cbbe0e95
[Packages/TYPO3.CMS.git] / typo3 / classes / class.workspaceselector.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2010 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 // TODO remove the include once the autoloader is in place
29 if(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
30 require_once('interfaces/interface.backend_toolbaritem.php');
31 }
32
33 /**
34 * class to render the workspace selector
35 *
36 * @author Ingo Renner <ingo@typo3.org>
37 * @package TYPO3
38 * @subpackage core
39 */
40 class WorkspaceSelector implements backend_toolbarItem {
41
42 protected $changeWorkspace;
43 protected $changeWorkspacePreview;
44
45 /**
46 * reference back to the backend object
47 *
48 * @var TYPO3backend
49 */
50 protected $backendReference;
51
52 /**
53 * constructor
54 *
55 * @param TYPO3backend TYPO3 backend object reference
56 */
57 public function __construct(TYPO3backend &$backendReference = null) {
58 $this->backendReference = $backendReference;
59
60 $this->changeWorkspace = t3lib_div::_GP('changeWorkspace');
61 $this->changeWorkspacePreview = t3lib_div::_GP('changeWorkspacePreview');
62 }
63
64 /**
65 * checks whether the user has access to this toolbar item
66 *
67 * @see typo3/alt_shortcut.php
68 * @return boolean true if user has access, false if not
69 */
70 public function checkAccess() {
71 if (t3lib_extMgm::isLoaded('version')) {
72 $MCONF = array();
73 include(t3lib_extMgm::extPath('version') . 'ws/conf.php');
74
75 return ($GLOBALS['BE_USER']->modAccess(array('name' => 'user', 'access' => 'user,group'), false) && $GLOBALS['BE_USER']->modAccess($MCONF, false));
76 }
77 return FALSE;
78 }
79
80 /**
81 * changes workspace if needed and then reloads the backend
82 *
83 * @return void
84 */
85 public function changeWorkspace() {
86 $reloadBackend = false;
87
88 // Changing workspace and if so, reloading entire backend:
89 if (strlen($this->changeWorkspace)) {
90 $GLOBALS['BE_USER']->setWorkspace($this->changeWorkspace);
91 $reloadBackend = true;
92 }
93
94 // Changing workspace preview and if so, reloading entire backend:
95 if (strlen($this->changeWorkspacePreview)) {
96 $GLOBALS['BE_USER']->setWorkspacePreview($this->changeWorkspacePreview);
97 $reloadBackend = true;
98 }
99
100 if($reloadBackend) {
101 $this->backendReference->addJavascript(
102 'top.location.href=\'backend.php\';'
103 );
104 }
105 }
106
107 /**
108 * toggles the frontend preview setting for workspaces. If the preview is
109 * activated it will turned off and vice versa. Gets called through AJAX
110 *
111 * @param array array of parameters from the AJAX interface, currently unused
112 * @param TYPO3AJAX object of type TYPO3AJAX
113 * @return void
114 */
115 public function toggleWorkspacePreview($parameters = array(), TYPO3AJAX &$ajaxObj = null) {
116 $newState = $GLOBALS['BE_USER']->user['workspace_preview'] ? '0' : '1';
117 $GLOBALS['BE_USER']->setWorkspacePreview($newState);
118
119 $ajaxObj->addContent('newWorkspacePreviewState', $newState);
120 $ajaxObj->setContentFormat('json');
121 }
122
123 /**
124 * sets the workspace for the backend
125 *
126 * @param unknown_type $params
127 * @param TYPO3AJAX $ajaxObj
128 */
129 public function setWorkspace($parameters = array(), TYPO3AJAX &$ajaxObj = null) {
130 $workspaceId = (int) t3lib_div::_POST('workspaceId');
131
132 $GLOBALS['BE_USER']->setWorkspace($workspaceId);
133
134 $ajaxObj->addContent('setWorkspaceId', $workspaceId);
135 $ajaxObj->setContentFormat('json');
136 }
137
138 /**
139 * retrieves the available workspaces from the database and checks whether
140 * they're available to the current BE user
141 *
142 * @return array array of worspaces available to the current user
143 */
144 protected function getAvailableWorkspaces() {
145 $availableWorkspaces = array();
146
147 // add default workspaces
148 if($GLOBALS['BE_USER']->checkWorkspace(array('uid' => 0))) {
149 $availableWorkspaces[0] = '['.$GLOBALS['LANG']->getLL('bookmark_onlineWS').']';
150 }
151 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => -1))) {
152 $availableWorkspaces[-1] = '['.$GLOBALS['LANG']->getLL('bookmark_offlineWS').']';
153 }
154
155 // add custom workspaces (selecting all, filtering by BE_USER check):
156 $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
157 'uid, title, adminusers, members, reviewers',
158 'sys_workspace',
159 'pid = 0'.t3lib_BEfunc::deleteClause('sys_workspace'),
160 '',
161 'title'
162 );
163 if(count($customWorkspaces)) {
164 foreach($customWorkspaces as $workspace) {
165 if($GLOBALS['BE_USER']->checkWorkspace($workspace)) {
166 $availableWorkspaces[$workspace['uid']] = $workspace['uid'] . ': ' . htmlspecialchars($workspace['title']);
167 }
168 }
169 }
170
171 return $availableWorkspaces;
172 }
173
174 /**
175 * Creates the selector for workspaces
176 *
177 * @return string workspace selector as HTML select
178 */
179 public function render() {
180 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:toolbarItems.workspace', true);
181 $this->addJavascriptToBackend();
182 $this->changeWorkspace();
183 $availableWorkspaces = $this->getAvailableWorkspaces();
184 $workspaceMenu = array();
185
186 $stateCheckedIcon = t3lib_iconWorks::getSpriteIcon('status-status-checked');
187
188 $stateUncheckedIcon = t3lib_iconWorks::getSpriteIcon('empty-empty', array(
189 'title' => $GLOBALS['LANG']->getLL('bookmark_inactive')
190 ));
191
192 $workspaceMenu[] = '<a href="#" class="toolbar-item">' .
193 t3lib_iconWorks::getSpriteIcon('apps-toolbar-menu-workspace', array('title' => $title)) .
194 '</a>';
195 $workspaceMenu[] = '<ul class="toolbar-item-menu" style="display: none;">';
196
197 if (count($availableWorkspaces)) {
198 foreach($availableWorkspaces as $workspaceId => $label) {
199 $selected = '';
200 $icon = $stateUncheckedIcon;
201 if((int) $GLOBALS['BE_USER']->workspace === $workspaceId) {
202 $selected = ' class="selected"';
203 $icon = $stateCheckedIcon;
204 }
205
206 $workspaceMenu[] = '<li' . $selected . '>' . $icon .
207 ' <a href="backend.php?changeWorkspace=' .
208 intval($workspaceId) . '" id="ws-' . intval($workspaceId) .
209 '" class="ws">' . $label . '</a></li>';
210 }
211 } else {
212 $workspaceMenu[] = '<li>' . $stateUncheckedIcon . ' ' .
213 $GLOBALS['LANG']->getLL('bookmark_noWSfound', true) .
214 '</li>';
215 }
216
217 // frontend preview toggle
218 $frontendPreviewActiveIcon = $stateUncheckedIcon;
219 if ($GLOBALS['BE_USER']->user['workspace_preview']) {
220 $frontendPreviewActiveIcon = $stateCheckedIcon;
221 }
222
223 $workspaceMenu[] = '<li class="divider">' . $frontendPreviewActiveIcon .
224 '<a href="backend.php?changeWorkspacePreview=' .
225 ($GLOBALS['BE_USER']->user['workspace_preview'] ? '0' : '1') .
226 '" id="frontendPreviewToggle">' . $GLOBALS['LANG']->getLL('bookmark_FEPreview', true) . '</a></li>';
227
228 // go to workspace module link
229 $workspaceMenu[] = '<li>' . $stateUncheckedIcon . ' ' .
230 '<a href="' . t3lib_extMgm::extRelPath('version') . 'ws/index.php" target="content" id="goToWsModule">' .
231 ' '. $GLOBALS['LANG']->getLL('bookmark_workspace', true) . '</a></li>';
232
233 $workspaceMenu[] = '</ul>';
234
235 return implode(LF, $workspaceMenu);
236 }
237
238 /**
239 * adds the necessary JavaScript to the backend
240 *
241 * @return void
242 */
243 protected function addJavascriptToBackend() {
244 $this->backendReference->addJavascriptFile('js/workspacemenu.js');
245 }
246
247 /**
248 * returns additional attributes for the list item in the toolbar
249 *
250 * @return string list item HTML attibutes
251 */
252 public function getAdditionalAttributes() {
253 return ' id="workspace-selector-menu"';
254 }
255 }
256
257 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']) {
258 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']);
259 }
260
261 ?>