[TASK] Remove global declarations inside methods
[Packages/TYPO3.CMS.git] / typo3 / wizard_tsconfig.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 * Wizard for inserting TSconfig in form fields. (page,user or TS)
29 *
30 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
31 * XHTML compliant
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 98: class ext_TSparser extends t3lib_tsparser_ext
41 * 106: function makeHtmlspecialchars($P)
42 *
43 *
44 * 127: class SC_wizard_tsconfig
45 * 149: function init()
46 * 202: function setValue(field,value)
47 * 232: function mixerField(cmd,objString)
48 * 258: function str_replace(match,replace,string)
49 * 280: function jump(show,objString)
50 * 295: function main()
51 * 320: function printContent()
52 * 333: function browseTSprop($mode,$show)
53 *
54 * SECTION: Module functions
55 * 419: function getObjTree()
56 * 449: function setObj(&$objTree,$strArr,$params)
57 * 469: function revertFromSpecialChars($str)
58 * 482: function doLink($params)
59 * 495: function removePointerObjects($objArray)
60 * 514: function linkToObj($str,$uid,$objString='')
61 * 527: function printTable($table,$objString,$objTree)
62 * 608: function linkProperty($str,$propertyName,$prefix,$datatype)
63 *
64 * TOTAL FUNCTIONS: 17
65 * (This index is automatically created/updated by the extension "extdeveval")
66 *
67 */
68
69
70
71 $GLOBALS['BACK_PATH']='';
72 require ('init.php');
73 require ('template.php');
74 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_wizards.xml');
75
76
77
78
79
80
81
82 /**
83 * TypoScript parser extension class.
84 *
85 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
86 * @package TYPO3
87 * @subpackage core
88 */
89 class ext_TSparser extends t3lib_tsparser_ext {
90
91 /**
92 * Pass through of incoming value for link.
93 *
94 * @param array P array
95 * @return string The "_LINK" key value, straight away.
96 */
97 function makeHtmlspecialchars($P) {
98 return $P['_LINK'];
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111 /**
112 * Script Class for rendering the TSconfig/TypoScript property browser.
113 *
114 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
115 * @package TYPO3
116 * @subpackage core
117 */
118 class SC_wizard_tsconfig {
119
120 // Internal, dynamic:
121 /**
122 * document template object
123 *
124 * @var mediumDoc
125 */
126 var $doc;
127 var $content; // Content accumulation for the module.
128
129 // Internal, static: GPvars
130 var $P; // Wizard parameters, coming from TCEforms linking to the wizard.
131 var $mode; // "page", "tsref" or "beuser"
132 var $show; // Pointing to an entry in static_tsconfig_help to show.
133 var $objString; // Object path - for display.
134 var $onlyProperty; // If set, the "mixed-field" is not shown and you can select only one property at a time.
135
136
137
138
139
140 /**
141 * Initialization of the class
142 *
143 * @return void
144 */
145 function init() {
146 // Check if the tsconfig_help extension is loaded - which is mandatory for this wizard to work.
147 t3lib_extMgm::isLoaded('tsconfig_help',1);
148
149 // Init GPvars:
150 $this->P = t3lib_div::_GP('P');
151 $this->mode = t3lib_div::_GP('mode');
152 $this->show = t3lib_div::_GP('show');
153 $this->objString = t3lib_div::_GP('objString');
154 $this->onlyProperty = t3lib_div::_GP('onlyProperty');
155 // Preparing some JavaScript code:
156 if (!$this->areFieldChangeFunctionsValid()) {
157 $this->P['fieldChangeFunc']=array();
158 }
159 unset($this->P['fieldChangeFunc']['alert']);
160 $update='';
161 foreach($this->P['fieldChangeFunc'] as $k=>$v) {
162 $update.= '
163 window.opener.'.$v;
164 }
165
166 // Init the document table object:
167 $this->doc = t3lib_div::makeInstance('template');
168 $this->doc->backPath = $GLOBALS['BACK_PATH'];
169 $this->doc->form='<form action="" name="editform">';
170
171 // Adding Styles (should go into stylesheet?)
172 $this->doc->inDocStylesArray[] = '
173 A:link {text-decoration: bold; color: '.$this->doc->hoverColor.';}
174 A:visited {text-decoration: bold; color: '.$this->doc->hoverColor.';}
175 A:active {text-decoration: bold; color: '.$this->doc->hoverColor.';}
176 A:hover {color: '.$this->doc->bgColor2.'}
177 ';
178
179 $this->doc->JScode.=$this->doc->wrapScriptTags('
180 function checkReference_name() { // Checks if the input field containing the name exists in the document
181 if (window.opener && window.opener.document && window.opener.document.'.$this->P['formName'].' && window.opener.document.'.$this->P['formName'].'["'.$this->P['itemName'].'"] ) {
182 return window.opener.document.'.$this->P['formName'].'["'.$this->P['itemName'].'"];
183 }
184 }
185 function checkReference_value() { // Checks if the input field containing the value exists in the document
186 if (window.opener && window.opener.document && window.opener.document.'.$this->P['formName'].' && window.opener.document.'.$this->P['formName'].'["'.$this->P['itemValue'].'"] ) {
187 return window.opener.document.'.$this->P['formName'].'["'.$this->P['itemValue'].'"];
188 }
189 }
190
191 /**
192 * [Describe function...]
193 *
194 * @param [type] $field,value: ...
195 * @return [type] ...
196 */
197 function setValue(field,value) {
198 var nameField = checkReference_name();
199 var valueField = checkReference_value();
200 if (nameField) {
201 if (valueField) { // This applies to the TS Object Browser module
202 nameField.value=field;
203 valueField.value=value;
204 } else { // This applies to the Info/Modify module and the Page TSconfig field
205 if (value) {
206 nameField.value=field+"="+value+"\n"+nameField.value;
207 } else {
208 nameField.value=field+"\n"+nameField.value;
209 }
210 }
211 '.$update.'
212 window.opener.focus();
213 }
214 close();
215 }
216 function getValue() { // This is never used. Remove it?
217 var field = checkReference_name();
218 if (field) {
219 return field.value;
220 } else {
221 close();
222 }
223 }
224
225 /**
226 * [Describe function...]
227 *
228 * @param [type] $cmd,objString: ...
229 * @return [type] ...
230 */
231 function mixerField(cmd,objString) {
232 var temp;
233 switch(cmd) {
234 case "Indent":
235 temp = str_replace("\n","\n ","\n"+document.editform.mixer.value);
236 document.editform.mixer.value = temp.substr(1);
237 break;
238 case "Outdent":
239 temp = str_replace("\n ","\n","\n"+document.editform.mixer.value);
240 document.editform.mixer.value = temp.substr(1);
241 break;
242 case "Transfer":
243 setValue(document.editform.mixer.value);
244 break;
245 case "Wrap":
246 document.editform.mixer.value=objString+" {\n"+document.editform.mixer.value+"\n}";
247 break;
248 }
249 }
250
251 /**
252 * [Describe function...]
253 *
254 * @param [type] $match,replace,string: ...
255 * @return [type] ...
256 */
257 function str_replace(match,replace,string) {
258 var input = ""+string;
259 var matchStr = ""+match;
260 if (!matchStr) {return string;}
261 var output = "";
262 var pointer=0;
263 var pos = input.indexOf(matchStr);
264 while (pos!=-1) {
265 output+=""+input.substr(pointer, pos-pointer)+replace;
266 pointer=pos+matchStr.length;
267 pos = input.indexOf(match,pos+1);
268 }
269 output+=""+input.substr(pointer);
270 return output;
271 }
272
273 /**
274 * [Describe function...]
275 *
276 * @param [type] $show,objString: ...
277 * @return [type] ...
278 */
279 function jump(show,objString) {
280 window.location.href = "'.t3lib_div::linkThisScript(array('show'=>'','objString'=>'')).'&show="+show+"&objString="+objString;
281 }
282 ');
283
284
285 // Start the page:
286 $this->content.=$this->doc->startPage($GLOBALS['LANG']->getLL('tsprop'));
287 }
288
289 /**
290 * Main function, rendering the content of the TypoScript property browser, including links to online resources
291 *
292 * @return void
293 */
294 function main() {
295 // Adding module content:
296 $this->content.=$this->doc->section($GLOBALS['LANG']->getLL('tsprop'),$this->browseTSprop($this->mode,$this->show),0,1);
297
298 // Adding link to TSref:
299 if ($this->mode=='tsref') {
300 $this->content.=$this->doc->section($GLOBALS['LANG']->getLL('tsprop_TSref'),'
301 <a href="'. TYPO3_URL_DOCUMENTATION_TSREF.'" target="_blank">'.$GLOBALS['LANG']->getLL('tsprop_TSref',1).'</a>
302 ',0,1);
303 }
304 // Adding link to admin guides etc:
305 if ($this->mode=='page' || $this->mode=='beuser') {
306 $this->content.=$this->doc->section($GLOBALS['LANG']->getLL('tsprop_tsconfig'),'
307 <a href="' . TYPO3_URL_DOCUMENTATION_TSCONFIG . '" target="_blank">' . $GLOBALS['LANG']->getLL('tsprop_tsconfig',1) . '</a>
308 ',0,1);
309 }
310 }
311
312 /**
313 * Outputting the accumulated content to screen
314 *
315 * @return void
316 */
317 function printContent() {
318 $this->content.= $this->doc->endPage();
319 $this->content = $this->doc->insertStylesAndJS($this->content);
320 echo $this->content;
321 }
322
323 /**
324 * Create the content of the module:
325 *
326 * @param string Object string
327 * @param integer Pointing to an entry in static_tsconfig_help to show.
328 * @return string HTML
329 */
330 function browseTSprop($mode,$show) {
331 // Get object tree:
332 $objTree = $this->getObjTree();
333
334 // Show single element, if show is set.
335 $out='';
336 if ($show) {
337 // Get the entry data:
338 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'static_tsconfig_help', 'uid='.intval($show));
339 $rec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
340 $table = unserialize($rec['appdata']);
341 $obj_string = strtr($this->objString,'()','[]'); // Title:
342
343 // Title and description:
344 $out.='<a href="'.htmlspecialchars(t3lib_div::linkThisScript(array('show'=>''))).'" class="typo3-goBack">'.
345 t3lib_iconWorks::getSpriteIcon('actions-view-go-back') .
346 htmlspecialchars($obj_string).
347 '</a><br />';
348 if ($rec['title']) $out.= '<strong>'.htmlspecialchars($rec['title']).': </strong>';
349 if ($rec['description']) $out.= nl2br(htmlspecialchars(trim($rec['description']))).'<br />';
350
351 // Printing the content:
352 $out.= '<br />'.$this->printTable($table, $obj_string, $objTree[$mode.'.']);
353 $out.='<hr />';
354
355 // Printing the "mixer-field":
356 if (!$this->onlyProperty) {
357 $links=array();
358 $links[]='<a href="#" onclick="mixerField(\'Indent\');return false;">'.$GLOBALS['LANG']->getLL('tsprop_mixer_indent',1).'</a>';
359 $links[]='<a href="#" onclick="mixerField(\'Outdent\');return false;">'.$GLOBALS['LANG']->getLL('tsprop_mixer_outdent',1).'</a>';
360 $links[]='<a href="#" onclick="mixerField(\'Wrap\',unescape(\''.rawurlencode($obj_string).'\'));return false;">'.$GLOBALS['LANG']->getLL('tsprop_mixer_wrap',1).'</a>';
361 $links[]='<a href="#" onclick="mixerField(\'Transfer\');return false;">'.$GLOBALS['LANG']->getLL('tsprop_mixer_transfer',1).'</a>';
362 $out.='<textarea rows="5" name="mixer" wrap="off"'.$this->doc->formWidthText(48,'','off').' class="fixed-font enable-tab"></textarea>';
363 $out.='<br /><strong>'.implode('&nbsp; | &nbsp;',$links).'</strong>';
364 $out.='<hr />';
365 }
366 }
367
368
369 // SECTION: Showing property tree:
370 $tmpl = t3lib_div::makeInstance('ext_TSparser');
371 $tmpl->tt_track = 0; // Do not log time-performance information
372 $tmpl->fixedLgd=0;
373 $tmpl->linkObjects=0;
374 $tmpl->bType='';
375 $tmpl->ext_expandAllNotes=1;
376 $tmpl->ext_noPMicons=1;
377 $tmpl->ext_noSpecialCharsOnLabels=1;
378
379 if (is_array($objTree[$mode.'.'])) {
380 $out.='
381
382
383 <!--
384 TSconfig, object tree:
385 -->
386 <table border="0" cellpadding="0" cellspacing="0" class="t3-tree t3-tree-config" id="typo3-objtree">
387 <tr class="t3-row-header"><td>TSref</td></tr>
388 <tr>
389 <td nowrap="nowrap">'.$tmpl->ext_getObjTree($this->removePointerObjects($objTree[$mode.'.']),'','','','','1').'</td>
390 </tr>
391 </table>';
392 }
393
394 return $out;
395 }
396
397
398
399
400
401
402
403 /***************************
404 *
405 * Module functions
406 *
407 ***************************/
408
409 /**
410 * Create object tree from static_tsconfig_help table
411 *
412 * @return array Object tree.
413 * @access private
414 */
415 function getObjTree() {
416 $objTree=array();
417
418 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,obj_string,title', 'static_tsconfig_help', '');
419 while($rec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
420 $rec['obj_string'] = $this->revertFromSpecialChars($rec['obj_string']);
421 $p = explode(';',$rec['obj_string']);
422 foreach ($p as $v) {
423 $p2 = t3lib_div::trimExplode(':',$v,1);
424 $subp=t3lib_div::trimExplode('/',$p2[1],1);
425 foreach ($subp as $v2) {
426 $this->setObj($objTree,explode('.',$p2[0].'.'.$v2),array($rec,$v2));
427 }
428 }
429 }
430 return $objTree;
431 }
432
433 /**
434 * Sets the information from a static_tsconfig_help record in the object array.
435 * Makes recursive calls.
436 *
437 * @param array Object tree array, passed by value!
438 * @param array Array of elements from object path (?)
439 * @param array Array with record and something else (?)
440 * @return void
441 * @access private
442 * @see getObjTree()
443 */
444 function setObj(&$objTree,$strArr,$params) {
445 $key = current($strArr);
446 reset($strArr);
447 if (count($strArr)>1) {
448 array_shift($strArr);
449 if (!isset($objTree[$key.'.'])) $objTree[$key.'.']=array();
450 $this->setObj($objTree[$key.'.'],$strArr,$params);
451 } else {
452 $objTree[$key]=$params;
453 $objTree[$key]['_LINK']=$this->doLink($params);
454 }
455 }
456
457 /**
458 * Converts &gt; and &lt; to > and <
459 *
460 * @param string Input string
461 * @return string Output string
462 * @access private
463 */
464 function revertFromSpecialChars($str) {
465 $str = str_replace('&gt;','>',$str);
466 $str = str_replace('&lt;','<',$str);
467 return $str;
468 }
469
470 /**
471 * Creates a link based on input params array:
472 *
473 * @param array Parameters
474 * @return string The link.
475 * @access private
476 */
477 function doLink($params) {
478 $title = trim($params[0]['title'])?trim($params[0]['title']):'[GO]';
479 $str = $this->linkToObj($title,$params[0]['uid'],$params[1]);
480 return $str;
481 }
482
483 /**
484 * Remove pointer strings from an array
485 *
486 * @param array Input array
487 * @return array Modified input array
488 * @access private
489 */
490 function removePointerObjects($objArray) {
491 foreach ($objArray as $k => $value) {
492 if (substr(trim($k),0,2)=="->" && trim($k)!='->.') {
493 $objArray['->.'][substr(trim($k),2)]=$objArray[$k];
494 unset($objArray[$k]);
495 }
496 }
497 return $objArray;
498 }
499
500 /**
501 * Linking string to object by UID
502 *
503 * @param string String to link
504 * @param integer UID of a static_tsconfig_help record.
505 * @param string Title string for that record!
506 * @return string Linked string
507 */
508 function linkToObj($str,$uid,$objString='') {
509 $aOnClick='jump(\''.rawurlencode($uid).'\',\''.rawurlencode($objString).'\');return false;';
510 return '<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.htmlspecialchars($str).'</a>';
511 }
512
513 /**
514 * Creates a table of properties:
515 *
516 * @param array Array with properties for the current object path
517 * @param string Object path
518 * @param array Object tree
519 * @return string HTML content.
520 */
521 function printTable($table,$objString,$objTree) {
522 if (is_array($table['rows'])) {
523
524 // Initialize:
525 $lines=array();
526
527 // Adding header:
528 $lines[]='
529 <tr class="t3-row-header">
530 <td>Property:</td>
531 <td>Data type:</td>
532 <td>Description:</td>
533 <td>Default:</td>
534 </tr>';
535
536 // Traverse the content of "rows":
537 foreach($table['rows'] as $i => $row) {
538
539 // Linking:
540 $lP=t3lib_div::trimExplode(LF,$row['property'],1);
541 $lP2=array();
542 foreach ($lP as $k => $lStr) {
543 $lP2[$k] = $this->linkProperty($lStr,$lStr,$objString,$row['datatype']);
544 }
545 $linkedProperties=implode('<hr />',$lP2);
546
547 // Data type:
548 $dataType = $row['datatype'];
549
550 // Generally "->[something]"
551 $reg=array();
552 preg_match('/->[[:alnum:]_]*/',$dataType,$reg);
553 if ($reg[0] && is_array($objTree[$reg[0]])) {
554 $dataType = str_replace($reg[0],'<a href="'.htmlspecialchars(t3lib_div::linkThisScript(array('show'=>$objTree[$reg[0]][0]['uid'],'objString'=>$objString.'.'.$lP[0]))).'">'.htmlspecialchars($reg[0]).'</a>',$dataType);
555 }
556
557 // stdWrap
558 if (!strstr($dataType,'->stdWrap') && strstr(strip_tags($dataType),'stdWrap')) {
559 // Potential problem can be that "stdWrap" is substituted inside another A-tag. So maybe we should even check if there is already a <A>-tag present and if so, not make a substitution?
560 $dataType = str_replace('stdWrap','<a href="'.htmlspecialchars(t3lib_div::linkThisScript(array('show'=>$objTree['->stdWrap'][0]['uid'],'objString'=>$objString.'.'.$lP[0]))).'">stdWrap</a>',$dataType);
561 }
562
563
564 $lines[]='
565 <tr class="t3-row ' . ($i % 2 ? 't3-row-even' : 't3-row-odd') . '">
566 <td valign="top" class="bgColor4-20" nowrap="nowrap"><strong>'.$linkedProperties.'</strong></td>
567 <td valign="top">'.nl2br($dataType.'&nbsp;').'</td>
568 <td valign="top">'.nl2br($row['description']).'</td>
569 <td valign="top">'.nl2br($row['default']).'</td>
570 </tr>';
571 }
572 // Return it all:
573 return '
574
575
576
577 <!--
578 TSconfig, attribute selector:
579 -->
580 <table border="0" cellpadding="0" cellspacing="1" width="98%" class="t3-table" id="typo3-attributes">
581 '.implode('',$lines).'
582 </table>';
583 }
584 }
585
586 /**
587 * Creates a link on a property.
588 *
589 * @param string String to link
590 * @param string Property value.
591 * @param string Object path prefix to value
592 * @param string Data type
593 * @return string Linked $str
594 */
595 function linkProperty($str,$propertyName,$prefix,$datatype) {
596 $out='';
597
598 // Setting preset value:
599 if (strstr($datatype,'boolean')) {
600 $propertyVal='1'; // preset "1" to boolean values.
601 }
602
603 // Adding mixer features; The plus icon:
604 if(!$this->onlyProperty) {
605 $aOnClick = 'document.editform.mixer.value=unescape(\' '.rawurlencode($propertyName.'='.$propertyVal).'\')+\'\n\'+document.editform.mixer.value; return false;';
606 $out.= '<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.
607 t3lib_iconWorks::getSpriteIcon('actions-edit-add', array('title' => $GLOBALS['LANG']->getLL('tsprop_addToList', TRUE))) .
608 '</a>';
609 $propertyName = $prefix.'.'.$propertyName;
610 }
611
612 // Wrap string:
613 $aOnClick = 'setValue(unescape(\''.rawurlencode($propertyName).'\'),unescape(\''.rawurlencode($propertyVal).'\')); return false;';
614 $out.= '<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$str.'</a>';
615
616 // Return link:
617 return $out;
618 }
619
620 /**
621 * Determines whether submitted field change functions are valid
622 * and are coming from the system and not from an external abuse.
623 *
624 * @return boolean Whether the submitted field change functions are valid
625 */
626 protected function areFieldChangeFunctionsValid() {
627 return (
628 isset($this->P['fieldChangeFunc']) && is_array($this->P['fieldChangeFunc']) && isset($this->P['fieldChangeFuncHash'])
629 && $this->P['fieldChangeFuncHash'] == t3lib_div::hmac(serialize($this->P['fieldChangeFunc']))
630 );
631 }
632 }
633
634
635 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/wizard_tsconfig.php'])) {
636 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/wizard_tsconfig.php']);
637 }
638
639
640
641 // Make instance:
642 $SOBE = t3lib_div::makeInstance('SC_wizard_tsconfig');
643 $SOBE->init();
644 $SOBE->main();
645 $SOBE->printContent();
646
647 ?>