Przeglądaj źródła

implement the custom domains definition

Olivier Massot 4 lat temu
rodzic
commit
a2b515aeea

+ 50 - 8
ot_admin/Classes/Controller/SiteController.php

@@ -171,7 +171,7 @@ class SiteController extends ActionController
             $rootPage['tx_opentalent_matomo_id'],
             self::IS_PRODUCT_PREMIUM[$organizationExtraData['admin']['product']] ?? false,
             (bool)$rootPage['deleted'],
-            (bool)($rootPage['hidden'] || $rootPage['fe_group'] < 0),
+            ($rootPage['hidden'] || $rootPage['fe_group'] < 0),
             null,
             null,
             $rootPage['perms_userid'],
@@ -476,7 +476,7 @@ class SiteController extends ActionController
 
             // Build and update the domain
             if ($mode == self::MODE_PROD) {
-                $domain = $organization->getSubDomain() .  '.opentalent.fr';
+                $domain = $this->getOrganizationDomain($organizationId);
             } elseif ($mode == self::MODE_DEV) {
                 $domain = $organization->getSubDomain() . '/';
             } else {
@@ -620,7 +620,7 @@ class SiteController extends ActionController
      *
      * - Update the pages table (structure id, structure domain)
      * - (deep update only) Update the config.yaml file
-     * - Update the `sys_template`.`constants` field
+     * - Update the `sys_template`.`constants` and the `pages`.`TSConfig` fields
      * - (deep update only) Reset the users permissions
      * - [todo] Reset the routing index
      * - Clear the Typo3 cache for the website
@@ -640,7 +640,7 @@ class SiteController extends ActionController
         $rootUid = $this->findRootUidFor($organizationId);
 
         $organization = $this->fetchOrganization($organizationId);
-        $organizationDomain = $organization->getSubDomain() . '.opentalent.fr';
+        $organizationDomain = $this->getOrganizationDomain($organizationId);
 
         // This extra-data can not be retrieved from the API for now, but
         // this shall be set up as soon as possible, to avoid requesting
@@ -668,7 +668,7 @@ class SiteController extends ActionController
                 $this->writeConfigFile($organizationId, $rootUid, $organizationDomain, true);
             }
 
-            // ## Update the `sys_template`.`constants` field
+            // ## Update the `sys_template`.`constants` and the `pages`.`TSConfig` fields
             $constants = $this->getTemplateConstants($organizationId, $organizationExtraData);
 
             $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
@@ -678,6 +678,13 @@ class SiteController extends ActionController
                 ->where($queryBuilder->expr()->eq('pid', $rootUid))
                 ->execute();
 
+            $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
+            $queryBuilder
+                ->update('pages')
+                ->set('TSconfig', $this->getRootPageTsConfig($organization))
+                ->where($queryBuilder->expr()->eq('uid', $rootUid))
+                ->execute();
+
             // ## Reset the users permissions
             if ($deep) {
                 $this->resetBeUserPermsAction($organizationId, true);
@@ -1693,6 +1700,32 @@ class SiteController extends ActionController
         }
     }
 
+    /**
+     * Retrieves the current full domain of the given organization.
+     *
+     * @param int $organizationId
+     * @return string
+     */
+    private function getOrganizationDomain(int $organizationId): string
+    {
+        $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
+        $queryBuilder->getRestrictions()->removeAll();
+        $customDomain = $queryBuilder
+            ->select('domain')
+            ->from('tx_opentalent_custom_domains')
+            ->where($queryBuilder->expr()->eq('organization_id', $organizationId))
+            ->execute()
+            ->fetchColumn(0);
+
+        if ($customDomain) {
+            return $customDomain;
+        }
+
+        $organization = $this->fetchOrganization($organizationId);
+
+        return $organization->getSubDomain() . '.opentalent.fr';
+    }
+
     /**
      * Try to find the root page uid of the organization's website and return it.
      * Throw a Opentalent\OtAdmin\NoSuchWebsiteException exception if the website does not exist.
@@ -1830,7 +1863,7 @@ class SiteController extends ActionController
             'backend_layout' => 'flux__grid',
             'backend_layout_next_level' => 'flux__grid',
             'tx_opentalent_structure_id' => $organization->getId(),
-            'tx_opentalent_structure_domain' => $organization->getSubDomain() . '.opentalent.fr',
+            'tx_opentalent_structure_domain' => $this->getOrganizationDomain($organization->getId()),
         ];
 
         if ($template) {
@@ -1868,8 +1901,7 @@ class SiteController extends ActionController
             self::TEMPLATE_HOME,
             [
                 'is_siteroot' => 1,
-                'TSconfig' => 'TCAdefaults.pages.tx_opentalent_structure_id =' . $organization->getId() . '\n' .
-                              'TCAdefaults.pages.tx_opentalent_structure_domain = ' . $organization->getSubDomain() .  '.opentalent.fr',
+                'TSconfig' => $this->getRootPageTsConfig($organization),
                 'tx_opentalent_template' => self::DEFAULT_THEME,
                 'tx_opentalent_template_preferences' => '{"themeColor":"' . self::DEFAULT_COLOR . '","displayCarousel":"1"}'
             ]
@@ -1999,6 +2031,16 @@ class SiteController extends ActionController
             "}";
     }
 
+    /**
+     * Return the TSConfig for the root page of the given organization
+     *
+     * @param Organization $organization
+     * @return string
+     */
+    private function getRootPageTsConfig(Organization $organization): string {
+        return "TCAdefaults.pages.tx_opentalent_structure_id=" . $organization->getId() . "\r\n" .
+               "TCAdefaults.pages.tx_opentalent_structure_domain=" . $this->getOrganizationDomain($organization->getId());
+    }
 
     /**
      * Create the given directory, give its property to the www-data group and

+ 10 - 0
ot_core/ext_tables.sql

@@ -8,6 +8,16 @@ CREATE TABLE pages (
 	tx_opentalent_structure_domain varchar(255)
 );
 
+#
+# Table structure for table 'tx_opentalent_custom_domains'
+#
+CREATE TABLE tx_opentalent_custom_domains
+(
+    uid INT UNSIGNED NOT NULL,
+    organization_id INT NOT NULL,
+    domain VARCHAR(2048) NOT NULL
+);
+
 #
 # Table structure for table 'tx_opentalent_log'
 #