cleaner backend project: modified interface for toolbar items to add access chechking
[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 * @param TYPO3backend TYPO3 backend object reference
52 */
53 public function __construct(TYPO3backend &$backendReference) {
54 $this->backendReference = $backendReference;
55
56 $this->changeWorkspace = t3lib_div::_GP('changeWorkspace');
57 $this->changeWorkspacePreview = t3lib_div::_GP('changeWorkspacePreview');
58 }
59
60 /**
61 * checks whether the user has access to this toolbar item
62 *
63 * @return boolean true if user has access, false if not
64 */
65 public function checkAccess() {
66 // FIXME - needs proper access check
67 return true;
68 }
69
70 /**
71 * changes workspace if needed and then reloads the backend
72 *
73 * @return void
74 */
75 public function changeWorkspace() {
76 $reloadBackend = false;
77
78 // Changing workspace and if so, reloading entire backend:
79 if (strlen($this->changeWorkspace)) {
80 $GLOBALS['BE_USER']->setWorkspace($this->changeWorkspace);
81 $reloadBackend = true;
82 }
83
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;
88 }
89
90 if($reloadBackend) {
91 $this->backendReference->addJavascript(
92 'top.location.href=\'backend.php\';'
93 );
94 }
95 }
96
97 /**
98 * retrieves the available workspaces from the database and checks whether
99 * they're available to the current BE user
100 *
101 * @return array array of worspaces available to the current user
102 */
103 private function getAvailableWorkspaces() {
104 $availableWorkspaces = array();
105
106 // add default workspaces
107 if($GLOBALS['BE_USER']->checkWorkspace(array('uid' => 0))) {
108 $availableWorkspaces[0] = '['.$GLOBALS['LANG']->getLL('shortcut_onlineWS').']';
109 }
110 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => -1))) {
111 $availableWorkspaces[-1] = '['.$GLOBALS['LANG']->getLL('shortcut_offlineWS').']';
112 }
113
114 // add custom workspaces (selecting all, filtering by BE_USER check):
115 $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
116 'uid, title, adminusers, members, reviewers',
117 'sys_workspace',
118 'pid = 0'.t3lib_BEfunc::deleteClause('sys_workspace'),
119 '',
120 'title'
121 );
122 if(count($customWorkspaces)) {
123 foreach($customWorkspaces as $workspace) {
124 if($GLOBALS['BE_USER']->checkWorkspace($workspace)) {
125 $availableWorkspaces[$workspace['uid']] = $workspace['uid'].': '.$workspace['title'];
126 }
127 }
128 }
129
130 return $availableWorkspaces;
131 }
132
133 /**
134 * Creates the selector for workspaces
135 *
136 * @return string workspace selector as HTML select
137 */
138 public function render() {
139 $this->addJavascriptToBackend();
140 $this->changeWorkspace();
141
142 $options = array();
143 $workspaceSelector = '<span class="toolbar-item">';
144 $availableWorkspaces = $this->getAvailableWorkspaces();
145
146 // build selector box options
147 if(count($availableWorkspaces)) {
148 foreach($availableWorkspaces as $workspaceId => $label) {
149
150 $selected = '';
151 if((int) $GLOBALS['BE_USER']->workspace === $workspaceId) {
152 $selected = ' selected="selected"';
153 }
154
155 $options[$workspaceId] = '<option value="'.htmlspecialchars($workspaceId).'"'.$selected.'>'.htmlspecialchars($label).'</option>';
156 }
157 } else {
158 $options[] = '<option value="-99">'.$GLOBALS['LANG']->getLL('shortcut_noWSfound',1).'</option>';
159 }
160
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)
166 .'</select>';
167 }
168
169 // preview
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"' : '').'/>';
172 }
173
174 $workspaceSelector.= ' <a href="mod/user/ws/index.php" target="content">'.
175 t3lib_iconWorks::getIconImage(
176 'sys_workspace',
177 array(),
178 $this->doc->backPath,
179 'align="top"'
180 ).'</a>';
181
182 return $workspaceSelector.'</span>';
183 }
184
185 /**
186 * adds the neccessary javascript ot the backend
187 *
188 */
189 private function addJavascriptToBackend() {
190 $this->backendReference->addJavascriptFile('js/workspaces.js');
191 }
192
193 /**
194 * returns additional attributes for the list item in the toolbar
195 *
196 * @return string list item HTML attibutes
197 */
198 public function getAdditionalAttributes() {
199 return ' id="workspace-selector"';
200 }
201 }
202
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']);
205 }
206
207 ?>