‪TYPO3CMS  9.5
FrontendEditDataHandler.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
28 
35 {
39  protected ‪$configuration;
40 
44  protected ‪$user;
45 
52  {
53  $this->user = ‪$user ?: ‪$GLOBALS['BE_USER'];
54  $this->configuration = ‪$configuration;
55  }
56 
63  public function ‪editAction()
64  {
65  // Commands
66  list($table, $uid) = explode(':', $this->configuration['record']);
67  $uid = (int)$uid;
68  $cmd = $this->configuration['cmd'];
69  // Look for some configuration data that indicates we should save.
70  if (($this->configuration['doSave'] || $this->configuration['update'] || $this->configuration['update_close']) && is_array($this->configuration['data'])) {
71  $cmd = 'save';
72  }
73  if ($cmd === 'save' || $cmd && $table && $uid && isset(‪$GLOBALS['TCA'][$table])) {
74  // Perform the requested editing command.
75  $cmdAction = 'do' . ucwords($cmd);
76  if (method_exists($this, $cmdAction)) {
77  call_user_func_array([$this, $cmdAction], [$table, $uid]);
78  } else {
79  throw new \UnexpectedValueException('The specified frontend edit command (' . $cmd . ') is not valid.', 1225818110);
80  }
81  }
82  }
83 
90  protected function ‪doHide(string $table, int $uid)
91  {
92  $hideField = ‪$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
93  if ($hideField) {
94  $recData = [];
95  $recData[$table][$uid][$hideField] = 1;
96  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
97  $dataHandler->start($recData, []);
98  $dataHandler->process_datamap();
99  }
100  }
101 
108  protected function ‪doUnhide(string $table, int $uid)
109  {
110  $hideField = ‪$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
111  if ($hideField) {
112  $recData = [];
113  $recData[$table][$uid][$hideField] = 0;
114  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
115  $dataHandler->start($recData, []);
116  $dataHandler->process_datamap();
117  }
118  }
119 
126  protected function ‪doUp(string $table, int $uid)
127  {
128  $this->‪move($table, $uid, 'up');
129  }
130 
137  protected function ‪doDown(string $table, int $uid)
138  {
139  $this->‪move($table, $uid, 'down');
140  }
141 
148  protected function ‪doMoveAfter(string $table, int $uid)
149  {
150  $afterUID = (int)$this->configuration['moveAfter'];
151  $this->‪move($table, $uid, '', $afterUID);
152  }
153 
162  protected function ‪move(string $table, int $uid, string $direction = '', int $afterUID = 0)
163  {
164  $dataHandlerCommands = [];
165  $sortField = ‪$GLOBALS['TCA'][$table]['ctrl']['sortby'];
166  if ($sortField) {
167  // Get the current record
168  // Only fetch uid, pid and the fields that are necessary to detect the sorting factors
169  if (isset(‪$GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields'])) {
170  $copyAfterDuplicateFields = GeneralUtility::trimExplode(',', ‪$GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields'], true);
171  } else {
172  $copyAfterDuplicateFields = [];
173  }
174 
175  ‪$fields = $copyAfterDuplicateFields;
176  ‪$fields[] = 'uid';
177  ‪$fields[] = 'pid';
178  ‪$fields[] = $sortField;
179  ‪$fields = array_unique(‪$fields);
180 
181  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
182  ->getQueryBuilderForTable($table);
183  $queryBuilder->getRestrictions()->removeAll();
184 
185  $currentRecord = $queryBuilder
186  ->select(...‪$fields)
187  ->from($table)
188  ->where($queryBuilder->expr()->eq(
189  'uid',
190  $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
191  ))
192  ->execute()
193  ->fetch();
194 
195  if (is_array($currentRecord)) {
196  // Fetch the record before or after the current one
197  // to define the data handler commands
198  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
199  ->getQueryBuilderForTable($table);
200 
201  $queryBuilder
202  ->select('uid', 'pid')
203  ->from($table)
204  ->where($queryBuilder->expr()->eq(
205  'pid',
206  $queryBuilder->createNamedParameter($currentRecord['pid'], \PDO::PARAM_INT)
207  ))
208  ->setMaxResults(2);
209 
210  // Disable the default restrictions (but not all) if the admin panel is in preview mode
211  if ($this->user->adminPanel instanceof ‪AdminPanelView && $this->user->adminPanel->‪extGetFeAdminValue('preview')) {
212  $queryBuilder->getRestrictions()
213  ->removeByType(StartTimeRestriction::class)
214  ->removeByType(EndTimeRestriction::class)
215  ->removeByType(HiddenRestriction::class)
216  ->removeByType(FrontendGroupRestriction::class);
217  }
218 
219  if (!empty($copyAfterDuplicateFields)) {
220  foreach ($copyAfterDuplicateFields as $fieldName) {
221  $queryBuilder->andWhere($queryBuilder->expr()->eq(
222  $fieldName,
223  $queryBuilder->createNamedParameter($currentRecord[$fieldName], \PDO::PARAM_STR)
224  ));
225  }
226  }
227  if (!empty($direction)) {
228  if ($direction === 'up') {
229  $queryBuilder->andWhere(
230  $queryBuilder->expr()->lt(
231  $sortField,
232  $queryBuilder->createNamedParameter($currentRecord[$sortField], \PDO::PARAM_INT)
233  )
234  );
235  $queryBuilder->orderBy($sortField, 'DESC');
236  } else {
237  $queryBuilder->andWhere(
238  $queryBuilder->expr()->gt(
239  $sortField,
240  $queryBuilder->createNamedParameter($currentRecord[$sortField], \PDO::PARAM_INT)
241  )
242  );
243  $queryBuilder->orderBy($sortField, 'ASC');
244  }
245  }
246 
247  $result = $queryBuilder->execute();
248  if ($recordBefore = $result->fetch()) {
249  if ($afterUID) {
250  $dataHandlerCommands[$table][$uid]['move'] = -$afterUID;
251  } elseif ($direction === 'down') {
252  $dataHandlerCommands[$table][$uid]['move'] = -$recordBefore['uid'];
253  } elseif ($recordAfter = $result->fetch()) {
254  // Must take the second record above...
255  $dataHandlerCommands[$table][$uid]['move'] = -$recordAfter['uid'];
256  } else {
257  // ... and if that does not exist, use pid
258  $dataHandlerCommands[$table][$uid]['move'] = $currentRecord['pid'];
259  }
260  } elseif ($direction === 'up') {
261  $dataHandlerCommands[$table][$uid]['move'] = $currentRecord['pid'];
262  }
263  }
264 
265  // If any data handler commands were set, execute the data handler command
266  if (!empty($dataHandlerCommands)) {
267  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
268  $dataHandler->start([], $dataHandlerCommands);
269  $dataHandler->process_cmdmap();
270  }
271  }
272  }
273 
280  protected function ‪doDelete(string $table, int $uid)
281  {
282  $cmdData[$table][$uid]['delete'] = 1;
283  if (!empty($cmdData)) {
284  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
285  $dataHandler->start([], $cmdData);
286  $dataHandler->process_cmdmap();
287  }
288  }
289 
296  protected function ‪doSave(string $table, int $uid)
297  {
298  $data = $this->configuration['data'];
299  if (!empty($data)) {
300  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
301  $dataHandler->start($data, []);
302  $dataHandler->process_uploads($_FILES);
303  $dataHandler->process_datamap();
304  // Save the new UID back into configuration
305  $newUID = $dataHandler->substNEWwithIDs['NEW'];
306  if ($newUID) {
307  $this->configuration['newUID'] = $newUID;
308  }
309  }
310  }
311 
319  protected function ‪doSaveAndClose(string $table, int $uid)
320  {
321  $this->‪doSave($table, $uid);
322  }
323 
331  protected function ‪doClose(string $table, int $uid)
332  {
333  }
334 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:81
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doHide
‪doHide(string $table, int $uid)
Definition: FrontendEditDataHandler.php:88
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:25
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doUnhide
‪doUnhide(string $table, int $uid)
Definition: FrontendEditDataHandler.php:106
‪TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction
Definition: EndTimeRestriction.php:25
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\__construct
‪__construct(array $configuration, FrontendBackendUserAuthentication $user=null)
Definition: FrontendEditDataHandler.php:49
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication
Definition: FrontendBackendUserAuthentication.php:35
‪TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction
Definition: StartTimeRestriction.php:25
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler
Definition: FrontendEditDataHandler.php:35
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\$configuration
‪array $configuration
Definition: FrontendEditDataHandler.php:38
‪$fields
‪$fields
Definition: pages.php:4
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendGroupRestriction
Definition: FrontendGroupRestriction.php:28
‪TYPO3\CMS\Feedit\DataHandling
Definition: FrontendEditDataHandler.php:4
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doDown
‪doDown(string $table, int $uid)
Definition: FrontendEditDataHandler.php:135
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doClose
‪doClose(string $table, int $uid)
Definition: FrontendEditDataHandler.php:329
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\editAction
‪editAction()
Definition: FrontendEditDataHandler.php:61
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doMoveAfter
‪doMoveAfter(string $table, int $uid)
Definition: FrontendEditDataHandler.php:146
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\move
‪move(string $table, int $uid, string $direction='', int $afterUID=0)
Definition: FrontendEditDataHandler.php:160
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\$user
‪FrontendBackendUserAuthentication $user
Definition: FrontendEditDataHandler.php:42
‪TYPO3\CMS\Frontend\View\AdminPanelView
Definition: LegacyClassesForIde.php:8
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doUp
‪doUp(string $table, int $uid)
Definition: FrontendEditDataHandler.php:124
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doSaveAndClose
‪doSaveAndClose(string $table, int $uid)
Definition: FrontendEditDataHandler.php:317
‪TYPO3\CMS\Adminpanel\View\AdminPanelView\extGetFeAdminValue
‪mixed extGetFeAdminValue($sectionName, $val='')
Definition: AdminPanelView.php:323
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doSave
‪doSave(string $table, int $uid)
Definition: FrontendEditDataHandler.php:294
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Feedit\DataHandling\FrontendEditDataHandler\doDelete
‪doDelete(string $table, int $uid)
Definition: FrontendEditDataHandler.php:278