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