[BUGFIX] getAvailableWorkspaces called statically
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / ExtDirect / WorkspaceSelectorToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\ExtDirect;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2007-2013 Ingo Renner <ingo@typo3.org>
8 * (c) 2010-2013 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
34 require_once \TYPO3\CMS\Core\Extension\ExtensionManager::extPath('backend') . 'Classes/Toolbar/ToolbarItemHookInterface.php';
35 }
36
37 /**
38 * Class to render the workspace selector
39 *
40 * @author Ingo Renner <ingo@typo3.org>
41 */
42 class WorkspaceSelectorToolbarItem implements \TYPO3\CMS\Backend\Toolbar\ToolbarItemHookInterface {
43
44 /**
45 * Reference back to the backend object
46 *
47 * @var \TYPO3\CMS\Backend\Controller\BackendController
48 */
49 protected $backendReference;
50
51 /**
52 * @var bool|null
53 */
54 protected $checkAccess = NULL;
55
56 /**
57 * Constructor
58 *
59 * @param \TYPO3\CMS\Backend\Controller\BackendController $backendReference TYPO3 backend object reference
60 */
61 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
62 $this->backendReference = $backendReference;
63 $this->backendReference->addJavaScript(
64 'TYPO3.Workspaces = { workspaceTitle : ' . GeneralUtility::quoteJSvalue(addslashes(\TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($GLOBALS['BE_USER']->workspace))) . '};'
65 );
66 }
67
68 /**
69 * Checks whether the user has access to this toolbar item
70 *
71 * @return boolean TRUE if user has access, FALSE if not
72 */
73 public function checkAccess() {
74 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
75 return FALSE;
76 }
77
78 if ($this->checkAccess === NULL) {
79 /** @var \TYPO3\CMS\Workspaces\Service\WorkspaceService $wsService */
80 $wsService = GeneralUtility::makeInstance('TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService');
81 $availableWorkspaces = $wsService->getAvailableWorkspaces();
82 if (count($availableWorkspaces) > 0) {
83 $this->checkAccess = TRUE;
84 } else {
85 $this->checkAccess = FALSE;
86 }
87 }
88 return $this->checkAccess;
89 }
90
91 /**
92 * Creates the selector for workspaces
93 *
94 * @return string workspace selector as HTML select
95 */
96 public function render() {
97 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.workspace', TRUE);
98 $this->addJavascriptToBackend();
99
100 $index = 0;
101 /** @var \TYPO3\CMS\Workspaces\Service\WorkspaceService $wsService */
102 $wsService = GeneralUtility::makeInstance('TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService');
103 $availableWorkspaces = $wsService->getAvailableWorkspaces();
104 $activeWorkspace = (int)$GLOBALS['BE_USER']->workspace;
105 $stateCheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-status-checked');
106 $stateUncheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('empty-empty', array(
107 'title' => $GLOBALS['LANG']->getLL('bookmark_inactive')
108 ));
109
110 $workspaceSections = array(
111 'top' => array(),
112 'items' => array(),
113 );
114
115 foreach ($availableWorkspaces as $workspaceId => $label) {
116 $workspaceId = (int)$workspaceId;
117 $iconState = ($workspaceId === $activeWorkspace ? $stateCheckedIcon : $stateUncheckedIcon);
118 $classValue = ($workspaceId === $activeWorkspace ? ' class="selected"' : '');
119 $sectionName = ($index++ === 0 ? 'top' : 'items');
120 $workspaceSections[$sectionName][] = '<li' . $classValue . '>' . '<a href="backend.php?changeWorkspace=' . $workspaceId . '" id="ws-' . $workspaceId . '" class="ws">' . $iconState . ' ' . htmlspecialchars($label) . '</a></li>';
121 }
122
123 if (count($workspaceSections['top']) > 0) {
124 // Go to workspace module link
125 if ($GLOBALS['BE_USER']->check('modules', 'web_WorkspacesWorkspaces')) {
126 $workspaceSections['top'][] = '<li>' . '<a href="javascript:top.goToModule(\'web_WorkspacesWorkspaces\');" target="content" id="goToWsModule">' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_workspace', TRUE) . '</a></li>';
127 }
128 $workspaceSections['top'][] = '<li class="divider"></li>';
129 } else {
130 $workspaceSections['top'][] = '<li>' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_noWSfound', TRUE) . '</li>';
131 }
132
133 $workspaceMenu = array(
134 '<a href="#" class="toolbar-item">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-toolbar-menu-workspace', array('title' => $title)) . '</a>',
135 '<div class="toolbar-item-menu" style="display: none">' ,
136 '<ul class="top">',
137 implode(LF, $workspaceSections['top']),
138 '</ul>',
139 '<ul class="items">',
140 implode(LF, $workspaceSections['items']),
141 '</ul>',
142 '</div>'
143 );
144
145 return implode(LF, $workspaceMenu);
146 }
147
148 /**
149 * Adds the necessary JavaScript to the backend
150 *
151 * @return void
152 */
153 protected function addJavascriptToBackend() {
154 $this->backendReference->addJavascriptFile(
155 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('workspaces') . 'Resources/Public/JavaScript/workspacemenu.js'
156 );
157 }
158
159 /**
160 * Returns additional attributes for the list item in the toolbar
161 *
162 * @return string List item HTML attibutes
163 */
164 public function getAdditionalAttributes() {
165 return 'id="workspace-selector-menu"';
166 }
167
168 }
169
170 if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX)) {
171 $GLOBALS['TYPO3backend']->addToolbarItem('workSpaceSelector', 'TYPO3\\CMS\\Workspaces\\ExtDirect\\WorkspaceSelectorToolbarItem');
172 }