‪TYPO3CMS  10.4
PresetRepository.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 
25 
31 {
36  public function ‪getPresets($pageId)
37  {
38  $options = [''];
39  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
40  ->getQueryBuilderForTable('tx_impexp_presets');
41 
42  $queryBuilder->select('*')
43  ->from('tx_impexp_presets')
44  ->where(
45  $queryBuilder->expr()->orX(
46  $queryBuilder->expr()->gt(
47  'public',
48  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
49  ),
50  $queryBuilder->expr()->eq(
51  'user_uid',
52  $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], \PDO::PARAM_INT)
53  )
54  )
55  );
56 
57  if ($pageId) {
58  $queryBuilder->andWhere(
59  $queryBuilder->expr()->orX(
60  $queryBuilder->expr()->eq(
61  'item_uid',
62  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
63  ),
64  $queryBuilder->expr()->eq(
65  'item_uid',
66  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
67  )
68  )
69  );
70  }
71 
72  $presets = $queryBuilder->execute();
73  while ($presetCfg = $presets->fetch()) {
74  $options[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
75  . ($presetCfg['public'] ? ' [Public]' : '')
76  . ($presetCfg['user_uid'] === $this->‪getBackendUser()->user['uid'] ? ' [Own]' : '');
77  }
78  return $options;
79  }
80 
87  public function ‪getPreset($uid)
88  {
89  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
90  ->getQueryBuilderForTable('tx_impexp_presets');
91 
92  return $queryBuilder->select('*')
93  ->from('tx_impexp_presets')
94  ->where(
95  $queryBuilder->expr()->eq(
96  'uid',
97  $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
98  )
99  )
100  ->execute()
101  ->fetch();
102  }
103 
109  public function ‪processPresets(&$inData)
110  {
111  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_impexp_presets');
112  $presetData = GeneralUtility::_GP('preset');
113  $context = GeneralUtility::makeInstance(Context::class);
114  $currentTimestamp = $context->getPropertyFromAspect('date', 'timestamp');
115  $err = false;
116  $msg = '';
117  // Save preset
118  $beUser = $this->‪getBackendUser();
119  // cast public checkbox to int, since this is an int field and NULL is not allowed
120  $inData['preset']['public'] = (int)$inData['preset']['public'];
121  if (isset($presetData['save'])) {
122  $preset = $this->‪getPreset($presetData['select']);
123  // Update existing
124  if (is_array($preset)) {
125  if ($beUser->isAdmin() || $preset['user_uid'] === $beUser->user['uid']) {
126  $connection->update(
127  'tx_impexp_presets',
128  [
129  'public' => $inData['preset']['public'],
130  'title' => $inData['preset']['title'],
131  'item_uid' => $inData['pagetree']['id'],
132  'preset_data' => serialize($inData),
133  'tstamp' => $currentTimestamp
134  ],
135  ['uid' => (int)$preset['uid']],
136  ['preset_data' => ‪Connection::PARAM_LOB]
137  );
138 
139  $msg = 'Preset #' . $preset['uid'] . ' saved!';
140  } else {
141  $msg = 'ERROR: The preset was not saved because you were not the owner of it!';
142  $err = true;
143  }
144  } else {
145  // Insert new:
146  $connection->insert(
147  'tx_impexp_presets',
148  [
149  'user_uid' => $beUser->user['uid'],
150  'public' => $inData['preset']['public'],
151  'title' => $inData['preset']['title'],
152  'item_uid' => (int)$inData['pagetree']['id'],
153  'preset_data' => serialize($inData),
154  'tstamp' => $currentTimestamp,
155  'crdate' => $currentTimestamp
156  ],
157  ['preset_data' => ‪Connection::PARAM_LOB]
158  );
159 
160  $msg = 'New preset "' . htmlspecialchars($inData['preset']['title']) . '" is created';
161  }
162  }
163  // Delete preset:
164  if (isset($presetData['delete'])) {
165  $preset = $this->‪getPreset($presetData['select']);
166  if (is_array($preset)) {
167  // Update existing
168  if ($beUser->isAdmin() || $preset['user_uid'] === $beUser->user['uid']) {
169  $connection->delete(
170  'tx_impexp_presets',
171  ['uid' => (int)$preset['uid']]
172  );
173 
174  $msg = 'Preset #' . $preset['uid'] . ' deleted!';
175  } else {
176  $msg = 'ERROR: You were not the owner of the preset so you could not delete it.';
177  $err = true;
178  }
179  } else {
180  $msg = 'ERROR: No preset selected for deletion.';
181  $err = true;
182  }
183  }
184  // Load preset
185  if (isset($presetData['load']) || isset($presetData['merge'])) {
186  $preset = $this->‪getPreset($presetData['select']);
187  if (is_array($preset)) {
188  // Update existing
189  $inData_temp = unserialize($preset['preset_data'], ['allowed_classes' => false]);
190  if (is_array($inData_temp)) {
191  if (isset($presetData['merge'])) {
192  // Merge records in:
193  if (is_array($inData_temp['record'])) {
194  $inData['record'] = array_merge((array)$inData['record'], $inData_temp['record']);
195  }
196  // Merge lists in:
197  if (is_array($inData_temp['list'])) {
198  $inData['list'] = array_merge((array)$inData['list'], $inData_temp['list']);
199  }
200  } else {
201  $msg = 'Preset #' . $preset['uid'] . ' loaded!';
202  $inData = $inData_temp;
203  }
204  } else {
205  $msg = 'ERROR: No configuration data found in preset record!';
206  $err = true;
207  }
208  } else {
209  $msg = 'ERROR: No preset selected for loading.';
210  $err = true;
211  }
212  }
213  // Show message:
214  if ($msg !== '') {
216  $flashMessage = GeneralUtility::makeInstance(
217  FlashMessage::class,
218  'Presets',
219  $msg,
221  );
223  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
225  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
226  $defaultFlashMessageQueue->enqueue($flashMessage);
227  }
228  }
229 
233  protected function ‪getBackendUser()
234  {
235  return ‪$GLOBALS['BE_USER'];
236  }
237 }
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository
Definition: PresetRepository.php:31
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: PresetRepository.php:233
‪TYPO3\CMS\Impexp\Domain\Repository
Definition: PresetRepository.php:16
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPresets
‪array getPresets($pageId)
Definition: PresetRepository.php:36
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPreset
‪array getPreset($uid)
Definition: PresetRepository.php:87
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\processPresets
‪processPresets(&$inData)
Definition: PresetRepository.php:109
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:57