‪TYPO3CMS  ‪main
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;
24 
34 {
38  protected ‪$context;
39 
44  public function ‪__construct(‪Context ‪$context = null, int ‪$pageId = null, array $rootLine = null)
45  {
46  trigger_error(
47  'The BE condition matcher has been deprecated and will be removed with TYPO3 v13. This logic' .
48  ' has been integrated into the new TypoScript parser structure, see IncludeTreeConditionMatcherVisitor',
49  E_USER_DEPRECATED
50  );
51  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
52  $this->pageId = ‪$pageId ?? $this->‪determinePageId();
53  if ($rootLine === null) {
54  $rootLine = BackendUtility::BEgetRootLine($this->pageId, '', true);
55  ksort($rootLine);
56  }
57  $this->rootline = $rootLine;
59  }
60 
61  protected function ‪updateExpressionLanguageVariables(): void
62  {
63  $page = BackendUtility::getRecord('pages', $this->pageId ?? $this->‪determinePageId()) ?: [];
64 
65  $treeLevel = $this->rootline ? count($this->rootline) - 1 : 0;
66  $tree = new \stdClass();
67  $tree->level = $treeLevel;
68  $tree->rootLine = ‪$this->rootline;
69  $tree->rootLineIds = array_column($this->rootline, 'uid');
70  $tree->rootLineParentIds = array_slice(array_column($this->rootline, 'pid'), 2);
71  // Reverse rootline here to look for first match "up" in rootline starting with deepest nested page.
72  $tree->pagelayout = GeneralUtility::makeInstance(PageLayoutResolver::class)->getLayoutForPage($page, array_reverse($this->rootline));
73 
74  $backendUserAspect = $this->context->getAspect('backend.user');
75  $backend = new \stdClass();
76  $backend->user = new \stdClass();
77  $backend->user->isAdmin = $backendUserAspect->get('isAdmin');
78  $backend->user->isLoggedIn = $backendUserAspect->get('isLoggedIn');
79  $backend->user->userId = $backendUserAspect->get('id');
80  $backend->user->userGroupList = implode(',', $backendUserAspect->get('groupIds'));
81  $backend->user->userGroupIds = $backendUserAspect->get('groupIds');
82 
83  $workspaceAspect = $this->context->getAspect('workspace');
84  $workspace = new \stdClass();
85  $workspace->workspaceId = $workspaceAspect->get('id');
86  $workspace->isLive = $workspaceAspect->get('isLive');
87  $workspace->isOffline = $workspaceAspect->get('isOffline');
88 
89  $this->expressionLanguageResolverVariables = [
90  'tree' => $tree,
91  'backend' => $backend,
92  'workspace' => $workspace,
93  'page' => $page,
94  'request' => new ‪RequestWrapper(‪$GLOBALS['TYPO3_REQUEST'] ?? null),
95  'date' => GeneralUtility::makeInstance(Context::class)->getAspect('date'),
96  ];
97  }
98 
107  private function ‪determinePageId(): int
108  {
109  ‪$pageId = 0;
110  $editStatement = ‪GeneralUtility::_GP('edit');
111  $commandStatement = ‪GeneralUtility::_GP('cmd');
112  // Determine id from module that was called with an id:
113  if ($id = (int)‪GeneralUtility::_GP('id')) {
114  ‪$pageId = $id;
115  } elseif (is_array($editStatement)) {
116  $table = key($editStatement);
117  $uidAndAction = current($editStatement);
118  ‪$uid = (int)key($uidAndAction);
119  $action = current($uidAndAction);
120  if ($action === 'edit') {
121  ‪$pageId = $this->‪getPageIdByRecord($table, ‪$uid);
122  } elseif ($action === 'new') {
123  ‪$pageId = $this->‪getPageIdByRecord($table, ‪$uid, true);
124  }
125  } elseif (is_array($commandStatement)) {
126  $table = key($commandStatement);
127  $uidActionAndTarget = current($commandStatement);
128  ‪$uid = (int)key($uidActionAndTarget);
129  $actionAndTarget = current($uidActionAndTarget);
130  $action = key($actionAndTarget);
131  $target = current($actionAndTarget);
132  if ($action === 'delete') {
133  ‪$pageId = $this->‪getPageIdByRecord($table, ‪$uid);
134  } elseif ($action === 'copy' || $action === 'move') {
135  ‪$pageId = $this->‪getPageIdByRecord($table, (int)($target['target'] ?? $target), true);
136  }
137  }
138  return ‪$pageId;
139  }
140 
149  private function ‪getPageIdByRecord(string $table, int $id, bool $ignoreTable = false): int
150  {
151  ‪$pageId = 0;
152  if ($table && $id) {
153  if (($ignoreTable || $table === 'pages') && $id >= 0) {
154  ‪$pageId = $id;
155  } else {
156  ‪$record = BackendUtility::getRecordWSOL($table, abs($id), '*', '', false);
157  ‪$pageId = (int)(‪$record['pid'] ?? 0);
158  }
159  }
160  return ‪$pageId;
161  }
162 }
‪TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper
Definition: RequestWrapper.php:35
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\updateExpressionLanguageVariables
‪updateExpressionLanguageVariables()
Definition: ConditionMatcher.php:60
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching
Definition: ConditionMatcher.php:16
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\determinePageId
‪int determinePageId()
Definition: ConditionMatcher.php:106
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
Definition: ConditionMatcher.php:34
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\$pageId
‪int $pageId
Definition: AbstractConditionMatcher.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility\_GP
‪static mixed _GP($var)
Definition: GeneralUtility.php:107
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:55
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\$context
‪Context $context
Definition: ConditionMatcher.php:37
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher
Definition: AbstractConditionMatcher.php:37
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\$rootline
‪array $rootline
Definition: AbstractConditionMatcher.php:54
‪TYPO3\CMS\Frontend\Page\PageLayoutResolver
Definition: PageLayoutResolver.php:32
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\__construct
‪__construct(Context $context=null, int $pageId=null, array $rootLine=null)
Definition: ConditionMatcher.php:43
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\initializeExpressionLanguageResolver
‪initializeExpressionLanguageResolver()
Definition: AbstractConditionMatcher.php:78
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getPageIdByRecord
‪int getPageIdByRecord(string $table, int $id, bool $ignoreTable=false)
Definition: ConditionMatcher.php:148
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51