‪TYPO3CMS  ‪main
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 
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
31 
39 {
42 
44  {
45  $this->connectionPool = ‪$connectionPool;
46  $this->flashMessageService = ‪$flashMessageService;
47  }
48 
55  #[AsEventListener('synchronize-file-collections-after-folder-renamed')]
57  {
58  $sourceIdentifier = $event->‪getSourceFolder()->getCombinedIdentifier();
59  $targetIdentifier = $event->‪getFolder()->getCombinedIdentifier();
60 
61  $synchronized = 0;
62  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_file_collection');
63  $statement = $queryBuilder
64  ->select('uid', 'folder_identifier')
65  ->from('sys_file_collection')
66  ->where(
67  $queryBuilder->expr()->like('folder_identifier', $queryBuilder->quote($sourceIdentifier . '%')),
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_identifier']) ?? '';
74  if ($folder !== '') {
75  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_file_collection');
76  $synchronized += (int)$queryBuilder
77  ->update('sys_file_collection')
78  ->set('folder_identifier', $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 
95  #[AsEventListener('synchronize-filemounts-after-folder-renamed')]
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', 'identifier')
106  ->from('sys_filemounts')
107  ->where(
108  $queryBuilder->expr()->like('identifier', $queryBuilder->quote($storageId . ':' . $sourceIdentifier . '%'))
109  )
110  ->executeQuery();
111 
112  while ($row = $statement->fetchAssociative()) {
113  [$base, $path] = ‪GeneralUtility::trimExplode(':', $row['identifier'], false, 2);
114  $path = preg_replace(sprintf('/^%s/', preg_quote($sourceIdentifier, '/')), $targetIdentifier, $path) ?? '';
115  if ($path !== '') {
116  $queryBuilder = $this->‪getPreparedQueryBuilder('sys_filemounts');
117  $synchronized += (int)$queryBuilder
118  ->update('sys_filemounts')
119  ->set('identifier', $base . ':' . $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, '', ContextualFeedbackSeverity::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:52
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\getPreparedQueryBuilder
‪getPreparedQueryBuilder(string $table)
Definition: SynchronizeFolderRelations.php:153
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\$connectionPool
‪ConnectionPool $connectionPool
Definition: SynchronizeFolderRelations.php:40
‪TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent
Definition: AfterFolderRenamedEvent.php:28
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪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:43
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\$flashMessageService
‪FlashMessageService $flashMessageService
Definition: SynchronizeFolderRelations.php:41
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent\getSourceFolder
‪getSourceFolder()
Definition: AfterFolderRenamedEvent.php:39
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent\getFolder
‪getFolder()
Definition: AfterFolderRenamedEvent.php:34
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\synchronizeFileCollectionsAfterRename
‪synchronizeFileCollectionsAfterRename(AfterFolderRenamedEvent $event)
Definition: SynchronizeFolderRelations.php:56
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations\getLanguageServcie
‪getLanguageServcie()
Definition: SynchronizeFolderRelations.php:160
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\SynchronizeFolderRelations
Definition: SynchronizeFolderRelations.php:39
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822