‪TYPO3CMS  11.5
SynchronizeFolderRelations.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 
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
29 
37 {
40 
42  {
43  $this->connectionPool = ‪$connectionPool;
44  $this->flashMessageService = ‪$flashMessageService;
45  }
46 
55  {
56  $storageId = $event->‪getSourceFolder()->getStorage()->getUid();
57  $sourceIdentifier = $event->‪getSourceFolder()->getIdentifier();
58  $targetIdentifier = $event->‪getFolder()->getIdentifier();
59 
60  $synchronized = 0;
61  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_file_collection');
62  $statement = $queryBuilder
63  ->select('uid', 'folder')
64  ->from('sys_file_collection')
65  ->where(
66  $queryBuilder->expr()->like('folder', $queryBuilder->quote($sourceIdentifier . '%')),
67  $queryBuilder->expr()->eq('storage', $queryBuilder->createNamedParameter($storageId, ‪Connection::PARAM_INT)),
68  $queryBuilder->expr()->eq('type', $queryBuilder->createNamedParameter('folder'))
69  )
70  ->executeQuery();
71 
72  while ($row = $statement->fetchAssociative()) {
73  $folder = preg_replace(sprintf('/^%s/', preg_quote($sourceIdentifier, '/')), $targetIdentifier, $row['folder']) ?? '';
74  if ($folder !== '') {
75  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_file_collection');
76  $synchronized += (int)$queryBuilder
77  ->update('sys_file_collection')
78  ->set('folder', $folder)
79  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter((int)$row['uid'], ‪Connection::PARAM_INT)))
80  ->executeStatement();
81  }
82  }
83 
84  if ($synchronized) {
85  $this->‪addFlashMessage((int)$synchronized, 'sys_file_collection', 'afterFolderRenamed');
86  }
87  }
88 
97  {
98  $storageId = $event->‪getSourceFolder()->getStorage()->getUid();
99  $sourceIdentifier = $event->‪getSourceFolder()->getIdentifier();
100  $targetIdentifier = $event->‪getFolder()->getIdentifier();
101 
102  $synchronized = 0;
103  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_filemounts');
104  $statement = $queryBuilder
105  ->select('uid', 'path')
106  ->from('sys_filemounts')
107  ->where(
108  $queryBuilder->expr()->like('path', $queryBuilder->quote($sourceIdentifier . '%')),
109  $queryBuilder->expr()->eq('base', $queryBuilder->createNamedParameter((string)$storageId))
110  )
111  ->executeQuery();
112 
113  while ($row = $statement->fetchAssociative()) {
114  $path = preg_replace(sprintf('/^%s/', preg_quote($sourceIdentifier, '/')), $targetIdentifier, $row['path']) ?? '';
115  if ($path !== '') {
116  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_filemounts');
117  $synchronized += (int)$queryBuilder
118  ->update('sys_filemounts')
119  ->set('path', $path)
120  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter((int)$row['uid'], ‪Connection::PARAM_INT)))
121  ->executeStatement();
122  }
123  }
124 
125  if ($synchronized) {
126  $this->‪addFlashMessage((int)$synchronized, 'sys_filemounts', 'afterFolderRenamed');
127  }
128  }
129 
139  protected function ‪addFlashMessage(int $updatedRelationsCount, string $table, string $event): void
140  {
141  $languageService = $this->‪getLanguageServcie();
142  $message = sprintf(
143  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:synchronizeFolderRelations.' . $event),
144  $updatedRelationsCount,
145  $languageService->sL(‪$GLOBALS['TCA'][$table]['ctrl']['title']),
146  );
147 
148  $this->flashMessageService
149  ->getMessageQueueByIdentifier()
150  ->enqueue(GeneralUtility::makeInstance(FlashMessage::class, $message, '', ‪FlashMessage::OK, true));
151  }
152 
153  protected function ‪getPreparedQueryBuilder(string $table): QueryBuilder
154  {
155  $queryBuilder = $this->connectionPool->getQueryBuilderForTable($table);
156  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
157  return $queryBuilder;
158  }
159 
161  {
162  return ‪$GLOBALS['LANG'];
163  }
164 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\getPreparedQueryBuilder
‪getPreparedQueryBuilder(string $table)
Definition: SynchronizeFolderRelations.php:153
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\$connectionPool
‪ConnectionPool $connectionPool
Definition: SynchronizeFolderRelations.php:38
‪TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent
Definition: AfterFolderRenamedEvent.php:28
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\synchronizeFilemountsAfterRename
‪synchronizeFilemountsAfterRename(AfterFolderRenamedEvent $event)
Definition: SynchronizeFolderRelations.php:96
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\addFlashMessage
‪addFlashMessage(int $updatedRelationsCount, string $table, string $event)
Definition: SynchronizeFolderRelations.php:139
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\__construct
‪__construct(ConnectionPool $connectionPool, FlashMessageService $flashMessageService)
Definition: SynchronizeFolderRelations.php:41
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\$flashMessageService
‪FlashMessageService $flashMessageService
Definition: SynchronizeFolderRelations.php:39
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent\getSourceFolder
‪getSourceFolder()
Definition: AfterFolderRenamedEvent.php:43
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent\getFolder
‪getFolder()
Definition: AfterFolderRenamedEvent.php:38
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\synchronizeFileCollectionsAfterRename
‪synchronizeFileCollectionsAfterRename(AfterFolderRenamedEvent $event)
Definition: SynchronizeFolderRelations.php:54
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\getLanguageServcie
‪getLanguageServcie()
Definition: SynchronizeFolderRelations.php:160
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations
Definition: SynchronizeFolderRelations.php:37
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27