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