فهرست منبع

Merge branch 'feature/V8-6691-suppression-des-pages-de-typo3-e' into develop

Olivier Massot 1 سال پیش
والد
کامیت
39267fe9dd

+ 3 - 1
ot_admin/Classes/Command/ClearSiteCacheCommand.php

@@ -25,7 +25,9 @@ class ClearSiteCacheCommand extends Command
 {
     public function __construct(
         private readonly SiteController $siteController
-    ) {}
+    ) {
+        parent::__construct();
+    }
 
     /**
      * -- This method is expected by Typo3, do not rename ou remove --

+ 79 - 0
ot_admin/Classes/Command/DeleteUserCreatedPagesCommand.php

@@ -0,0 +1,79 @@
+<?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;
+
+/**
+ * This CLI command provoke the definitive deletion of all the pages
+ * created by the users, leaving only the starting mandatory pages.
+ *
+ * /!\ Warning: this is a destructive operation
+ *
+ * @package Opentalent\OtAdmin\Command
+ */
+class DeleteUserCreatedPagesCommand extends Command
+{
+    public function __construct(
+        private readonly SiteController $siteController
+    ) {
+        parent::__construct();
+    }
+
+    /**
+     * -- 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(): void
+    {
+        $this
+            ->setName("ot:site:delete-user-created-pages")
+            ->setDescription("Delete all the pages created by the users, leaving only the starting mandatory pages.")
+            ->setHelp("This CLI command provoke the definitive deletion of all the pages 
+                            created by the users, leaving only the starting mandatory pages. 
+                            /!\ Warning: this is a destructive operation.")
+            ->addArgument(
+                'organization-id',
+                InputArgument::OPTIONAL,
+                "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
+     * @return int
+     * @throws \Exception
+     */
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $org_id = $input->getArgument('organization-id');
+
+        $io = new SymfonyStyle($input, $output);
+
+        if (
+            !$io->confirm("Are you sure you want to delete all the pages created " .
+                "by the users? (organization id: " . $org_id . ")")
+        ) {
+            $io->error("Aborting.");
+            return 1;
+        }
+
+        $rootUid = $this->siteController->deleteUserCreatedPagesAction($org_id);
+
+        $io->success(sprintf("The website with root uid " . $rootUid . " had its user-created pages deleted."));
+
+        return 0;
+    }
+
+}

+ 1 - 3
ot_admin/Classes/Controller/ScanController.php

@@ -17,9 +17,7 @@ class ScanController extends ActionController
     public function __construct(
         private readonly SiteController $siteController,
         private readonly OrganizationRepository $organizationRepository
-    ) {
-        parent::__construct();
-    }
+    ) {}
 
     /**
      * Perform a full scan of the Typo3 DB, and confront it to the

+ 71 - 23
ot_admin/Classes/Controller/SiteController.php

@@ -5,6 +5,7 @@ namespace Opentalent\OtAdmin\Controller;
 use Doctrine\DBAL\Driver\Exception;
 use Opentalent\OtAdmin\Domain\Entity\SiteInfos;
 use Opentalent\OtAdmin\Domain\Entity\SiteStatus;
+use Opentalent\OtAdmin\Enum\OtPageTypeEnum;
 use Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException;
 use Opentalent\OtCore\Exception\NoSuchOrganizationException;
 use Opentalent\OtCore\Exception\NoSuchRecordException;
@@ -19,6 +20,7 @@ use Opentalent\OtCore\Utility\FileUtility;
 use PDO;
 use RuntimeException;
 use Symfony\Component\Yaml\Yaml;
+use Throwable;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
 use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
@@ -290,6 +292,7 @@ class SiteController extends ActionController
                 $rootUid,
                 'Accueil',
                 '/accueil',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 '',
                 [
                     'dokType' => self::DOK_SHORTCUT,
@@ -302,7 +305,8 @@ class SiteController extends ActionController
                 $websiteUid,
                 $rootUid,
                 'Présentation',
-                '/presentation'
+                '/presentation',
+                OtPageTypeEnum::MANDATORY_EDITABLE
             );
 
             // > 'Présentation > Qui sommes-nous ?' page (hidden by default)
@@ -311,6 +315,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/presentation'],
                 'Qui sommes nous?',
                 '/qui-sommes-nous',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 '',
                 ['hidden' => 1]
             );
@@ -321,6 +326,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/presentation'],
                 'Les adhérents',
                 '/les-adherents',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 self::TEMPLATE_MEMBERS
             );
 
@@ -330,6 +336,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/presentation'],
                 'Les membres du CA',
                 '/les-membres-du-ca',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 self::TEMPLATE_MEMBERSCA
             );
 
@@ -339,6 +346,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/presentation'],
                 'Informations Pratiques',
                 '/informations-pratiques',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 self::TEMPLATE_STRUCTUREDETAILS
             );
 
@@ -349,6 +357,7 @@ class SiteController extends ActionController
                     $this->createdPagesIndex['/presentation'],
                     'Les sociétés adhérentes',
                     '/societes-adherentes',
+                    OtPageTypeEnum::MANDATORY_EDITABLE,
                     self::TEMPLATE_STRUCTURES
                 );
             }
@@ -359,6 +368,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/presentation'],
                 'Historique',
                 '/historique',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 '',
                 ['hidden' => 1]
             );
@@ -371,6 +381,7 @@ class SiteController extends ActionController
                 $rootUid,
                 'Actualités',
                 '/actualites',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 self::TEMPLATE_NEWS,
                 ['hidden' => 1]
             );
@@ -389,6 +400,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/saison-en-cours'],
                 'Les évènements',
                 '/les-evenements',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 self::TEMPLATE_EVENTS
             );
 
@@ -399,6 +411,7 @@ class SiteController extends ActionController
                     $this->createdPagesIndex['/presentation'],
                     'Évènements des structures',
                     '/evenements-des-structures',
+                    OtPageTypeEnum::MANDATORY_EDITABLE,
                     self::TEMPLATE_STRUCTURESEVENTS
                 );
             }
@@ -409,6 +422,7 @@ class SiteController extends ActionController
                 $rootUid,
                 'Vie interne',
                 '/vie-interne',
+                OtPageTypeEnum::NON_MANDATORY,
                 '',
                 [
                     'hidden' => 1,
@@ -422,6 +436,7 @@ class SiteController extends ActionController
                 $rootUid,
                 'Footer',
                 '/footer',
+                OtPageTypeEnum::MANDATORY_NON_EDITABLE,
                 '',
                 [
                     'dokType' => self::DOK_FOLDER,
@@ -435,6 +450,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/footer'],
                 'Contact',
                 '/contact',
+                OtPageTypeEnum::MANDATORY_NON_EDITABLE,
                 self::TEMPLATE_CONTACT
             );
 
@@ -443,7 +459,8 @@ class SiteController extends ActionController
                 $websiteUid,
                 $this->createdPagesIndex['/footer'],
                 'Plan du site',
-                '/plan-du-site'
+                '/plan-du-site',
+                OtPageTypeEnum::MANDATORY_NON_EDITABLE
             );
 
             // > 'Footer > Mentions légales' page
@@ -452,6 +469,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/footer'],
                 'Mentions légales',
                 '/mentions-legales',
+                OtPageTypeEnum::MANDATORY_NON_EDITABLE,
                 self::TEMPLATE_LEGAL
             );
 
@@ -461,6 +479,7 @@ class SiteController extends ActionController
                 $this->createdPagesIndex['/presentation'],
                 'Contact',
                 '/ecrivez-nous',
+                OtPageTypeEnum::MANDATORY_EDITABLE,
                 '',
                 [
                     'dokType' => self::DOK_SHORTCUT,
@@ -474,6 +493,7 @@ class SiteController extends ActionController
                 $rootUid,
                 'Page introuvable',
                 '/page-introuvable',
+                OtPageTypeEnum::NON_MANDATORY,
                 '',
                 [
                     'nav_hide' => 1,
@@ -1726,7 +1746,7 @@ class SiteController extends ActionController
         // @see https://ressources.opentalent.fr/display/EX/Droits+des+BE+Users
         foreach ($pages as $page) {
 
-            if ($page['is_siteroot']) {
+            if ($page['ot_page_type'] === OtPageTypeEnum::ROOT) {
 
                 $adminPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE;
                 if ($isPremium)  {
@@ -1734,26 +1754,14 @@ class SiteController extends ActionController
                 }
                 $editorsPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT;
 
-            } else if (
-                $page['slug'] === '/footer' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->legal' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->contact' ||
-                $page['slug'] === '/plan-du-site'
-            ) {
+            } else if ($page['ot_page_type'] === OtPageTypeEnum::MANDATORY_NON_EDITABLE) {
                 $adminPerms = self::PERM_SHOW;
                 if ($isPremium)  {
                     $adminPerms += self::PERM_NEW;
                 }
                 $editorsPerms = self::PERM_SHOW;
 
-            } else if (
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->members' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->membersCa' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->structures' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->events' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->structuresEvents' ||
-                $page['tx_fed_page_controller_action'] === 'OpenTalent.OtTemplating->structureDetails'
-            ) {
+            } else if ($page['ot_page_type'] === OtPageTypeEnum::MANDATORY_EDITABLE) {
                 $adminPerms = self::PERM_SHOW;
                 if ($isPremium)  {
                     $adminPerms += self::PERM_NEW + self::PERM_EDIT_PAGE;
@@ -1817,6 +1825,42 @@ class SiteController extends ActionController
         }
     }
 
+    /**
+     * Delete all the pages created by the users, leaving only the starting mandatory pages.
+     *
+     * @param int $organizationId
+     * @return int
+     * @throws NoSuchRecordException
+     * @throws NoSuchWebsiteException
+     * @throws \Doctrine\DBAL\Exception
+     * @throws Throwable
+     */
+    public function deleteUserCreatedPagesAction(int $organizationId): int
+    {
+        $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
+        $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
+
+        $pages = $this->otPageRepository->getPageWithSubpages($rootUid);
+
+        $this->connectionPool->getConnectionByName('Default')->beginTransaction();
+
+        try {
+            foreach($pages as $page) {
+                if ($page['ot_page_type'] === OtPageTypeEnum::USER_CREATED->value) {
+                    $this->delete('tt_content', 'pid', $page['uid'], true);
+                    $this->delete('pages', 'uid', $page['uid'], true);
+                }
+            }
+
+            $this->connectionPool->getConnectionByName('Default')->commit();
+        } catch (\Throwable $e) {
+            $this->connectionPool->getConnectionByName('Default')->rollBack();
+            throw $e;
+        }
+
+        return $rootUid;
+    }
+
     /**
      * Delete then regenerate all the typo3 sites yaml config files
      *
@@ -1995,16 +2039,18 @@ class SiteController extends ActionController
      * @param int $pid
      * @param string $title
      * @param string $slug
+     * @param OtPageTypeEnum $otPageType
      * @param string $template
      * @param array $moreValues
      * @return int
      */
-    private function insertPage(int $website_uid,
-                                int $pid,
-                                string $title,
-                                string $slug,
-                                string $template = '',
-                                array $moreValues = []
+    private function insertPage(int            $website_uid,
+                                int            $pid,
+                                string         $title,
+                                string         $slug,
+                                OtPageTypeEnum $otPageType = OtPageTypeEnum::MANDATORY_EDITABLE,
+                                string         $template = '',
+                                array          $moreValues = []
                                 ): int
     {
         $defaultValues = [
@@ -2017,6 +2063,7 @@ class SiteController extends ActionController
             'backend_layout' => 'flux__grid',
             'backend_layout_next_level' => 'flux__grid',
             'ot_website_uid' => $website_uid,
+            'ot_page_type' => $otPageType->value
         ];
 
         if ($template) {
@@ -2052,6 +2099,7 @@ class SiteController extends ActionController
             $this->getParentFolderUid(),
             $title,
             '/',
+            OtPageTypeEnum::ROOT,
             self::TEMPLATE_HOME,
             [
                 'is_siteroot' => 1,

+ 13 - 0
ot_admin/Classes/Enum/OtPageTypeEnum.php

@@ -0,0 +1,13 @@
+<?php
+declare(strict_types=1);
+
+namespace Opentalent\OtAdmin\Enum;
+
+enum OtPageTypeEnum: string
+{
+    case ROOT = 'ROOT';
+    case MANDATORY_EDITABLE = 'MANDATORY_EDITABLE';
+    case MANDATORY_NON_EDITABLE = 'MANDATORY_NON_EDITABLE';
+    case NON_MANDATORY = 'NON_MANDATORY';
+    case USER_CREATED = 'USER_CREATED';
+}

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

@@ -268,6 +268,8 @@ class ApiController implements LoggerAwareInterface
      * shall be named 'Confirmation-Token' and its value shall be DEL-XXXX-YYYYMMDD, where XXXX is the id of
      * the organization owning the website, and YYYYMMDD is the date of the current day.
      *
+     * /!\ Warning: this is a destructive operation
+     *
      * @param ServerRequest $request
      * @return JsonResponse
      * @throws \Exception
@@ -478,4 +480,36 @@ class ApiController implements LoggerAwareInterface
 
         return new JsonResponse($results);
     }
+
+    /**
+     * -- Target of the route 'delete-user-created-pages' --
+     * >> Requires a query param named 'organization-id' (int)
+     *
+     * Delete all user-created pages for the organization's website
+     *
+     * /!\ Warning: this is a destructive operation
+     *
+     * @param ServerRequest $request
+     * @return JsonResponse
+     * @throws \Exception
+     */
+    public function deleteUserCreatedPagesAction(ServerRequest $request): JsonResponse
+    {
+        $this->assertIpAllowed();
+
+        $organizationId = $this->getOrganizationId($request);
+
+        $this->preventIfIsDubious();
+        $this->preventOnMissingConfirmationToken($organizationId);
+
+        $rootUid = $this->siteController->deleteUserCreatedPagesAction($organizationId);
+
+        return new JsonResponse(
+            [
+                'organization_id' => $organizationId,
+                'msg' => "The website with root uid " . $rootUid . " had its user-created pages deleted.",
+                'root_uid' => $rootUid
+            ]
+        );
+    }
 }

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

@@ -56,5 +56,10 @@ return [
         'path' => '/otadmin/scan',
         'target' => ApiController::class . '::scanAllAction',
         'access' => 'public'
+    ],
+    'delete-user-created-pages' => [
+        'path' => '/otadmin/site/delete-user-created-pages',
+        'target' => ApiController::class . '::deleteUserCreatedPagesAction',
+        'access' => 'public'
     ]
 ];

+ 6 - 0
ot_admin/Configuration/Services.yaml

@@ -72,3 +72,9 @@ services:
       - name: console.command
         command: 'ot:site:scan'
         schedulable: true
+
+  Opentalent\OtAdmin\Command\DeleteUserCreatedPagesCommand:
+    tags:
+      - name: console.command
+        command: 'ot:site:delete-user-created-pages'
+        schedulable: false

+ 3 - 1
ot_admin/ext_tables.sql

@@ -2,9 +2,11 @@
 
 #
 # Table structure for table 'pages'
+# Possible values for ot_page_type are : ROOT, MANDATORY_EDITABLE, MANDATORY_NON_EDITABLE, NON_MANDATORY, USER_CREATED
 #
 CREATE TABLE pages (
-   manually_deleted smallint(5) unsigned NOT NULL DEFAULT 0
+    manually_deleted smallint(5) unsigned NOT NULL DEFAULT 0,
+    ot_page_type text DEFAULT 'USER_CREATED'
 );
 
 #

+ 1 - 1
ot_core/Classes/Domain/Repository/DonorRepository.php

@@ -65,7 +65,7 @@ class DonorRepository extends BaseApiRepository
         $donor->setWebsite($record['website']);
         $donor->setWording($record['wording']);
         $donor->setDisplayedOn($record['displayedOn']);
-        $donor->setLogo($record['logo']);
+        $donor->setLogo($record['logo'] ?? null);
 
         return $donor;
     }