‪TYPO3CMS  11.5
ConditionMatcher.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 
31 {
35  protected ‪$context;
36 
37  public function ‪__construct(‪Context ‪$context = null, int ‪$pageId = null, array $rootLine = null)
38  {
39  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
40  $this->pageId = ‪$pageId ?? $this->‪determinePageId();
41  if ($rootLine === null) {
42  $rootLine = BackendUtility::BEgetRootLine($this->pageId, '', true);
43  ksort($rootLine);
44  }
45  $this->rootline = $rootLine;
47  }
48 
49  protected function ‪updateExpressionLanguageVariables(): void
50  {
51  $page = BackendUtility::getRecord('pages', $this->pageId ?? $this->‪determinePageId()) ?: [];
52 
53  $treeLevel = $this->rootline ? count($this->rootline) - 1 : 0;
54  $tree = new \stdClass();
55  $tree->level = $treeLevel;
56  $tree->rootLine = ‪$this->rootline;
57  $tree->rootLineIds = array_column($this->rootline, 'uid');
58  $tree->rootLineParentIds = array_slice(array_column($this->rootline, 'pid'), 2);
59  // Reverse rootline here to look for first match "up" in rootline starting with deepest nested page.
60  $tree->pagelayout = GeneralUtility::makeInstance(PageLayoutResolver::class)->getLayoutForPage($page, array_reverse($this->rootline));
61 
62  $backendUserAspect = $this->context->getAspect('backend.user');
63  $backend = new \stdClass();
64  $backend->user = new \stdClass();
65  $backend->user->isAdmin = $backendUserAspect->get('isAdmin');
66  $backend->user->isLoggedIn = $backendUserAspect->get('isLoggedIn');
67  $backend->user->userId = $backendUserAspect->get('id');
68  $backend->user->userGroupList = implode(',', $backendUserAspect->get('groupIds'));
69  $backend->user->userGroupIds = $backendUserAspect->get('groupIds');
70 
71  $workspaceAspect = $this->context->getAspect('workspace');
72  $workspace = new \stdClass();
73  $workspace->workspaceId = $workspaceAspect->get('id');
74  $workspace->isLive = $workspaceAspect->get('isLive');
75  $workspace->isOffline = $workspaceAspect->get('isOffline');
76 
77  $this->expressionLanguageResolverVariables = [
78  'tree' => $tree,
79  'backend' => $backend,
80  'workspace' => $workspace,
81  'page' => $page,
82  ];
83  }
84 
93  private function ‪determinePageId(): int
94  {
95  ‪$pageId = 0;
96  $editStatement = GeneralUtility::_GP('edit');
97  $commandStatement = GeneralUtility::_GP('cmd');
98  // Determine id from module that was called with an id:
99  if ($id = (int)GeneralUtility::_GP('id')) {
100  ‪$pageId = $id;
101  } elseif (is_array($editStatement)) {
102  $table = key($editStatement);
103  $uidAndAction = current($editStatement);
104  $uid = (int)key($uidAndAction);
105  $action = current($uidAndAction);
106  if ($action === 'edit') {
107  ‪$pageId = $this->‪getPageIdByRecord($table, $uid);
108  } elseif ($action === 'new') {
109  ‪$pageId = $this->‪getPageIdByRecord($table, $uid, true);
110  }
111  } elseif (is_array($commandStatement)) {
112  $table = key($commandStatement);
113  $uidActionAndTarget = current($commandStatement);
114  $uid = (int)key($uidActionAndTarget);
115  $actionAndTarget = current($uidActionAndTarget);
116  $action = key($actionAndTarget);
117  $target = current($actionAndTarget);
118  if ($action === 'delete') {
119  ‪$pageId = $this->‪getPageIdByRecord($table, $uid);
120  } elseif ($action === 'copy' || $action === 'move') {
121  ‪$pageId = $this->‪getPageIdByRecord($table, (int)($target['target'] ?? $target), true);
122  }
123  }
124  return ‪$pageId;
125  }
126 
135  private function ‪getPageIdByRecord(string $table, int $id, bool $ignoreTable = false): int
136  {
137  ‪$pageId = 0;
138  if ($table && $id) {
139  if (($ignoreTable || $table === 'pages') && $id >= 0) {
140  ‪$pageId = $id;
141  } else {
142  $record = BackendUtility::getRecordWSOL($table, abs($id), '*', '', false);
143  ‪$pageId = (int)($record['pid'] ?? 0);
144  }
145  }
146  return ‪$pageId;
147  }
148 }
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\updateExpressionLanguageVariables
‪updateExpressionLanguageVariables()
Definition: ConditionMatcher.php:48
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching
Definition: ConditionMatcher.php:16
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\determinePageId
‪int determinePageId()
Definition: ConditionMatcher.php:92
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
Definition: ConditionMatcher.php:31
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\$pageId
‪int $pageId
Definition: AbstractConditionMatcher.php:43
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\$context
‪Context $context
Definition: ConditionMatcher.php:34
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher
Definition: AbstractConditionMatcher.php:36
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\$rootline
‪array $rootline
Definition: AbstractConditionMatcher.php:49
‪TYPO3\CMS\Frontend\Page\PageLayoutResolver
Definition: PageLayoutResolver.php:32
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\__construct
‪__construct(Context $context=null, int $pageId=null, array $rootLine=null)
Definition: ConditionMatcher.php:36
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\initializeExpressionLanguageResolver
‪initializeExpressionLanguageResolver()
Definition: AbstractConditionMatcher.php:73
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getPageIdByRecord
‪int getPageIdByRecord(string $table, int $id, bool $ignoreTable=false)
Definition: ConditionMatcher.php:134
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50