From 0b1e3ff096b4c1ec6e0629b3a601be507e6e0d3f Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Thu, 28 Mar 2024 19:01:31 -0500 Subject: [PATCH] Internal: Migration: Create shortcut for lp and lp categories published on home #5088 --- .../Schema/V200/Version20180928172830.php | 7 ++ .../Schema/V200/Version20240327172830.php | 72 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/CoreBundle/Migrations/Schema/V200/Version20240327172830.php diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20180928172830.php b/src/CoreBundle/Migrations/Schema/V200/Version20180928172830.php index e16df5c0fff..d2378e95c9c 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20180928172830.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20180928172830.php @@ -48,6 +48,13 @@ public function up(Schema $schema): void } if ($table->hasColumn('link')) { + $lpTools = $this->connection + ->prepare("SELECT c_id, session_id, link FROM c_tool WHERE link LIKE '%lp_controller.php%'") + ->executeQuery() + ->fetchAllAssociative(); + + $this->writeFile('tool_links', serialize($lpTools)); + $this->addSql('ALTER TABLE c_tool DROP link'); } diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20240327172830.php b/src/CoreBundle/Migrations/Schema/V200/Version20240327172830.php new file mode 100644 index 00000000000..56a24cb9730 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20240327172830.php @@ -0,0 +1,72 @@ +readFile('tool_links'); + + if (empty($toolLinksContent)) { + $this->write('tool_links file not found. Exiting.'); + + return; + } + + $toolLinks = unserialize($toolLinksContent); + + $lpRepo = $this->container->get(CLpRepository::class); + $lpCategoryRepo = $this->container->get(CLpCategoryRepository::class); + $shortcutRepo = $this->container->get(CShortcutRepository::class); + + foreach ($toolLinks as $toolLink) { + $url = parse_url($toolLink['link']); + $query = []; + parse_str($url['query'] ?? '', $query); + + $admin = $this->getAdmin(); + $course = $this->findCourse($toolLink['c_id']); + $session = $this->findSession($toolLink['session_id']); + $resource = null; + + if (str_contains($url['path'], 'lp_controller.php') && isset($query['action'])) { + if (isset($query['lp_id']) && 'view' === $query['action']) { + $resource = $lpRepo->find($query['lp_id']); + } elseif (isset($query['id']) && 'view_category' === $query['action']) { + $resource = $lpCategoryRepo->find($query['id']); + } + } + + if ($resource) { + $shortcut = $shortcutRepo->getShortcutFromResource($resource); + + if ($shortcut) { + continue; + } + + $shortcutRepo->addShortCut($resource, $admin, $course, $session); + } + } + + $this->entityManager->flush(); + + $this->removeFile('tool_links'); + } +}