1c877b5cf7683b7c4668ca944caac4ddc718c3ab
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Classes / Controller / OpendocsController.php
1 <?php
2 namespace TYPO3\CMS\Opendocs\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Benjamin Mack <mack@xnos.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Adding a list of all open documents of a user to the backend.php
32 *
33 * @author Benjamin Mack <benni@typo3.org>
34 * @author Ingo Renner <ingo@typo3.org>
35 */
36 class OpendocsController implements \TYPO3\CMS\Backend\Toolbar\ToolbarItemHookInterface {
37
38 /**
39 * reference back to the backend object
40 *
41 * @var TYPO3backend
42 */
43 protected $backendReference;
44
45 protected $openDocs;
46
47 protected $recentDocs;
48
49 protected $EXTKEY = 'opendocs';
50
51 /**
52 * Constructor, loads the documents from the user control
53 *
54 * @param \TYPO3\CMS\Backend\Controller\BackendController TYPO3 backend object reference
55 */
56 public function __construct(\TYPO3\CMS\Backend\Controller\BackendController &$backendReference = NULL) {
57 $GLOBALS['LANG']->includeLLFile('EXT:opendocs/locallang_opendocs.xlf');
58 $this->backendReference = $backendReference;
59 $this->loadDocsFromUserSession();
60 }
61
62 /**
63 * Checks whether the user has access to this toolbar item
64 *
65 * @return boolean TRUE if user has access, FALSE if not
66 */
67 public function checkAccess() {
68 $conf = $GLOBALS['BE_USER']->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
69 return $conf['value'] != 1;
70 }
71
72 /**
73 * Loads the opened and recently opened documents from the user
74 *
75 * @return void
76 */
77 public function loadDocsFromUserSession() {
78 list($this->openDocs, ) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
79 $this->recentDocs = $GLOBALS['BE_USER']->getModuleData('opendocs::recent');
80 }
81
82 /**
83 * Renders the toolbar item and the initial menu
84 *
85 * @return string The toolbar item including the initial menu content as HTML
86 */
87 public function render() {
88 $this->addJavascriptToBackend();
89 $this->addCssToBackend();
90 $numDocs = count($this->openDocs);
91 $opendocsMenu = array();
92 $title = $GLOBALS['LANG']->getLL('toolbaritem', TRUE);
93
94 // Toolbar item icon
95 $opendocsMenu[] = '<a href="#" class="toolbar-item">';
96 $opendocsMenu[] = '<input type="text" id="tx-opendocs-counter" disabled="disabled" value="' . $numDocs . '" />';
97 $opendocsMenu[] = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-toolbar-menu-opendocs', array('title' => $title)) . '</a>';
98
99 // Toolbar item menu and initial content
100 $opendocsMenu[] = '<div class="toolbar-item-menu" style="display: none;">';
101 $opendocsMenu[] = $this->renderMenu();
102 $opendocsMenu[] = '</div>';
103 return implode(LF, $opendocsMenu);
104 }
105
106 /**
107 * renders the pure contents of the menu
108 *
109 * @return string The menu's content
110 */
111 public function renderMenu() {
112 $openDocuments = $this->openDocs;
113 $recentDocuments = $this->recentDocs;
114 $entries = array();
115 $content = '';
116 if (count($openDocuments)) {
117 $entries[] = '<tr><th colspan="3">' . $GLOBALS['LANG']->getLL('open_docs', TRUE) . '</th></tr>';
118 $i = 0;
119 foreach ($openDocuments as $md5sum => $openDocument) {
120 $i++;
121 $entries[] = $this->renderMenuEntry($openDocument, $md5sum, FALSE, $i == 1);
122 }
123 }
124 // If there are "recent documents" in the list, add them
125 if (count($recentDocuments)) {
126 $entries[] = '<tr><th colspan="3">' . $GLOBALS['LANG']->getLL('recent_docs', TRUE) . '</th></tr>';
127 $i = 0;
128 foreach ($recentDocuments as $md5sum => $recentDocument) {
129 $i++;
130 $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, TRUE, $i == 1);
131 }
132 }
133 if (count($entries)) {
134 $content = '<table class="list" cellspacing="0" cellpadding="0" border="0">' . implode('', $entries) . '</table>';
135 } else {
136 $content = '<div class="no-docs">' . $GLOBALS['LANG']->getLL('no_docs', TRUE) . '</div>';
137 }
138 return $content;
139 }
140
141 /**
142 * Returns the recent documents list as an array
143 *
144 * @return array All recent documents as list-items
145 */
146 public function renderMenuEntry($document, $md5sum, $isRecentDoc = FALSE, $isFirstDoc = FALSE) {
147 $table = $document[3]['table'];
148 $uid = $document[3]['uid'];
149 $record = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL($table, $uid);
150 if (!is_array($record)) {
151 // Record seems to be deleted
152 return '';
153 }
154 $label = htmlspecialchars(strip_tags(htmlspecialchars_decode($document[0])));
155 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord($table, $record);
156 $link = $GLOBALS['BACK_PATH'] . 'alt_doc.php?' . $document[2];
157 $pageId = (int)$document[3]['uid'];
158 if ($document[3]['table'] !== 'pages') {
159 $pageId = (int)$document[3]['pid'];
160 }
161 $firstRow = '';
162 if ($isFirstDoc) {
163 $firstRow = ' first-row';
164 }
165 if (!$isRecentDoc) {
166 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', TRUE);
167 // Open document
168 $closeIcon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-close');
169 $entry = '
170 <tr class="opendoc' . $firstRow . '">
171 <td class="icon">' . $icon . '</td>
172 <td class="label"><a href="#" onclick="jump(unescape(\'' . htmlspecialchars($link) . '\'), \'web_list\', \'web\', ' . $pageId . '); TYPO3BackendOpenDocs.toggleMenu(); return false;" target="content">' . $label . '</a></td>
173 <td class="close" onclick="return TYPO3BackendOpenDocs.closeDocument(\'' . $md5sum . '\');">' . $closeIcon . '</td>
174 </tr>';
175 } else {
176 // Recently used document
177 $entry = '
178 <tr class="recentdoc' . $firstRow . '">
179 <td class="icon">' . $icon . '</td>
180 <td class="label" colspan="2"><a href="#" onclick="jump(unescape(\'' . htmlspecialchars($link) . '\'), \'web_list\', \'web\', ' . $pageId . '); TYPO3BackendOpenDocs.toggleMenu(); return false;" target="content">' . $label . '</a></td>
181 </tr>';
182 }
183 return $entry;
184 }
185
186 /**
187 * Returns additional attributes for the list item in the toolbar
188 *
189 * @return string List item HTML attibutes
190 */
191 public function getAdditionalAttributes() {
192 return 'id="tx-opendocs-menu"';
193 }
194
195 /**
196 * Adds the neccessary javascript to the backend
197 *
198 * @return void
199 */
200 protected function addJavascriptToBackend() {
201 $this->backendReference->addJavascriptFile(
202 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath($this->EXTKEY) . 'Resources/Public/JavaScript/opendocs.js'
203 );
204 }
205
206 /**
207 * Adds the necessary CSS to the backend
208 *
209 * @return void
210 */
211 protected function addCssToBackend() {
212 $this->backendReference->addCssFile(
213 'opendocs',
214 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath($this->EXTKEY) . '/Resources/Public/Css/opendocs.css'
215 );
216 }
217
218 /*******************
219 *** HOOKS ***
220 *******************/
221 /**
222 * Called as a hook in \TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal, calls a JS function to change
223 * the number of opened documents
224 *
225 * @param array $params
226 * @param unknown_type $ref
227 * @return string list item HTML attibutes
228 */
229 public function updateNumberOfOpenDocsHook(&$params, $ref) {
230 $params['JScode'] = '
231 if (top && top.TYPO3BackendOpenDocs) {
232 top.TYPO3BackendOpenDocs.updateNumberOfDocs(' . count($this->openDocs) . ', true);
233 }
234 ';
235 }
236
237 /******************
238 *** AJAX CALLS ***
239 ******************/
240 /**
241 * Closes a document in the session and
242 *
243 * @param array $params Array of parameters from the AJAX interface, currently unused
244 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
245 * @return string List item HTML attibutes
246 */
247 public function closeDocument($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
248 $md5sum = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('md5sum');
249 if ($md5sum && isset($this->openDocs[$md5sum])) {
250 // Add the document to be closed to the recent documents
251 $this->recentDocs = array_merge(array($md5sum => $this->openDocs[$md5sum]), $this->recentDocs);
252 // Allow a maximum of 8 recent documents
253 if (count($this->recentDocs) > 8) {
254 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
255 }
256 // Remove it from the list of the open documents, and store the status
257 unset($this->openDocs[$md5sum]);
258 list(, $docDat) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
259 $GLOBALS['BE_USER']->pushModuleData('alt_doc.php', array($this->openDocs, $docDat));
260 $GLOBALS['BE_USER']->pushModuleData('opendocs::recent', $this->recentDocs);
261 }
262 $this->renderAjax($params, $ajaxObj);
263 }
264
265 /**
266 * Renders the menu so that it can be returned as response to an AJAX call
267 *
268 * @param array $params Array of parameters from the AJAX interface, currently unused
269 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
270 * @return void
271 */
272 public function renderAjax($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
273 $menuContent = $this->renderMenu();
274 $ajaxObj->addContent('opendocsMenu', $menuContent);
275 }
276
277 }