[TASK] Make use of "use"-statement for BackendUtility
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Controller / RecyclerModuleController.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Julian Kleinhans <typo3@kj187.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Backend\Utility\BackendUtility;
28
29 // This checks permissions and exits if the users has no permission for entry.
30
31 /**
32 * Module 'Recycler' for the 'recycler' extension.
33 *
34 * @author Julian Kleinhans <typo3@kj187.de>
35 */
36 class RecyclerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClass {
37
38 /**
39 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
40 */
41 public $doc;
42
43 protected $relativePath;
44
45 protected $pageRecord = array();
46
47 protected $isAccessibleForCurrentUser = FALSE;
48
49 protected $allowDelete = FALSE;
50
51 protected $recordsPageLimit = 50;
52
53 /**
54 * @var \TYPO3\CMS\Core\Page\PageRenderer
55 */
56 protected $pageRenderer;
57
58 /**
59 * Initializes the Module
60 *
61 * @return void
62 */
63 public function initialize() {
64 parent::init();
65 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
66 $this->doc->setModuleTemplate(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('recycler') . 'mod1/mod_template.html');
67 $this->doc->backPath = $GLOBALS['BACK_PATH'];
68 $this->doc->setExtDirectStateProvider();
69 $this->pageRenderer = $this->doc->getPageRenderer();
70 $this->relativePath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('recycler');
71 $this->pageRecord = BackendUtility::readPageAccess($this->id, $this->perms_clause);
72 $this->isAccessibleForCurrentUser = $this->id && is_array($this->pageRecord) || !$this->id && $this->isCurrentUserAdmin();
73 //don't access in workspace
74 if ($GLOBALS['BE_USER']->workspace !== 0) {
75 $this->isAccessibleForCurrentUser = FALSE;
76 }
77 //read configuration
78 $modTS = $GLOBALS['BE_USER']->getTSConfig('mod.recycler');
79 if ($this->isCurrentUserAdmin()) {
80 $this->allowDelete = TRUE;
81 } else {
82 $this->allowDelete = $modTS['properties']['allowDelete'] == '1';
83 }
84 if (isset($modTS['properties']['recordsPageLimit']) && intval($modTS['properties']['recordsPageLimit']) > 0) {
85 $this->recordsPageLimit = intval($modTS['properties']['recordsPageLimit']);
86 }
87 }
88
89 /**
90 * Renders the content of the module.
91 *
92 * @return void
93 */
94 public function render() {
95 $this->content .= $this->doc->header($GLOBALS['LANG']->getLL('title'));
96 $this->content .= '<p class="lead">' . $GLOBALS['LANG']->getLL('description') . '</p>';
97 if ($this->isAccessibleForCurrentUser) {
98 $this->loadHeaderData();
99 // div container for renderTo
100 $this->content .= '<div id="recyclerContent"></div>';
101 } else {
102 // If no access or if ID == zero
103 $this->content .= $this->doc->spacer(10);
104 }
105 }
106
107 /**
108 * Flushes the rendered content to browser.
109 *
110 * @return void
111 */
112 public function flush() {
113 $content = $this->doc->moduleBody($this->pageRecord, $this->getDocHeaderButtons(), $this->getTemplateMarkers());
114 // Renders the module page
115 $content = $this->doc->render($GLOBALS['LANG']->getLL('title'), $content);
116 $this->content = NULL;
117 $this->doc = NULL;
118 echo $content;
119 }
120
121 /**
122 * Determines whether the current user is admin.
123 *
124 * @return boolean Whether the current user is admin
125 */
126 protected function isCurrentUserAdmin() {
127 return (bool) $GLOBALS['BE_USER']->user['admin'];
128 }
129
130 /**
131 * Loads data in the HTML head section (e.g. JavaScript or stylesheet information).
132 *
133 * @return void
134 */
135 protected function loadHeaderData() {
136 // Load CSS Stylesheets:
137 $this->pageRenderer->addCssFile($this->relativePath . 'res/css/customExtJs.css');
138 // Load Ext JS:
139 $this->pageRenderer->loadExtJS();
140 $this->pageRenderer->enableExtJSQuickTips();
141 // Integrate dynamic JavaScript such as configuration or lables:
142 $this->pageRenderer->addInlineSettingArray('Recycler', $this->getJavaScriptConfiguration());
143 $this->pageRenderer->addInlineLanguageLabelArray($this->getJavaScriptLabels());
144 // Load Recycler JavaScript:
145 // Load Plugins
146 $uxPath = $this->doc->backpath . 'js/extjs/ux/';
147 $this->pageRenderer->addJsFile($uxPath . 'Ext.grid.RowExpander.js');
148 $this->pageRenderer->addJsFile($uxPath . 'Ext.app.SearchField.js');
149 $this->pageRenderer->addJsFile($uxPath . 'Ext.ux.FitToParent.js');
150 // Load main script
151 $this->pageRenderer->addJsFile($this->relativePath . 'res/js/t3_recycler.js');
152 }
153
154 /**
155 * Gets the JavaScript configuration for the Ext JS interface.
156 *
157 * @return array The JavaScript configuration
158 */
159 protected function getJavaScriptConfiguration() {
160 $configuration = array(
161 'pagingSize' => $this->recordsPageLimit,
162 'showDepthMenu' => 1,
163 'startUid' => $this->id,
164 'tableDefault' => 'pages',
165 'renderTo' => 'recyclerContent',
166 'isSSL' => \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL'),
167 'ajaxController' => $this->doc->backPath . 'ajax.php?ajaxID=RecyclerAjaxController::init',
168 'deleteDisable' => $this->allowDelete ? 0 : 1,
169 'depthSelection' => $this->getDataFromSession('depthSelection', 0),
170 'tableSelection' => $this->getDataFromSession('tableSelection', 'pages'),
171 'States' => $GLOBALS['BE_USER']->uc['moduleData']['web_recycler']['States']
172 );
173 return $configuration;
174 }
175
176 /**
177 * Gets the labels to be used in JavaScript in the Ext JS interface.
178 *
179 * @return array The labels to be used in JavaScript
180 */
181 protected function getJavaScriptLabels() {
182 $coreLabels = array(
183 'title' => $GLOBALS['LANG']->getLL('title'),
184 'path' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.path'),
185 'table' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.table'),
186 'depth' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_web_perm.xlf:Depth'),
187 'depth_0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_0'),
188 'depth_1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_1'),
189 'depth_2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_2'),
190 'depth_3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_3'),
191 'depth_4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_4'),
192 'depth_infi' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi')
193 );
194 $extensionLabels = $this->getJavaScriptLabelsFromLocallang('js.', 'label_');
195 $javaScriptLabels = array_merge($coreLabels, $extensionLabels);
196 return $javaScriptLabels;
197 }
198
199 /**
200 * Gets labels to be used in JavaScript fetched from the current locallang file.
201 *
202 * @param string $selectionPrefix: Prefix to select the correct labels (default: 'js.')
203 * @param string $stripFromSelectionName: Sub-prefix to be removed from label names in the result (default: '')
204 * @return array Lables to be used in JavaScript of the current locallang file
205 * @todo Check, whether this method can be moved in a generic way to $GLOBALS['LANG']
206 */
207 protected function getJavaScriptLabelsFromLocallang($selectionPrefix = 'js.', $stripFromSelectionName = '') {
208 $extraction = array();
209 $labels = array_merge((array) $GLOBALS['LOCAL_LANG']['default'], (array) $GLOBALS['LOCAL_LANG'][$GLOBALS['LANG']->lang]);
210 // Regular expression to strip the selection prefix and possibly something from the label name:
211 $labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
212 // Iterate through all locallang lables:
213 foreach ($labels as $label => $value) {
214 if (strpos($label, $selectionPrefix) === 0) {
215 $key = preg_replace($labelPattern, '', $label);
216 $extraction[$key] = $value;
217 }
218 }
219 return $extraction;
220 }
221
222 /**
223 * Gets the buttons that shall be rendered in the docHeader.
224 *
225 * @return array Available buttons for the docHeader
226 */
227 protected function getDocHeaderButtons() {
228 $buttons = array(
229 'csh' => BackendUtility::cshItem('_MOD_web_func', '', $GLOBALS['BACK_PATH']),
230 'shortcut' => $this->getShortcutButton(),
231 'save' => ''
232 );
233 // SAVE button
234 $buttons['save'] = '';
235 return $buttons;
236 }
237
238 /**
239 * Gets the button to set a new shortcut in the backend (if current user is allowed to).
240 *
241 * @return string HTML representation of the shortcut button
242 */
243 protected function getShortcutButton() {
244 $result = '';
245 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
246 $result = $this->doc->makeShortcutIcon('', 'function', $this->MCONF['name']);
247 }
248 return $result;
249 }
250
251 /**
252 * Gets the filled markers that are used in the HTML template.
253 *
254 * @return array The filled marker array
255 */
256 protected function getTemplateMarkers() {
257 $markers = array(
258 'FUNC_MENU' => $this->getFunctionMenu(),
259 'CONTENT' => $this->content,
260 'TITLE' => $GLOBALS['LANG']->getLL('title')
261 );
262 return $markers;
263 }
264
265 /**
266 * Gets the function menu selector for this backend module.
267 *
268 * @return string The HTML representation of the function menu selector
269 */
270 protected function getFunctionMenu() {
271 return BackendUtility::getFuncMenu(0, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']);
272 }
273
274 /**
275 * Gets data from the session of the current backend user.
276 *
277 * @param string $identifier: The identifier to be used to get the data
278 * @param string $default: The default date to be used if nothing was found in the session
279 * @return string The accordant data in the session of the current backend user
280 */
281 protected function getDataFromSession($identifier, $default = NULL) {
282 $sessionData = &$GLOBALS['BE_USER']->uc['tx_recycler'];
283 if (isset($sessionData[$identifier]) && $sessionData[$identifier]) {
284 $data = $sessionData[$identifier];
285 } else {
286 $data = $default;
287 }
288 return $data;
289 }
290
291 }
292
293
294 ?>