[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / ExtDirect / WorkspaceSelectorToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\ExtDirect;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
20 require_once \TYPO3\CMS\Core\Extension\ExtensionManager::extPath('backend') . 'Classes/Toolbar/ToolbarItemHookInterface.php';
21 }
22
23 /**
24 * class to render the workspace selector
25 *
26 * @author Ingo Renner <ingo@typo3.org>
27 */
28 class WorkspaceSelectorToolbarItem implements \TYPO3\CMS\Backend\Toolbar\ToolbarItemHookInterface {
29
30 protected $changeWorkspace;
31
32 protected $changeWorkspacePreview;
33
34 /**
35 * reference back to the backend object
36 *
37 * @var \TYPO3\CMS\Backend\Controller\BackendController
38 */
39 protected $backendReference;
40
41 protected $checkAccess = NULL;
42
43 /**
44 * constructor
45 *
46 * @param \TYPO3\CMS\Backend\Controller\BackendController TYPO3 backend object reference
47 */
48 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
49 $this->backendReference = $backendReference;
50 $this->changeWorkspace = GeneralUtility::_GP('changeWorkspace');
51 $this->changeWorkspacePreview = GeneralUtility::_GP('changeWorkspacePreview');
52 $pageRenderer = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Page\\PageRenderer');
53 $this->backendReference->addJavaScript('TYPO3.Workspaces = { workspaceTitle : \'' . addslashes(\TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($GLOBALS['BE_USER']->workspace)) . '\'};
54 ');
55 }
56
57 /**
58 * Checks whether the user has access to this toolbar item
59 *
60 * @return boolean TRUE if user has access, FALSE if not
61 */
62 public function checkAccess() {
63 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
64 return FALSE;
65 }
66
67 if ($this->checkAccess == NULL) {
68 /** @var \TYPO3\CMS\Workspaces\Service\WorkspaceService $wsService */
69 $wsService = GeneralUtility::makeInstance('TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService');
70 $availableWorkspaces = $wsService->getAvailableWorkspaces();
71 if (count($availableWorkspaces) > 0) {
72 $this->checkAccess = TRUE;
73 } else {
74 $this->checkAccess = FALSE;
75 }
76 }
77 return $this->checkAccess;
78 }
79
80 /**
81 * Creates the selector for workspaces
82 *
83 * @return string workspace selector as HTML select
84 */
85 public function render() {
86 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.workspace', TRUE);
87 $this->addJavascriptToBackend();
88
89 $index = 0;
90 $availableWorkspaces = \TYPO3\CMS\Workspaces\Service\WorkspaceService::getAvailableWorkspaces();
91 $activeWorkspace = (int)$GLOBALS['BE_USER']->workspace;
92 $stateCheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-status-checked');
93 $stateUncheckedIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('empty-empty', array(
94 'title' => $GLOBALS['LANG']->getLL('bookmark_inactive')
95 ));
96
97 $workspaceSections = array(
98 'top' => array(),
99 'items' => array(),
100 );
101
102 foreach ($availableWorkspaces as $workspaceId => $label) {
103 $workspaceId = (int)$workspaceId;
104 $iconState = ($workspaceId === $activeWorkspace ? $stateCheckedIcon : $stateUncheckedIcon);
105 $classValue = ($workspaceId === $activeWorkspace ? ' class="selected"' : '');
106 $sectionName = ($index++ === 0 ? 'top' : 'items');
107 $workspaceSections[$sectionName][] = '<li' . $classValue . '>' . '<a href="backend.php?changeWorkspace=' . $workspaceId . '" id="ws-' . $workspaceId . '" class="ws">' . $iconState . ' ' . htmlspecialchars($label) . '</a></li>';
108 }
109
110 if (count($workspaceSections['top']) > 0) {
111 // Go to workspace module link
112 if ($GLOBALS['BE_USER']->check('modules', 'web_WorkspacesWorkspaces')) {
113 $workspaceSections['top'][] = '<li>' . '<a href="javascript:top.goToModule(\'web_WorkspacesWorkspaces\');" target="content" id="goToWsModule">' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_workspace', TRUE) . '</a></li>';
114 }
115 $workspaceSections['top'][] = '<li class="divider"></li>';
116 } else {
117 $workspaceSections['top'][] = '<li>' . $stateUncheckedIcon . ' ' . $GLOBALS['LANG']->getLL('bookmark_noWSfound', TRUE) . '</li>';
118 }
119
120
121 $workspaceMenu = array(
122 '<a href="#" class="toolbar-item">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-toolbar-menu-workspace', array('title' => $title)) . '</a>',
123 '<div class="toolbar-item-menu" style="display: none">' ,
124 '<ul class="top">',
125 implode(LF, $workspaceSections['top']),
126 '</ul>',
127 '<ul class="items">',
128 implode(LF, $workspaceSections['items']),
129 '</ul>',
130 '</div>'
131 );
132
133 return implode(LF, $workspaceMenu);
134 }
135
136 /**
137 * adds the necessary JavaScript to the backend
138 *
139 * @return void
140 */
141 protected function addJavascriptToBackend() {
142 $this->backendReference->addJavascriptFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('workspaces') . 'Resources/Public/JavaScript/workspacemenu.js');
143 }
144
145 /**
146 * returns additional attributes for the list item in the toolbar
147 *
148 * @return string list item HTML attibutes
149 */
150 public function getAdditionalAttributes() {
151 return 'id="workspace-selector-menu"';
152 }
153
154 }
155
156
157 if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX)) {
158 $GLOBALS['TYPO3backend']->addToolbarItem('workSpaceSelector', 'TYPO3\\CMS\\Workspaces\\ExtDirect\\WorkspaceSelectorToolbarItem');
159 }