2c1c9f66ad1c4d3bf642a0449066cf9b5b2f77a6
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / Query / Restriction / FrontendWorkspaceRestriction.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Database\Query\Restriction;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
19 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
20 use TYPO3\CMS\Core\Versioning\VersionState;
21
22 /**
23 * Restriction to filter records for fronted workspaces preview
24 */
25 class FrontendWorkspaceRestriction implements QueryRestrictionInterface
26 {
27 /**
28 * @var int
29 */
30 protected $workspaceId;
31
32 /**
33 * @var bool
34 */
35 protected $includeRowsForWorkspacePreview;
36
37 /**
38 * @var bool
39 */
40 protected $enforceLiveRowsOnly;
41
42 /**
43 * @param int $workspaceId (PageRepository::$versioningWorkspaceId property)
44 * @param bool $includeRowsForWorkspacePreview (PageRepository::$versioningPreview property)
45 * @param bool $enforceLiveRowsOnly (!$noVersionPreview argument from PageRepository::enableFields()) This is ONLY for use in PageRepository class and most likely will be removed
46 */
47 public function __construct(int $workspaceId = null, bool $includeRowsForWorkspacePreview = null, bool $enforceLiveRowsOnly = true)
48 {
49 $this->workspaceId = $workspaceId === null ? $GLOBALS['TSFE']->sys_page->versioningWorkspaceId : $workspaceId;
50 $this->includeRowsForWorkspacePreview = $includeRowsForWorkspacePreview === null ? $GLOBALS['TSFE']->sys_page->versioningPreview : $includeRowsForWorkspacePreview;
51 $this->enforceLiveRowsOnly = $enforceLiveRowsOnly;
52 }
53
54 /**
55 * Main method to build expressions for given tables
56 * Evaluates the ctrl/versioningWS flag of the table and adds various workspace related restrictions if set
57 *
58 * @param array $queriedTables Array of tables, where array key is table alias and value is a table name
59 * @param ExpressionBuilder $expressionBuilder Expression builder instance to add restrictions with
60 * @return CompositeExpression The result of query builder expression(s)
61 */
62 public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression
63 {
64 $constraints = [];
65 foreach ($queriedTables as $tableAlias => $tableName) {
66 $workspaceEnabled = $GLOBALS['TCA'][$tableName]['ctrl']['versioningWS'] ?? null;
67 if (!empty($workspaceEnabled)) {
68 if (!$this->includeRowsForWorkspacePreview) {
69 // Filter out placeholder records (new/moved/deleted items)
70 // in case we are NOT in a versioning preview (That means we are online!)
71 $constraints[] = $expressionBuilder->lte(
72 $tableAlias . '.t3ver_state',
73 // Trigger __toString(), then cast int
74 (int)(string)new VersionState(VersionState::DEFAULT_STATE)
75 );
76 } elseif ($tableName !== 'pages') {
77 // Show only records of the live and current workspace in case we are in a versioning preview
78 $constraints[] = $expressionBuilder->orX(
79 $expressionBuilder->eq($tableAlias . '.t3ver_wsid', 0),
80 $expressionBuilder->eq($tableAlias . '.t3ver_wsid', (int)$this->workspaceId)
81 );
82 }
83 // Filter out versioned records
84 if ($this->enforceLiveRowsOnly) {
85 $constraints[] = $expressionBuilder->neq($tableAlias . '.pid', -1);
86 }
87 }
88 }
89 return $expressionBuilder->andX(...$constraints);
90 }
91 }