f1c904e4be26e8c536c59539b77bb2f15dfa0e12
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / TypoScript / ExtendedTemplateService.php
1 <?php
2 namespace TYPO3\CMS\Core\TypoScript;
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\Template\DocumentTemplate;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Exception;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24 use TYPO3\CMS\Core\Utility\PathUtility;
25 use TYPO3\CMS\Dbal\Database\DatabaseConnection;
26 use TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
27 use TYPO3\CMS\Lang\LanguageService;
28
29 /**
30 * TSParser extension class to TemplateService
31 * Contains functions for the TS module in TYPO3 backend
32 */
33 class ExtendedTemplateService extends TemplateService
34 {
35 /**
36 * This string is used to indicate the point in a template from where the editable constants are listed.
37 * Any vars before this point (if it exists though) is regarded as default values.
38 *
39 * @var string
40 */
41 public $edit_divider = '###MOD_TS:EDITABLE_CONSTANTS###';
42
43 /**
44 * @var string
45 */
46 public $HTMLcolorList = 'aqua,beige,black,blue,brown,fuchsia,gold,gray,green,lime,maroon,navy,olive,orange,purple,red,silver,tan,teal,turquoise,yellow,white';
47
48 /**
49 * @var array
50 */
51 public $categories = array(
52 'basic' => array(),
53 // Constants of superior importance for the template-layout. This is dimensions, imagefiles and enabling of various features. The most basic constants, which you would almost always want to configure.
54 'menu' => array(),
55 // Menu setup. This includes fontfiles, sizes, background images. Depending on the menutype.
56 'content' => array(),
57 // All constants related to the display of pagecontent elements
58 'page' => array(),
59 // General configuration like metatags, link targets
60 'advanced' => array(),
61 // Advanced functions, which are used very seldomly.
62 'all' => array()
63 );
64
65 /**
66 * Translated categories
67 *
68 * @var array
69 */
70 protected $categoryLabels = array();
71
72 /**
73 * This will be filled with the available categories of the current template.
74 *
75 * @var array
76 */
77 public $subCategories = array(
78 // Standard categories:
79 'enable' => array('Enable features', 'a'),
80 'dims' => array('Dimensions, widths, heights, pixels', 'b'),
81 'file' => array('Files', 'c'),
82 'typo' => array('Typography', 'd'),
83 'color' => array('Colors', 'e'),
84 'links' => array('Links and targets', 'f'),
85 'language' => array('Language specific constants', 'g'),
86 // subcategories based on the default content elements
87 'cheader' => array('Content: \'Header\'', 'ma'),
88 'cheader_g' => array('Content: \'Header\', Graphical', 'ma'),
89 'ctext' => array('Content: \'Text\'', 'mb'),
90 'cimage' => array('Content: \'Image\'', 'md'),
91 'ctextmedia' => array('Content: \'Textmedia\'', 'ml'),
92 'cbullets' => array('Content: \'Bullet list\'', 'me'),
93 'ctable' => array('Content: \'Table\'', 'mf'),
94 'cuploads' => array('Content: \'Filelinks\'', 'mg'),
95 'cmultimedia' => array('Content: \'Multimedia\'', 'mh'),
96 'cmedia' => array('Content: \'Media\'', 'mr'),
97 'cmailform' => array('Content: \'Form\'', 'mi'),
98 'csearch' => array('Content: \'Search\'', 'mj'),
99 'clogin' => array('Content: \'Login\'', 'mk'),
100 'cmenu' => array('Content: \'Menu/Sitemap\'', 'mm'),
101 'cshortcut' => array('Content: \'Insert records\'', 'mn'),
102 'clist' => array('Content: \'List of records\'', 'mo'),
103 'chtml' => array('Content: \'HTML\'', 'mq')
104 );
105
106 /**
107 * @var bool
108 */
109 public $backend_info = true;
110
111 /**
112 * Tsconstanteditor
113 *
114 * @var int
115 */
116 public $ext_inBrace = 0;
117
118 /**
119 * Tsbrowser
120 *
121 * @var array
122 */
123 public $tsbrowser_searchKeys = array();
124
125 /**
126 * @var array
127 */
128 public $tsbrowser_depthKeys = array();
129
130 /**
131 * @var string
132 */
133 public $constantMode = '';
134
135 /**
136 * @var string
137 */
138 public $regexMode = '';
139
140 /**
141 * @var string
142 */
143 public $fixedLgd = '';
144
145 /**
146 * @var int
147 */
148 public $ext_lineNumberOffset = 0;
149
150 /**
151 * @var string
152 */
153 public $ext_localGfxPrefix = '';
154
155 /**
156 * @var string
157 */
158 public $ext_localWebGfxPrefix = '';
159
160 /**
161 * @var int
162 */
163 public $ext_expandAllNotes = 0;
164
165 /**
166 * @var int
167 */
168 public $ext_noPMicons = 0;
169
170 /**
171 * @var array
172 */
173 public $ext_listOfTemplatesArr = array();
174
175 /**
176 * @var string
177 */
178 public $ext_lineNumberOffset_mode = '';
179
180 /**
181 * Don't change
182 *
183 * @var int
184 */
185 public $ext_dontCheckIssetValues = 0;
186
187 /**
188 * @var int
189 */
190 public $ext_printAll = 0;
191
192 /**
193 * @var string
194 */
195 public $ext_CEformName = 'forms[0]';
196
197 /**
198 * @var bool
199 */
200 public $doNotSortCategoriesBeforeMakingForm = false;
201
202 /**
203 * Ts analyzer
204 *
205 * @var array
206 */
207 public $templateTitles = array();
208
209 /**
210 * @var array|NULL
211 */
212 protected $lnToScript = null;
213
214 /**
215 * @var array
216 */
217 public $clearList_const_temp;
218
219 /**
220 * @var array
221 */
222 public $clearList_setup_temp;
223
224 /**
225 * @var string
226 */
227 protected $Cmarker = '';
228
229 /**
230 * @var string
231 */
232 public $bType = '';
233
234 /**
235 * @var bool
236 */
237 public $linkObjects = false;
238
239 /**
240 * @var array
241 */
242 public $helpConfig = array();
243
244 /**
245 * @var bool
246 */
247 public $changed = false;
248
249 /**
250 * @var int[]
251 */
252 protected $objReg = array();
253
254 /**
255 * @var array
256 */
257 public $raw = array();
258
259 /**
260 * @var int
261 */
262 public $rawP = 0;
263
264 /**
265 * @var string
266 */
267 public $lastComment = '';
268
269 /**
270 * Substitute constant
271 *
272 * @param string $all
273 * @return string
274 */
275 public function substituteConstants($all)
276 {
277 $this->Cmarker = substr(md5(uniqid('', true)), 0, 6);
278 return preg_replace_callback('/\\{\\$(.[^}]+)\\}/', array($this, 'substituteConstantsCallBack'), $all);
279 }
280
281 /**
282 * Call back method for preg_replace_callback in substituteConstants
283 *
284 * @param array $matches Regular expression matches
285 * @return string Replacement
286 * @see substituteConstants()
287 */
288 public function substituteConstantsCallBack($matches)
289 {
290 switch ($this->constantMode) {
291 case 'const':
292 $ret_val = isset($this->flatSetup[$matches[1]]) && !is_array($this->flatSetup[$matches[1]]) ? '##' . $this->Cmarker . '_B##' . $matches[0] . '##' . $this->Cmarker . '_E##' : $matches[0];
293 break;
294 case 'subst':
295 $ret_val = isset($this->flatSetup[$matches[1]]) && !is_array($this->flatSetup[$matches[1]]) ? '##' . $this->Cmarker . '_B##' . $this->flatSetup[$matches[1]] . '##' . $this->Cmarker . '_E##' : $matches[0];
296 break;
297 case 'untouched':
298 $ret_val = $matches[0];
299 break;
300 default:
301 $ret_val = isset($this->flatSetup[$matches[1]]) && !is_array($this->flatSetup[$matches[1]]) ? $this->flatSetup[$matches[1]] : $matches[0];
302 }
303 return $ret_val;
304 }
305
306 /**
307 * Subsitute markers
308 *
309 * @param string $all
310 * @return string
311 */
312 public function substituteCMarkers($all)
313 {
314 switch ($this->constantMode) {
315 case 'const':
316 case 'subst':
317 $all = str_replace(
318 array('##' . $this->Cmarker . '_B##', '##' . $this->Cmarker . '_E##'),
319 array('<strong style="color: green;">', '</strong>'),
320 $all
321 );
322 break;
323 default:
324 }
325 return $all;
326 }
327
328 /**
329 * Parse constants with respect to the constant-editor in this module.
330 * In particular comments in the code are registered and the edit_divider is taken into account.
331 *
332 * @return array
333 */
334 public function generateConfig_constants()
335 {
336 // These vars are also set lateron...
337 $this->setup['sitetitle'] = $this->sitetitle;
338 // Parse constants
339 $constants = GeneralUtility::makeInstance(Parser\TypoScriptParser::class);
340 // Register comments!
341 $constants->regComments = 1;
342 $constants->setup = $this->mergeConstantsFromPageTSconfig(array());
343 /** @var ConditionMatcher $matchObj */
344 $matchObj = GeneralUtility::makeInstance(ConditionMatcher::class);
345 // Matches ALL conditions in TypoScript
346 $matchObj->setSimulateMatchResult(true);
347 $c = 0;
348 $cc = count($this->constants);
349 $defaultConstants = array();
350 foreach ($this->constants as $str) {
351 $c++;
352 if ($c == $cc) {
353 if (strstr($str, $this->edit_divider)) {
354 $parts = explode($this->edit_divider, $str, 2);
355 $str = $parts[1];
356 $constants->parse($parts[0], $matchObj);
357 }
358 $this->flatSetup = array();
359 $this->flattenSetup($constants->setup, '', '');
360 $defaultConstants = $this->flatSetup;
361 }
362 $constants->parse($str, $matchObj);
363 }
364 $this->flatSetup = array();
365 $this->flattenSetup($constants->setup, '', '');
366 $this->setup['constants'] = $constants->setup;
367 return $this->ext_compareFlatSetups($defaultConstants);
368 }
369
370 /**
371 * @param array $theSetup
372 * @param string $theKey
373 * @return array
374 */
375 public function ext_getSetup($theSetup, $theKey)
376 {
377 $parts = explode('.', $theKey, 2);
378 if ((string)$parts[0] !== '' && is_array($theSetup[$parts[0] . '.'])) {
379 if (trim($parts[1]) !== '') {
380 return $this->ext_getSetup($theSetup[$parts[0] . '.'], trim($parts[1]));
381 } else {
382 return array($theSetup[$parts[0] . '.'], $theSetup[$parts[0]]);
383 }
384 } else {
385 if (trim($theKey) !== '') {
386 return array(array(), $theSetup[$theKey]);
387 } else {
388 return array($theSetup, '');
389 }
390 }
391 }
392
393 /**
394 * Get object tree
395 *
396 * @param array $arr
397 * @param string $depth_in
398 * @param string $depthData
399 * @param string $parentType (unused)
400 * @param string $parentValue (unused)
401 * @param string $alphaSort sorts the array keys / tree by alphabet when set to 1
402 * @return array
403 */
404 public function ext_getObjTree($arr, $depth_in, $depthData, $parentType = '', $parentValue = '', $alphaSort = '0')
405 {
406 $HTML = '';
407 $a = 0;
408 if ($alphaSort == '1') {
409 ksort($arr);
410 }
411 $keyArr_num = array();
412 $keyArr_alpha = array();
413 foreach ($arr as $key => $value) {
414 // Don't do anything with comments / linenumber registrations...
415 if (substr($key, -2) != '..') {
416 $key = preg_replace('/\\.$/', '', $key);
417 if (substr($key, -1) != '.') {
418 if (MathUtility::canBeInterpretedAsInteger($key)) {
419 $keyArr_num[$key] = $arr[$key];
420 } else {
421 $keyArr_alpha[$key] = $arr[$key];
422 }
423 }
424 }
425 }
426 ksort($keyArr_num);
427 $keyArr = $keyArr_num + $keyArr_alpha;
428 $c = count($keyArr);
429 if ($depth_in) {
430 $depth_in = $depth_in . '.';
431 }
432 foreach ($keyArr as $key => $value) {
433 $a++;
434 $depth = $depth_in . $key;
435 // This excludes all constants starting with '_' from being shown.
436 if ($this->bType !== 'const' || $depth[0] !== '_') {
437 $goto = substr(md5($depth), 0, 6);
438 $deeper = is_array($arr[$key . '.']) && ($this->tsbrowser_depthKeys[$depth] || $this->ext_expandAllNotes) ? 1 : 0;
439 $LN = $a == $c ? 'blank' : 'line';
440 $BTM = $a == $c ? 'bottom' : '';
441 $PM = is_array($arr[$key . '.']) && !$this->ext_noPMicons ? ($deeper ? 'minus' : 'plus') : 'join';
442 $HTML .= $depthData . '<li>';
443 if ($PM !== 'join') {
444 $urlParameters = array(
445 'id' => $GLOBALS['SOBE']->id,
446 'tsbr[' . $depth . ']' => $deeper ? 0 : 1
447 );
448 if (GeneralUtility::_GP('breakPointLN')) {
449 $urlParameters['breakPointLN'] = GeneralUtility::_GP('breakPointLN');
450 }
451 $aHref = BackendUtility::getModuleUrl('web_ts', $urlParameters) . '#' . $goto;
452 $HTML .= '<a class="list-tree-control' . ($PM === 'minus' ? ' list-tree-control-open' : ' list-tree-control-closed') . '" name="' . $goto . '" href="' . htmlspecialchars($aHref) . '"><i class="fa"></i></a>';
453 }
454 $label = $key;
455 // Read only...
456 if (($depth === 'types' || $depth === 'resources' || $depth === 'sitetitle') && $this->bType === 'setup') {
457 $label = '<span style="color: #666666;">' . $label . '</span>';
458 } else {
459 if ($this->linkObjects) {
460 $urlParameters = array(
461 'id' => $GLOBALS['SOBE']->id,
462 'sObj' => $depth
463 );
464 if (GeneralUtility::_GP('breakPointLN')) {
465 $urlParameters['breakPointLN'] = GeneralUtility::_GP('breakPointLN');
466 }
467 $aHref = BackendUtility::getModuleUrl('web_ts', $urlParameters);
468 if ($this->bType != 'const') {
469 $ln = is_array($arr[$key . '.ln..']) ? 'Defined in: ' . $this->lineNumberToScript($arr[$key . '.ln..']) : 'N/A';
470 } else {
471 $ln = '';
472 }
473 if ($this->tsbrowser_searchKeys[$depth] & 4) {
474 $label = '<strong class="text-danger">' . $label . '</strong>';
475 }
476 // The key has matched the search string
477 $label = '<a href="' . htmlspecialchars($aHref) . '" title="' . htmlspecialchars($ln) . '">' . $label . '</a>';
478 }
479 }
480 $HTML .= '
481 <span class="list-tree-group">
482 <span class="list-tree-label">[' . $label . ']</span>';
483 if (isset($arr[$key])) {
484 $theValue = $arr[$key];
485 if ($this->fixedLgd) {
486 $imgBlocks = ceil(1 + strlen($depthData) / 77);
487 $lgdChars = 68 - ceil(strlen(('[' . $key . ']')) * 0.8) - $imgBlocks * 3;
488 $theValue = $this->ext_fixed_lgd($theValue, $lgdChars);
489 }
490 // The value has matched the search string
491 if ($this->tsbrowser_searchKeys[$depth] & 2) {
492 $HTML .= ' = <span class="list-tree-value text-danger">' . htmlspecialchars($theValue) . '</span>';
493 } else {
494 $HTML .= ' = <span class="list-tree-value">' . htmlspecialchars($theValue) . '</span>';
495 }
496 if ($this->ext_regComments && isset($arr[$key . '..'])) {
497 $comment = $arr[$key . '..'];
498 // Skip INCLUDE_TYPOSCRIPT comments, they are almost useless
499 if (!preg_match('/### <INCLUDE_TYPOSCRIPT:.*/', $comment)) {
500 // Remove linebreaks, replace with ' '
501 $comment = preg_replace('/[\\r\\n]/', ' ', $comment);
502 // Remove # and * if more than twice in a row
503 $comment = preg_replace('/[#\\*]{2,}/', '', $comment);
504 // Replace leading # (just if it exists) and add it again. Result: Every comment should be prefixed by a '#'.
505 $comment = preg_replace('/^[#\\*\\s]+/', '# ', $comment);
506 // Masking HTML Tags: Replace < with &lt; and > with &gt;
507 $comment = htmlspecialchars($comment);
508 $HTML .= ' <i class="text-muted">' . trim($comment) . '</i>';
509 }
510 }
511 }
512 $HTML .= '</span>';
513 if ($deeper) {
514 $HTML .= $this->ext_getObjTree($arr[$key . '.'], $depth, $depthData, '', $arr[$key], $alphaSort);
515 }
516 }
517 }
518 if ($HTML !== '') {
519 $HTML = '<ul class="list-tree text-monospace">' . $HTML . '</ul>';
520 }
521
522 return $HTML;
523 }
524
525 /**
526 * Find the originating template name for an array of line numbers (TypoScript setup only!)
527 * Given an array of linenumbers the method will try to find the corresponding template where this line originated
528 * The linenumber indicates the *last* lineNumber that is part of the template
529 *
530 * lineNumbers are in sync with the calculated lineNumbers '.ln..' in TypoScriptParser
531 *
532 * @param array $lnArr Array with linenumbers (might have some extra symbols, for example for unsetting) to be processed
533 * @return array The same array where each entry has been prepended by the template title if available
534 */
535 public function lineNumberToScript(array $lnArr)
536 {
537 // On the first call, construct the lnToScript array.
538 if (!is_array($this->lnToScript)) {
539 $this->lnToScript = array();
540
541 // aggregatedTotalLineCount
542 $c = 0;
543 foreach ($this->hierarchyInfo as $templateNumber => $info) {
544 // hierarchyInfo has the number of lines in configLines, but unfortunatly this value
545 // was calculated *before* processing of any INCLUDE instructions
546 // for some yet unknown reason we have to add an extra +2 offset
547 $linecountAfterIncludeProcessing = substr_count($this->config[$templateNumber], LF) + 2;
548 $c += $linecountAfterIncludeProcessing;
549 $this->lnToScript[$c] = $info['title'];
550 }
551 }
552
553 foreach ($lnArr as $k => $ln) {
554 foreach ($this->lnToScript as $endLn => $title) {
555 if ($endLn >= (int)$ln) {
556 $lnArr[$k] = '"' . $title . '", ' . $ln;
557 break;
558 }
559 }
560 }
561
562 return implode('; ', $lnArr);
563 }
564
565 /**
566 * @param array $arr
567 * @param string $depth_in
568 * @param string $searchString
569 * @param array $keyArray
570 * @return array
571 * @throws Exception
572 */
573 public function ext_getSearchKeys($arr, $depth_in, $searchString, $keyArray)
574 {
575 $keyArr = array();
576 foreach ($arr as $key => $value) {
577 $key = preg_replace('/\\.$/', '', $key);
578 if (substr($key, -1) != '.') {
579 $keyArr[$key] = 1;
580 }
581 }
582 if ($depth_in) {
583 $depth_in = $depth_in . '.';
584 }
585 $searchPattern = '';
586 if ($this->regexMode) {
587 $searchPattern = '/' . addcslashes($searchString, '/') . '/';
588 $matchResult = @preg_match($searchPattern, '');
589 if ($matchResult === false) {
590 throw new Exception(sprintf('Error evaluating regular expression "%s".', $searchPattern), 1446559458);
591 }
592 }
593 foreach ($keyArr as $key => $value) {
594 $depth = $depth_in . $key;
595 $deeper = is_array($arr[$key . '.']);
596 if ($this->regexMode) {
597 // The value has matched
598 if (preg_match($searchPattern, $arr[$key])) {
599 $this->tsbrowser_searchKeys[$depth] += 2;
600 }
601 // The key has matched
602 if (preg_match($searchPattern, $key)) {
603 $this->tsbrowser_searchKeys[$depth] += 4;
604 }
605 // Just open this subtree if the parent key has matched the search
606 if (preg_match($searchPattern, $depth_in)) {
607 $this->tsbrowser_searchKeys[$depth] = 1;
608 }
609 } else {
610 // The value has matched
611 if (stristr($arr[$key], $searchString)) {
612 $this->tsbrowser_searchKeys[$depth] += 2;
613 }
614 // The key has matches
615 if (stristr($key, $searchString)) {
616 $this->tsbrowser_searchKeys[$depth] += 4;
617 }
618 // Just open this subtree if the parent key has matched the search
619 if (stristr($depth_in, $searchString)) {
620 $this->tsbrowser_searchKeys[$depth] = 1;
621 }
622 }
623 if ($deeper) {
624 $cS = count($this->tsbrowser_searchKeys);
625 $keyArray = $this->ext_getSearchKeys($arr[$key . '.'], $depth, $searchString, $keyArray);
626 if ($cS != count($this->tsbrowser_searchKeys)) {
627 $keyArray[$depth] = 1;
628 }
629 }
630 }
631 return $keyArray;
632 }
633
634 /**
635 * @param int $pid
636 * @return int
637 */
638 public function ext_getRootlineNumber($pid)
639 {
640 if ($pid) {
641 foreach ($this->getRootLine() as $key => $val) {
642 if ((int)$val['uid'] === (int)$pid) {
643 return (int)$key;
644 }
645 }
646 }
647 return -1;
648 }
649
650 /**
651 * @param array $arr
652 * @param string $depthData
653 * @param array $keyArray
654 * @param int $first
655 * @return array
656 */
657 public function ext_getTemplateHierarchyArr($arr, $depthData, $keyArray, $first = 0)
658 {
659 $keyArr = array();
660 foreach ($arr as $key => $value) {
661 $key = preg_replace('/\\.$/', '', $key);
662 if (substr($key, -1) != '.') {
663 $keyArr[$key] = 1;
664 }
665 }
666 $a = 0;
667 $c = count($keyArr);
668 static $i = 0;
669 /** @var IconFactory $iconFactory */
670 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
671 foreach ($keyArr as $key => $value) {
672 $HTML = '';
673 $a++;
674 $deeper = is_array($arr[$key . '.']);
675 $row = $arr[$key];
676 $LN = $a == $c ? 'blank' : 'line';
677 $BTM = $a == $c ? 'top' : '';
678 $HTML .= $depthData;
679 $alttext = '[' . $row['templateID'] . ']';
680 $alttext .= $row['pid'] ? ' - ' . BackendUtility::getRecordPath($row['pid'], $GLOBALS['SOBE']->perms_clause, 20) : '';
681 $icon = substr($row['templateID'], 0, 3) === 'sys'
682 ? '<span title="' . htmlspecialchars($alttext) . '">' . $iconFactory->getIconForRecord('sys_template', $row, Icon::SIZE_SMALL)->render() . '</span>'
683 : '<span title="' . htmlspecialchars($alttext) . '">' . $iconFactory->getIcon('mimetypes-x-content-template-static', Icon::SIZE_SMALL)->render() . '</span>';
684 if (in_array($row['templateID'], $this->clearList_const) || in_array($row['templateID'], $this->clearList_setup)) {
685 $urlParameters = array(
686 'id' => $GLOBALS['SOBE']->id,
687 'template' => $row['templateID']
688 );
689 $aHref = BackendUtility::getModuleUrl('web_ts', $urlParameters);
690 $A_B = '<a href="' . htmlspecialchars($aHref) . '">';
691 $A_E = '</a>';
692 if (GeneralUtility::_GP('template') == $row['templateID']) {
693 $A_B = '<strong>' . $A_B;
694 $A_E .= '</strong>';
695 }
696 } else {
697 $A_B = '';
698 $A_E = '';
699 }
700 $HTML .= ($first ? '' : '<span class="treeline-icon treeline-icon-join' . $BTM . '"></span>') . $icon . ' ' . $A_B
701 . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $GLOBALS['BE_USER']->uc['titleLen']))
702 . $A_E . '&nbsp;&nbsp;';
703 $RL = $this->ext_getRootlineNumber($row['pid']);
704 $statusCheckedIcon = $iconFactory->getIcon('status-status-checked', Icon::SIZE_SMALL)->render();
705 $keyArray[] = '<tr>
706 <td nowrap="nowrap">' . $HTML . '</td>
707 <td align="center">' . ($row['root'] ? $statusCheckedIcon : '') . '</td>
708 <td align="center">' . ($row['clConf'] ? $statusCheckedIcon : '') . '</td>
709 <td align="center">' . ($row['clConst'] ? $statusCheckedIcon : '') . '</td>
710 <td align="center">' . ($row['pid'] ?: '') . '</td>
711 <td align="center">' . ($RL >= 0 ? $RL : '') . '</td>
712 <td>' . ($row['next'] ? $row['next'] : '') . '</td>
713 </tr>';
714 if ($deeper) {
715 $keyArray = $this->ext_getTemplateHierarchyArr($arr[$key . '.'], $depthData . ($first ? '' : '<span class="treeline-icon treeline-icon-' . $LN . '"></span>'), $keyArray);
716 }
717 }
718 return $keyArray;
719 }
720
721 /**
722 * Processes the flat array from TemplateService->hierarchyInfo
723 * and turns it into a hierachical array to show dependencies (used by TemplateAnalyzer)
724 *
725 * @param array $depthDataArr (empty array on external call)
726 * @param int &$pointer Element number (1! to count()) of $this->hierarchyInfo that should be processed.
727 * @return array Processed hierachyInfo.
728 */
729 public function ext_process_hierarchyInfo(array $depthDataArr, &$pointer)
730 {
731 $parent = $this->hierarchyInfo[$pointer - 1]['templateParent'];
732 while ($pointer > 0 && $this->hierarchyInfo[$pointer - 1]['templateParent'] == $parent) {
733 $pointer--;
734 $row = $this->hierarchyInfo[$pointer];
735 $depthDataArr[$row['templateID']] = $row;
736 unset($this->clearList_setup_temp[$row['templateID']]);
737 unset($this->clearList_const_temp[$row['templateID']]);
738 $this->templateTitles[$row['templateID']] = $row['title'];
739 if ($row['templateID'] == $this->hierarchyInfo[$pointer - 1]['templateParent']) {
740 $depthDataArr[$row['templateID'] . '.'] = $this->ext_process_hierarchyInfo(array(), $pointer);
741 }
742 }
743 return $depthDataArr;
744 }
745
746 /**
747 * Get formatted HTML output for TypoScript either with Syntaxhiglighting or in plain mode
748 *
749 * @param array $config Array with simple strings of typoscript code.
750 * @param bool $lineNumbers Prepend linNumbers to each line.
751 * @param bool $comments Enable including comments in output.
752 * @param bool $crop Enable cropping of long lines.
753 * @param bool $syntaxHL Enrich output with syntaxhighlighting.
754 * @param int $syntaxHLBlockmode
755 * @return string
756 */
757 public function ext_outputTS(
758 array $config, $lineNumbers = false, $comments = false, $crop = false, $syntaxHL = false, $syntaxHLBlockmode = 0
759 ) {
760 $all = '';
761 foreach ($config as $str) {
762 $all .= '[GLOBAL]' . LF . $str;
763 }
764 if ($syntaxHL) {
765 $tsparser = GeneralUtility::makeInstance(Parser\TypoScriptParser::class);
766 $tsparser->lineNumberOffset = $this->ext_lineNumberOffset + 1;
767 $tsparser->parentObject = $this;
768 return $tsparser->doSyntaxHighlight($all, $lineNumbers ? array($this->ext_lineNumberOffset + 1) : '', $syntaxHLBlockmode);
769 } else {
770 return $this->ext_formatTS($all, $lineNumbers, $comments, $crop);
771 }
772 }
773
774 /**
775 * Returns a new string of max. $chars length
776 * If the string is longer, it will be truncated and prepended with '...'
777 * $chars must be an integer of at least 4
778 *
779 * @param string $string
780 * @param int $chars
781 * @return string
782 */
783 public function ext_fixed_lgd($string, $chars)
784 {
785 if ($chars >= 4) {
786 if (strlen($string) > $chars) {
787 if (strlen($string) > 24 && substr($string, 0, 12) == '##' . $this->Cmarker . '_B##') {
788 return '##' . $this->Cmarker . '_B##' . GeneralUtility::fixed_lgd_cs(substr($string, 12, -12), ($chars - 3))
789 . '##' . $this->Cmarker . '_E##';
790 } else {
791 return GeneralUtility::fixed_lgd_cs($string, $chars - 3);
792 }
793 }
794 }
795 return $string;
796 }
797
798 /**
799 * @param int $lineNumber Line Number
800 * @param array $str
801 * @return string
802 */
803 public function ext_lnBreakPointWrap($lineNumber, $str)
804 {
805 return '<a href="#" id="line-' . $lineNumber . '" onClick="return brPoint(' . $lineNumber . ','
806 . ($this->ext_lineNumberOffset_mode == 'setup' ? 1 : 0) . ');">' . $str . '</a>';
807 }
808
809 /**
810 * @param string $input
811 * @param bool $ln
812 * @param bool $comments
813 * @param bool $crop
814 * @return string
815 */
816 public function ext_formatTS($input, $ln, $comments = true, $crop = false)
817 {
818 $cArr = explode(LF, $input);
819 $n = ceil(log10(count($cArr) + $this->ext_lineNumberOffset));
820 $lineNum = '';
821 foreach ($cArr as $k => $v) {
822 $lln = $k + $this->ext_lineNumberOffset + 1;
823 if ($ln) {
824 $lineNum = $this->ext_lnBreakPointWrap($lln, str_replace(' ', '&nbsp;', sprintf(('% ' . $n . 'd'), $lln))) . ': ';
825 }
826 $v = htmlspecialchars($v);
827 if ($crop) {
828 $v = $this->ext_fixed_lgd($v, $ln ? 71 : 77);
829 }
830 $cArr[$k] = $lineNum . str_replace(' ', '&nbsp;', $v);
831 $firstChar = substr(trim($v), 0, 1);
832 if ($firstChar == '[') {
833 $cArr[$k] = '<strong class="text-success">' . $cArr[$k] . '</strong>';
834 } elseif ($firstChar == '/' || $firstChar == '#') {
835 if ($comments) {
836 $cArr[$k] = '<span class="text-muted">' . $cArr[$k] . '</span>';
837 } else {
838 unset($cArr[$k]);
839 }
840 }
841 }
842 $output = implode($cArr, '<br />') . '<br />';
843 return $output;
844 }
845
846 /**
847 * @param int $id
848 * @param int $template_uid
849 * @return array|NULL Returns the template record or NULL if none was found
850 */
851 public function ext_getFirstTemplate($id, $template_uid = 0)
852 {
853 // Query is taken from the runThroughTemplates($theRootLine) function in the parent class.
854 if ((int)$id) {
855 $addC = $template_uid ? ' AND uid=' . (int)$template_uid : '';
856 $where = 'pid=' . (int)$id . $addC . BackendUtility::deleteClause('sys_template');
857 $res = $this->getDatabaseConnection()->exec_SELECTquery('*', 'sys_template', $where, '', 'sorting', '1');
858 $row = $this->getDatabaseConnection()->sql_fetch_assoc($res);
859 BackendUtility::workspaceOL('sys_template', $row);
860 $this->getDatabaseConnection()->sql_free_result($res);
861 // Returns the template row if found.
862 return $row;
863 }
864 return null;
865 }
866
867 /**
868 * @param int $id
869 * @return array[] Array of template records
870 */
871 public function ext_getAllTemplates($id)
872 {
873 if (!$id) {
874 return array();
875 }
876
877 // Query is taken from the runThroughTemplates($theRootLine) function in the parent class.
878 $res = $this->getDatabaseConnection()->exec_SELECTquery(
879 '*',
880 'sys_template',
881 'pid=' . (int)$id
882 . BackendUtility::deleteClause('sys_template'),
883 '',
884 'sorting'
885 );
886
887 $outRes = array();
888 while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
889 BackendUtility::workspaceOL('sys_template', $row);
890 if (is_array($row)) {
891 $outRes[] = $row;
892 }
893 }
894 $this->getDatabaseConnection()->sql_free_result($res);
895
896 return $outRes;
897 }
898
899 /**
900 * This function compares the flattened constants (default and all).
901 * Returns an array with the constants from the whole template which may be edited by the module.
902 *
903 * @param array $default
904 * @return array
905 */
906 public function ext_compareFlatSetups($default)
907 {
908 $editableComments = array();
909 foreach ($this->flatSetup as $const => $value) {
910 if (substr($const, -2) === '..' || !isset($this->flatSetup[$const . '..'])) {
911 continue;
912 }
913 $comment = trim($this->flatSetup[$const . '..']);
914 $c_arr = explode(LF, $comment);
915 foreach ($c_arr as $k => $v) {
916 $line = trim(preg_replace('/^[#\\/]*/', '', $v));
917 if (!$line) {
918 continue;
919 }
920 $parts = explode(';', $line);
921 foreach ($parts as $par) {
922 if (strstr($par, '=')) {
923 $keyValPair = explode('=', $par, 2);
924 switch (trim(strtolower($keyValPair[0]))) {
925 case 'type':
926 // Type:
927 $editableComments[$const]['type'] = trim($keyValPair[1]);
928 break;
929 case 'cat':
930 // List of categories.
931 $catSplit = explode('/', strtolower($keyValPair[1]));
932 $catSplit[0] = trim($catSplit[0]);
933 if (isset($this->categoryLabels[$catSplit[0]])) {
934 $catSplit[0] = $this->categoryLabels[$catSplit[0]];
935 }
936 $editableComments[$const]['cat'] = $catSplit[0];
937 // This is the subcategory. Must be a key in $this->subCategories[].
938 // catSplit[2] represents the search-order within the subcat.
939 $catSplit[1] = trim($catSplit[1]);
940 if ($catSplit[1] && isset($this->subCategories[$catSplit[1]])) {
941 $editableComments[$const]['subcat_name'] = $catSplit[1];
942 $editableComments[$const]['subcat'] = $this->subCategories[$catSplit[1]][1]
943 . '/' . $catSplit[1] . '/' . trim($catSplit[2]) . 'z';
944 } else {
945 $editableComments[$const]['subcat'] = 'x' . '/' . trim($catSplit[2]) . 'z';
946 }
947 break;
948 case 'label':
949 // Label
950 $editableComments[$const]['label'] = trim($keyValPair[1]);
951 break;
952 case 'customcategory':
953 // Custom category label
954 $customCategory = explode('=', $keyValPair[1], 2);
955 if (trim($customCategory[0])) {
956 $categoryKey = strtolower($customCategory[0]);
957 $this->categoryLabels[$categoryKey] = $this->getLanguageService()->sL($customCategory[1]);
958 }
959 break;
960 case 'customsubcategory':
961 // Custom subCategory label
962 $customSubcategory = explode('=', $keyValPair[1], 2);
963 if (trim($customSubcategory[0])) {
964 $subCategoryKey = strtolower($customSubcategory[0]);
965 $this->subCategories[$subCategoryKey][0] = $this->getLanguageService()->sL($customSubcategory[1]);
966 }
967 break;
968 }
969 }
970 }
971 }
972 if (isset($editableComments[$const])) {
973 $editableComments[$const]['name'] = $const;
974 $editableComments[$const]['value'] = trim($value);
975 if (isset($default[$const])) {
976 $editableComments[$const]['default_value'] = trim($default[$const]);
977 }
978 }
979 }
980 return $editableComments;
981 }
982
983 /**
984 * @param array $editConstArray
985 * @return void
986 */
987 public function ext_categorizeEditableConstants($editConstArray)
988 {
989 // Runs through the available constants and fills the $this->categories array with pointers and priority-info
990 foreach ($editConstArray as $constName => $constData) {
991 if (!$constData['type']) {
992 $constData['type'] = 'string';
993 }
994 $cats = explode(',', $constData['cat']);
995 // if = only one category, while allows for many. We have agreed on only one category is the most basic way...
996 foreach ($cats as $theCat) {
997 $theCat = trim($theCat);
998 if ($theCat) {
999 $this->categories[$theCat][$constName] = $constData['subcat'];
1000 }
1001 }
1002 }
1003 }
1004
1005 /**
1006 * @return array
1007 */
1008 public function ext_getCategoryLabelArray()
1009 {
1010 // Returns array used for labels in the menu.
1011 $retArr = array();
1012 foreach ($this->categories as $k => $v) {
1013 if (!empty($v)) {
1014 $retArr[$k] = strtoupper($k) . ' (' . count($v) . ')';
1015 }
1016 }
1017 return $retArr;
1018 }
1019
1020 /**
1021 * @param string $type
1022 * @return array
1023 */
1024 public function ext_getTypeData($type)
1025 {
1026 $retArr = array();
1027 $type = trim($type);
1028 if (!$type) {
1029 $retArr['type'] = 'string';
1030 } else {
1031 $m = strcspn($type, ' [');
1032 $retArr['type'] = strtolower(substr($type, 0, $m));
1033 $types = ['int' => 1, 'options' => 1, 'file' => 1, 'boolean' => 1, 'offset' => 1, 'user' => 1];
1034 if (isset($types[$retArr['type']])) {
1035 $p = trim(substr($type, $m));
1036 $reg = array();
1037 preg_match('/\\[(.*)\\]/', $p, $reg);
1038 $p = trim($reg[1]);
1039 if ($p) {
1040 $retArr['paramstr'] = $p;
1041 switch ($retArr['type']) {
1042 case 'int':
1043 if ($retArr['paramstr'][0] === '-') {
1044 $retArr['params'] = GeneralUtility::intExplode('-', substr($retArr['paramstr'], 1));
1045 $retArr['params'][0] = (int)('-' . $retArr['params'][0]);
1046 } else {
1047 $retArr['params'] = GeneralUtility::intExplode('-', $retArr['paramstr']);
1048 }
1049 $retArr['paramstr'] = $retArr['params'][0] . ' - ' . $retArr['params'][1];
1050 break;
1051 case 'options':
1052 $retArr['params'] = explode(',', $retArr['paramstr']);
1053 break;
1054 }
1055 }
1056 }
1057 }
1058 return $retArr;
1059 }
1060
1061 /**
1062 * @param string $category
1063 * @return void
1064 */
1065 public function ext_getTSCE_config($category)
1066 {
1067 $catConf = $this->setup['constants']['TSConstantEditor.'][$category . '.'];
1068 $out = array();
1069 if (is_array($catConf)) {
1070 foreach ($catConf as $key => $val) {
1071 switch ($key) {
1072 case 'image':
1073 $out['imagetag'] = $this->ext_getTSCE_config_image($catConf['image']);
1074 break;
1075 case 'description':
1076 case 'bulletlist':
1077 case 'header':
1078 $out[$key] = $val;
1079 break;
1080 default:
1081 if (MathUtility::canBeInterpretedAsInteger($key)) {
1082 $constRefs = explode(',', $val);
1083 foreach ($constRefs as $const) {
1084 $const = trim($const);
1085 if ($const) {
1086 $out['constants'][$const] .= '<span class="label label-danger">' . $key . '</span>';
1087 }
1088 }
1089 }
1090 }
1091 }
1092 }
1093 $this->helpConfig = $out;
1094 }
1095
1096 /**
1097 * @param string $imgConf
1098 * @return string
1099 */
1100 public function ext_getTSCE_config_image($imgConf)
1101 {
1102 $iFile = null;
1103 $tFile = null;
1104 if (substr($imgConf, 0, 4) == 'gfx/') {
1105 $iFile = $this->ext_localGfxPrefix . $imgConf;
1106 $tFile = $this->ext_localWebGfxPrefix . $imgConf;
1107 } elseif (substr($imgConf, 0, 4) == 'EXT:') {
1108 $iFile = GeneralUtility::getFileAbsFileName($imgConf);
1109 if ($iFile) {
1110 $tFile = '../' . PathUtility::stripPathSitePrefix($iFile);
1111 }
1112 }
1113 if ($iFile !== null) {
1114 $imageInfo = @getimagesize($iFile);
1115 return '<img src="' . $tFile . '" ' . $imageInfo[3] . '>';
1116 }
1117 return '';
1118 }
1119
1120 /**
1121 * @param array $params
1122 * @return array
1123 */
1124 public function ext_fNandV($params)
1125 {
1126 $fN = 'data[' . $params['name'] . ']';
1127 $idName = str_replace('.', '-', $params['name']);
1128 $fV = $params['value'];
1129 // Values entered from the constantsedit cannot be constants! 230502; removed \{ and set {
1130 if (preg_match('/^{[\\$][a-zA-Z0-9\\.]*}$/', trim($fV), $reg)) {
1131 $fV = '';
1132 }
1133 $fV = htmlspecialchars($fV);
1134 return array($fN, $fV, $params, $idName);
1135 }
1136
1137 /**
1138 * This functions returns the HTML-code that creates the editor-layout of the module.
1139 *
1140 * @param array $theConstants
1141 * @param string $category
1142 * @return string
1143 */
1144 public function ext_printFields($theConstants, $category)
1145 {
1146 reset($theConstants);
1147 $output = '';
1148 $subcat = '';
1149 if (is_array($this->categories[$category])) {
1150 $help = $this->helpConfig;
1151 if (!$this->doNotSortCategoriesBeforeMakingForm) {
1152 asort($this->categories[$category]);
1153 }
1154 /** @var IconFactory $iconFactory */
1155 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
1156 foreach ($this->categories[$category] as $name => $type) {
1157 $params = $theConstants[$name];
1158 if (is_array($params)) {
1159 if ($subcat != $params['subcat_name']) {
1160 $subcat = $params['subcat_name'];
1161 $subcat_name = $params['subcat_name'] ? $this->subCategories[$params['subcat_name']][0] : 'Others';
1162 $output .= '<h3>' . $subcat_name . '</h3>';
1163 }
1164 $label = $this->getLanguageService()->sL($params['label']);
1165 $label_parts = explode(':', $label, 2);
1166 if (count($label_parts) === 2) {
1167 $head = trim($label_parts[0]);
1168 $body = trim($label_parts[1]);
1169 } else {
1170 $head = trim($label_parts[0]);
1171 $body = '';
1172 }
1173 if (strlen($head) > 35) {
1174 if (!$body) {
1175 $body = $head;
1176 }
1177 $head = GeneralUtility::fixed_lgd_cs($head, 35);
1178 }
1179 $typeDat = $this->ext_getTypeData($params['type']);
1180 $p_field = '';
1181 $raname = substr(md5($params['name']), 0, 10);
1182 $aname = '\'' . $raname . '\'';
1183 list($fN, $fV, $params, $idName) = $this->ext_fNandV($params);
1184 $idName = htmlspecialchars($idName);
1185 $hint = '';
1186 switch ($typeDat['type']) {
1187 case 'int':
1188 case 'int+':
1189 if ($typeDat['paramstr']) {
1190 $hint = ' Range: ' . $typeDat['paramstr'];
1191 } elseif ($typeDat['type'] === 'int+') {
1192 $hint = ' Range: 0 - ';
1193 } else {
1194 $hint = ' (Integer)';
1195 }
1196
1197 $p_field =
1198 '<input class="form-control" id="' . $idName . '" type="text"'
1199 . ' name="' . $fN . '" value="' . $fV . '"' . ' onChange="uFormUrl(' . $aname . ')" />';
1200 break;
1201 case 'color':
1202 $colorNames = explode(',', ',' . $this->HTMLcolorList);
1203 $p_field = '';
1204 foreach ($colorNames as $val) {
1205 $sel = '';
1206 if ($val == strtolower($params['value'])) {
1207 $sel = ' selected';
1208 }
1209 $p_field .= '<option value="' . htmlspecialchars($val) . '"' . $sel . '>' . $val . '</option>';
1210 }
1211 $p_field = '<select class="form-control t3js-color-select" id="select-' . $idName . '" rel="' . $idName . '" name="C' . $fN . '" onChange="uFormUrl(' . $aname . ');"' . $this->getDocumentTemplate()->formWidth(7) . '>' . $p_field . '</select>';
1212 $p_field .= '<input class="form-control t3js-color-input" type="text" id="input-' . $idName . '" rel="' . $idName . '" name="' . $fN . '" value="' . $fV . '"' . $this->getDocumentTemplate()->formWidth(7) . ' onChange="uFormUrl(' . $aname . ')" />';
1213 break;
1214 case 'wrap':
1215 $wArr = explode('|', $fV);
1216 $p_field = '<input class="form-control" type="text" id="' . $idName . '" name="' . $fN . '" value="' . $wArr[0] . '"' . $this->getDocumentTemplate()->formWidth(29) . ' onChange="uFormUrl(' . $aname . ')" />';
1217 $p_field .= ' | ';
1218 $p_field .= '<input class="form-control" type="text" name="W' . $fN . '" value="' . $wArr[1] . '"' . $this->getDocumentTemplate()->formWidth(15) . ' onChange="uFormUrl(' . $aname . ')" />';
1219 break;
1220 case 'offset':
1221 $wArr = explode(',', $fV);
1222 $labels = GeneralUtility::trimExplode(',', $typeDat['paramstr']);
1223 $p_field = ($labels[0] ? $labels[0] : 'x') . ':<input type="text" name="' . $fN . '" value="' . $wArr[0] . '"' . $this->getDocumentTemplate()->formWidth(4) . ' onChange="uFormUrl(' . $aname . ')" />';
1224 $p_field .= ' , ';
1225 $p_field .= ($labels[1] ? $labels[1] : 'y') . ':<input type="text" name="W' . $fN . '" value="' . $wArr[1] . '"' . $this->getDocumentTemplate()->formWidth(4) . ' onChange="uFormUrl(' . $aname . ')" />';
1226 $labelsCount = count($labels);
1227 for ($aa = 2; $aa < $labelsCount; $aa++) {
1228 if ($labels[$aa]) {
1229 $p_field .= ' , ' . $labels[$aa] . ':<input type="text" name="W' . $aa . $fN . '" value="' . $wArr[$aa] . '"' . $this->getDocumentTemplate()->formWidth(4) . ' onChange="uFormUrl(' . $aname . ')" />';
1230 } else {
1231 $p_field .= '<input type="hidden" name="W' . $aa . $fN . '" value="' . $wArr[$aa] . '" />';
1232 }
1233 }
1234 break;
1235 case 'options':
1236 if (is_array($typeDat['params'])) {
1237 $p_field = '';
1238 foreach ($typeDat['params'] as $val) {
1239 $vParts = explode('=', $val, 2);
1240 $label = $vParts[0];
1241 $val = isset($vParts[1]) ? $vParts[1] : $vParts[0];
1242 // option tag:
1243 $sel = '';
1244 if ($val === $params['value']) {
1245 $sel = ' selected';
1246 }
1247 $p_field .= '<option value="' . htmlspecialchars($val) . '"' . $sel . '>' . $this->getLanguageService()->sL($label) . '</option>';
1248 }
1249 $p_field = '<select class="form-control" id="' . $idName . '" name="' . $fN . '" onChange="uFormUrl(' . $aname . ')">' . $p_field . '</select>';
1250 }
1251 break;
1252 case 'boolean':
1253 $sel = $fV ? 'checked' : '';
1254 $p_field =
1255 '<label class="btn btn-default btn-checkbox">'
1256 . '<input type="hidden" name="' . $fN . '" value="0" />'
1257 . '<input id="' . $idName . '" type="checkbox" name="' . $fN . '" value="' . ($typeDat['paramstr'] ? $typeDat['paramstr'] : 1) . '" ' . $sel . ' onClick="uFormUrl(' . $aname . ')" />'
1258 . '<span class="t3-icon fa"></span>'
1259 . '</label>';
1260 break;
1261 case 'comment':
1262 $sel = $fV ? 'checked' : '';
1263 $p_field =
1264 '<label class="btn btn-default btn-checkbox">'
1265 . '<input type="hidden" name="' . $fN . '" value="#" />'
1266 . '<input id="' . $idName . '" type="checkbox" name="' . $fN . '" value="" ' . $sel . ' onClick="uFormUrl(' . $aname . ')" />'
1267 . '<span class="t3-icon fa"></span>'
1268 . '</label>';
1269 break;
1270 case 'file':
1271 // extensionlist
1272 $extList = $typeDat['paramstr'];
1273 if ($extList === 'IMAGE_EXT') {
1274 $extList = $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'];
1275 }
1276 $p_field = '<option value="">(' . $extList . ')</option>';
1277 if (trim($params['value'])) {
1278 $val = $params['value'];
1279 $p_field .= '<option value=""></option>';
1280 $p_field .= '<option value="' . htmlspecialchars($val) . '" selected>' . $val . '</option>';
1281 }
1282 $p_field = '<select class="form-select" id="' . $idName . '" name="' . $fN . '" onChange="uFormUrl(' . $aname . ')">' . $p_field . '</select>';
1283 break;
1284 case 'user':
1285 $userFunction = $typeDat['paramstr'];
1286 $userFunctionParams = array('fieldName' => $fN, 'fieldValue' => $fV);
1287 $p_field = GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this);
1288 break;
1289 case 'small':
1290
1291 default:
1292 $p_field = '<input class="form-control" id="' . $idName . '" type="text" name="' . $fN . '" value="' . $fV . '"'
1293 . ' onChange="uFormUrl(' . $aname . ')" />';
1294 }
1295 // Define default names and IDs
1296 $userTyposcriptID = 'userTS-' . $idName;
1297 $defaultTyposcriptID = 'defaultTS-' . $idName;
1298 $checkboxName = 'check[' . $params['name'] . ']';
1299 $checkboxID = 'check-' . $idName;
1300 // Handle type=color specially
1301 if ($typeDat['type'] === 'color' && substr($params['value'], 0, 2) != '{$') {
1302 $appendedGroupAddon = '<span class="input-group-addon colorbox" id="colorbox-' . $idName . '" style="background-color:' . $params['value'] . ';"></span>';
1303 } else {
1304 $appendedGroupAddon = '';
1305 }
1306 $userTyposcriptStyle = '';
1307 $deleteIconHTML = '';
1308 $constantCheckbox = '';
1309 $constantDefaultRow = '';
1310 if (!$this->ext_dontCheckIssetValues) {
1311 // Set the default styling options
1312 if (isset($this->objReg[$params['name']])) {
1313 $checkboxValue = 'checked';
1314 $defaultTyposcriptStyle = 'style="display:none;"';
1315 } else {
1316 $checkboxValue = '';
1317 $userTyposcriptStyle = 'style="display:none;"';
1318 $defaultTyposcriptStyle = '';
1319 }
1320 $deleteTitle = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.deleteTitle', true);
1321 $deleteIcon = $iconFactory->getIcon('actions-edit-undo', Icon::SIZE_SMALL)->render();
1322 $deleteIconHTML =
1323 '<button type="button" class="btn btn-default t3js-toggle" data-toggle="undo" rel="' . $idName . '">'
1324 . '<span title="' . $deleteTitle . '" alt="' . $deleteTitle . '">'
1325 . $deleteIcon
1326 . '</span>'
1327 . '</button>';
1328 $editTitle = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.editTitle', true);
1329 $editIcon = $iconFactory->getIcon('actions-open', Icon::SIZE_SMALL)->render();
1330 $editIconHTML =
1331 '<button type="button" class="btn btn-default t3js-toggle" data-toggle="edit" rel="' . $idName . '">'
1332 . '<span title="' . $editTitle . '" alt="' . $editTitle . '">'
1333 . $editIcon
1334 . '</span>'
1335 . '</button>';
1336 $constantCheckbox = '<input type="hidden" name="' . $checkboxName . '" id="' . $checkboxID . '" value="' . $checkboxValue . '"/>';
1337 // If there's no default value for the field, use a static label.
1338 if (!$params['default_value']) {
1339 $params['default_value'] = '[Empty]';
1340 }
1341 $constantDefaultRow =
1342 '<div class="input-group defaultTS" id="' . $defaultTyposcriptID . '" ' . $defaultTyposcriptStyle . '>'
1343 . '<span class="input-group-btn">' . $editIconHTML . '</span>'
1344 . '<input class="form-control" type="text" placeholder="' . htmlspecialchars($params['default_value']) . '" readonly>'
1345 . $appendedGroupAddon
1346 . '</div>';
1347 }
1348 $constantEditRow =
1349 '<div class="input-group userTS" id="' . $userTyposcriptID . '" ' . $userTyposcriptStyle . '>'
1350 . '<span class="input-group-btn">' . $deleteIconHTML . '</span>'
1351 . $p_field
1352 . $appendedGroupAddon
1353 . '</div>';
1354 $constantLabel = '<label class="t3js-formengine-label"><span>' . htmlspecialchars($head) . '</span></label>';
1355 $constantName = '<span class="help-block">[' . $params['name'] . ']</span>';
1356 $constantDescription = $body ? '<p class="help-block">' . htmlspecialchars($body) . '</p>' : '';
1357 $constantData = '';
1358 if ($hint !== '') {
1359 $constantData .= '<span class="help-block">' . $hint . '</span>';
1360 }
1361 $constantData .=
1362 $constantCheckbox
1363 . $constantEditRow
1364 . $constantDefaultRow;
1365
1366 $output .=
1367 '<fieldset class="form-section">'
1368 . '<a name="' . $raname . '"></a>' . $help['constants'][$params['name']]
1369 . '<div class="form-group">'
1370 . $constantLabel . $constantName . $constantDescription . $constantData
1371 . '</div>'
1372 . '</fieldset>';
1373 } else {
1374 debug('Error. Constant did not exist. Should not happen.');
1375 }
1376 }
1377 }
1378 return '<div class="tstemplate-constanteditor">' . $output . '</div>';
1379 }
1380
1381 /***************************
1382 *
1383 * Processing input values
1384 *
1385 ***************************/
1386 /**
1387 * @param string $constants
1388 * @return void
1389 */
1390 public function ext_regObjectPositions($constants)
1391 {
1392 // This runs through the lines of the constants-field of the active template and registers the constants-names
1393 // and line positions in an array, $this->objReg
1394 $this->raw = explode(LF, $constants);
1395 $this->rawP = 0;
1396 // Resetting the objReg if the divider is found!!
1397 $this->objReg = array();
1398 $this->ext_regObjects('');
1399 }
1400
1401 /**
1402 * @param string $pre
1403 * @return void
1404 */
1405 public function ext_regObjects($pre)
1406 {
1407 // Works with regObjectPositions. "expands" the names of the TypoScript objects
1408 while (isset($this->raw[$this->rawP])) {
1409 $line = ltrim($this->raw[$this->rawP]);
1410 if (strstr($line, $this->edit_divider)) {
1411 // Resetting the objReg if the divider is found!!
1412 $this->objReg = array();
1413 }
1414 $this->rawP++;
1415 if ($line) {
1416 if ($line[0] === '[') {
1417 } elseif (strcspn($line, '}#/') != 0) {
1418 $varL = strcspn($line, ' {=<');
1419 $var = substr($line, 0, $varL);
1420 $line = ltrim(substr($line, $varL));
1421 switch ($line[0]) {
1422 case '=':
1423 $this->objReg[$pre . $var] = $this->rawP - 1;
1424 break;
1425 case '{':
1426 $this->ext_inBrace++;
1427 $this->ext_regObjects($pre . $var . '.');
1428 break;
1429 }
1430 $this->lastComment = '';
1431 } elseif ($line[0] === '}') {
1432 $this->lastComment = '';
1433 $this->ext_inBrace--;
1434 if ($this->ext_inBrace < 0) {
1435 $this->ext_inBrace = 0;
1436 } else {
1437 break;
1438 }
1439 }
1440 }
1441 }
1442 }
1443
1444 /**
1445 * @param string $key
1446 * @param string $var
1447 * @return void
1448 */
1449 public function ext_putValueInConf($key, $var)
1450 {
1451 // Puts the value $var to the TypoScript value $key in the current lines of the templates.
1452 // If the $key is not found in the template constants field, a new line is inserted in the bottom.
1453 $theValue = ' ' . trim($var);
1454 if (isset($this->objReg[$key])) {
1455 $lineNum = $this->objReg[$key];
1456 $parts = explode('=', $this->raw[$lineNum], 2);
1457 if (count($parts) === 2) {
1458 $parts[1] = $theValue;
1459 }
1460 $this->raw[$lineNum] = implode($parts, '=');
1461 } else {
1462 $this->raw[] = $key . ' =' . $theValue;
1463 }
1464 $this->changed = true;
1465 }
1466
1467 /**
1468 * @param string $key
1469 * @return void
1470 */
1471 public function ext_removeValueInConf($key)
1472 {
1473 // Removes the value in the configuration
1474 if (isset($this->objReg[$key])) {
1475 $lineNum = $this->objReg[$key];
1476 unset($this->raw[$lineNum]);
1477 }
1478 $this->changed = true;
1479 }
1480
1481 /**
1482 * @param array $arr
1483 * @param array $settings
1484 * @return array
1485 */
1486 public function ext_depthKeys($arr, $settings)
1487 {
1488 $tsbrArray = array();
1489 foreach ($arr as $theK => $theV) {
1490 $theKeyParts = explode('.', $theK);
1491 $depth = '';
1492 $c = count($theKeyParts);
1493 $a = 0;
1494 foreach ($theKeyParts as $p) {
1495 $a++;
1496 $depth .= ($depth ? '.' : '') . $p;
1497 $tsbrArray[$depth] = $c == $a ? $theV : 1;
1498 }
1499 }
1500 // Modify settings
1501 foreach ($tsbrArray as $theK => $theV) {
1502 if ($theV) {
1503 $settings[$theK] = 1;
1504 } else {
1505 unset($settings[$theK]);
1506 }
1507 }
1508 return $settings;
1509 }
1510
1511 /**
1512 * Proces input
1513 *
1514 * @param array $http_post_vars
1515 * @param array $http_post_files (not used anymore)
1516 * @param array $theConstants
1517 * @param array $tplRow Not used
1518 * @return void
1519 */
1520 public function ext_procesInput($http_post_vars, $http_post_files, $theConstants, $tplRow)
1521 {
1522 $data = $http_post_vars['data'];
1523 $check = $http_post_vars['check'];
1524 $Wdata = $http_post_vars['Wdata'];
1525 $W2data = $http_post_vars['W2data'];
1526 $W3data = $http_post_vars['W3data'];
1527 $W4data = $http_post_vars['W4data'];
1528 $W5data = $http_post_vars['W5data'];
1529 if (is_array($data)) {
1530 foreach ($data as $key => $var) {
1531 if (isset($theConstants[$key])) {
1532 // If checkbox is set, update the value
1533 if ($this->ext_dontCheckIssetValues || isset($check[$key])) {
1534 // Exploding with linebreak, just to make sure that no multiline input is given!
1535 list($var) = explode(LF, $var);
1536 $typeDat = $this->ext_getTypeData($theConstants[$key]['type']);
1537 switch ($typeDat['type']) {
1538 case 'int':
1539 if ($typeDat['paramstr']) {
1540 $var = MathUtility::forceIntegerInRange($var, $typeDat['params'][0], $typeDat['params'][1]);
1541 } else {
1542 $var = (int)$var;
1543 }
1544 break;
1545 case 'int+':
1546 $var = max(0, (int)$var);
1547 break;
1548 case 'color':
1549 $col = array();
1550 if ($var && !GeneralUtility::inList($this->HTMLcolorList, strtolower($var))) {
1551 $var = preg_replace('/[^A-Fa-f0-9]*/', '', $var);
1552 $useFulHex = strlen($var) > 3;
1553 $col[] = hexdec($var[0]);
1554 $col[] = hexdec($var[1]);
1555 $col[] = hexdec($var[2]);
1556 if ($useFulHex) {
1557 $col[] = hexdec($var[3]);
1558 $col[] = hexdec($var[4]);
1559 $col[] = hexdec($var[5]);
1560 }
1561 $var = substr(('0' . dechex($col[0])), -1) . substr(('0' . dechex($col[1])), -1) . substr(('0' . dechex($col[2])), -1);
1562 if ($useFulHex) {
1563 $var .= substr(('0' . dechex($col[3])), -1) . substr(('0' . dechex($col[4])), -1) . substr(('0' . dechex($col[5])), -1);
1564 }
1565 $var = '#' . strtoupper($var);
1566 }
1567 break;
1568 case 'comment':
1569 if ($var) {
1570 $var = '#';
1571 } else {
1572 $var = '';
1573 }
1574 break;
1575 case 'wrap':
1576 if (isset($Wdata[$key])) {
1577 $var .= '|' . $Wdata[$key];
1578 }
1579 break;
1580 case 'offset':
1581 if (isset($Wdata[$key])) {
1582 $var = (int)$var . ',' . (int)$Wdata[$key];
1583 if (isset($W2data[$key])) {
1584 $var .= ',' . (int)$W2data[$key];
1585 if (isset($W3data[$key])) {
1586 $var .= ',' . (int)$W3data[$key];
1587 if (isset($W4data[$key])) {
1588 $var .= ',' . (int)$W4data[$key];
1589 if (isset($W5data[$key])) {
1590 $var .= ',' . (int)$W5data[$key];
1591 }
1592 }
1593 }
1594 }
1595 }
1596 break;
1597 case 'boolean':
1598 if ($var) {
1599 $var = $typeDat['paramstr'] ? $typeDat['paramstr'] : 1;
1600 }
1601 break;
1602 }
1603 if ($this->ext_printAll || (string)$theConstants[$key]['value'] !== (string)$var) {
1604 // Put value in, if changed.
1605 $this->ext_putValueInConf($key, $var);
1606 }
1607 // Remove the entry because it has been "used"
1608 unset($check[$key]);
1609 } else {
1610 $this->ext_removeValueInConf($key);
1611 }
1612 }
1613 }
1614 }
1615 // Remaining keys in $check indicates fields that are just clicked "on" to be edited.
1616 // Therefore we get the default value and puts that in the template as a start...
1617 if (!$this->ext_dontCheckIssetValues && is_array($check)) {
1618 foreach ($check as $key => $var) {
1619 if (isset($theConstants[$key])) {
1620 $dValue = $theConstants[$key]['default_value'];
1621 $this->ext_putValueInConf($key, $dValue);
1622 }
1623 }
1624 }
1625 }
1626
1627 /**
1628 * @param int $id
1629 * @param string $perms_clause
1630 * @return array
1631 */
1632 public function ext_prevPageWithTemplate($id, $perms_clause)
1633 {
1634 $rootLine = BackendUtility::BEgetRootLine($id, $perms_clause ? ' AND ' . $perms_clause : '');
1635 foreach ($rootLine as $p) {
1636 if ($this->ext_getFirstTemplate($p['uid'])) {
1637 return $p;
1638 }
1639 }
1640 return array();
1641 }
1642
1643 /**
1644 * @return array
1645 */
1646 protected function getRootLine()
1647 {
1648 return isset($GLOBALS['rootLine']) ? $GLOBALS['rootLine'] : array();
1649 }
1650
1651 /**
1652 * @return DatabaseConnection
1653 */
1654 protected function getDatabaseConnection()
1655 {
1656 return $GLOBALS['TYPO3_DB'];
1657 }
1658
1659 /**
1660 * @return LanguageService
1661 */
1662 protected function getLanguageService()
1663 {
1664 return $GLOBALS['LANG'];
1665 }
1666
1667 /**
1668 * @return DocumentTemplate
1669 */
1670 protected function getDocumentTemplate()
1671 {
1672 return $GLOBALS['TBE_TEMPLATE'];
1673 }
1674 }