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