[TASK] Cleanup WorkspaceSelectorToolbarItem
[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 $availableWorkspaces = \TYPO3\CMS\Workspaces\Service\WorkspaceService::getAvailableWorkspaces();
102 $activeWorkspace = (int)$GLOBALS['BE_USER']->workspace;
103 $stateCheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-status-checked');
104 $stateUncheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('empty-empty', array(
105 'title' => $GLOBALS['LANG']->getLL('bookmark_inactive')
106 ));
107
108 $workspaceSections = array(
109 'top' => array(),
110 'items' => array(),
111 );
112
113 foreach ($availableWorkspaces as $workspaceId => $label) {
114 $workspaceId = (int)$workspaceId;
115 $iconState = ($workspaceId === $activeWorkspace ? $stateCheckedIcon : $stateUncheckedIcon);
116 $classValue = ($workspaceId === $activeWorkspace ? ' class="selected"' : '');
117 $sectionName = ($index++ === 0 ? 'top' : 'items');
118 $workspaceSections[$sectionName][] = '<li' . $classValue . '>' . '<a href="backend.php?changeWorkspace=' . $workspaceId . '" id="ws-' . $workspaceId . '" class="ws">' . $iconState . ' ' . htmlspecialchars($label) . '</a></li>';
119 }
120
121 if (count($workspaceSections['top']) > 0) {
122 // Go to workspace module link
123 if ($GLOBALS['BE_USER']->check('modules', 'web_WorkspacesWorkspaces')) {
124 $workspaceSections['top'][] = '<li>' . '<a href="javascript:top.goToModule(\'web_WorkspacesWorkspaces\');" target="content" id="goToWsModule">' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_workspace', TRUE) . '</a></li>';
125 }
126 $workspaceSections['top'][] = '<li class="divider"></li>';
127 } else {
128 $workspaceSections['top'][] = '<li>' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_noWSfound', TRUE) . '</li>';
129 }
130
131 $workspaceMenu = array(
132 '<a href="#" class="toolbar-item">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-toolbar-menu-workspace', array('title' => $title)) . '</a>',
133 '<div class="toolbar-item-menu" style="display: none">' ,
134 '<ul class="top">',
135 implode(LF, $workspaceSections['top']),
136 '</ul>',
137 '<ul class="items">',
138 implode(LF, $workspaceSections['items']),
139 '</ul>',
140 '</div>'
141 );
142
143 return implode(LF, $workspaceMenu);
144 }
145
146 /**
147 * Adds the necessary JavaScript to the backend
148 *
149 * @return void
150 */
151 protected function addJavascriptToBackend() {
152 $this->backendReference->addJavascriptFile(
153 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('workspaces') . 'Resources/Public/JavaScript/workspacemenu.js'
154 );
155 }
156
157 /**
158 * Returns additional attributes for the list item in the toolbar
159 *
160 * @return string List item HTML attibutes
161 */
162 public function getAdditionalAttributes() {
163 return 'id="workspace-selector-menu"';
164 }
165
166 }
167
168 if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX)) {
169 $GLOBALS['TYPO3backend']->addToolbarItem('workSpaceSelector', 'TYPO3\\CMS\\Workspaces\\ExtDirect\\WorkspaceSelectorToolbarItem');
170 }