2 /***************************************************************
5 * (c) 2007 Ingo Renner <ingo@typo3.org>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
30 * class to render the workspace selector
32 * @author Ingo Renner <ingo@typo3.org>
36 class WorkspaceSelector
implements backend_toolbarItem
{
38 private $changeWorkspace;
39 private $changeWorkspacePreview;
42 * reference back to the backend object
46 private $backendReference;
51 * @param TYPO3backend TYPO3 backend object reference
53 public function __construct(TYPO3backend
&$backendReference = null) {
54 $this->backendReference
= $backendReference;
56 $this->changeWorkspace
= t3lib_div
::_GP('changeWorkspace');
57 $this->changeWorkspacePreview
= t3lib_div
::_GP('changeWorkspacePreview');
61 * checks whether the user has access to this toolbar item
63 * @return boolean true if user has access, false if not
65 public function checkAccess() {
66 // FIXME - needs proper access check
71 * changes workspace if needed and then reloads the backend
75 public function changeWorkspace() {
76 $reloadBackend = false;
78 // Changing workspace and if so, reloading entire backend:
79 if (strlen($this->changeWorkspace
)) {
80 $GLOBALS['BE_USER']->setWorkspace($this->changeWorkspace
);
81 $reloadBackend = true;
84 // Changing workspace preview and if so, reloading entire backend:
85 if (strlen($this->changeWorkspacePreview
)) {
86 $GLOBALS['BE_USER']->setWorkspacePreview($this->changeWorkspacePreview
);
87 $reloadBackend = true;
91 $this->backendReference
->addJavascript(
92 'top.location.href=\'backend.php\';'
98 * retrieves the available workspaces from the database and checks whether
99 * they're available to the current BE user
101 * @return array array of worspaces available to the current user
103 private function getAvailableWorkspaces() {
104 $availableWorkspaces = array();
106 // add default workspaces
107 if($GLOBALS['BE_USER']->checkWorkspace(array('uid' => 0))) {
108 $availableWorkspaces[0] = '['.$GLOBALS['LANG']->getLL('shortcut_onlineWS').']';
110 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => -1))) {
111 $availableWorkspaces[-1] = '['.$GLOBALS['LANG']->getLL('shortcut_offlineWS').']';
114 // add custom workspaces (selecting all, filtering by BE_USER check):
115 $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
116 'uid, title, adminusers, members, reviewers',
118 'pid = 0'.t3lib_BEfunc
::deleteClause('sys_workspace'),
122 if(count($customWorkspaces)) {
123 foreach($customWorkspaces as $workspace) {
124 if($GLOBALS['BE_USER']->checkWorkspace($workspace)) {
125 $availableWorkspaces[$workspace['uid']] = $workspace['uid'].': '.$workspace['title'];
130 return $availableWorkspaces;
134 * Creates the selector for workspaces
136 * @return string workspace selector as HTML select
138 public function render() {
139 $this->addJavascriptToBackend();
140 $this->changeWorkspace();
143 $workspaceSelector = '<span class="toolbar-item">';
144 $availableWorkspaces = $this->getAvailableWorkspaces();
146 // build selector box options
147 if(count($availableWorkspaces)) {
148 foreach($availableWorkspaces as $workspaceId => $label) {
151 if((int) $GLOBALS['BE_USER']->workspace
=== $workspaceId) {
152 $selected = ' selected="selected"';
155 $options[$workspaceId] = '<option value="'.htmlspecialchars($workspaceId).'"'.$selected.'>'.htmlspecialchars($label).'</option>';
158 $options[] = '<option value="-99">'.$GLOBALS['LANG']->getLL('shortcut_noWSfound',1).'</option>';
161 // build selector box
162 if(count($options) > 1) {
163 $workspaceSelector .=
164 '<select name="_workspaceSelector" onchange="changeWorkspace(this.options[this.selectedIndex].value);">'
165 .implode("\n", $options)
170 if($GLOBALS['BE_USER']->workspace
!== 0) {
171 $workspaceSelector.= ' <label for="workspacePreview">Frontend Preview:</label> <input type="checkbox" name="workspacePreview" id="workspacePreview" onclick="changeWorkspacePreview('.($GLOBALS['BE_USER']->user
['workspace_preview'] ?
0 : 1).')"; '.($GLOBALS['BE_USER']->user
['workspace_preview'] ?
'checked="checked"' : '').'/>';
174 $workspaceSelector.= ' <a href="mod/user/ws/index.php" target="content">'.
175 t3lib_iconWorks
::getIconImage(
178 $this->doc
->backPath
,
182 return $workspaceSelector.'</span>';
186 * adds the neccessary javascript ot the backend
189 private function addJavascriptToBackend() {
190 $this->backendReference
->addJavascriptFile('js/workspaces.js');
194 * returns additional attributes for the list item in the toolbar
196 * @return string list item HTML attibutes
198 public function getAdditionalAttributes() {
199 return ' id="workspace-selector"';
203 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['typo3/classes/class.workspaceselector.php']) {
204 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['typo3/classes/class.workspaceselector.php']);