13963f65a51796a5307cbdf3b9b890c84a0d6999
[Packages/TYPO3.CMS.git] / typo3 / sysext / redirects / Classes / FormDataProvider / ValuePickerItemDataProvider.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Redirects\FormDataProvider;
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
18 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Site\SiteFinder;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Inject available domain hosts into a valuepicker form
25 * @internal
26 */
27 class ValuePickerItemDataProvider implements FormDataProviderInterface
28 {
29 /**
30 * @var SiteFinder
31 */
32 protected $siteFinder;
33
34 /**
35 * ValuePickerItemDataProvider constructor.
36 * @param SiteFinder|null $siteFinder
37 */
38 public function __construct(SiteFinder $siteFinder = null)
39 {
40 $this->siteFinder = $siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
41 }
42
43 /**
44 * Add sys_domains into $result data array
45 *
46 * @param array $result Initialized result array
47 * @return array Result filled with more data
48 */
49 public function addData(array $result): array
50 {
51 if ($result['tableName'] === 'sys_redirect' && isset($result['processedTca']['columns']['source_host'])) {
52 $domains = $this->getDomains();
53 foreach ($domains as $domain) {
54 $result['processedTca']['columns']['source_host']['config']['valuePicker']['items'][] =
55 [
56 $domain,
57 $domain,
58 ];
59 }
60 }
61 return $result;
62 }
63
64 /**
65 * Get sys_domain records from database, and all from pseudo-sites
66 *
67 * @return array domain records
68 */
69 protected function getDomains(): array
70 {
71 $domains = $this->getDomainsFromAllSites();
72
73 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
74 $sysDomainRecords = $queryBuilder
75 ->select('domainName')
76 ->from('sys_domain')
77 ->execute()
78 ->fetchAll();
79 foreach ($sysDomainRecords as $domainRecord) {
80 $domains[] = $domainRecord['domainName'];
81 }
82 $domains = array_unique($domains);
83 sort($domains, SORT_NATURAL);
84 return $domains;
85 }
86
87 /**
88 * @return array
89 */
90 protected function getDomainsFromAllSites(): array
91 {
92 $domains = [];
93 foreach ($this->siteFinder->getAllSites() as $site) {
94 foreach ($site->getAllLanguages() as $language) {
95 $domains[] = $language->getBase()->getHost();
96 }
97 }
98 return $domains;
99 }
100 }