Issue #7307: Bugfixes for "Open Documents" extension - Part 1
[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 <mack@xnos.org>
38 * @package TYPO3
39 * @subpackage opendocs
40 */
41 class tx_opendocs implements backend_toolbarItem {
42
43 /**
44 * reference back to the backend object
45 *
46 * @var TYPO3backend
47 */
48 private $backendReference;
49 private $openDocs;
50 private $recentDocs;
51 private $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 /**
66 * checks whether the user has access to this toolbar item
67 *
68 * @return boolean true if user has access, false if not
69 */
70 public function checkAccess() {
71 $conf = $GLOBALS['BE_USER']->getTSConfig('backendToolbarItem.tx_opendocs.disabled');
72 return ($conf['value'] == 1 ? false : true);
73 }
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 /**
88 * renders the toolbar item and the empty menu
89 *
90 * @return void
91 */
92 public function render() {
93 $this->addJavascriptToBackend();
94 $this->addCssToBackend();
95 $numDocs = count($this->openDocs);
96
97 // return the toolbar item and an empty UL
98 $output = '<a href="#" class="toolbar-item">';
99 $output .= '<span id="tx-opendocs-num">'.($numDocs > 0 ? $numDocs : '').'</span>';
100 $output .= '<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], t3lib_extMgm::extRelPath($this->EXTKEY).'opendocs.png', 'width="23" height="14"').' title="'.$GLOBALS['LANG']->getLL('toolbaritem',1).'" alt="" />';
101 $output .= '</a>';
102 $output .= '<div class="toolbar-item-menu" style="display: none;"></div>';
103
104 return $output;
105 }
106
107
108 /**
109 * returns the recent documents list as an array
110 *
111 * @return array all recent documents as list-items
112 */
113 public function renderMenuEntry($itm, $md5sum, $isRecentDoc = false) {
114 $table = $itm[3]['table'];
115 $uid = $itm[3]['uid'];
116 $rec = t3lib_BEfunc::getRecordWSOL($table, $uid);
117 $label = htmlspecialchars(strip_tags(t3lib_div::htmlspecialchars_decode($itm[0])));
118 $icon = '<img src="'.t3lib_iconWorks::getIcon($table, $rec).'" alt="'.$label.'" />';
119 $link = $GLOBALS['BACK_PATH'].'alt_doc.php?'.$itm[2];
120
121 if ($isRecentDoc) {
122 $entry = '
123 <tr id="opendocs-'.$table.'-'.$uid.'" class="recentdoc">
124 <td class="opendocs-icon">'.$icon.'</td>
125 <td class="opendocs-label" colspan="2" id="opendocs-label-'.$table.'-'.$uid.'"><a href="'.$link.'" target="content" onclick="TYPO3BackendOpenDocs.hideMenu();">'.$label.'</a></td>
126 </tr>';
127 } else {
128 $closeIcon = '<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/closedok.gif', 'width="16" height="16"').' title="Close Document" alt="" />';
129 $entry = '
130 <tr id="opendocs-'.$table.'-'.$uid.'" class="opendoc">
131 <td class="opendocs-icon">'.$icon.'</td>
132 <td class="opendocs-label" id="opendocs-label-'.$table.'-'.$uid.'"><a href="'.$link.'" target="content" onclick="TYPO3BackendOpenDocs.hideMenu();">'.$label.'</a></td>
133 <td class="opendocs-close" onclick="return TYPO3BackendOpenDocs.closeDocument(\''.$md5sum.'\');">'.$closeIcon.'</td>
134 </tr>';
135 }
136 return $entry;
137 }
138
139
140
141 /**
142 * returns additional attributes for the list item in the toolbar
143 *
144 * @return string list item HTML attibutes
145 */
146 public function getAdditionalAttributes() {
147 return ' id="open-documents-menu"';
148 }
149
150
151 /**
152 * adds the neccessary javascript to the backend
153 *
154 * @return void
155 */
156 private function addJavascriptToBackend() {
157 $this->backendReference->addJavascriptFile(t3lib_extMgm::extRelPath($this->EXTKEY).'opendocs.js');
158 }
159
160
161 /**
162 * adds the neccessary CSS to the backend
163 *
164 * @return void
165 */
166 private function addCssToBackend() {
167 $this->backendReference->addCssFile('opendocs', t3lib_extMgm::extRelPath($this->EXTKEY).'opendocs.css');
168 }
169
170
171 /*******************
172 *** HOOKS ***
173 *******************/
174
175 /**
176 * called as a hook in t3lib_BEfunc::setUpdateSignal, calls a JS function to change
177 * the number of opened documents
178 *
179 * @return string list item HTML attibutes
180 */
181 public function updateNumberOfOpenDocsHook(&$params, &$ref) {
182 $params['JScode'] = '
183 if (top && top.TYPO3BackendOpenDocs) {
184 top.TYPO3BackendOpenDocs.updateNumberOfDocs('.count($this->openDocs).', false);
185 }
186 ';
187 }
188
189
190
191 /******************
192 *** AJAX CALLS ***
193 ******************/
194
195 /**
196 * returns the opened documents list for the AJAX call formatted as HTML list
197 *
198 * @param array array full of additional params, not used yet
199 * @param TYPO3AJAX Ajax request object
200 * @return string list item HTML attibutes
201 */
202 public function renderBackendMenuContents($params, &$ajaxObj) {
203 $itms = $this->openDocs;
204 $itmsRecent = $this->recentDocs;
205 $entries = array();
206
207 if (count($itms)) {
208 $entries[] = '<tr class="menu-item-div"><td colspan="3">'.$GLOBALS['LANG']->getLL('open_docs',1).'</td></tr>';
209 foreach ($itms as $md5sum => $itm) {
210 $entries[] = $this->renderMenuEntry($itm, $md5sum);
211 }
212 }
213
214
215
216 // if there are "recent documents" in the list, add them
217 if (count($itmsRecent)) {
218 $entries[] = '<tr class="menu-item-div"><td colspan="3">'.$GLOBALS['LANG']->getLL('recent_docs',1).'</td></tr>';
219 foreach ($itmsRecent as $md5sum => $itm) {
220 $entries[] = $this->renderMenuEntry($itm, $md5sum, true);
221 }
222 }
223
224 if (count($entries)) {
225 $content = '<table class="opendocs-list">'.implode('', $entries).'</table>';
226 $ajaxObj->addContent('opendocs', $content);
227 } else {
228 $ajaxObj->addContent('opendocs', '<div id="opendocs-nodocs">'.$GLOBALS['LANG']->getLL('no_docs',1).'</div>');
229 }
230 }
231
232
233
234 /**
235 * closes a document in the session and
236 * @param array array full of additional params, not used yet
237 * @param TYPO3AJAX Ajax request object
238 * @return string list item HTML attibutes
239 */
240 public function closeDocument($params, &$ajaxObj) {
241 $md5sum = t3lib_div::_GP('md5sum');
242 if ($md5sum && isset($this->openDocs[$md5sum])) {
243
244 // add the closing document to the recent documents
245 $this->recentDocs = array_merge(array($md5sum => $this->openDocs[$md5sum]), $this->recentDocs);
246 if (count($this->recentDocs) > 8) {
247 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
248 }
249
250 // remove it from the list of the open documents
251 unset($this->openDocs[$md5sum]);
252
253 // store it again
254 list(,$docDat) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php','ses');
255 $GLOBALS['BE_USER']->pushModuleData('alt_doc.php', array($this->openDocs, $docDat));
256 $GLOBALS['BE_USER']->pushModuleData('opendocs::recent', $this->recentDocs);
257 }
258 $this->renderBackendMenuContents($params, $ajaxObj);
259 }
260
261 }
262
263 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['EXT:opendocs/class.tx_opendocs.php']) {
264 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['EXT:opendocs/class.tx_opendocs.php']);
265 }
266 ?>