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