91dde3bc934a0ad05e4dd032f06a71d69fb011ae
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / BackendUserInterface / WorkspaceSelectorToolbarItem.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2010 Ingo Renner <ingo@typo3.org>
6 * (c) 2010 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 if(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
30 require_once(PATH_typo3 . '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 Workspaces
38 * @subpackage BackendUserInterface
39 */
40 class WorkspaceSelectorToolbarItem 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 protected $checkAccess = NULL;
53
54 /**
55 * constructor
56 *
57 * @param TYPO3backend TYPO3 backend object reference
58 */
59 public function __construct(TYPO3backend &$backendReference = null) {
60 $this->backendReference = $backendReference;
61 $this->changeWorkspace = t3lib_div::_GP('changeWorkspace');
62 $this->changeWorkspacePreview = t3lib_div::_GP('changeWorkspacePreview');
63
64 $pageRenderer = t3lib_div::makeInstance('t3lib_pageRenderer');
65 $this->backendReference->addJavaScript("TYPO3.Workspaces = { workspaceTitle : '" . tx_Workspaces_Service_Workspaces::getWorkspaceTitle($GLOBALS['BE_USER']->workspace) . "'};\n");
66 }
67
68 /**
69 * checks whether the user has access to this toolbar item
70 *
71 * @see typo3/alt_shortcut.php
72 * @return boolean true if user has access, false if not
73 */
74 public function checkAccess() {
75 if (t3lib_extMgm::isLoaded('workspaces')) {
76 if ($this->checkAccess == NULL) {
77 $availableWorkspaces = tx_Workspaces_Service_Workspaces::getAvailableWorkspaces();
78 if (count($availableWorkspaces) > 1) {
79 $this->checkAccess = TRUE;
80 } else {
81 $this->checkAccess = FALSE;
82 }
83 }
84 return $this->checkAccess;
85 }
86 return FALSE;
87 }
88
89 /**
90 * Creates the selector for workspaces
91 *
92 * @return string workspace selector as HTML select
93 */
94 public function render() {
95 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:toolbarItems.workspace', true);
96 $this->addJavascriptToBackend();
97 $availableWorkspaces = tx_Workspaces_Service_Workspaces::getAvailableWorkspaces();
98 $workspaceMenu = array();
99
100 $stateCheckedIcon = t3lib_iconWorks::getSpriteIcon('status-status-checked');
101
102 $stateUncheckedIcon = t3lib_iconWorks::getSpriteIcon('empty-empty', array(
103 'title' => $GLOBALS['LANG']->getLL('bookmark_inactive')
104 ));
105
106 $workspaceMenu[] = '<a href="#" class="toolbar-item">' .
107 t3lib_iconWorks::getSpriteIcon('apps-toolbar-menu-workspace', array('title' => $title)) .
108 '</a>';
109 $workspaceMenu[] = '<ul class="toolbar-item-menu" style="display: none;">';
110
111 if (count($availableWorkspaces)) {
112 foreach($availableWorkspaces as $workspaceId => $label) {
113 $selected = '';
114 $icon = $stateUncheckedIcon;
115 if((int) $GLOBALS['BE_USER']->workspace === $workspaceId) {
116 $selected = ' class="selected"';
117 $icon = $stateCheckedIcon;
118 }
119
120 $workspaceMenu[] = '<li' . $selected . '>' . $icon .
121 ' <a href="backend.php?changeWorkspace=' .
122 intval($workspaceId) . '" id="ws-' . intval($workspaceId) .
123 '" class="ws">' . $label . '</a></li>';
124 }
125 } else {
126 $workspaceMenu[] = '<li>' . $stateUncheckedIcon . ' ' .
127 $GLOBALS['LANG']->getLL('bookmark_noWSfound', true) .
128 '</li>';
129 }
130
131 // frontend preview toggle
132 $frontendPreviewActiveIcon = $stateUncheckedIcon;
133 if (!$GLOBALS['BE_USER']->user['workspace_preview']) {
134 $frontendPreviewActiveIcon = $stateCheckedIcon;
135 }
136
137 $workspaceMenu[] = '<li class="divider">' . $frontendPreviewActiveIcon .
138 '<a href="backend.php?changeWorkspacePreview=' .
139 ($GLOBALS['BE_USER']->user['workspace_preview'] ? '0' : '1') .
140 '" id="frontendPreviewToggle">' . $GLOBALS['LANG']->getLL('bookmark_FEPreview', true) . '</a></li>';
141
142 // go to workspace module link
143 $workspaceMenu[] = '<li>' . $stateUncheckedIcon . ' ' .
144 '<a href="javascript:top.goToModule(\'web_WorkspacesWorkspaces\');" target="content" id="goToWsModule">' .
145 ' '. $GLOBALS['LANG']->getLL('bookmark_workspace', true) . '</a></li>';
146
147 $workspaceMenu[] = '</ul>';
148
149 return implode(LF, $workspaceMenu);
150 }
151
152 /**
153 * adds the necessary JavaScript to the backend
154 *
155 * @return void
156 */
157 protected function addJavascriptToBackend() {
158 $this->backendReference->addJavascriptFile(t3lib_extMgm::extRelPath('workspaces') . 'Resources/Public/JavaScript/workspacemenu.js');
159 }
160
161 /**
162 * returns additional attributes for the list item in the toolbar
163 *
164 * @return string list item HTML attibutes
165 */
166 public function getAdditionalAttributes() {
167 return ' id="workspace-selector-menu"';
168 }
169 }
170
171
172 if(!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX)) {
173 $GLOBALS['TYPO3backend']->addToolbarItem('workSpaceSelector', 'WorkspaceSelectorToolbarItem');
174 }
175
176
177 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/BackendUserInterface/WorkspaceSelectorToolbarItem.php']) {
178 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/BackendUserInterface/WorkspaceSelectorToolbarItem.php']);
179 }
180 ?>