‪TYPO3CMS  11.5
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;
29 
36 {
40  protected ‪$table = 'tx_impexp_presets';
41 
46  public function ‪getPresets(int $pageId): array
47  {
48  $queryBuilder = $this->‪createQueryBuilder();
49 
50  $queryBuilder->select('*')
51  ->from($this->table)
52  ->where(
53  $queryBuilder->expr()->orX(
54  $queryBuilder->expr()->gt(
55  'public',
56  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
57  ),
58  $queryBuilder->expr()->eq(
59  'user_uid',
60  $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], ‪Connection::PARAM_INT)
61  )
62  )
63  );
64 
65  if ($pageId) {
66  $queryBuilder->andWhere(
67  $queryBuilder->expr()->orX(
68  $queryBuilder->expr()->eq(
69  'item_uid',
70  $queryBuilder->createNamedParameter($pageId, ‪Connection::PARAM_INT)
71  ),
72  $queryBuilder->expr()->eq(
73  'item_uid',
74  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
75  )
76  )
77  );
78  }
79 
80  $presets = $queryBuilder->executeQuery();
81 
82  $options = [''];
83  while ($presetCfg = $presets->fetchAssociative()) {
84  $options[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
85  . ($presetCfg['public'] ? ' [Public]' : '')
86  . ((int)$presetCfg['user_uid'] === (int)$this->‪getBackendUser()->user['uid'] ? ' [Own]' : '');
87  }
88  return $options;
89  }
90 
98  protected function ‪getPreset(int $uid): array
99  {
100  $queryBuilder = $this->‪createQueryBuilder();
101 
102  $preset = $queryBuilder->select('*')
103  ->from($this->table)
104  ->where(
105  $queryBuilder->expr()->eq(
106  'uid',
107  $queryBuilder->createNamedParameter($uid, ‪Connection::PARAM_INT)
108  )
109  )
110  ->executeQuery()
111  ->fetchAssociative();
112 
113  if (!is_array($preset)) {
114  throw new ‪PresetNotFoundException(
115  'ERROR: No valid preset #' . $uid . ' found.',
116  1604608843
117  );
118  }
119 
120  return $preset;
121  }
122 
123  public function ‪createPreset(array $data): void
124  {
125  $timestamp = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
126  $connection = $this->‪createConnection();
127  $connection->insert(
128  $this->table,
129  [
130  'user_uid' => $this->‪getBackendUser()->user['uid'],
131  'public' => $data['preset']['public'],
132  'title' => $data['preset']['title'],
133  'item_uid' => (int)$data['pagetree']['id'],
134  'preset_data' => serialize($data),
135  'tstamp' => $timestamp,
136  'crdate' => $timestamp,
137  ],
138  ['preset_data' => ‪Connection::PARAM_LOB]
139  );
140  }
141 
147  public function ‪updatePreset(int $uid, array $data): void
148  {
149  $preset = $this->‪getPreset($uid);
150 
151  if (!($this->‪getBackendUser()->isAdmin() || (int)$preset['user_uid'] === (int)$this->‪getBackendUser()->user['uid'])) {
153  'ERROR: You were not the owner of the preset so you could not delete it.',
154  1604584766
155  );
156  }
157 
158  $timestamp = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
159 
160  $connection = $this->‪createConnection();
161  $connection->update(
162  $this->table,
163  [
164  'public' => $data['preset']['public'],
165  'title' => $data['preset']['title'],
166  'item_uid' => $data['pagetree']['id'],
167  'preset_data' => serialize($data),
168  'tstamp' => $timestamp,
169  ],
170  ['uid' => $uid],
171  ['preset_data' => ‪Connection::PARAM_LOB]
172  );
173  }
174 
180  public function ‪loadPreset(int $uid): array
181  {
182  $preset = $this->‪getPreset($uid);
183 
184  $presetData = unserialize($preset['preset_data'], ['allowed_classes' => false]);
185  if (!is_array($presetData)) {
186  throw new MalformedPresetException(
187  'ERROR: No configuration data found in preset record!',
188  1604608922
189  );
190  }
191 
192  return $presetData;
193  }
194 
199  public function ‪deletePreset(int $uid): void
200  {
201  $preset = $this->‪getPreset($uid);
202 
203  if (!($this->‪getBackendUser()->isAdmin() || (int)$preset['user_uid'] === (int)$this->‪getBackendUser()->user['uid'])) {
204  throw new InsufficientUserPermissionsException(
205  'ERROR: You were not the owner of the preset so you could not delete it.',
206  1604564346
207  );
208  }
209 
210  $connection = $this->‪createConnection();
211  $connection->delete(
212  $this->table,
213  ['uid' => $uid]
214  );
215  }
216 
220  protected function ‪createConnection(): Connection
221  {
222  return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
223  }
224 
228  protected function ‪createQueryBuilder(): QueryBuilder
229  {
230  return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
231  }
232 
236  protected function ‪getBackendUser(): BackendUserAuthentication
237  {
238  return ‪$GLOBALS['BE_USER'];
239  }
240 }
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPreset
‪array getPreset(int $uid)
Definition: PresetRepository.php:97
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\updatePreset
‪updatePreset(int $uid, array $data)
Definition: PresetRepository.php:146
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\createQueryBuilder
‪QueryBuilder createQueryBuilder()
Definition: PresetRepository.php:227
‪TYPO3\CMS\Impexp\Exception\MalformedPresetException
Definition: MalformedPresetException.php:27
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\loadPreset
‪array loadPreset(int $uid)
Definition: PresetRepository.php:179
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository
Definition: PresetRepository.php:36
‪TYPO3\CMS\Impexp\Domain\Repository
Definition: PresetRepository.php:18
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\createPreset
‪createPreset(array $data)
Definition: PresetRepository.php:122
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPresets
‪array getPresets(int $pageId)
Definition: PresetRepository.php:45
‪TYPO3\CMS\Impexp\Exception\InsufficientUserPermissionsException
Definition: InsufficientUserPermissionsException.php:27
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\$table
‪string $table
Definition: PresetRepository.php:39
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\deletePreset
‪deletePreset(int $uid)
Definition: PresetRepository.php:198
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\createConnection
‪Connection createConnection()
Definition: PresetRepository.php:219
‪TYPO3\CMS\Impexp\Exception\PresetNotFoundException
Definition: PresetNotFoundException.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: PresetRepository.php:235
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:59