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