[TASK] Replace sprite icon "apps-toolbar-menu-opendocs" with the new IconFactory
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Classes / Backend / ToolbarItems / OpendocsToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Opendocs\Backend\ToolbarItems;
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\Backend\Toolbar\ToolbarItemInterface;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Page\PageRenderer;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Alist of all open documents
26 */
27 class OpendocsToolbarItem implements ToolbarItemInterface {
28
29 /**
30 * @var array
31 */
32 protected $openDocs;
33
34 /**
35 * @var array
36 */
37 protected $recentDocs;
38
39 /**
40 * @var IconFactory
41 */
42 protected $iconFactory;
43
44 /**
45 * Constructor
46 */
47 public function __construct() {
48 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
49 $this->getLanguageService()->includeLLFile('EXT:opendocs/Resources/Private/Language/locallang.xlf');
50 $this->loadDocsFromUserSession();
51 $pageRenderer = $this->getPageRenderer();
52 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Opendocs/Toolbar/OpendocsMenu');
53 }
54
55 /**
56 * Checks whether the user has access to this toolbar item
57 *
58 * @return bool TRUE if user has access, FALSE if not
59 */
60 public function checkAccess() {
61 $conf = $this->getBackendUser()->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
62 return $conf['value'] != 1;
63 }
64
65 /**
66 * Loads the opened and recently opened documents from the user
67 *
68 * @return void
69 */
70 public function loadDocsFromUserSession() {
71 $backendUser = $this->getBackendUser();
72 list($this->openDocs, ) = $backendUser->getModuleData('FormEngine', 'ses');
73 $this->recentDocs = $backendUser->getModuleData('opendocs::recent');
74 }
75
76 /**
77 * Render toolbar icon
78 *
79 * @return string HTML
80 */
81 public function getItem() {
82 $numDocs = count($this->openDocs);
83 $title = $this->getLanguageService()->getLL('toolbaritem', TRUE);
84
85 $opendocsMenu = array();
86 $opendocsMenu[] = '<span title="' . $title . '">' . $this->iconFactory->getIcon('apps-toolbar-menu-opendocs', Icon::SIZE_SMALL) . '</span>';
87 $opendocsMenu[] = '<span class="badge" id="tx-opendocs-counter">' . $numDocs . '</span>';
88
89 return implode(LF, $opendocsMenu);
90 }
91
92 /**
93 * Render drop down
94 *
95 * @return string HTML
96 */
97 public function getDropDown() {
98 $languageService = $this->getLanguageService();
99 $openDocuments = $this->openDocs;
100 $recentDocuments = $this->recentDocs;
101 $entries = array();
102 if (!empty($openDocuments)) {
103 $entries[] = '<li class="dropdown-header">' . $languageService->getLL('open_docs', TRUE) . '</li>';
104 $i = 0;
105 foreach ($openDocuments as $md5sum => $openDocument) {
106 $i++;
107 $entries[] = $this->renderMenuEntry($openDocument, $md5sum, FALSE, $i == 1);
108 }
109 $entries[] = '<li class="divider"></li>';
110 }
111 // If there are "recent documents" in the list, add them
112 if (!empty($recentDocuments)) {
113 $entries[] = '<li class="dropdown-header">' . $languageService->getLL('recent_docs', TRUE) . '</li>';
114 $i = 0;
115 foreach ($recentDocuments as $md5sum => $recentDocument) {
116 $i++;
117 $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, TRUE, $i == 1);
118 }
119 }
120 if (!empty($entries)) {
121 $content = '<ul class="dropdown-list">' . implode('', $entries) . '</ul>';
122 } else {
123 $content = '<p>' . $languageService->getLL('no_docs', TRUE) . '</p>';
124 }
125 return $content;
126 }
127
128 /**
129 * Returns the recent documents list as an array
130 *
131 * @param array $document
132 * @param string $md5sum
133 * @param bool $isRecentDoc
134 * @param bool $isFirstDoc
135 * @return array All recent documents as list-items
136 */
137 protected function renderMenuEntry($document, $md5sum, $isRecentDoc = FALSE, $isFirstDoc = FALSE) {
138 $table = $document[3]['table'];
139 $uid = $document[3]['uid'];
140 $record = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL($table, $uid);
141 if (!is_array($record)) {
142 // Record seems to be deleted
143 return '';
144 }
145 $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
146 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord($table, $record);
147 $link = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('record_edit') . '&' . $document[2];
148 $pageId = (int)$document[3]['uid'];
149 if ($document[3]['table'] !== 'pages') {
150 $pageId = (int)$document[3]['pid'];
151 }
152 $onClickCode = 'jump(' . GeneralUtility::quoteJSvalue($link) . ', \'web_list\', \'web\', ' . $pageId . '); TYPO3.OpendocsMenu.toggleMenu(); return false;';
153 if (!$isRecentDoc) {
154 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', TRUE);
155 // Open document
156 $closeIcon = $this->iconFactory->getIcon('actions-document-close', Icon::SIZE_SMALL);
157 $entry = '
158 <li class="opendoc">
159 <a href="#" class="dropdown-list-link dropdown-link-list-add-close" onclick="' . htmlspecialchars($onClickCode) . '" target="content">' . $icon . ' ' . $label . '</a>
160 <a href="#" class="dropdown-list-link-close" data-opendocsidentifier="' . $md5sum . '" title="' . $title . '">' . $closeIcon . '</a>
161 </li>';
162 } else {
163 // Recently used document
164 $entry = '
165 <li>
166 <a href="#" class="dropdown-list-link" onclick="' . htmlspecialchars($onClickCode) . '" target="content">' . $icon . ' ' . $label . '</a>
167 </li>';
168 }
169 return $entry;
170 }
171
172 /**
173 * No additional attributes
174 *
175 * @return string List item HTML attibutes
176 */
177 public function getAdditionalAttributes() {
178 return array();
179 }
180
181 /**
182 * This item has a drop down
183 *
184 * @return bool
185 */
186 public function hasDropDown() {
187 return TRUE;
188 }
189
190
191 /*******************
192 *** HOOKS ***
193 *******************/
194 /**
195 * Called as a hook in \TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal, calls a JS function to change
196 * the number of opened documents
197 *
198 * @param array $params
199 * @param unknown_type $ref
200 * @return string list item HTML attributes
201 */
202 public function updateNumberOfOpenDocsHook(&$params, $ref) {
203 $params['JScode'] = '
204 if (top && top.TYPO3.OpendocsMenu) {
205 top.TYPO3.OpendocsMenu.updateMenu();
206 }
207 ';
208 }
209
210 /******************
211 *** AJAX CALLS ***
212 ******************/
213 /**
214 * Closes a document in the session and
215 *
216 * @param array $params Array of parameters from the AJAX interface, currently unused
217 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
218 * @return string List item HTML attributes
219 */
220 public function closeDocument($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj = NULL) {
221 $backendUser = $this->getBackendUser();
222 $md5sum = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('md5sum');
223 if ($md5sum && isset($this->openDocs[$md5sum])) {
224 // Add the document to be closed to the recent documents
225 $this->recentDocs = array_merge(array($md5sum => $this->openDocs[$md5sum]), $this->recentDocs);
226 // Allow a maximum of 8 recent documents
227 if (count($this->recentDocs) > 8) {
228 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
229 }
230 // Remove it from the list of the open documents, and store the status
231 unset($this->openDocs[$md5sum]);
232 list(, $docDat) = $backendUser->getModuleData('FormEngine', 'ses');
233 $backendUser->pushModuleData('FormEngine', array($this->openDocs, $docDat));
234 $backendUser->pushModuleData('opendocs::recent', $this->recentDocs);
235 }
236 $this->renderAjax($params, $ajaxObj);
237 }
238
239 /**
240 * Renders the menu so that it can be returned as response to an AJAX call
241 *
242 * @param array $params Array of parameters from the AJAX interface, currently unused
243 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
244 * @return void
245 */
246 public function renderAjax($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj = NULL) {
247 $ajaxObj->addContent('opendocsMenu', $this->getDropDown());
248 }
249
250 /**
251 * Position relative to others
252 *
253 * @return int
254 */
255 public function getIndex() {
256 return 30;
257 }
258
259 /**
260 * Returns the current BE user.
261 *
262 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
263 */
264 protected function getBackendUser() {
265 return $GLOBALS['BE_USER'];
266 }
267
268 /**
269 * Returns current PageRenderer
270 *
271 * @return PageRenderer
272 */
273 protected function getPageRenderer() {
274 return GeneralUtility::makeInstance(PageRenderer::class);
275 }
276
277 /**
278 * Returns LanguageService
279 *
280 * @return \TYPO3\CMS\Lang\LanguageService
281 */
282 protected function getLanguageService() {
283 return $GLOBALS['LANG'];
284 }
285
286 /**
287 * Return DatabaseConnection
288 *
289 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
290 */
291 protected function getDatabaseConnection() {
292 return $GLOBALS['TYPO3_DB'];
293 }
294
295 }