added feature #6721: add the workspace selector to the cleaner backend toolbar
[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 t3lib_backendToolbarItem {
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 public function __construct() {
49 $this->changeWorkspace = t3lib_div::_GP('changeWorkspace');
50 $this->changeWorkspacePreview = t3lib_div::_GP('changeWorkspacePreview');
51 }
52
53 /**
54 * sets the backend reference
55 *
56 * @param TYPO3backend backend object reference
57 */
58 public function setBackend(&$backendReference) {
59 $this->backendReference = $backendReference;
60 }
61
62 /**
63 * changes workspace if needed and then reloads the backend
64 *
65 * @return void
66 */
67 public function changeWorkspace() {
68 $reloadBackend = false;
69
70 // Changing workspace and if so, reloading entire backend:
71 if (strlen($this->changeWorkspace)) {
72 $GLOBALS['BE_USER']->setWorkspace($this->changeWorkspace);
73 $reloadBackend = true;
74 }
75
76 // Changing workspace preview and if so, reloading entire backend:
77 if (strlen($this->changeWorkspacePreview)) {
78 $GLOBALS['BE_USER']->setWorkspacePreview($this->changeWorkspacePreview);
79 $reloadBackend = true;
80 }
81
82 if($reloadBackend) {
83 $this->backendReference->addJavascript(
84 'top.location.href=\'backend.php\';'
85 );
86 }
87 }
88
89 /**
90 * retrieves the available workspaces from the database and checks whether
91 * they're available to the current BE user
92 *
93 * @return array array of worspaces available to the current user
94 */
95 private function getAvailableWorkspaces() {
96 $availableWorkspaces = array();
97
98 // add default workspaces
99 if($GLOBALS['BE_USER']->checkWorkspace(array('uid' => 0))) {
100 $availableWorkspaces[0] = '['.$GLOBALS['LANG']->getLL('shortcut_onlineWS').']';
101 }
102 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => -1))) {
103 $availableWorkspaces[-1] = '['.$GLOBALS['LANG']->getLL('shortcut_offlineWS').']';
104 }
105
106 // add custom workspaces (selecting all, filtering by BE_USER check):
107 $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
108 'uid, title, adminusers, members, reviewers',
109 'sys_workspace',
110 'pid = 0'.t3lib_BEfunc::deleteClause('sys_workspace'),
111 '',
112 'title'
113 );
114 if(count($customWorkspaces)) {
115 foreach($customWorkspaces as $workspace) {
116 if($GLOBALS['BE_USER']->checkWorkspace($workspace)) {
117 $availableWorkspaces[$workspace['uid']] = $workspace['uid'].': '.$workspace['title'];
118 }
119 }
120 }
121
122 return $availableWorkspaces;
123 }
124
125 /**
126 * Creates the selector for workspaces
127 *
128 * @return string workspace selector as HTML select
129 */
130 public function render() {
131 $this->addJavascriptToBackend();
132 $this->changeWorkspace();
133
134 $options = array();
135 $workspaceSelector = '';
136 $availableWorkspaces = $this->getAvailableWorkspaces();
137
138 // build selector box options
139 if(count($availableWorkspaces)) {
140 foreach($availableWorkspaces as $workspaceId => $label) {
141
142 $selected = '';
143 if((int) $GLOBALS['BE_USER']->workspace === $workspaceId) {
144 $selected = ' selected="selected"';
145 }
146
147 $options[$workspaceId] = '<option value="'.htmlspecialchars($workspaceId).'"'.$selected.'>'.htmlspecialchars($label).'</option>';
148 }
149 } else {
150 $options[] = '<option value="-99">'.$GLOBALS['LANG']->getLL('shortcut_noWSfound',1).'</option>';
151 }
152
153 // build selector box
154 if(count($options) > 1) {
155 $workspaceSelector .=
156 '<select name="_workspaceSelector" onchange="changeWorkspace(this.options[this.selectedIndex].value);">'
157 .implode("\n", $options)
158 .'</select>';
159 }
160
161 // preview
162 if($GLOBALS['BE_USER']->workspace !== 0) {
163 $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"' : '').'/>';
164 }
165
166 $workspaceSelector.= ' <a href="mod/user/ws/index.php" target="content">'.
167 t3lib_iconWorks::getIconImage(
168 'sys_workspace',
169 array(),
170 $this->doc->backPath,
171 'align="top"'
172 ).'</a>';
173
174 return $workspaceSelector;
175 }
176
177 /**
178 * adds the neccessary javascript ot the backend
179 *
180 */
181 private function addJavascriptToBackend() {
182 $this->backendReference->addJavascriptFile('typo3/js/workspaces.js');
183 }
184 }
185
186 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']) {
187 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.workspaceselector.php']);
188 }
189
190 ?>