Fixed issue #10764: Use new JS-function for loading prototype/scriptaculous/extJS
[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$
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($this->relativePath . 'res/css/customExtJs.css');
141 // Load Ext JS:
142 $this->doc->loadExtJS();
143 // Integrate dynamic JavaScript such as configuration or lables:
144 $this->doc->JScode.= t3lib_div::wrapJS('
145 Ext.namespace("Recycler");
146 Recycler.statics = ' . json_encode($this->getJavaScriptConfiguration()) . ';
147 Recycler.lang = ' . json_encode($this->getJavaScriptLabels()) . ';'
148 );
149 // Load Recycler JavaScript:
150 $this->loadJavaScript($this->relativePath . 'res/js/ext_expander.js');
151 $this->loadJavaScript($this->relativePath . 'res/js/search_field.js');
152 $this->loadJavaScript($this->relativePath . 'res/js/t3_recycler.js');
153 }
154
155 /**
156 * Loads a stylesheet by adding it to the HTML head section.
157 *
158 * @param string $fileName: Name of the file to be loaded
159 * @return void
160 */
161 protected function loadStylesheet($fileName) {
162 $fileName = t3lib_div::resolveBackPath($this->doc->backPath . $fileName);
163 $this->doc->JScode.= "\t" . '<link rel="stylesheet" type="text/css" href="' . $fileName . '" />' . "\n";
164 }
165
166 /**
167 * Loads a JavaScript file.
168 *
169 * @param string $fileName: Name of the file to be loaded
170 * @return void
171 */
172 protected function loadJavaScript($fileName) {
173 $fileName = t3lib_div::resolveBackPath($this->doc->backPath . $fileName);
174 $this->doc->JScode.= "\t" . '<script language="javascript" type="text/javascript" src="' . $fileName . '"></script>' . "\n";
175 }
176
177 /**
178 * Gets the JavaScript configuration for the Ext JS interface.
179 *
180 * @return array The JavaScript configuration
181 */
182 protected function getJavaScriptConfiguration() {
183 $configuration = array(
184 'pagingSize' => $this->recordsPageLimit,
185 'showDepthMenu' => 1,
186 'startUid' => $this->id,
187 'tableDefault' => 'pages',
188 'renderTo' => 'recyclerContent',
189 'isSSL' => t3lib_div::getIndpEnv('TYPO3_SSL'),
190 'ajaxController' => $this->doc->backPath . 'ajax.php?ajaxID=tx_recycler::controller',
191 'deleteDisable' => $this->allowDelete ? 0 : 1,
192 'depthSelection' => $this->getDataFromSession('depthSelection', 0),
193 'tableSelection' => $this->getDataFromSession('tableSelection', 'pages'),
194 );
195 return $configuration;
196 }
197
198 /**
199 * Gets the labels to be used in JavaScript in the Ext JS interface.
200 *
201 * @return array The labels to be used in JavaScript
202 */
203 protected function getJavaScriptLabels() {
204 $coreLabels = array(
205 'title' => $GLOBALS['LANG']->getLL('title'),
206 'path' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.path'),
207 'table' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.table'),
208 'depth' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_web_perm.xml:Depth'),
209 'depth_0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_0'),
210 'depth_1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_1'),
211 'depth_2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_2'),
212 'depth_3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_3'),
213 'depth_4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_4'),
214 'depth_infi' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.depth_infi'),
215 );
216 $extensionLabels = $this->getJavaScriptLabelsFromLocallang('js.', 'label_');
217 return array_merge($coreLabels, $extensionLabels);
218 }
219
220 /**
221 * Gets labels to be used in JavaScript fetched from the current locallang file.
222 *
223 * @param string $selectionPrefix: Prefix to select the correct labels (default: 'js.')
224 * @param string $stripFromSelectionName: Sub-prefix to be removed from label names in the result (default: '')
225 * @return array Lables to be used in JavaScript of the current locallang file
226 * @todo Check, whether this method can be moved in a generic way to $GLOBALS['LANG']
227 */
228 protected function getJavaScriptLabelsFromLocallang($selectionPrefix = 'js.', $stripFromSelectionName = '') {
229 $extraction = array();
230 $labels = array_merge(
231 (array)$GLOBALS['LOCAL_LANG']['default'],
232 (array)$GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]
233 );
234 // Regular expression to strip the selection prefix and possibly something from the label name:
235 $labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
236 // Iterate throuh all locallang lables:
237 foreach ($labels as $label => $value) {
238 if (strpos($label, $selectionPrefix) === 0) {
239 $key = preg_replace($labelPattern, '', $label);
240 $extraction[$key] = $value;
241 }
242 }
243 return $extraction;
244 }
245
246 /**
247 * Gets the buttons that shall be rendered in the docHeader.
248 *
249 * @return array Available buttons for the docHeader
250 */
251 protected function getDocHeaderButtons() {
252 $buttons = array(
253 'csh' => t3lib_BEfunc::cshItem('_MOD_web_func', '', $GLOBALS['BACK_PATH']),
254 'shortcut' => $this->getShortcutButton(),
255 'save' => ''
256 );
257
258 // SAVE button
259 $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) . '" />';
260
261 return $buttons;
262 }
263
264 /**
265 * Gets the button to set a new shortcut in the backend (if current user is allowed to).
266 *
267 * @return string HTML representiation of the shortcut button
268 */
269 protected function getShortcutButton() {
270 $result = '';
271 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
272 $result = $this->doc->makeShortcutIcon('', 'function', $this->MCONF['name']);
273 }
274 return $result;
275 }
276
277 /**
278 * Gets the filled markers that are used in the HTML template.
279 *
280 * @return array The filled marker array
281 */
282 protected function getTemplateMarkers() {
283 $markers = array(
284 'FUNC_MENU' => $this->getFunctionMenu(),
285 'CONTENT' => $this->content,
286 'TITLE' => $GLOBALS['LANG']->getLL('title'),
287 );
288 return $markers;
289 }
290
291 /**
292 * Gets the function menu selector for this backend module.
293 *
294 * @return string The HTML representation of the function menu selector
295 */
296 protected function getFunctionMenu() {
297 return t3lib_BEfunc::getFuncMenu(
298 0,
299 'SET[function]',
300 $this->MOD_SETTINGS['function'],
301 $this->MOD_MENU['function']
302 );
303 }
304
305 /**
306 * Gets data from the session of the current backend user.
307 *
308 * @param string $identifier: The identifier to be used to get the data
309 * @param string $default: The default date to be used if nothing was found in the session
310 * @return string The accordant data in the session of the current backend user
311 */
312 protected function getDataFromSession($identifier, $default = NULL) {
313 $sessionData =& $GLOBALS['BE_USER']->uc['tx_recycler'];
314 if (isset($sessionData[$identifier]) && $sessionData[$identifier]) {
315 $data = $sessionData[$identifier];
316 } else {
317 $data = $default;
318 }
319 return $data;
320 }
321 }
322
323
324
325 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/recycler/mod1/index.php']) {
326 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/recycler/mod1/index.php']);
327 }
328
329
330
331 // Make instance:
332 $SOBE = t3lib_div::makeInstance('tx_recycler_module1');
333 $SOBE->initialize();
334
335 // Include files?
336 foreach($SOBE->include_once as $INC_FILE) {
337 include_once($INC_FILE);
338 }
339
340 $SOBE->render();
341 $SOBE->flush();
342 ?>