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