‪TYPO3CMS  9.5
ImageCropUpdater.php
Go to the documentation of this file.
1 <?php
2 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 
24 
31 {
38  protected ‪$payload = [];
39 
45  public function ‪getTitle(): string
46  {
47  return 'Migrate values in sys_file_reference crop field';
48  }
49 
56  public function ‪hasPotentialUpdateForTable(string $tableName): bool
57  {
58  $result = false;
59  ‪$payload = $this->‪getPayloadForTable($tableName);
60  if (count(‪$payload) !== 0) {
61  $this->payload[$tableName] = ‪$payload;
62  $result = true;
63  }
64  return $result;
65  }
66 
74  public function ‪updateTableRow(string $tableName, array $inputRow): array
75  {
76  $tablePayload = $this->payload[$tableName];
77 
78  foreach ($tablePayload['fields'] as $field) {
79  if (strpos($inputRow[$field], '{"x":') === 0) {
80  $cropArray = json_decode($inputRow[$field], true);
81  if (is_array($cropArray)) {
82  $file = $this->‪getFile($inputRow, $tablePayload['fileReferenceField'] ?: 'uid_local');
83  if (null === $file) {
84  continue;
85  }
86 
87  $cropArea = ‪Area::createFromConfiguration(json_decode($inputRow[$field], true));
88  $cropVariantCollectionConfig = [
89  'default' => [
90  'cropArea' => $cropArea->makeRelativeBasedOnFile($file)->asArray(),
91  ]
92  ];
93  $inputRow[$field] = json_encode($cropVariantCollectionConfig);
94  }
95  }
96  }
97 
98  return $inputRow;
99  }
100 
114  protected function ‪getPayloadForTable(string $tableName): array
115  {
116  if (!is_array(‪$GLOBALS['TCA'][$tableName])) {
117  throw new \RuntimeException(
118  'Globals TCA of given table name must exist',
119  1485386982
120  );
121  }
122  $tableDefinition = ‪$GLOBALS['TCA'][$tableName];
123 
124  if (
125  empty($tableDefinition['columns'])
126  || !is_array($tableDefinition['columns'])
127  ) {
128  return [];
129  }
130 
131  ‪$fields = [];
132  $fileReferenceField = null;
133  foreach ($tableDefinition['columns'] as $fieldName => $fieldConfiguration) {
134  if (
135  !empty($fieldConfiguration['config']['type'])
136  && $fieldConfiguration['config']['type'] === 'group'
137  && !empty($fieldConfiguration['config']['internal_type'])
138  && $fieldConfiguration['config']['internal_type'] === 'db'
139  && !empty($fieldConfiguration['config']['allowed'])
140  && $fieldConfiguration['config']['allowed'] === 'sys_file'
141  ) {
142  $fileReferenceField = $fieldName;
143  }
144  if (
145  !empty($fieldConfiguration['config']['type'])
146  && $fieldConfiguration['config']['type'] === 'imageManipulation'
147  ) {
148  ‪$fields[] = $fieldName;
149  }
150  }
151 
152  if (empty(‪$fields)) {
153  return [];
154  }
155 
156  ‪$payload = [];
157  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
158 
159  foreach (‪$fields as $fieldName) {
160  $queryBuilder = $connectionPool->getQueryBuilderForTable($tableName);
161  $queryBuilder->getRestrictions()->removeAll();
162 
163  $query = $queryBuilder
164  ->from($tableName)
165  ->count($fieldName)
166  ->where(
167  $queryBuilder->expr()->like(
168  $fieldName,
169  $queryBuilder->createNamedParameter('{"x":%', \PDO::PARAM_STR)
170  )
171  );
172  if ((int)$query->execute()->fetchColumn(0) > 0) {
173  ‪$payload['fields'][] = $fieldName;
174  if (isset($fileReferenceField)) {
175  ‪$payload['fileReferenceField'] = $fileReferenceField;
176  } else {
177  ‪$payload['fileReferenceField'] = null;
178  }
179  }
180  }
181  return ‪$payload;
182  }
183 
191  private function ‪getFile(array $row, $fieldName)
192  {
193  $file = null;
194  $fileUid = !empty($row[$fieldName]) ? $row[$fieldName] : null;
195  if (is_array($fileUid) && isset($fileUid[0]['uid'])) {
196  $fileUid = $fileUid[0]['uid'];
197  }
199  try {
200  $file = ‪ResourceFactory::getInstance()->‪getFileObject((int)$fileUid);
201  } catch (‪FileDoesNotExistException $e) {
202  } catch (\InvalidArgumentException $e) {
203  }
204  }
205  return $file;
206  }
207 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static Area createFromConfiguration(array $config)
Definition: Area.php:57
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:21
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater\updateTableRow
‪array updateTableRow(string $tableName, array $inputRow)
Definition: ImageCropUpdater.php:73
‪TYPO3\CMS\Install\Updates\RowUpdater\RowUpdaterInterface
Definition: RowUpdaterInterface.php:22
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater\getPayloadForTable
‪array getPayloadForTable(string $tableName)
Definition: ImageCropUpdater.php:113
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater\hasPotentialUpdateForTable
‪bool hasPotentialUpdateForTable(string $tableName)
Definition: ImageCropUpdater.php:55
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:21
‪$fields
‪$fields
Definition: pages.php:4
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater\getFile
‪TYPO3 CMS Core Resource File null getFile(array $row, $fieldName)
Definition: ImageCropUpdater.php:190
‪TYPO3\CMS\Install\Updates\RowUpdater
Definition: ImageCropUpdater.php:3
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater
Definition: ImageCropUpdater.php:31
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater\getTitle
‪string getTitle()
Definition: ImageCropUpdater.php:44
‪TYPO3\CMS\Install\Updates\RowUpdater\ImageCropUpdater\$payload
‪array $payload
Definition: ImageCropUpdater.php:37
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:399
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45