c893f0c3b88a5109670e6bed739a7552c4ec88e9
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / class.tx_opendocs.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 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 // load the language file
29 $GLOBALS['LANG']->includeLLFile('EXT:opendocs/locallang_opendocs.xml');
30
31
32 /**
33 * Adding a list of all open documents of a user to the backend.php
34 *
35 * @author Benjamin Mack <benni@typo3.org>
36 * @author Ingo Renner <ingo@typo3.org>
37 * @package TYPO3
38 * @subpackage opendocs
39 */
40 class tx_opendocs implements backend_toolbarItem {
41
42 /**
43 * reference back to the backend object
44 *
45 * @var TYPO3backend
46 */
47 protected $backendReference;
48
49 protected $openDocs;
50 protected $recentDocs;
51 protected $EXTKEY = 'opendocs';
52
53
54 /**
55 * constructor, loads the documents from the user control
56 *
57 * @param TYPO3backend TYPO3 backend object reference
58 */
59 public function __construct(TYPO3backend &$backendReference = null) {
60 $this->backendReference = $backendReference;
61 $this->loadDocsFromUserSession();
62 }
63
64 /**
65 * checks whether the user has access to this toolbar item
66 *
67 * @return boolean true if user has access, false if not
68 */
69 public function checkAccess() {
70 $conf = $GLOBALS['BE_USER']->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
71 return ($conf['value'] == 1 ? false : true);
72 }
73
74 /**
75 * loads the opened and recently opened documents from the user
76 *
77 * @return void
78 */
79 public function loadDocsFromUserSession() {
80 list($this->openDocs, ) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
81 $this->recentDocs = $GLOBALS['BE_USER']->getModuleData('opendocs::recent');
82 }
83
84 /**
85 * renders the toolbar item and the initial menu
86 *
87 * @return string the toolbar item including the initial menu content as HTML
88 */
89 public function render() {
90 $this->addJavascriptToBackend();
91 $this->addCssToBackend();
92 $numDocs = count($this->openDocs);
93 $opendocsMenu = array();
94 $title = $GLOBALS['LANG']->getLL('toolbaritem', true);
95
96 // toolbar item icon
97 $opendocsMenu[] = '<a href="#" class="toolbar-item">';
98 $opendocsMenu[] = '<input type="text" id="tx-opendocs-counter" disabled="disabled" value="' . $numDocs . '" />';
99 $opendocsMenu[] = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.png', 'width="23" height="16"') . ' alt="' . $title . '" title="' . $title . '" /></a>';
100
101 // toolbar item menu and initial content
102 $opendocsMenu[] = '<div class="toolbar-item-menu" style="display: none;">';
103 $opendocsMenu[] = $this->renderMenu();
104 $opendocsMenu[] = '</div>';
105
106 return implode("\n", $opendocsMenu);
107 }
108
109 /**
110 * renders the pure contents of the menu
111 *
112 * @return string the menu's content
113 */
114 public function renderMenu() {
115 $openDocuments = $this->openDocs;
116 $recentDocuments = $this->recentDocs;
117 $entries = array();
118 $content = '';
119
120 if (count($openDocuments)) {
121 $entries[] = '<tr><th colspan="3">' . $GLOBALS['LANG']->getLL('open_docs', true) . '</th></tr>';
122
123 $i = 0;
124 foreach ($openDocuments as $md5sum => $openDocument) {
125 $i++;
126 $entries[] = $this->renderMenuEntry($openDocument, $md5sum, false, ($i == 1));
127 }
128 }
129
130 // if there are "recent documents" in the list, add them
131 if (count($recentDocuments)) {
132 $entries[] = '<tr><th colspan="3">' . $GLOBALS['LANG']->getLL('recent_docs', true) . '</th></tr>';
133
134 $i = 0;
135 foreach ($recentDocuments as $md5sum => $recentDocument) {
136 $i++;
137 $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, true, ($i == 1));
138 }
139 }
140
141 if (count($entries)) {
142 $content = '<table class="list" cellspacing="0" cellpadding="0" border="0">' . implode('', $entries) . '</table>';
143 } else {
144 $content = '<div class="no-docs">' . $GLOBALS['LANG']->getLL('no_docs', true) . '</div>';
145 }
146
147 return $content;
148 }
149
150 /**
151 * returns the recent documents list as an array
152 *
153 * @return array all recent documents as list-items
154 */
155 public function renderMenuEntry($document, $md5sum, $isRecentDoc = false, $isFirstDoc = false) {
156 $table = $document[3]['table'];
157 $uid = $document[3]['uid'];
158 $record = t3lib_BEfunc::getRecordWSOL($table, $uid);
159 $label = htmlspecialchars(strip_tags(t3lib_div::htmlspecialchars_decode($document[0])));
160 $icon = t3lib_iconWorks::getIconImage($table, $record, $GLOBALS['BACK_PATH']);
161 $link = $GLOBALS['BACK_PATH'] . 'alt_doc.php?' . $document[2];
162
163 $firstRow = '';
164 if ($isFirstDoc) {
165 $firstRow = ' first-row';
166 }
167
168 if (!$isRecentDoc) {
169 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:rm.closeDoc', true);
170
171 // open document
172 $closeIcon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/closedok.gif', 'width="16" height="16"') . ' title="' . $title . '" alt="' . $title . '" />';
173
174 $entry = '
175 <tr id="opendocs-' . $table . '-' . $uid . '" class="opendoc' . $firstRow . '">
176 <td class="icon">' . $icon . '</td>
177 <td class="label"><a href="' . $link . '" target="content" onclick="TYPO3BackendOpenDocs.toggleMenu();">' . $label . '</a></td>
178 <td class="close" onclick="return TYPO3BackendOpenDocs.closeDocument(\'' . $md5sum . '\');">' . $closeIcon . '</td>
179 </tr>';
180 } else {
181 // recently used document
182 $entry = '
183 <tr id="opendocs-' . $table . '-' . $uid . '" class="recentdoc' . $firstRow . '">
184 <td class="icon">' . $icon . '</td>
185 <td class="label" colspan="2"><a href="' . $link . '" target="content" onclick="TYPO3BackendOpenDocs.toggleMenu();">' . $label . '</a></td>
186 </tr>';
187 }
188
189 return $entry;
190 }
191
192 /**
193 * returns additional attributes for the list item in the toolbar
194 *
195 * @return string list item HTML attibutes
196 */
197 public function getAdditionalAttributes() {
198 return ' id="tx-opendocs-menu"';
199 }
200
201 /**
202 * adds the neccessary javascript to the backend
203 *
204 * @return void
205 */
206 protected function addJavascriptToBackend() {
207 $this->backendReference->addJavascriptFile(t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.js');
208 }
209
210 /**
211 * adds the neccessary CSS to the backend
212 *
213 * @return void
214 */
215 protected function addCssToBackend() {
216 $this->backendReference->addCssFile('opendocs', t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.css');
217 }
218
219
220 /*******************
221 *** HOOKS ***
222 *******************/
223
224 /**
225 * called as a hook in t3lib_BEfunc::setUpdateSignal, calls a JS function to change
226 * the number of opened documents
227 *
228 * @param array $params
229 * @param unknown_type $ref
230 * @return string list item HTML attibutes
231 */
232 public function updateNumberOfOpenDocsHook(&$params, &$ref) {
233 $params['JScode'] = '
234 if (top && top.TYPO3BackendOpenDocs) {
235 top.TYPO3BackendOpenDocs.updateNumberOfDocs(' . count($this->openDocs) . ', true);
236 }
237 ';
238 }
239
240
241 /******************
242 *** AJAX CALLS ***
243 ******************/
244
245 /**
246 * closes a document in the session and
247 *
248 * @param array array of parameters from the AJAX interface, currently unused
249 * @param TYPO3AJAX object of type TYPO3AJAX
250 * @return string list item HTML attibutes
251 */
252 public function closeDocument($params = array(), TYPO3AJAX &$ajaxObj = null) {
253 $md5sum = t3lib_div::_GP('md5sum');
254
255 if ($md5sum && isset($this->openDocs[$md5sum])) {
256
257 // add the document to be closed to the recent documents
258 $this->recentDocs = array_merge(
259 array($md5sum => $this->openDocs[$md5sum]),
260 $this->recentDocs
261 );
262
263 // allow a maximum of 8 recent documents
264 if (count($this->recentDocs) > 8) {
265 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
266 }
267
268 // remove it from the list of the open documents, and store the status
269 unset($this->openDocs[$md5sum]);
270 list(, $docDat) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
271 $GLOBALS['BE_USER']->pushModuleData('alt_doc.php', array($this->openDocs, $docDat));
272 $GLOBALS['BE_USER']->pushModuleData('opendocs::recent', $this->recentDocs);
273 }
274
275 $this->renderAjax($params, $ajaxObj);
276 }
277
278 /**
279 * renders the menu so that it can be returned as response to an AJAX call
280 *
281 * @param array array of parameters from the AJAX interface, currently unused
282 * @param TYPO3AJAX object of type TYPO3AJAX
283 * @return void
284 */
285 public function renderAjax($params = array(), TYPO3AJAX &$ajaxObj = null) {
286 $menuContent = $this->renderMenu();
287
288 $ajaxObj->addContent('opendocsMenu', $menuContent);
289 }
290
291 }
292
293
294 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/opendocs/class.tx_opendocs.php']) {
295 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/opendocs/class.tx_opendocs.php']);
296 }
297 ?>