8ee913ee1686624166e61e4a44ccb13f2832ac09
[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 protected $changeWorkspace;
45
46 protected $changeWorkspacePreview;
47
48 /**
49 * reference back to the backend object
50 *
51 * @var \TYPO3\CMS\Backend\Controller\BackendController
52 */
53 protected $backendReference;
54
55 protected $checkAccess = NULL;
56
57 /**
58 * constructor
59 *
60 * @param \TYPO3\CMS\Backend\Controller\BackendController TYPO3 backend object reference
61 */
62 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
63 $this->backendReference = $backendReference;
64 $this->changeWorkspace = GeneralUtility::_GP('changeWorkspace');
65 $this->changeWorkspacePreview = GeneralUtility::_GP('changeWorkspacePreview');
66 $pageRenderer = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Page\\PageRenderer');
67 $this->backendReference->addJavaScript('TYPO3.Workspaces = { workspaceTitle : \'' . addslashes(\TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($GLOBALS['BE_USER']->workspace)) . '\'};
68 ');
69 }
70
71 /**
72 * Checks whether the user has access to this toolbar item
73 *
74 * @return boolean TRUE if user has access, FALSE if not
75 */
76 public function checkAccess() {
77 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
78 return FALSE;
79 }
80
81 if ($this->checkAccess == NULL) {
82 /** @var \TYPO3\CMS\Workspaces\Service\WorkspaceService $wsService */
83 $wsService = GeneralUtility::makeInstance('TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService');
84 $availableWorkspaces = $wsService->getAvailableWorkspaces();
85 if (count($availableWorkspaces) > 0) {
86 $this->checkAccess = TRUE;
87 } else {
88 $this->checkAccess = FALSE;
89 }
90 }
91 return $this->checkAccess;
92 }
93
94 /**
95 * Creates the selector for workspaces
96 *
97 * @return string workspace selector as HTML select
98 */
99 public function render() {
100 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.workspace', TRUE);
101 $this->addJavascriptToBackend();
102
103 $index = 0;
104 $availableWorkspaces = \TYPO3\CMS\Workspaces\Service\WorkspaceService::getAvailableWorkspaces();
105 $activeWorkspace = (int)$GLOBALS['BE_USER']->workspace;
106 $stateCheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-status-checked');
107 $stateUncheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('empty-empty', array(
108 'title' => $GLOBALS['LANG']->getLL('bookmark_inactive')
109 ));
110
111 $workspaceSections = array(
112 'top' => array(),
113 'items' => array(),
114 );
115
116 foreach ($availableWorkspaces as $workspaceId => $label) {
117 $workspaceId = (int)$workspaceId;
118 $iconState = ($workspaceId === $activeWorkspace ? $stateCheckedIcon : $stateUncheckedIcon);
119 $classValue = ($workspaceId === $activeWorkspace ? ' class="selected"' : '');
120 $sectionName = ($index++ === 0 ? 'top' : 'items');
121 $workspaceSections[$sectionName][] = '<li' . $classValue . '>' . '<a href="backend.php?changeWorkspace=' . $workspaceId . '" id="ws-' . $workspaceId . '" class="ws">' . $iconState . ' ' . htmlspecialchars($label) . '</a></li>';
122 }
123
124 if (count($workspaceSections['top']) > 0) {
125 // Go to workspace module link
126 if ($GLOBALS['BE_USER']->check('modules', 'web_WorkspacesWorkspaces')) {
127 $workspaceSections['top'][] = '<li>' . '<a href="javascript:top.goToModule(\'web_WorkspacesWorkspaces\');" target="content" id="goToWsModule">' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_workspace', TRUE) . '</a></li>';
128 }
129 $workspaceSections['top'][] = '<li class="divider"></li>';
130 } else {
131 $workspaceSections['top'][] = '<li>' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_noWSfound', TRUE) . '</li>';
132 }
133
134
135 $workspaceMenu = array(
136 '<a href="#" class="toolbar-item">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-toolbar-menu-workspace', array('title' => $title)) . '</a>',
137 '<div class="toolbar-item-menu" style="display: none">' ,
138 '<ul class="top">',
139 implode(LF, $workspaceSections['top']),
140 '</ul>',
141 '<ul class="items">',
142 implode(LF, $workspaceSections['items']),
143 '</ul>',
144 '</div>'
145 );
146
147 return implode(LF, $workspaceMenu);
148 }
149
150 /**
151 * adds the necessary JavaScript to the backend
152 *
153 * @return void
154 */
155 protected function addJavascriptToBackend() {
156 $this->backendReference->addJavascriptFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('workspaces') . 'Resources/Public/JavaScript/workspacemenu.js');
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
171 if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX)) {
172 $GLOBALS['TYPO3backend']->addToolbarItem('workSpaceSelector', 'TYPO3\\CMS\\Workspaces\\ExtDirect\\WorkspaceSelectorToolbarItem');
173 }