Cleanup #11357: Make use of new t3lib_div::redirect function throughout the core
[Packages/TYPO3.CMS.git] / typo3 / classes / class.workspaceselector.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2009 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('mod/user/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('shortcut_onlineWS').']';
150 }
151 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => -1))) {
152 $availableWorkspaces[-1] = '['.$GLOBALS['LANG']->getLL('shortcut_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'].': '.$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 = '<img' . t3lib_iconWorks::skinImg(
187 $this->backPath,
188 'gfx/state_checked.png',
189 'width="16" height="16"') .
190 ' title="' . $GLOBALS['LANG']->getLL('shortcut_active') .
191 '" alt="' . $GLOBALS['LANG']->getLL('shortcut_active') . '" class="state-active" />';
192 $stateUncheckedIcon = '<img src="clear.gif" width="16" height="16"
193 title="' . $GLOBALS['LANG']->getLL('shortcut_inactive') .
194 '" alt="' . $GLOBALS['LANG']->getLL('shortcut_inactive') . '" class="state-inactive" />';
195
196
197 $workspaceMenu[] = '<a href="#" class="toolbar-item"><img' .
198 t3lib_iconWorks::skinImg(
199 $this->backPath,
200 'gfx/i/sys_workspace.png',
201 'width="16" height="16"') .
202 ' title="' . $title . '" alt="' . $title . '" /></a>';
203 $workspaceMenu[] = '<ul class="toolbar-item-menu" style="display: none;">';
204
205 if (count($availableWorkspaces)) {
206 foreach($availableWorkspaces as $workspaceId => $label) {
207 $selected = '';
208 $icon = $stateUncheckedIcon;
209 if((int) $GLOBALS['BE_USER']->workspace === $workspaceId) {
210 $selected = ' class="selected"';
211 $icon = $stateCheckedIcon;
212 }
213
214 $workspaceMenu[] = '<li' . $selected . '>' . $icon .
215 ' <a href="backend.php?changeWorkspace=' .
216 intval($workspaceId) . '" id="ws-' . intval($workspaceId) .
217 '" class="ws">' . $label . '</a></li>';
218 }
219 } else {
220 $workspaceMenu[] = '<li>' . $stateUncheckedIcon . ' ' .
221 $GLOBALS['LANG']->getLL('shortcut_noWSfound', true) .
222 '</li>';
223 }
224
225 // frontend preview toggle
226 $frontendPreviewActiveIcon = $stateUncheckedIcon;
227 if ($GLOBALS['BE_USER']->user['workspace_preview']) {
228 $frontendPreviewActiveIcon = $stateCheckedIcon;
229 }
230
231 $workspaceMenu[] = '<li class="divider">' . $frontendPreviewActiveIcon .
232 '<a href="backend.php?changeWorkspacePreview=' .
233 ($GLOBALS['BE_USER']->user['workspace_preview'] ? '0' : '1') .
234 '" id="frontendPreviewToggle">' . $GLOBALS['LANG']->getLL('shortcut_FEPreview', true) . '</a></li>';
235
236 // go to workspace module link
237 $workspaceMenu[] = '<li>' . $stateUncheckedIcon . ' ' .
238 '<a href="mod/user/ws/index.php" target="content" id="goToWsModule">' .
239 ' '. $GLOBALS['LANG']->getLL('shortcut_workspace', true) . '</a></li>';
240
241 $workspaceMenu[] = '</ul>';
242
243 return implode("\n", $workspaceMenu);
244 }
245
246 /**
247 * adds the necessary JavaScript to the backend
248 *
249 * @return void
250 */
251 protected function addJavascriptToBackend() {
252 $this->backendReference->addJavascriptFile('js/workspacemenu.js');
253 }
254
255 /**
256 * returns additional attributes for the list item in the toolbar
257 *
258 * @return string list item HTML attibutes
259 */
260 public function getAdditionalAttributes() {
261 return ' id="workspace-selector-menu"';
262 }
263 }
264
265 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']) {
266 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']);
267 }
268
269 ?>