‪TYPO3CMS  9.5
PresetRepository.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
23 
29 {
34  public function ‪getPresets($pageId)
35  {
36  $options = [''];
37  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
38  ->getQueryBuilderForTable('tx_impexp_presets');
39 
40  $queryBuilder->select('*')
41  ->from('tx_impexp_presets')
42  ->where(
43  $queryBuilder->expr()->orX(
44  $queryBuilder->expr()->gt(
45  'public',
46  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
47  ),
48  $queryBuilder->expr()->eq(
49  'user_uid',
50  $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], \PDO::PARAM_INT)
51  )
52  )
53  );
54 
55  if ($pageId) {
56  $queryBuilder->andWhere(
57  $queryBuilder->expr()->orX(
58  $queryBuilder->expr()->eq(
59  'item_uid',
60  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
61  ),
62  $queryBuilder->expr()->eq(
63  'item_uid',
64  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
65  )
66  )
67  );
68  }
69 
70  $presets = $queryBuilder->execute();
71  while ($presetCfg = $presets->fetch()) {
72  $options[$presetCfg['uid']] = $presetCfg['title'] . ' [' . $presetCfg['uid'] . ']'
73  . ($presetCfg['public'] ? ' [Public]' : '')
74  . ($presetCfg['user_uid'] === $this->‪getBackendUser()->user['uid'] ? ' [Own]' : '');
75  }
76  return $options;
77  }
78 
85  public function ‪getPreset($uid)
86  {
87  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
88  ->getQueryBuilderForTable('tx_impexp_presets');
89 
90  return $queryBuilder->select('*')
91  ->from('tx_impexp_presets')
92  ->where(
93  $queryBuilder->expr()->eq(
94  'uid',
95  $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
96  )
97  )
98  ->execute()
99  ->fetch();
100  }
101 
107  public function ‪processPresets(&$inData)
108  {
109  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_impexp_presets');
110  $presetData = GeneralUtility::_GP('preset');
111  $err = false;
112  $msg = '';
113  // Save preset
114  $beUser = $this->‪getBackendUser();
115  // cast public checkbox to int, since this is an int field and NULL is not allowed
116  $inData['preset']['public'] = (int)$inData['preset']['public'];
117  if (isset($presetData['save'])) {
118  $preset = $this->‪getPreset($presetData['select']);
119  // Update existing
120  if (is_array($preset)) {
121  if ($beUser->isAdmin() || $preset['user_uid'] === $beUser->user['uid']) {
122  $connection->update(
123  'tx_impexp_presets',
124  [
125  'public' => $inData['preset']['public'],
126  'title' => $inData['preset']['title'],
127  'item_uid' => $inData['pagetree']['id'],
128  'preset_data' => serialize($inData)
129  ],
130  ['uid' => (int)$preset['uid']],
131  ['preset_data' => ‪Connection::PARAM_LOB]
132  );
133 
134  $msg = 'Preset #' . $preset['uid'] . ' saved!';
135  } else {
136  $msg = 'ERROR: The preset was not saved because you were not the owner of it!';
137  $err = true;
138  }
139  } else {
140  // Insert new:
141  $connection->insert(
142  'tx_impexp_presets',
143  [
144  'user_uid' => $beUser->user['uid'],
145  'public' => $inData['preset']['public'],
146  'title' => $inData['preset']['title'],
147  'item_uid' => (int)$inData['pagetree']['id'],
148  'preset_data' => serialize($inData)
149  ],
150  ['preset_data' => ‪Connection::PARAM_LOB]
151  );
152 
153  $msg = 'New preset "' . htmlspecialchars($inData['preset']['title']) . '" is created';
154  }
155  }
156  // Delete preset:
157  if (isset($presetData['delete'])) {
158  $preset = $this->‪getPreset($presetData['select']);
159  if (is_array($preset)) {
160  // Update existing
161  if ($beUser->isAdmin() || $preset['user_uid'] === $beUser->user['uid']) {
162  $connection->delete(
163  'tx_impexp_presets',
164  ['uid' => (int)$preset['uid']]
165  );
166 
167  $msg = 'Preset #' . $preset['uid'] . ' deleted!';
168  } else {
169  $msg = 'ERROR: You were not the owner of the preset so you could not delete it.';
170  $err = true;
171  }
172  } else {
173  $msg = 'ERROR: No preset selected for deletion.';
174  $err = true;
175  }
176  }
177  // Load preset
178  if (isset($presetData['load']) || isset($presetData['merge'])) {
179  $preset = $this->‪getPreset($presetData['select']);
180  if (is_array($preset)) {
181  // Update existing
182  $inData_temp = unserialize($preset['preset_data'], ['allowed_classes' => false]);
183  if (is_array($inData_temp)) {
184  if (isset($presetData['merge'])) {
185  // Merge records in:
186  if (is_array($inData_temp['record'])) {
187  $inData['record'] = array_merge((array)$inData['record'], $inData_temp['record']);
188  }
189  // Merge lists in:
190  if (is_array($inData_temp['list'])) {
191  $inData['list'] = array_merge((array)$inData['list'], $inData_temp['list']);
192  }
193  } else {
194  $msg = 'Preset #' . $preset['uid'] . ' loaded!';
195  $inData = $inData_temp;
196  }
197  } else {
198  $msg = 'ERROR: No configuratio data found in preset record!';
199  $err = true;
200  }
201  } else {
202  $msg = 'ERROR: No preset selected for loading.';
203  $err = true;
204  }
205  }
206  // Show message:
207  if ($msg !== '') {
209  $flashMessage = GeneralUtility::makeInstance(
210  FlashMessage::class,
211  'Presets',
212  $msg,
214  );
216  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
218  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
219  $defaultFlashMessageQueue->enqueue($flashMessage);
220  }
221  }
222 
226  protected function ‪getBackendUser()
227  {
228  return ‪$GLOBALS['BE_USER'];
229  }
230 }
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository
Definition: PresetRepository.php:29
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: PresetRepository.php:226
‪TYPO3\CMS\Impexp\Domain\Repository
Definition: PresetRepository.php:2
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPresets
‪array getPresets($pageId)
Definition: PresetRepository.php:34
‪TYPO3\CMS\Impexp\Domain\Repository\PresetRepository\getPreset
‪array getPreset($uid)
Definition: PresetRepository.php:85
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪$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:107
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:25
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:25
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:52