瀏覽代碼

https://assistance.opentalent.fr/browse/V8-1893

Olivier Massot 4 年之前
父節點
當前提交
dc56cd7050

+ 73 - 0
ot_admin/Classes/Command/GetSiteInfosCommand.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace Opentalent\OtAdmin\Command;
+
+
+use Opentalent\OtAdmin\Controller\SiteController;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
+/**
+ * Display the main informations about the organization's website
+ *
+ * @package Opentalent\OtAdmin\Command
+ */
+class GetSiteInfosCommand extends Command
+{
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * Allows to configure the command.
+     * Allows to add a description, a help text, and / or define arguments.
+     *
+     */
+    protected function configure()
+    {
+        $this
+            ->setName("ot:site:infos")
+            ->setDescription("Display the main informations about the organization's website")
+            ->addArgument(
+                'organization_id',
+                InputArgument::REQUIRED,
+                "The organization's id in the opentalent DB"
+            );
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @throws \Exception
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $org_id = $input->getArgument('organization_id');
+
+        $io = new SymfonyStyle($input, $output);
+
+        $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
+        $infos = $siteController->getSiteInfosAction($org_id);
+        $io->title(sprintf("Informations about the organization " . $org_id . ' typo3 website'));
+
+        $headers = [];
+        $values = [];
+        foreach ($infos as $key => $value) {
+            $headers[] = $key;
+            $values[] = json_encode(
+                str_replace("\"", "'", $value),
+                JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
+            );
+        }
+
+        $io->horizontalTable($headers, [$values]);
+
+        $io->success(sprintf('Informations retrieved'));
+    }
+
+}

+ 88 - 4
ot_admin/Classes/Controller/SiteController.php

@@ -130,6 +130,50 @@ class SiteController extends ActionController
         $this->createdFiles = [];
     }
 
+    /**
+     * Return the main informations about the organization's website
+     *
+     * @param int $organizationId
+     * @return array
+     * @throws NoSuchWebsiteException
+     */
+    public function getSiteInfosAction(int $organizationId) {
+        $rootUid = $this->findRootUidFor($organizationId);
+        $config = $this->findConfigFor($organizationId, $rootUid);
+        $extraData = $this->fetchOrganizationExtraData($organizationId);
+
+        $rootPage = $this->otPageRepository->getPage($rootUid);
+
+        $site = [
+            'rootUid' => $rootUid,
+            'siteTitle' => $rootPage['title'],
+            'baseUrl' => $config['base'],
+            'template' => $rootPage['tx_opentalent_template'],
+            'preferences' => $rootPage['tx_opentalent_template_preferences'],
+            'matomo_id' => $rootPage['tx_opentalent_matomo_id'],
+            'deleted' => $rootPage['deleted'],
+            'hiddenOrRestricted' => (int)($rootPage['hidden'] || $rootPage['fe_group'] < 0),
+            'mountedForBeUsers' => []
+        ];
+
+        // Owners
+        $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
+        $queryBuilder->getRestrictions()->removeAll();
+        $beUsers = $queryBuilder
+            ->select('uid', 'username')
+            ->from('be_users')
+            ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
+            ->execute()
+            ->fetchAll();
+
+        foreach ($beUsers as $beUser) {
+            $site['mountedForBeUsers'][] = $beUser;
+        }
+
+        return $site;
+
+    }
+
     /**
      * Creates a new website for the given organization, and
      * returns the root page uid of the newly created site
@@ -547,8 +591,10 @@ class SiteController extends ActionController
      *
      * @param int $organizationId
      * @return int
+     * @throws NoSuchWebsiteException
      */
-    public function updateSiteConstantsAction(int $organizationId) {
+    public function updateSiteConstantsAction(int $organizationId): int
+    {
         $rootUid = $this->findRootUidFor($organizationId);
 
         // This extra-data can not be retrieved from the API for now, but
@@ -564,6 +610,8 @@ class SiteController extends ActionController
             ->set('constants', $constants)
             ->where($queryBuilder->expr()->eq('pid', $rootUid))
             ->execute();
+
+        OtCacheManager::clearSiteCache($rootUid, true);
         return $rootUid;
     }
 
@@ -990,8 +1038,7 @@ class SiteController extends ActionController
         $extraData = $this->fetchOrganizationExtraData($organizationId);
 
         // load site's settings (uncomment if needed)
-        // $config_dir = $_ENV['TYPO3_PATH_APP'] . "/config/sites/" . $organization->getSubDomain() . '_' . $organizationId;
-        // $config = Yaml::parseFile($config_dir . "/config.yaml");
+        // $config = $this->findConfigFor($organizationId, $rootUid);
 
         // Check site's title
         if (trim($rootPage['title']) != trim($organization->getName())) {
@@ -1454,11 +1501,48 @@ class SiteController extends ActionController
     }
 
     /**
-     * Write the .../sites/.../config.yml file of the given site
+     * Try to find the config file of the website in the less resource-consuming way
+     * and parse it.
+     *
+     * @param int $organizationId
+     * @param int $rootUid
+     * @return array   Configuration of the website
+     */
+    protected function findConfigFor(int $organizationId, int $rootUid) {
+
+        $configs_directory = $_ENV['TYPO3_PATH_APP'] . "/config/sites/";
+        $candidates = scandir($configs_directory);
+
+        // try to filter by directory name
+        foreach ($candidates as $subdir) {
+            if (preg_match('/\.*_' . $organizationId . '$/', $subdir)) {
+                $filename = $configs_directory . $subdir . '/config.yaml';
+                $yamlConfig = Yaml::parseFile($filename);
+
+                if ($yamlConfig['rootPageId'] === $rootUid) {
+                    return $yamlConfig;
+                }
+            }
+        }
+
+        // it wasn't found the easy way, let's look to each file... :(
+        foreach ($candidates as $subdir) {
+            $yamlConfig = Yaml::parseFile($filename);
+            if ($yamlConfig['rootPageId'] === $rootUid) {
+                return $yamlConfig;
+            }
+        }
+
+        throw new \RuntimeException("No config file found for this website");
+    }
+
+    /**
+     * Write the .../sites/.../config.yaml file of the given site
      *
      * @param int $organizationId
      * @param int $rootUid
      * @param string $domain
+     * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
      */
     private function writeConfigFile(int $organizationId, int $rootUid, string $domain) {
 

+ 21 - 0
ot_admin/Classes/Http/ApiController.php

@@ -74,6 +74,27 @@ class ApiController implements LoggerAwareInterface
         return (int)$organizationId;
     }
 
+    /**
+     * -- Target of the route 'site_infos' --
+     *
+     * Return the main informations about the organization's website
+     *
+     * @param ServerRequest $request
+     * @return JsonResponse
+     * @throws \Exception
+     */
+    public function getSiteInfosAction(ServerRequest $request) {
+        $this->assertIpAllowed();
+
+        $organizationId = $this->getOrganizationId($request);
+
+        $controller = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
+
+        $infos = $controller->getSiteInfosAction($organizationId);
+
+        return new JsonResponse($infos);
+    }
+
     /**
      * -- Target of the route 'site_create' --
      * >> Requires a query param named 'organization-id' (int)

+ 5 - 0
ot_admin/Configuration/Backend/Routes.php

@@ -7,6 +7,11 @@ use Opentalent\OtAdmin\Http\ApiController;
 
 return [
     // Create a new organization's website
+    'site_infos' => [
+        'path' => '/otadmin/site/infos',
+        'target' => ApiController::class . '::getSiteInfosAction',
+        'access' => 'public'
+    ],
     'site_create' => [
         'path' => '/otadmin/site/create',
         'target' => ApiController::class . '::createSiteAction',

+ 3 - 0
ot_admin/Configuration/Commands.php

@@ -10,6 +10,9 @@
 // https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/CommandControllers/Index.html#creating-a-new-command-in-extensions
 
 return [
+    'ot:site:infos' => [
+        'class' => Opentalent\OtAdmin\Command\GetSiteInfosCommand::class
+    ],
     'ot:site:create' => [
         'class' => Opentalent\OtAdmin\Command\CreateSiteCommand::class
     ],

+ 6 - 0
ot_admin/Configuration/Services.yaml

@@ -7,6 +7,12 @@ services:
   Opentalent\OtAdmin\:
     resource: '../Classes/*'
 
+  Opentalent\OtAdmin\Command\GetSiteInfosCommand:
+    tags:
+      - name: 'ot:site:infos'
+        command: 'ot:site:infos'
+        schedulable: false
+
   Opentalent\OtAdmin\Command\CreateOrganizationCommand:
     tags:
       - name: 'ot:site:create'