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