Fixed bug #7307: Move 'Open Documents' to Header in the BE
[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 require_once(PATH_typo3.'interfaces/interface.backend_toolbaritem.php');
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 private $backendReference;
50
51 private $openDocs;
52 private $recentDocs;
53 private $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
97 // toolbar item icon
98 $opendocsMenu[] = '<a href="#" class="toolbar-item">';
99 $opendocsMenu[] = '<input type="text" id="tx-opendocs-counter" disabled="disabled" value="'.$numDocs.'" />';
100 $opendocsMenu[] = '<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], t3lib_extMgm::extRelPath($this->EXTKEY).'opendocs.png', 'width="23" height="16"').' alt="Open Documents" title="Open Documents" /></a>';
101
102 // toolbar item menu and initial content
103 $opendocsMenu[] = '<div class="toolbar-item-menu" style="display: none;">';
104 $opendocsMenu[] = $this->renderMenu();
105 $opendocsMenu[] = '</div>';
106
107 return implode("\n", $opendocsMenu);
108 }
109
110 /**
111 * renders the pure contents of the menu
112 *
113 * @return string the menu's content
114 */
115 public function renderMenu() {
116 $openDocuments = $this->openDocs;
117 $recentDocuments = $this->recentDocs;
118 $entries = array();
119 $content = '';
120
121 if(count($openDocuments)) {
122 $entries[] = '<tr><th colspan="3">'.$GLOBALS['LANG']->getLL('open_docs', 1).'</th></tr>';
123
124 $i = 0;
125 foreach ($openDocuments as $md5sum => $openDocument) {
126 $i++;
127 $entries[] = $this->renderMenuEntry($openDocument, $md5sum, false, ($i == 1));
128 }
129 }
130
131 // if there are "recent documents" in the list, add them
132 if(count($recentDocuments)) {
133 $entries[] = '<tr><th colspan="3">'.$GLOBALS['LANG']->getLL('recent_docs', 1).'</th></tr>';
134
135 $i = 0;
136 foreach ($recentDocuments as $md5sum => $recentDocument) {
137 $i++;
138 $entries[] = $this->renderMenuEntry($recentDocument, $md5sum, true, ($i == 1));
139 }
140 }
141
142 if(count($entries)) {
143 $content = '<table class="list" cellspacing="0" cellpadding="0" border="0">'.implode('', $entries).'</table>';
144 } else {
145 $content = '<div class="no-docs">'.$GLOBALS['LANG']->getLL('no_docs', 1).'</div>';
146 }
147
148 return $content;
149 }
150
151 /**
152 * returns the recent documents list as an array
153 *
154 * @return array all recent documents as list-items
155 */
156 public function renderMenuEntry($document, $md5sum, $isRecentDoc = false, $isFirstDoc = false) {
157 $table = $document[3]['table'];
158 $uid = $document[3]['uid'];
159 $record = t3lib_BEfunc::getRecordWSOL($table, $uid);
160 $label = htmlspecialchars(strip_tags(t3lib_div::htmlspecialchars_decode($document[0])));
161 $icon = t3lib_iconWorks::getIconImage($table, $record, $GLOBALS['BACK_PATH']);
162 $link = $GLOBALS['BACK_PATH'].'alt_doc.php?'.$document[2];
163
164 $firstRow = '';
165 if($isFirstDoc) {
166 $firstRow = ' first-row';
167 }
168
169 if (!$isRecentDoc) {
170 // open document
171 $closeIcon = '<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/closedok.gif', 'width="16" height="16"').' title="'.$GLOBALS['LANG']->getLL('rm.closeDoc', 1).'" alt="" />';
172
173 $entry = '
174 <tr id="opendocs-'.$table.'-'.$uid.'" class="opendoc'.$firstRow.'">
175 <td class="icon">'.$icon.'</td>
176 <td class="label"><a href="'.$link.'" target="content" onclick="TYPO3BackendOpenDocs.toggleMenu();">'.$label.'</a></td>
177 <td class="close" onclick="return TYPO3BackendOpenDocs.closeDocument(\''.$md5sum.'\');">'.$closeIcon.'</td>
178 </tr>';
179 } else {
180 // recently used document
181 $entry = '
182 <tr id="opendocs-'.$table.'-'.$uid.'" class="recentdoc'.$firstRow.'">
183 <td class="icon">'.$icon.'</td>
184 <td class="label" colspan="2"><a href="'.$link.'" target="content" onclick="TYPO3BackendOpenDocs.toggleMenu();">'.$label.'</a></td>
185 </tr>';
186 }
187
188 return $entry;
189 }
190
191 /**
192 * returns additional attributes for the list item in the toolbar
193 *
194 * @return string list item HTML attibutes
195 */
196 public function getAdditionalAttributes() {
197 return ' id="tx-opendocs-menu"';
198 }
199
200 /**
201 * adds the neccessary javascript to the backend
202 *
203 * @return void
204 */
205 private function addJavascriptToBackend() {
206 $this->backendReference->addJavascriptFile(t3lib_extMgm::extRelPath($this->EXTKEY).'opendocs.js');
207 }
208
209 /**
210 * adds the neccessary CSS to the backend
211 *
212 * @return void
213 */
214 private function addCssToBackend() {
215 $this->backendReference->addCssFile('opendocs', t3lib_extMgm::extRelPath($this->EXTKEY).'opendocs.css');
216 }
217
218
219 /*******************
220 *** HOOKS ***
221 *******************/
222
223 /**
224 * called as a hook in t3lib_BEfunc::setUpdateSignal, calls a JS function to change
225 * the number of opened documents
226 *
227 * @param array $params
228 * @param unknown_type $ref
229 * @return string list item HTML attibutes
230 */
231 public function updateNumberOfOpenDocsHook(&$params, &$ref) {
232 $params['JScode'] = '
233 if (top && top.TYPO3BackendOpenDocs) {
234 top.TYPO3BackendOpenDocs.updateNumberOfDocs('.count($this->openDocs).', true);
235 }
236 ';
237 }
238
239
240 /******************
241 *** AJAX CALLS ***
242 ******************/
243
244 /**
245 * closes a document in the session and
246 *
247 * @param array array of parameters from the AJAX interface, currently unused
248 * @param TYPO3AJAX object of type TYPO3AJAX
249 * @return string list item HTML attibutes
250 */
251 public function closeDocument($params = array(), TYPO3AJAX &$ajaxObj = null) {
252 $md5sum = t3lib_div::_GP('md5sum');
253
254 if ($md5sum && isset($this->openDocs[$md5sum])) {
255
256 // add the document to be closed to the recent documents
257 $this->recentDocs = array_merge(
258 array($md5sum => $this->openDocs[$md5sum]),
259 $this->recentDocs
260 );
261
262 // allow a maximum of 8 recent documents
263 if (count($this->recentDocs) > 8) {
264 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
265 }
266
267 // remove it from the list of the open documents, and store the status
268 unset($this->openDocs[$md5sum]);
269 list(, $docDat) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
270 $GLOBALS['BE_USER']->pushModuleData('alt_doc.php', array($this->openDocs, $docDat));
271 $GLOBALS['BE_USER']->pushModuleData('opendocs::recent', $this->recentDocs);
272 }
273
274 $this->renderAjax($params, $ajaxObj);
275 }
276
277 /**
278 * renders the menu so that it can be returned as response to an AJAX call
279 *
280 * @param array array of parameters from the AJAX interface, currently unused
281 * @param TYPO3AJAX object of type TYPO3AJAX
282 * @return void
283 */
284 public function renderAjax($params = array(), TYPO3AJAX &$ajaxObj = null) {
285 $menuContent = $this->renderMenu();
286
287 $ajaxObj->addContent('opendocsMenu', $menuContent);
288 }
289
290 }
291
292 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['EXT:opendocs/class.tx_opendocs.php']) {
293 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['EXT:opendocs/class.tx_opendocs.php']);
294 }
295
296 ?>