‪TYPO3CMS  11.5
CreateSiteConfiguration.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 
18 namespace ‪TYPO3\CMS\Core\Hooks;
19 
31 use ‪TYPO3\CMS\Core\SysLog\Action\Site as SiteAction;
32 use ‪TYPO3\CMS\Core\SysLog\Error as SystemLogErrorClassification;
35 
42 {
46  protected ‪$allowedPageTypes = [
50  ];
51 
52  public function ‪processDatamap_afterDatabaseOperations(string $status, string $table, $id, array $fieldValues, ‪DataHandler $dataHandler): void
53  {
66  if ($status !== 'new'
67  || $table !== 'pages'
68  || $dataHandler->BE_USER->workspace > 0
69  || !isset($dataHandler->substNEWwithIDs[$id])
70  || (int)$fieldValues['pid'] !== 0
71  || (int)$fieldValues['l10n_parent'] !== 0
72  || (isset($fieldValues['t3ver_oid']) && (int)$fieldValues['t3ver_oid'] > 0)
73  || !in_array((int)$fieldValues['doktype'], $this->allowedPageTypes, true)
74  || $dataHandler->isImporting
75  ) {
76  return;
77  }
78 
79  $uid = (int)$dataHandler->substNEWwithIDs[$id];
80  $this->generateSiteConfigurationForRootPage($uid, $dataHandler->BE_USER);
81  }
82 
83  protected function ‪generateSiteConfigurationForRootPage(int $pageId, ‪BackendUserAuthentication $backendUser): void
84  {
85  $entryPoint = 'autogenerated-' . $pageId;
86  $siteIdentifier = $entryPoint . '-' . md5((string)$pageId);
87 
88  if (!$this->‪siteExistsByRootPageId($pageId)) {
89  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
90  $normalizedParams = $this->‪getNormalizedParams();
91  $basePrefix = ‪Environment::isCli() ? $normalizedParams->getSitePath() : $normalizedParams->getSiteUrl();
92  try {
93  $siteConfiguration->createNewBasicSite(
94  $siteIdentifier,
95  $pageId,
96  $basePrefix . $entryPoint
97  );
98  $backendUser->‪writelog(‪Type::SITE, SiteAction::CREATE, SystemLogErrorClassification::MESSAGE, 0, 'Site configuration \'%s\' was automatically created for new root page (%s).', [$siteIdentifier, $pageId], 'site');
99  $this->‪updateSlugForPage($pageId);
101  $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $e->getMessage(), '', ‪FlashMessage::WARNING, true);
102  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
103  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
104  $defaultFlashMessageQueue->enqueue($flashMessage);
105  }
106  }
107  }
108 
109  protected function ‪getNormalizedParams(): ‪NormalizedParams
110  {
111  $normalizedParams = null;
112  $serverParams = ‪Environment::isCli() ? ['HTTP_HOST' => 'localhost'] : $_SERVER;
113  if (isset(‪$GLOBALS['TYPO3_REQUEST'])) {
114  $normalizedParams = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams');
115  $serverParams = ‪$GLOBALS['TYPO3_REQUEST']->getServerParams();
116  }
117 
118  if (!$normalizedParams instanceof ‪NormalizedParams) {
119  $normalizedParams = ‪NormalizedParams::createFromServerParams($serverParams);
120  }
121 
122  return $normalizedParams;
123  }
124 
130  protected function ‪updateSlugForPage(int $pageId): void
131  {
132  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
133  $dataMap = [
134  'pages' => [
135  $pageId => [
136  'slug' => '',
137  ],
138  ],
139  ];
140  $dataHandler->start($dataMap, []);
141  $dataHandler->process_datamap();
142  }
143 
150  protected function ‪siteExistsByRootPageId(int $rootPageId): bool
151  {
152  try {
153  GeneralUtility::makeInstance(SiteFinder::class)->getSiteByRootPageId($rootPageId);
154  } catch (SiteNotFoundException $e) {
155  return false;
156  }
157  return true;
158  }
159 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:86
‪TYPO3\CMS\Core\SysLog\Action\Site
Definition: Site.php:24
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\updateSlugForPage
‪updateSlugForPage(int $pageId)
Definition: CreateSiteConfiguration.php:129
‪TYPO3\CMS\Core\Configuration\Exception\SiteConfigurationWriteException
Definition: SiteConfigurationWriteException.php:25
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_DEFAULT
‪const DOKTYPE_DEFAULT
Definition: PageRepository.php:110
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\generateSiteConfigurationForRootPage
‪generateSiteConfigurationForRootPage(int $pageId, BackendUserAuthentication $backendUser)
Definition: CreateSiteConfiguration.php:82
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SHORTCUT
‪const DOKTYPE_SHORTCUT
Definition: PageRepository.php:112
‪TYPO3\CMS\Core\SysLog\Type\SITE
‪const SITE
Definition: Type.php:34
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_LINK
‪const DOKTYPE_LINK
Definition: PageRepository.php:111
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\$allowedPageTypes
‪int[] $allowedPageTypes
Definition: CreateSiteConfiguration.php:45
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\siteExistsByRootPageId
‪bool siteExistsByRootPageId(int $rootPageId)
Definition: CreateSiteConfiguration.php:149
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration
Definition: CreateSiteConfiguration.php:42
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:43
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\processDatamap_afterDatabaseOperations
‪processDatamap_afterDatabaseOperations(string $status, string $table, $id, array $fieldValues, DataHandler $dataHandler)
Definition: CreateSiteConfiguration.php:51
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromServerParams
‪static static createFromServerParams(array $serverParams, array $systemConfiguration=null)
Definition: NormalizedParams.php:826
‪TYPO3\CMS\Core\SysLog\Error
Definition: Error.php:24
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\writelog
‪int writelog($type, $action, $error, $details_nr, $details, $data, $tablename='', $recuid='', $recpid='', $event_pid=-1, $NEWid='', $userId=0)
Definition: BackendUserAuthentication.php:2020
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:162
‪TYPO3\CMS\Core\Hooks\CreateSiteConfiguration\getNormalizedParams
‪getNormalizedParams()
Definition: CreateSiteConfiguration.php:108
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35
‪TYPO3\CMS\Core\SysLog\Type
Definition: Type.php:28
‪TYPO3\CMS\Core\Hooks
Definition: BackendUserGroupIntegrityCheck.php:16