fixed bug #7270: Shortcut menu does not load user's shortcuts if user is not admin...
[Packages/TYPO3.CMS.git] / typo3 / classes / class.workspaceselector.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007 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
29 /**
30 * class to render the workspace selector
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 * @package TYPO3
34 * @subpackage core
35 */
36 class WorkspaceSelector implements backend_toolbarItem {
37
38 private $changeWorkspace;
39 private $changeWorkspacePreview;
40
41 /**
42 * reference back to the backend object
43 *
44 * @var TYPO3backend
45 */
46 private $backendReference;
47
48 /**
49 * constructor
50 *
51 * @return void
52 */
53 public function __construct() {
54 $this->changeWorkspace = t3lib_div::_GP('changeWorkspace');
55 $this->changeWorkspacePreview = t3lib_div::_GP('changeWorkspacePreview');
56 }
57
58 /**
59 * sets the backend reference
60 *
61 * @param TYPO3backend backend object reference
62 */
63 public function setBackend(&$backendReference) {
64 $this->backendReference = $backendReference;
65 }
66
67 /**
68 * changes workspace if needed and then reloads the backend
69 *
70 * @return void
71 */
72 public function changeWorkspace() {
73 $reloadBackend = false;
74
75 // Changing workspace and if so, reloading entire backend:
76 if (strlen($this->changeWorkspace)) {
77 $GLOBALS['BE_USER']->setWorkspace($this->changeWorkspace);
78 $reloadBackend = true;
79 }
80
81 // Changing workspace preview and if so, reloading entire backend:
82 if (strlen($this->changeWorkspacePreview)) {
83 $GLOBALS['BE_USER']->setWorkspacePreview($this->changeWorkspacePreview);
84 $reloadBackend = true;
85 }
86
87 if($reloadBackend) {
88 $this->backendReference->addJavascript(
89 'top.location.href=\'backend.php\';'
90 );
91 }
92 }
93
94 /**
95 * retrieves the available workspaces from the database and checks whether
96 * they're available to the current BE user
97 *
98 * @return array array of worspaces available to the current user
99 */
100 private function getAvailableWorkspaces() {
101 $availableWorkspaces = array();
102
103 // add default workspaces
104 if($GLOBALS['BE_USER']->checkWorkspace(array('uid' => 0))) {
105 $availableWorkspaces[0] = '['.$GLOBALS['LANG']->getLL('shortcut_onlineWS').']';
106 }
107 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => -1))) {
108 $availableWorkspaces[-1] = '['.$GLOBALS['LANG']->getLL('shortcut_offlineWS').']';
109 }
110
111 // add custom workspaces (selecting all, filtering by BE_USER check):
112 $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
113 'uid, title, adminusers, members, reviewers',
114 'sys_workspace',
115 'pid = 0'.t3lib_BEfunc::deleteClause('sys_workspace'),
116 '',
117 'title'
118 );
119 if(count($customWorkspaces)) {
120 foreach($customWorkspaces as $workspace) {
121 if($GLOBALS['BE_USER']->checkWorkspace($workspace)) {
122 $availableWorkspaces[$workspace['uid']] = $workspace['uid'].': '.$workspace['title'];
123 }
124 }
125 }
126
127 return $availableWorkspaces;
128 }
129
130 /**
131 * Creates the selector for workspaces
132 *
133 * @return string workspace selector as HTML select
134 */
135 public function render() {
136 $this->addJavascriptToBackend();
137 $this->changeWorkspace();
138
139 $options = array();
140 $workspaceSelector = '<span class="toolbar-item">';
141 $availableWorkspaces = $this->getAvailableWorkspaces();
142
143 // build selector box options
144 if(count($availableWorkspaces)) {
145 foreach($availableWorkspaces as $workspaceId => $label) {
146
147 $selected = '';
148 if((int) $GLOBALS['BE_USER']->workspace === $workspaceId) {
149 $selected = ' selected="selected"';
150 }
151
152 $options[$workspaceId] = '<option value="'.htmlspecialchars($workspaceId).'"'.$selected.'>'.htmlspecialchars($label).'</option>';
153 }
154 } else {
155 $options[] = '<option value="-99">'.$GLOBALS['LANG']->getLL('shortcut_noWSfound',1).'</option>';
156 }
157
158 // build selector box
159 if(count($options) > 1) {
160 $workspaceSelector .=
161 '<select name="_workspaceSelector" onchange="changeWorkspace(this.options[this.selectedIndex].value);">'
162 .implode("\n", $options)
163 .'</select>';
164 }
165
166 // preview
167 if($GLOBALS['BE_USER']->workspace !== 0) {
168 $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"' : '').'/>';
169 }
170
171 $workspaceSelector.= ' <a href="mod/user/ws/index.php" target="content">'.
172 t3lib_iconWorks::getIconImage(
173 'sys_workspace',
174 array(),
175 $this->doc->backPath,
176 'align="top"'
177 ).'</a>';
178
179 return $workspaceSelector.'</span>';
180 }
181
182 /**
183 * adds the neccessary javascript ot the backend
184 *
185 */
186 private function addJavascriptToBackend() {
187 $this->backendReference->addJavascriptFile('typo3/js/workspaces.js');
188 }
189
190 /**
191 * returns additional attributes for the list item in the toolbar
192 *
193 * @return string list item HTML attibutes
194 */
195 public function getAdditionalAttributes() {
196 return ' id="workspace-selector"';
197 }
198 }
199
200 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']) {
201 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']);
202 }
203
204 ?>