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