279a7d151b31ac1d0eeee6effdbb8c1d62b326de
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / ViewHelpers / ReloadSqlDataViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\ViewHelpers;
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
17 use TYPO3\CMS\Core\Imaging\Icon;
18 use TYPO3\CMS\Core\Imaging\IconFactory;
19 use TYPO3\CMS\Core\Registry;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22 use TYPO3\CMS\Fluid\ViewHelpers\Link\ActionViewHelper;
23
24 /**
25 * View helper for update script link
26 */
27 class ReloadSqlDataViewHelper extends ActionViewHelper
28 {
29 /**
30 * @var string
31 */
32 protected static $registryNamespace = 'extensionDataImport';
33
34 public function initializeArguments()
35 {
36 parent::initializeArguments();
37 $this->registerArgument('extension', 'array', 'Extension key', true);
38 }
39
40 /**
41 * Renders a link to re-import the static SQL data of an extension
42 *
43 * @return string The rendered a tag
44 */
45 public function render()
46 {
47 $extension = $this->arguments['extension'];
48 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
49
50 $staticSqlDataFile = $extension['siteRelPath'] . 'ext_tables_static+adt.sql';
51 if (!file_exists(PATH_site . $staticSqlDataFile)) {
52 return '<span class="btn btn-default disabled">' . $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render() . '</span>';
53 }
54
55 $registry = GeneralUtility::makeInstance(Registry::class);
56 $oldMd5Hash = $registry->get(static::$registryNamespace, $staticSqlDataFile);
57
58 $md5HashIsEqual = true;
59 // We used to only store "1" in the database when data was imported
60 // No need to compare file content here and just show the reload icon
61 if (!empty($oldMd5Hash) && $oldMd5Hash !== 1) {
62 $currentMd5Hash = md5_file(PATH_site . $staticSqlDataFile);
63 $md5HashIsEqual = $oldMd5Hash === $currentMd5Hash;
64 }
65
66 if ($md5HashIsEqual) {
67 $iconIdentifier = 'actions-database-reload';
68 $languageKey = 'extensionList.databaseReload';
69 } else {
70 $iconIdentifier = 'actions-database-import';
71 $languageKey = 'extensionList.databaseImport';
72 }
73
74 $uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
75 $uriBuilder->reset();
76 $uri = $uriBuilder->uriFor('reloadExtensionData', ['extension' => $extension['key']], 'Action');
77 $this->tag->addAttribute('href', $uri);
78 $this->tag->addAttribute('title', LocalizationUtility::translate($languageKey, 'extensionmanager'));
79 $this->tag->setContent($iconFactory->getIcon($iconIdentifier, Icon::SIZE_SMALL)->render());
80
81 return $this->tag->render();
82 }
83 }