Fixed bug #9715: Editing records with OpenDocs enabled does not work
[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 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[] = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.png', 'width="23" height="16"') . ' alt="' . $title . '" 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("\n", $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 $label = htmlspecialchars(strip_tags(t3lib_div::htmlspecialchars_decode($document[0])));
162 $icon = t3lib_iconWorks::getIconImage($table, $record, $GLOBALS['BACK_PATH']);
163 $link = $GLOBALS['BACK_PATH'] . 'alt_doc.php?' . $document[2];
164
165 $firstRow = '';
166 if ($isFirstDoc) {
167 $firstRow = ' first-row';
168 }
169
170 if (!$isRecentDoc) {
171 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:rm.closeDoc', true);
172
173 // open document
174 $closeIcon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/closedok.gif', 'width="16" height="16"') . ' title="' . $title . '" alt="' . $title . '" />';
175
176 $entry = '
177 <tr id="opendocs-' . $table . '-' . $uid . '" class="opendoc' . $firstRow . '">
178 <td class="icon">' . $icon . '</td>
179 <td class="label"><a href="' . $link . '" target="content" onclick="TYPO3BackendOpenDocs.toggleMenu();">' . $label . '</a></td>
180 <td class="close" onclick="return TYPO3BackendOpenDocs.closeDocument(\'' . $md5sum . '\');">' . $closeIcon . '</td>
181 </tr>';
182 } else {
183 // recently used document
184 $entry = '
185 <tr id="opendocs-' . $table . '-' . $uid . '" class="recentdoc' . $firstRow . '">
186 <td class="icon">' . $icon . '</td>
187 <td class="label" colspan="2"><a href="' . $link . '" target="content" onclick="TYPO3BackendOpenDocs.toggleMenu();">' . $label . '</a></td>
188 </tr>';
189 }
190
191 return $entry;
192 }
193
194 /**
195 * returns additional attributes for the list item in the toolbar
196 *
197 * @return string list item HTML attibutes
198 */
199 public function getAdditionalAttributes() {
200 return ' id="tx-opendocs-menu"';
201 }
202
203 /**
204 * adds the neccessary javascript to the backend
205 *
206 * @return void
207 */
208 protected function addJavascriptToBackend() {
209 $this->backendReference->addJavascriptFile(t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.js');
210 }
211
212 /**
213 * adds the neccessary CSS to the backend
214 *
215 * @return void
216 */
217 protected function addCssToBackend() {
218 $this->backendReference->addCssFile('opendocs', t3lib_extMgm::extRelPath($this->EXTKEY) . 'opendocs.css');
219 }
220
221
222 /*******************
223 *** HOOKS ***
224 *******************/
225
226 /**
227 * called as a hook in t3lib_BEfunc::setUpdateSignal, calls a JS function to change
228 * the number of opened documents
229 *
230 * @param array $params
231 * @param unknown_type $ref
232 * @return string list item HTML attibutes
233 */
234 public function updateNumberOfOpenDocsHook(&$params, &$ref) {
235 $params['JScode'] = '
236 if (top && top.TYPO3BackendOpenDocs) {
237 top.TYPO3BackendOpenDocs.updateNumberOfDocs(' . count($this->openDocs) . ', true);
238 }
239 ';
240 }
241
242
243 /******************
244 *** AJAX CALLS ***
245 ******************/
246
247 /**
248 * closes a document in the session and
249 *
250 * @param array array of parameters from the AJAX interface, currently unused
251 * @param TYPO3AJAX object of type TYPO3AJAX
252 * @return string list item HTML attibutes
253 */
254 public function closeDocument($params = array(), TYPO3AJAX &$ajaxObj = null) {
255 $md5sum = t3lib_div::_GP('md5sum');
256
257 if ($md5sum && isset($this->openDocs[$md5sum])) {
258
259 // add the document to be closed to the recent documents
260 $this->recentDocs = array_merge(
261 array($md5sum => $this->openDocs[$md5sum]),
262 $this->recentDocs
263 );
264
265 // allow a maximum of 8 recent documents
266 if (count($this->recentDocs) > 8) {
267 $this->recentDocs = array_slice($this->recentDocs, 0, 8);
268 }
269
270 // remove it from the list of the open documents, and store the status
271 unset($this->openDocs[$md5sum]);
272 list(, $docDat) = $GLOBALS['BE_USER']->getModuleData('alt_doc.php', 'ses');
273 $GLOBALS['BE_USER']->pushModuleData('alt_doc.php', array($this->openDocs, $docDat));
274 $GLOBALS['BE_USER']->pushModuleData('opendocs::recent', $this->recentDocs);
275 }
276
277 $this->renderAjax($params, $ajaxObj);
278 }
279
280 /**
281 * renders the menu so that it can be returned as response to an AJAX call
282 *
283 * @param array array of parameters from the AJAX interface, currently unused
284 * @param TYPO3AJAX object of type TYPO3AJAX
285 * @return void
286 */
287 public function renderAjax($params = array(), TYPO3AJAX &$ajaxObj = null) {
288 $menuContent = $this->renderMenu();
289
290 $ajaxObj->addContent('opendocsMenu', $menuContent);
291 }
292
293 }
294
295
296 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/opendocs/class.tx_opendocs.php']) {
297 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/opendocs/class.tx_opendocs.php']);
298 }
299 ?>