[TASK] Remove SVN auto properties $Id$
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_scbase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
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 * Contains the parent class for 'ScriptClasses' in backend modules.
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 133: class t3lib_SCbase
40 * 249: function init()
41 * 269: function menuConfig()
42 * 292: function mergeExternalItems($modName,$menuKey,$menuArr)
43 * 317: function handleExternalFunctionValue($MM_key='function', $MS_value=NULL)
44 * 335: function getExternalItemConfig($modName,$menuKey,$value='')
45 * 349: function checkExtObj()
46 * 363: function checkSubExtObj()
47 * 375: function extObjHeader()
48 * 384: function extObjContent()
49 *
50 * TOTAL FUNCTIONS: 9
51 * (This index is automatically created/updated by the extension "extdeveval")
52 *
53 */
54
55
56 /**
57 * EXAMPLE PROTOTYPE
58 *
59 * As for examples there are lots of them if you search for classes which extends 't3lib_SCbase'.
60 * However you can see a prototype example of how a module might use this class in an index.php file typically hosting a backend module.
61 * NOTICE: This example only outlines the basic structure of how this class is used. You should consult the documentation and other real-world examples for some actual things to do when building modules.
62 *
63 * // TYPICAL 'HEADER' OF A BACKEND MODULE:
64 * unset($MCONF);
65 * require ('conf.php');
66 * require ($BACK_PATH.'init.php');
67 * require ($BACK_PATH.'template.php');
68 * $LANG->includeLLFile('EXT:prototype/locallang.php');
69 * $BE_USER->modAccess($MCONF,1);
70 *
71 * // SC_mod_prototype EXTENDS THE CLASS t3lib_SCbase with a main() and printContent() function:
72 * class SC_mod_prototype extends t3lib_SCbase {
73 * // MAIN FUNCTION - HERE YOU CREATE THE MODULE CONTENT IN $this->content
74 * function main() {
75 * // TYPICALLY THE INTERNAL VAR, $this->doc is instantiated like this:
76 * $this->doc = t3lib_div::makeInstance('mediumDoc');
77 * // TYPICALLY THE INTERNAL VAR, $this->backPath is set like this:
78 * $this->backPath = $this->doc->backPath = $GLOBALS['BACK_PATH'];
79 * // ... AND OF COURSE A LOT OF OTHER THINGS GOES ON - LIKE PUTTING CONTENT INTO $this->content
80 * $this->content='';
81 * }
82 * // PRINT CONTENT - DONE AS THE LAST THING
83 * function printContent() {
84 * echo $this->content;
85 * }
86 * }
87 *
88 * // CHECKING IF THERE ARE AN EXTENSION CLASS CONFIGURED FOR THIS CLASS:
89 * if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/prototype/index.php'])) {
90 * include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/prototype/index.php']);
91 * }
92 *
93 * // MAKE INSTANCE OF THE SCRIPT CLASS AND CALL init()
94 * $SOBE = t3lib_div::makeInstance('SC_mod_prototype');
95 * $SOBE->init();
96 *
97 * // AFTER INIT THE INTERNAL ARRAY ->include_once MAY HOLD FILENAMES TO INCLUDE
98 * foreach($SOBE->include_once as $INC_FILE) include_once($INC_FILE);
99 *
100 * // THEN WE WILL CHECK IF THERE IS A 'SUBMODULE' REGISTERED TO BE INITIALIZED AS WELL:
101 * $SOBE->checkExtObj();
102 *
103 * // THEN WE CALL THE main() METHOD AND THIS SHOULD SPARK THE CREATION OF THE MODULE OUTPUT.
104 * $SOBE->main();
105 * // FINALLY THE printContent() FUNCTION WILL OUTPUT THE ACCUMULATED CONTENT
106 * $SOBE->printContent();
107 */
108
109 /**
110 * Parent class for 'ScriptClasses' in backend modules.
111 * See example comment above.
112 *
113 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
114 * @package TYPO3
115 * @subpackage t3lib
116 * @see t3lib_extobjbase
117 */
118 class t3lib_SCbase {
119
120 /**
121 * Loaded with the global array $MCONF which holds some module configuration from the conf.php file of backend modules.
122 * @see init()
123 */
124 var $MCONF = array();
125
126 /**
127 * The integer value of the GET/POST var, 'id'. Used for submodules to the 'Web' module (page id)
128 * @see init()
129 */
130 var $id;
131
132 /**
133 * The value of GET/POST var, 'CMD'
134 * @see init()
135 */
136 var $CMD;
137
138 /**
139 * A WHERE clause for selection records from the pages table based on read-permissions of the current backend user.
140 * @see init()
141 */
142 var $perms_clause;
143
144
145 /**
146 * The module menu items array. Each key represents a key for which values can range between the items in the array of that key.
147 * @see init()
148 */
149 var $MOD_MENU = array(
150 'function' => array()
151 );
152
153 /**
154 * Current settings for the keys of the MOD_MENU array
155 * @see $MOD_MENU
156 */
157 var $MOD_SETTINGS = array();
158
159 /**
160 * Module TSconfig based on PAGE TSconfig / USER TSconfig
161 * @see menuConfig()
162 */
163 var $modTSconfig;
164
165 /**
166 * If type is 'ses' then the data is stored as session-lasting data. This means that it'll be wiped out the next time the user logs in.
167 * Can be set from extension classes of this class before the init() function is called.
168 *
169 * @see menuConfig(), t3lib_BEfunc::getModuleData()
170 */
171 var $modMenu_type = '';
172
173 /**
174 * dontValidateList can be used to list variables that should not be checked if their value is found in the MOD_MENU array. Used for dynamically generated menus.
175 * Can be set from extension classes of this class before the init() function is called.
176 *
177 * @see menuConfig(), t3lib_BEfunc::getModuleData()
178 */
179 var $modMenu_dontValidateList = '';
180
181 /**
182 * List of default values from $MOD_MENU to set in the output array (only if the values from MOD_MENU are not arrays)
183 * Can be set from extension classes of this class before the init() function is called.
184 *
185 * @see menuConfig(), t3lib_BEfunc::getModuleData()
186 */
187 var $modMenu_setDefaultList = '';
188
189 /**
190 * Contains module configuration parts from TBE_MODULES_EXT if found
191 *
192 * @see handleExternalFunctionValue()
193 */
194 var $extClassConf;
195
196 /**
197 * Contains absolute paths to class files to include from the global scope. This is done in the module index.php files after calling the init() function
198 *
199 * @see handleExternalFunctionValue()
200 */
201 var $include_once = array();
202
203 /**
204 * Generally used for accumulating the output content of backend modules
205 */
206 var $content = '';
207
208 /**
209 * Generally used to hold an instance of the 'template' class from typo3/template.php
210 *
211 * @var template
212 */
213 var $doc;
214
215 /**
216 * May contain an instance of a 'Function menu module' which connects to this backend module.
217 *
218 * @see checkExtObj()
219 */
220 var $extObj;
221
222
223 /**
224 * Initializes the backend module by setting internal variables, initializing the menu.
225 *
226 * @return void
227 * @see menuConfig()
228 */
229 function init() {
230 // name might be set from outside
231 if (!$this->MCONF['name']) {
232 $this->MCONF = $GLOBALS['MCONF'];
233 }
234 $this->id = intval(t3lib_div::_GP('id'));
235 $this->CMD = t3lib_div::_GP('CMD');
236 $this->perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
237 $this->menuConfig();
238 $this->handleExternalFunctionValue();
239 }
240
241 /**
242 * Initializes the internal MOD_MENU array setting and unsetting items based on various conditions. It also merges in external menu items from the global array TBE_MODULES_EXT (see mergeExternalItems())
243 * Then MOD_SETTINGS array is cleaned up (see t3lib_BEfunc::getModuleData()) so it contains only valid values. It's also updated with any SET[] values submitted.
244 * Also loads the modTSconfig internal variable.
245 *
246 * @return void
247 * @see init(), $MOD_MENU, $MOD_SETTINGS, t3lib_BEfunc::getModuleData(), mergeExternalItems()
248 */
249 function menuConfig() {
250 // page/be_user TSconfig settings and blinding of menu-items
251 $this->modTSconfig = t3lib_BEfunc::getModTSconfig($this->id, 'mod.' . $this->MCONF['name']);
252 $this->MOD_MENU['function'] = $this->mergeExternalItems($this->MCONF['name'], 'function', $this->MOD_MENU['function']);
253 $this->MOD_MENU['function'] = t3lib_BEfunc::unsetMenuItems($this->modTSconfig['properties'], $this->MOD_MENU['function'], 'menu.function');
254
255 #debug($this->MOD_MENU['function'],$this->MCONF['name']);
256 #debug($this->modTSconfig['properties']);
257
258 $this->MOD_SETTINGS = t3lib_BEfunc::getModuleData(
259 $this->MOD_MENU,
260 t3lib_div::_GP('SET'),
261 $this->MCONF['name'],
262 $this->modMenu_type,
263 $this->modMenu_dontValidateList,
264 $this->modMenu_setDefaultList
265 );
266 }
267
268 /**
269 * Merges menu items from global array $TBE_MODULES_EXT
270 *
271 * @param string Module name for which to find value
272 * @param string Menu key, eg. 'function' for the function menu.
273 * @param array The part of a MOD_MENU array to work on.
274 * @return array Modified array part.
275 * @access private
276 * @see t3lib_extMgm::insertModuleFunction(), menuConfig()
277 */
278 function mergeExternalItems($modName, $menuKey, $menuArr) {
279 $mergeArray = $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey];
280 if (is_array($mergeArray)) {
281 foreach ($mergeArray as $k => $v) {
282 if ((string) $v['ws'] === '' ||
283 ($GLOBALS['BE_USER']->workspace === 0 && t3lib_div::inList($v['ws'], 'online')) ||
284 ($GLOBALS['BE_USER']->workspace === -1 && t3lib_div::inList($v['ws'], 'offline')) ||
285 ($GLOBALS['BE_USER']->workspace > 0 && t3lib_div::inList($v['ws'], 'custom'))) {
286 $menuArr[$k] = $GLOBALS['LANG']->sL($v['title']);
287 }
288 }
289 }
290 return $menuArr;
291 }
292
293 /**
294 * Loads $this->extClassConf with the configuration for the CURRENT function of the menu.
295 * If for this array the key 'path' is set then that is expected to be an absolute path to a file which should be included - so it is set in the internal array $this->include_once
296 *
297 * @param string The key to MOD_MENU for which to fetch configuration. 'function' is default since it is first and foremost used to get information per "extension object" (I think that is what its called)
298 * @param string The value-key to fetch from the config array. If NULL (default) MOD_SETTINGS[$MM_key] will be used. This is usefull if you want to force another function than the one defined in MOD_SETTINGS[function]. Call this in init() function of your Script Class: handleExternalFunctionValue('function', $forcedSubModKey)
299 * @return void
300 * @see getExternalItemConfig(), $include_once, init()
301 */
302 function handleExternalFunctionValue($MM_key = 'function', $MS_value = NULL) {
303 $MS_value = is_null($MS_value) ? $this->MOD_SETTINGS[$MM_key] : $MS_value;
304 $this->extClassConf = $this->getExternalItemConfig($this->MCONF['name'], $MM_key, $MS_value);
305 if (is_array($this->extClassConf) && $this->extClassConf['path']) {
306 $this->include_once[] = $this->extClassConf['path'];
307 }
308 }
309
310 /**
311 * Returns configuration values from the global variable $TBE_MODULES_EXT for the module given.
312 * For example if the module is named "web_info" and the "function" key ($menuKey) of MOD_SETTINGS is "stat" ($value) then you will have the values of $TBE_MODULES_EXT['webinfo']['MOD_MENU']['function']['stat'] returned.
313 *
314 * @param string Module name
315 * @param string Menu key, eg. "function" for the function menu. See $this->MOD_MENU
316 * @param string Optionally the value-key to fetch from the array that would otherwise have been returned if this value was not set. Look source...
317 * @return mixed The value from the TBE_MODULES_EXT array.
318 * @see handleExternalFunctionValue()
319 */
320 function getExternalItemConfig($modName, $menuKey, $value = '') {
321 return strcmp($value, '') ? $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey][$value] : $GLOBALS['TBE_MODULES_EXT'][$modName]['MOD_MENU'][$menuKey];
322 }
323
324 /**
325 * Creates an instance of the class found in $this->extClassConf['name'] in $this->extObj if any (this should hold three keys, "name", "path" and "title" if a "Function menu module" tries to connect...)
326 * This value in extClassConf might be set by an extension (in a ext_tables/ext_localconf file) which thus "connects" to a module.
327 * The array $this->extClassConf is set in handleExternalFunctionValue() based on the value of MOD_SETTINGS[function]
328 * (Should be) called from global scope right after inclusion of files from the ->include_once array.
329 * If an instance is created it is initiated with $this passed as value and $this->extClassConf as second argument. Further the $this->MOD_SETTING is cleaned up again after calling the init function.
330 *
331 * @return void
332 * @see handleExternalFunctionValue(), t3lib_extMgm::insertModuleFunction(), $extObj
333 */
334 function checkExtObj() {
335 if (is_array($this->extClassConf) && $this->extClassConf['name']) {
336 $this->extObj = t3lib_div::makeInstance($this->extClassConf['name']);
337 $this->extObj->init($this, $this->extClassConf);
338 // Re-write:
339 $this->MOD_SETTINGS = t3lib_BEfunc::getModuleData(
340 $this->MOD_MENU,
341 t3lib_div::_GP('SET'),
342 $this->MCONF['name'],
343 $this->modMenu_type,
344 $this->modMenu_dontValidateList,
345 $this->modMenu_setDefaultList
346 );
347 }
348 }
349
350 /**
351 * Calls the checkExtObj function in sub module if present.
352 *
353 * @return void
354 */
355 function checkSubExtObj() {
356 if (is_object($this->extObj)) {
357 $this->extObj->checkExtObj();
358 }
359 }
360
361 /**
362 * Calls the 'header' function inside the "Function menu module" if present.
363 * A header function might be needed to add JavaScript or other stuff in the head. This can't be done in the main function because the head is already written.
364 * example call in the header function:
365 * $this->pObj->doc->JScode = $this->pObj->doc->wrapScriptTags(' ...
366 *
367 * @return void
368 */
369 function extObjHeader() {
370 if (is_callable(array($this->extObj, 'head'))) {
371 $this->extObj->head();
372 }
373 }
374
375 /**
376 * Calls the 'main' function inside the "Function menu module" if present
377 *
378 * @return void
379 */
380 function extObjContent() {
381 $this->extObj->pObj = $this;
382
383 if (is_callable(array($this->extObj, 'main'))) {
384 $this->content .= $this->extObj->main();
385 }
386 }
387 }
388
389 ?>