Added feature #10455: Improve recycling of deleted records
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / mod1 / index.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Julian Kleinhans <typo3@kj187.de>
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once(PATH_t3lib . 'class.t3lib_scbase.php');
26
27 $LANG->includeLLFile('EXT:recycler/mod1/locallang.xml');
28 $BE_USER->modAccess($MCONF, 1); // This checks permissions and exits if the users has no permission for entry.
29
30 /**
31 * Module 'Recycler' for the 'recycler' extension.
32 *
33 * @author Julian Kleinhans <typo3@kj187.de>
34 * @package TYPO3
35 * @subpackage tx_recycler
36 * @version $Id: index.php 17692 2009-03-10 21:55:38Z ohader $
37 */
38 class tx_recycler_module1 extends t3lib_SCbase {
39 /**
40 * @var template
41 */
42 public $doc;
43
44 protected $relativePath;
45 protected $pageRecord = array();
46 protected $isAccessibleForCurrentUser = false;
47
48 protected $allowDelete = false;
49 protected $recordsPageLimit = 50;
50
51 /**
52 * Initializes the Module
53 *
54 * @return void
55 */
56 public function initialize() {
57 parent::init();
58 $this->doc = t3lib_div::makeInstance('template');
59 $this->doc->setModuleTemplate(t3lib_extMgm::extPath('recycler') . 'mod1/mod_template.html');
60 $this->doc->backPath = $GLOBALS['BACK_PATH'];
61
62 $this->relativePath = t3lib_extMgm::extRelPath('recycler');
63 $this->pageRecord = t3lib_BEfunc::readPageAccess($this->id, $this->perms_clause);
64 $this->isAccessibleForCurrentUser = (
65 $this->id && is_array($this->pageRecord) || !$this->id && $this->isCurrentUserAdmin()
66 );
67
68 //don't access in workspace
69 if ($GLOBALS['BE_USER']->workspace !== 0) {
70 $this->isAccessibleForCurrentUser = false;
71 }
72
73 //read configuration
74 $modTS = $GLOBALS['BE_USER']->getTSConfig('mod.recycler');
75 if ($this->isCurrentUserAdmin()) {
76 $this->allowDelete = true;
77 } else {
78 $this->allowDelete = ($modTS['properties']['allowDelete'] == '1');
79 }
80 if (isset($modTS['properties']['recordsPageLimit']) && intval($modTS['properties']['recordsPageLimit']) > 0) {
81 $this->recordsPageLimit = intval($modTS['properties']['recordsPageLimit']);
82 }
83 }
84
85 /**
86 * Renders the contente of the module.
87 *
88 * @return void
89 */
90 public function render() {
91 global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;
92
93 if ($this->isAccessibleForCurrentUser) {
94 $this->loadHeaderData();
95 // div container for renderTo
96 $this->content.= '<div id="recyclerContent"></div>';
97 } else {
98 // If no access or if ID == zero
99 $this->content.= $this->doc->spacer(10);
100 }
101 }
102
103 /**
104 * Flushes the rendered content to browser.
105 *
106 * @return void
107 */
108 public function flush() {
109 $content = $this->doc->startPage($GLOBALS['LANG']->getLL('title'));
110 $content.= $this->doc->moduleBody(
111 $this->pageRecord,
112 $this->getDocHeaderButtons(),
113 $this->getTemplateMarkers()
114 );
115 $content.= $this->doc->endPage();
116 $content.= $this->doc->insertStylesAndJS($this->content);
117
118 $this->content = null;
119 $this->doc = null;
120
121 echo $content;
122 }
123
124 /**
125 * Determines whether the current user is admin.
126 *
127 * @return boolean Whether the current user is admin
128 */
129 protected function isCurrentUserAdmin() {
130 return (bool)$GLOBALS['BE_USER']->user['admin'];
131 }
132
133 /**
134 * Loads data in the HTML head section (e.g. JavaScript or stylesheet information).
135 *
136 * @return void
137 */
138 protected function loadHeaderData() {
139 // Load CSS Stylesheets:
140 $this->loadStylesheet('contrib/extjs/resources/css/ext-all.css');
141 $this->loadStylesheet('contrib/extjs/resources/css/xtheme-gray.css');
142 $this->loadStylesheet($this->relativePath . 'res/css/customExtJs.css');
143 // Load Ext JS:
144 $this->doc->loadJavascriptLib('contrib/extjs/adapter/ext/ext-base.js');
145 $this->doc->loadJavascriptLib('contrib/extjs/ext-all.js');
146 // Integrate dynamic JavaScript such as configuration or lables:
147 $this->doc->JScode.= t3lib_div::wrapJS('
148 Ext.namespace("Recycler");
149 Recycler.statics = ' . json_encode($this->getJavaScriptConfiguration()) . ';
150 Recycler.lang = ' . json_encode($this->getJavaScriptLabels()) . ';'
151 );
152 // Load Recycler JavaScript:
153 $this->loadJavaScript($this->relativePath . 'res/js/ext_expander.js');
154 $this->loadJavaScript($this->relativePath . 'res/js/search_field.js');
155 $this->loadJavaScript($this->relativePath . 'res/js/t3_recycler.js');
156 }
157
158 /**
159 * Loads a stylesheet by adding it to the HTML head section.
160 *
161 * @param string $fileName: Name of the file to be loaded
162 * @return void
163 */
164 protected function loadStylesheet($fileName) {
165 $fileName = t3lib_div::resolveBackPath($this->doc->backPath . $fileName);
166 $this->doc->JScode.= "\t" . '<link rel="stylesheet" type="text/css" href="' . $fileName . '" />' . "\n";
167 }
168
169 /**
170 * Loads a JavaScript file.
171 *
172 * @param string $fileName: Name of the file to be loaded
173 * @return void
174 */
175 protected function loadJavaScript($fileName) {
176 $fileName = t3lib_div::resolveBackPath($this->doc->backPath . $fileName);
177 $this->doc->JScode.= "\t" . '<script language="javascript" type="text/javascript" src="' . $fileName . '"></script>' . "\n";
178 }
179
180 /**
181 * Gets the JavaScript configuration for the Ext JS interface.
182 *
183 * @return array The JavaScript configuration
184 */
185 protected function getJavaScriptConfiguration() {
186 $configuration = array(
187 'pagingSize' => $this->recordsPageLimit,
188 'showDepthMenu' => 1,
189 'startUid' => $this->id,
190 'tableDefault' => 'pages',
191 'renderTo' => 'recyclerContent',
192 'isSSL' => t3lib_div::getIndpEnv('TYPO3_SSL'),
193 'ajaxController' => $this->doc->backPath . 'ajax.php?ajaxID=tx_recycler::controller',
194 'deleteDisable' => $this->allowDelete ? 0 : 1,
195 'depthSelection' => $this->getDataFromSession('depthSelection', 0),
196 'tableSelection' => $this->getDataFromSession('tableSelection', 'pages'),
197 );
198 return $configuration;
199 }
200
201 /**
202 * Gets the labels to be used in JavaScript in the Ext JS interface.
203 *
204 * @return array The labels to be used in JavaScript
205 */
206 protected function getJavaScriptLabels() {
207 $coreLabels = array(
208 'title' => $GLOBALS['LANG']->getLL('title'),
209 'path' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.path'),
210 'table' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.table'),
211 'depth' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_web_perm.xml:Depth'),
212 'depth_0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_0'),
213 'depth_1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_1'),
214 'depth_2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_2'),
215 'depth_3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_3'),
216 'depth_4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_4'),
217 'depth_infi' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_infi'),
218 );
219 $extensionLabels = $this->getJavaScriptLabelsFromLocallang('js.', 'label_');
220 return array_merge($coreLabels, $extensionLabels);
221 }
222
223 /**
224 * Gets labels to be used in JavaScript fetched from the current locallang file.
225 *
226 * @param string $selectionPrefix: Prefix to select the correct labels (default: 'js.')
227 * @param string $stripFromSelectionName: Sub-prefix to be removed from label names in the result (default: '')
228 * @return array Lables to be used in JavaScript of the current locallang file
229 * @todo Check, whether this method can be moved in a generic way to $GLOBALS['LANG']
230 */
231 protected function getJavaScriptLabelsFromLocallang($selectionPrefix = 'js.', $stripFromSelectionName = '') {
232 $extraction = array();
233 $labels = array_merge(
234 (array)$GLOBALS['LOCAL_LANG']['default'],
235 (array)$GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]
236 );
237 // Regular expression to strip the selection prefix and possibly something from the label name:
238 $labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
239 // Iterate throuh all locallang lables:
240 foreach ($labels as $label => $value) {
241 if (strpos($label, $selectionPrefix) === 0) {
242 $key = preg_replace($labelPattern, '', $label);
243 $extraction[$key] = $value;
244 }
245 }
246 return $extraction;
247 }
248
249 /**
250 * Gets the buttons that shall be rendered in the docHeader.
251 *
252 * @return array Available buttons for the docHeader
253 */
254 protected function getDocHeaderButtons() {
255 $buttons = array(
256 'csh' => t3lib_BEfunc::cshItem('_MOD_web_func', '', $GLOBALS['BACK_PATH']),
257 'shortcut' => $this->getShortcutButton(),
258 'save' => ''
259 );
260
261 // SAVE button
262 $buttons['save'] = ''; //<input type="image" class="c-inputButton" name="submit" value="Update"' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/savedok.gif', '') . ' title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:rm.saveDoc', 1) . '" />';
263
264 return $buttons;
265 }
266
267 /**
268 * Gets the button to set a new shortcut in the backend (if current user is allowed to).
269 *
270 * @return string HTML representiation of the shortcut button
271 */
272 protected function getShortcutButton() {
273 $result = '';
274 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
275 $result = $this->doc->makeShortcutIcon('', 'function', $this->MCONF['name']);
276 }
277 return $result;
278 }
279
280 /**
281 * Gets the filled markers that are used in the HTML template.
282 *
283 * @return array The filled marker array
284 */
285 protected function getTemplateMarkers() {
286 $markers = array(
287 'FUNC_MENU' => $this->getFunctionMenu(),
288 'CONTENT' => $this->content,
289 'TITLE' => $GLOBALS['LANG']->getLL('title'),
290 );
291 return $markers;
292 }
293
294 /**
295 * Gets the function menu selector for this backend module.
296 *
297 * @return string The HTML representation of the function menu selector
298 */
299 protected function getFunctionMenu() {
300 return t3lib_BEfunc::getFuncMenu(
301 0,
302 'SET[function]',
303 $this->MOD_SETTINGS['function'],
304 $this->MOD_MENU['function']
305 );
306 }
307
308 /**
309 * Gets data from the session of the current backend user.
310 *
311 * @param string $identifier: The identifier to be used to get the data
312 * @param string $default: The default date to be used if nothing was found in the session
313 * @return string The accordant data in the session of the current backend user
314 */
315 protected function getDataFromSession($identifier, $default = NULL) {
316 $sessionData =& $GLOBALS['BE_USER']->uc['tx_recycler'];
317 if (isset($sessionData[$identifier]) && $sessionData[$identifier]) {
318 $data = $sessionData[$identifier];
319 } else {
320 $data = $default;
321 }
322 return $data;
323 }
324 }
325
326
327
328 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/recycler/mod1/index.php']) {
329 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/recycler/mod1/index.php']);
330 }
331
332
333
334 // Make instance:
335 $SOBE = t3lib_div::makeInstance('tx_recycler_module1');
336 $SOBE->initialize();
337
338 // Include files?
339 foreach($SOBE->include_once as $INC_FILE) {
340 include_once($INC_FILE);
341 }
342
343 $SOBE->render();
344 $SOBE->flush();
345 ?>