‪TYPO3CMS  ‪main
PresetRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
24 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
28 
35 {
36  protected const ‪PRESET_TABLE = 'tx_impexp_presets';
37  protected QueryBuilder ‪$queryBuilder;
40 
41  public function ‪__construct(
42  ‪ConnectionPool $connectionPool,
44  ) {
45  $this->queryBuilder = $connectionPool->‪getQueryBuilderForTable(self::PRESET_TABLE);
46  $this->connection = $connectionPool->‪getConnectionForTable(self::PRESET_TABLE);
47  $this->context = ‪$context;
48  }
49 
50  public function ‪getPresets(int $pageId): array
51  {
52  $backendUser = $this->‪getBackendUser();
53  $queryBuilder = ‪$this->queryBuilder;
54  ‪$queryBuilder->select('*')
55  ->from(self::PRESET_TABLE)
56  ->where(
57  ‪$queryBuilder->expr()->or(
58  ‪$queryBuilder->expr()->gt('public', ‪$queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)),
59  ‪$queryBuilder->expr()->eq('user_uid', ‪$queryBuilder->createNamedParameter($backendUser->user['uid'], ‪Connection::PARAM_INT))
60  )
61  );
62  if ($pageId) {
63  ‪$queryBuilder->andWhere(
64  ‪$queryBuilder->expr()->or(
65  ‪$queryBuilder->expr()->eq('item_uid', ‪$queryBuilder->createNamedParameter($pageId, ‪Connection::PARAM_INT)),
66  ‪$queryBuilder->expr()->eq('item_uid', ‪$queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT))
67  )
68  );
69  }
70  $presets = ‪$queryBuilder->executeQuery();
71  // @todo: View should handle default option and data details parsing, not repository.
72  $options = [''];
73  while ($presetCfg = $presets->fetchAssociative()) {
74  $options[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
75  . ($presetCfg['public'] ? ' [Public]' : '')
76  . ((int)$presetCfg['user_uid'] === (int)$backendUser->user['uid'] ? ' [Own]' : '');
77  }
78  return $options;
79  }
80 
81  public function ‪createPreset(array $data): void
82  {
83  $backendUser = $this->‪getBackendUser();
84  $timestamp = $this->context->getPropertyFromAspect('date', 'timestamp');
85  $this->connection->insert(
86  self::PRESET_TABLE,
87  [
88  'user_uid' => $backendUser->user['uid'],
89  'public' => $data['preset']['public'],
90  'title' => $data['preset']['title'],
91  'item_uid' => (int)$data['pagetree']['id'],
92  'preset_data' => serialize($data),
93  'tstamp' => $timestamp,
94  'crdate' => $timestamp,
95  ],
96  ['preset_data' => ‪Connection::PARAM_LOB]
97  );
98  }
99 
104  public function ‪updatePreset(int ‪$uid, array $data): void
105  {
106  $backendUser = $this->‪getBackendUser();
107  $preset = $this->‪getPreset($uid);
108  if (!($backendUser->isAdmin() || (int)$preset['user_uid'] === (int)$backendUser->user['uid'])) {
110  'ERROR: You were not the owner of the preset so you could not delete it.',
111  1604584766
112  );
113  }
114  $timestamp = $this->context->getPropertyFromAspect('date', 'timestamp');
115  $this->connection->update(
116  self::PRESET_TABLE,
117  [
118  'public' => $data['preset']['public'],
119  'title' => $data['preset']['title'],
120  'item_uid' => $data['pagetree']['id'],
121  'preset_data' => serialize($data),
122  'tstamp' => $timestamp,
123  ],
124  ['uid' => ‪$uid],
125  ['preset_data' => ‪Connection::PARAM_LOB]
126  );
127  }
128 
132  public function ‪loadPreset(int ‪$uid): array
133  {
134  $preset = $this->‪getPreset($uid);
135  // @todo: Move this to a json array instead.
136  $presetData = unserialize($preset['preset_data'], ['allowed_classes' => false]);
137  if (!is_array($presetData)) {
138  throw new ‪MalformedPresetException(
139  'ERROR: No configuration data found in preset record!',
140  1604608922
141  );
142  }
143  return $presetData;
144  }
145 
150  public function ‪deletePreset(int ‪$uid): void
151  {
152  $backendUser = $this->‪getBackendUser();
153  $preset = $this->‪getPreset($uid);
154  if (!($backendUser->isAdmin() || (int)$preset['user_uid'] === (int)$backendUser->user['uid'])) {
156  'ERROR: You were not the owner of the preset so you could not delete it.',
157  1604564346
158  );
159  }
160  $this->connection->delete(
161  self::PRESET_TABLE,
162  ['uid' => ‪$uid]
163  );
164  }
165 
171  protected function ‪getPreset(int ‪$uid): array
172  {
174  $preset = ‪$queryBuilder->select('*')
175  ->from(self::PRESET_TABLE)
176  ->where(‪$queryBuilder->expr()->eq('uid', ‪$queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT)))
177  ->executeQuery()
178  ->fetchAssociative();
179  if (!is_array($preset)) {
180  throw new ‪PresetNotFoundException(
181  'ERROR: No valid preset #' . ‪$uid . ' found.',
182  1604608843
183  );
184  }
185  return $preset;
186  }
187 
189  {
190  return ‪$GLOBALS['BE_USER'];
191  }
192 }
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\updatePreset
‪updatePreset(int $uid, array $data)
Definition: PresetRepository.php:104
‪TYPO3\CMS\Impexp\Exception\MalformedPresetException
Definition: MalformedPresetException.php:27
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\PRESET_TABLE
‪const PRESET_TABLE
Definition: PresetRepository.php:36
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPreset
‪getPreset(int $uid)
Definition: PresetRepository.php:171
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getBackendUser
‪getBackendUser()
Definition: PresetRepository.php:188
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\$queryBuilder
‪QueryBuilder $queryBuilder
Definition: PresetRepository.php:37
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository
Definition: PresetRepository.php:35
‪TYPO3\CMS\Impexp\Domain\Repository
Definition: PresetRepository.php:18
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\createPreset
‪createPreset(array $data)
Definition: PresetRepository.php:81
‪TYPO3\CMS\Impexp\Exception\InsufficientUserPermissionsException
Definition: InsufficientUserPermissionsException.php:27
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPresets
‪getPresets(int $pageId)
Definition: PresetRepository.php:50
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\deletePreset
‪deletePreset(int $uid)
Definition: PresetRepository.php:150
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\$context
‪Context $context
Definition: PresetRepository.php:39
‪TYPO3\CMS\Impexp\Exception\PresetNotFoundException
Definition: PresetNotFoundException.php:27
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\loadPreset
‪loadPreset(int $uid)
Definition: PresetRepository.php:132
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Database\ConnectionPool\getQueryBuilderForTable
‪getQueryBuilderForTable(string $tableName)
Definition: ConnectionPool.php:305
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\__construct
‪__construct(ConnectionPool $connectionPool, Context $context)
Definition: PresetRepository.php:41
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool\getConnectionForTable
‪getConnectionForTable(string $tableName)
Definition: ConnectionPool.php:82
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\$connection
‪Connection $connection
Definition: PresetRepository.php:38
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:62