From 2bb81a50a820c6383f1c753e8c5a47866fa6f265 Mon Sep 17 00:00:00 2001 From: Fabrice Gangler <fabrice.gangler@adullact.org> Date: Thu, 20 Feb 2025 12:12:04 +0100 Subject: [PATCH] feat: import organizations --- .../src/Controller/RecupOldDataController.php | 344 +++++++++++++++++- 1 file changed, 339 insertions(+), 5 deletions(-) diff --git a/webapp/src/Controller/RecupOldDataController.php b/webapp/src/Controller/RecupOldDataController.php index 0ae720a..8941865 100644 --- a/webapp/src/Controller/RecupOldDataController.php +++ b/webapp/src/Controller/RecupOldDataController.php @@ -21,11 +21,17 @@ namespace App\Controller; +use App\Entity\Organization; +use App\Entity\OrganizationI18n; +use App\Entity\OrganizationType; use App\Entity\Software; use App\Entity\SoftwareI18n; use App\Entity\SoftwareScreenshot; use App\Entity\Tag; use App\Entity\User; +use App\Repository\OrganizationI18nRepository; +use App\Repository\OrganizationRepository; +use App\Repository\OrganizationTypeRepository; use App\Repository\SoftwareI18nRepository; use App\Repository\SoftwareRepository; use App\Repository\SoftwareScreenshotRepository; @@ -235,7 +241,7 @@ private function cacheOldDataFromCustomApi( name: 'app_sysamdin_migration_index', methods: ['GET'] )] - public function migrationOldDataIndex(): Response + public function index(): Response { $content = ''; $content .= '<h1>Import </h1>'; @@ -249,7 +255,7 @@ public function migrationOldDataIndex(): Response $content .= '<a href="/sysamdin/migration/recup_old_data_of_screenshots">Get all Screenshot files</a><br>'; $content .= '<a href="/sysamdin/migration/recup_old_data_of_software_logo">Get all Software logos</a><br>'; $content .= '<hr>'; - $content .= '<a href="/sysamdin/migration/recup_old_data_of_users_logo">Get all Users logos</a><br>'; + $content .= '<a href="/sysamdin/migration/recup_old_data_user_photos">Get all Users logos</a><br>'; $content .= '<hr>'; $content .= '<a href="/sysamdin/migration/recup_old_data_of_all_software">Import SOFTWARE</a><br>'; $content .= '<a href="/sysamdin/migration/recup_old_data_of_all_software?clearCache=true"> @@ -261,7 +267,12 @@ public function migrationOldDataIndex(): Response $content .= '<hr>'; $content .= '<a href="/sysamdin/migration/import_old_data_of_screenshots">Import software screenshots</a><br>'; $content .= '<a href="/sysamdin/migration/import_old_data_of_software_logo">Import software logos</a><br>'; + $content .= '<a href="/sysamdin/migration/import_old_data_of_user_photo">Import user avatar</a><br>'; $content .= '<hr>'; + $content .= '<a href="/sysamdin/migration/recup_old_data_users_accounts">Import USER accounts</a><br>'; + $content .= '<a href="/sysamdin/migration/recup_old_data_organizations">Import ORGANIZATIONS</a><br>'; + $content .= '<hr>'; + return new Response(content: $content,); } @@ -477,6 +488,92 @@ public function importSoftwareLogos( } + #[Route( + path: '/sysamdin/migration/recup_old_data_user_photos', + name: 'app_sysamdin_migration_recup_old_data_user_photos', + methods: ['GET'] + )] + public function getOldDataUserAvatar( + Filesystem $filesystem, + HttpClientInterface $httpClient, + UserRepository $userRepository, + OrganizationRepository $organizationRepository, + OrganizationTypeRepository $organizationTypeRepository, + OrganizationI18nRepository $organizationI18nRepository, + #[MapQueryParameter] bool $clearCache = false, + ) { + $this->httpClient = $httpClient; + $type = "users_via-opendata_private"; + + $jsonFilePath = '../../comptoir_users_dev_2024.10.30_11h00.38.json'; + if (false === $filesystem->exists("$jsonFilePath")) { + throw new IOException(sprintf('Failed to read "%s": ', $jsonFilePath)); + } + $data = $this->getJsonDataFromFile($jsonFilePath); + + $imageDirectoryPath = $this->getCacheDirectoryLogoPath($type); + if (!is_dir("$imageDirectoryPath")) { + dump("----> Get " . strtoupper($type) . " data, and cache it."); + try { + $filesystem->mkdir("$imageDirectoryPath", 0770); + } catch (IOExceptionInterface $exception) { + echo "An error occurred while creating your directory at " . $exception->getPath(); + } + } + + + + foreach ($data['users'] as $userData) { +// dump($userData); exit(); + // "user_logo_directory" => "files/Users/photo/1078/avatar" + // "user_logo_image" => "logo_ville.jfif" + // "user_logo_url" => "https://comptoir-du-libre.org/img/files/Users/photo/1078/avatar/logo_ville.jfif" + + // Extract data + $userOldComptoirId = $userData['user_id']; + $userName = trim($userData['user_name']); + $logoDirectory = trim($userData['user_logo_directory']); + $logoFile = trim($userData['user_logo_image']); + $logoUrl = trim($userData['user_logo_url']); + if (empty($logoUrl)) { + continue; + } + echo "$userOldComptoirId - $userName<br>"; + echo $logoDirectory . "<br>"; + echo $logoFile . "<br>"; + + $response = $this->httpClient->request('GET', "$logoUrl"); + $statusCode = $response->getStatusCode(); // 200 + echo $statusCode . " $logoUrl<br>"; + if ($statusCode === 200) { + if (isset($response->getHeaders()['content-type'][0])) { + $contentType = $response->getHeaders()['content-type'][0]; // application/json + echo $contentType . "<br>"; + } else { // TODO add log + echo 'DEBUG ----> Content Type' . "<br>"; + } + + if (!is_dir("$imageDirectoryPath/$userOldComptoirId")) { + try { + $filesystem->mkdir("$imageDirectoryPath/$userOldComptoirId", 0770); + } catch (IOExceptionInterface $exception) { + echo "An error occurred while creating your directory at " . $exception->getPath(); + } + } + + $imagePath = "$imageDirectoryPath/$userOldComptoirId/$logoFile"; + $filesystem->dumpFile("$imagePath", $response->getContent()); + echo $imagePath . "<hr>"; + } elseif ($statusCode === 404) { + } + echo "<hr>"; + } + exit(); + } + + + + #[Route( path: '/sysamdin/migration/recup_old_data_of_software_logo', name: 'app_sysamdin_migration_recup_old_data_of_software_logo', @@ -509,11 +606,16 @@ public function getOldDataSofwareLogos( $logoDirectory = $softwareData['logo_directory']; $logoFile = $softwareData['logo_file']; $logoUrl = $softwareData['logo_url']; - $response = $this->httpClient->request('GET', "$logoUrl"); - $statusCode = $response->getStatusCode(); // 200 + echo "$softwareId - $softwareName<br>"; echo $logoDirectory . "<br>"; echo $logoFile . "<br>"; + $statusCode = 900; + if (!empty($logoUrl)) { + $response = $this->httpClient->request('GET', "$logoUrl"); + $statusCode = $response->getStatusCode(); // 200 + } + echo $statusCode . " $logoUrl<br>"; if ($statusCode === 200) { if (isset($response->getHeaders()['content-type'][0])) { @@ -534,6 +636,8 @@ public function getOldDataSofwareLogos( $imagePath = "$imageDirectoryPath/$softwareId/$logoFile"; $filesystem->dumpFile("$imagePath", $response->getContent()); echo $imagePath . "<hr>"; + } elseif ($statusCode === 900) { + echo "pas d'image associé à ce logiciel"; } elseif ($statusCode === 404) { } echo "<hr>"; @@ -935,7 +1039,8 @@ public function getOldDataUsersAccount( UserRepository $userRepository, #[MapQueryParameter] bool $clearCache = false, ) { - $jsonFilePath = '../../comptoir_users_dev_2024.10.30_11h00.38.json'; + $type = "users_via-opendata_private"; + $jsonFilePath = '../../comptoir_users_dev.json'; if (false === $filesystem->exists("$jsonFilePath")) { throw new IOException(sprintf('Failed to read "%s": ', $jsonFilePath)); } @@ -1014,4 +1119,233 @@ public function getOldDataUsersAccount( dump($roles); exit(); } + + + #[Route( + path: '/sysamdin/migration/recup_old_data_organizations', + name: 'app_sysamdin_migration_recup_old_data_organizations', + methods: ['GET'] + )] + public function getOldDataOrganizations( + Filesystem $filesystem, + HttpClientInterface $httpClient, + UserRepository $userRepository, + OrganizationRepository $organizationRepository, + OrganizationTypeRepository $organizationTypeRepository, + OrganizationI18nRepository $organizationI18nRepository, + #[MapQueryParameter] bool $clearCache = false, + ) { + $type = "users_via-opendata_private"; + $jsonFilePath = '../../comptoir_users_dev_2024.10.30_11h00.38.json'; + if (false === $filesystem->exists("$jsonFilePath")) { + throw new IOException(sprintf('Failed to read "%s": ', $jsonFilePath)); + } + $data = $this->getJsonDataFromFile($jsonFilePath); + $roles = []; + foreach ($data['users'] as $orgData) { +// dump($orgData); +// exit(); + + // Extract data + $userEmail = $orgData['user_email']; + $orgOldComptoirId = $orgData['user_id']; + $orgTypeName = $orgData['user_type']; + $orgCreated = $orgData['user_created']; + $orgModified = $orgData['user_modified']; + $orgName = trim($orgData['user_name']); + $orgWebsite = trim($orgData['user_website']); + $orgDescription = trim($orgData['user_description']); +// $orgLogo = trim($orgData['user_logo_image']); + + if ($orgTypeName === 'Person') { + continue; + } + + // Compute data + $slugger = new AsciiSlugger(); + $orgSlug = u($slugger->slug("$orgName")->toString())->lower()->toString(); + $accountCreateAt = new \DateTimeImmutable($orgCreated); + $accountUpdateAt = new \DateTimeImmutable($orgModified); + + + // Import new data ---> TODO use OLD user ID instead of user EMAIL + $user = $userRepository->findOneBy(['email' => $userEmail]); +// $user = $userRepository->findOneBy(['old_comptoir_id' => $userOldComptoirId]); + if (\is_null($user)) { + echo "ERROR : $userEmail account not found"; + // TODO log + } + + if ($orgTypeName === 'Company') { + $orgTypeName = 'company'; + } elseif ($orgTypeName === 'Administration') { + $orgTypeName = 'public_sector'; + } elseif ($orgTypeName === 'Association') { + $orgTypeName = 'no_profit'; + } else { + throw new \Exception("Organization type [ $orgTypeName ] not yet define"); + } + $orgType = $organizationTypeRepository->findOneBy(['name' => $orgTypeName]); + if (\is_null($orgType)) { + $orgType = new OrganizationType(); + $orgType->setName($orgTypeName); + $organizationTypeRepository->save($orgType, true); + echo "<br>-----> ADD [ $orgTypeName ] type"; + } + + $organization = $organizationRepository->findOneBy(['old_comptoir_id' => $orgOldComptoirId]); + if (\is_null($organization)) { + $organization = new Organization(); + $organization->setName($orgName); + $organization->setSlug($orgSlug); +// $organization->setLogo($orgLogo); + $organization->setWebsite($orgWebsite); + $organization->setOldComptoirId($orgOldComptoirId); + $organization->setCreatedAt($accountCreateAt); + $organization->setUpdatedAt($accountUpdateAt); + $organization->setType($orgType); + $organizationRepository->save($organization, true); + if (!empty($orgDescription)) { + $organizationI18n = new OrganizationI18n(); + $organizationI18n->setOrganization($organization); + $organizationI18n->setLocale('fr'); + $organizationI18n->setType(1); + $organizationI18n->setText($orgDescription); + $organizationI18nRepository->save($organizationI18n, true); + } + echo "<br>-----> ADD $orgOldComptoirId - $orgName"; + continue; + } + + + // Update data +// if ($user->getEmail() !== $userEmail) { +// $user->setEmail($userEmail); +// $update = true; +// } +// if ($user->getPassword() !== $userPassword) { +// $user->setPassword($userPassword); +// $update = true; +// } +// if ($user->getOldComptoirId() !== $userOldComptoirId) { +// $user->setOldComptoirId($userOldComptoirId); +// $update = true; +// } +// if ($user->getCreatedAt()->format('Y-m-d H:i:s') !== $accountCreateAt->format('Y-m-d H:i:s')) { +// $user->setCreatedAt($accountCreateAt); +// $update = true; +// } +// if ($user->getUpdatedAt()->format('Y-m-d H:i:s') !== $accountUpdateAt->format('Y-m-d H:i:s')) { +// $user->setUpdatedAt($accountUpdateAt); +// $update = true; +// } +// if ($user->getRoles() !== $userRoles) { +// $user->setRoles($userRoles); +// $update = true; +// } +// +// if (true === $update) { +// $userRepository->save($user, true); +// echo "<br>-----> UPDATE $userEmail"; +// } + } + dump($user); + dump($roles); + exit(); + } + + + + #[Route( + path: '/sysamdin/migration/import_old_data_of_user_photo', + name: 'app_sysamdin_migration_import_old_data_of_user_photo', + methods: ['GET'] + )] + public function getImportOldUserPhoto( + PublicDirectory $publicDirectory, + Filesystem $filesystem, + HttpClientInterface $httpClient, + UserRepository $userRepository, + OrganizationRepository $organizationRepository, + OrganizationTypeRepository $organizationTypeRepository, + OrganizationI18nRepository $organizationI18nRepository, + #[MapQueryParameter] bool $clearCache = false, + ) { + $type = "users_via-opendata_private"; + $jsonFilePath = '../../comptoir_users_dev_2024.10.30_11h00.38.json'; + if (false === $filesystem->exists("$jsonFilePath")) { + throw new IOException(sprintf('Failed to read "%s": ', $jsonFilePath)); + } + + $cacheDirectory = $this->getCacheDirectoryBase($type); + $cacheImageDirectoryPath = "$cacheDirectory/logos"; + echo $cacheImageDirectoryPath . "<hr>"; + print_r($cacheImageDirectoryPath); + + $data = $this->getJsonDataFromFile($jsonFilePath); + $roles = []; + $slugger = new AsciiSlugger(); + foreach ($data['users'] as $orgData) { +// dump($orgData); +// exit(); + + // Extract data + $userOldComptoirId = $orgData['user_id']; + $userTypeName = $orgData['user_type']; + $userName = trim($orgData['user_name']); + $userLogo = trim($orgData['user_logo_image']); + $srcLogoFile = $userLogo; + $srcLogoPath = "$cacheImageDirectoryPath/$userOldComptoirId/$userLogo"; + $srcLogoInfo = pathinfo($srcLogoPath); + echo "<hr>$userOldComptoirId - $userTypeName - $userName<br>"; + echo "<pre>" . print_r($srcLogoInfo, true) . "</pre>"; + + if (!isset($srcLogoInfo['extension'])) { // TODO fixme + continue; + } elseif (!is_file($srcLogoPath)) { + continue; + } + + if ($userTypeName === 'Person') { + $user = $userRepository->findOneBy(['old_comptoir_id' => $userOldComptoirId]); + continue; + } + + + // Import new data +// if (\is_null($user)) { +// echo "ERROR : $userEmail account not found"; +// exit(); +// } + + $organization = $organizationRepository->findOneBy(['old_comptoir_id' => $userOldComptoirId]); + dump($organization); + dump($srcLogoFile); + dump(!\is_null($organization)); + dump(!empty($srcLogoFile)); + dump(!\is_null($organization) && !empty($srcLogoFile)); + + if (!\is_null($organization) && !empty($srcLogoFile)) { + dump(!\is_null($organization) && !empty($srcLogoFile)); + exit(); + $organizationId = $organization->getId(); + $publicDirectory->createPublicDirectory( + $this->getContentImagesDirectoryBasePath() . "organization/$organizationId/" + ); + $targetDirectory = $this->getContentImagesDirectoryBasePath() . "organization/$organizationId/logo"; + $publicDirectory->createPublicDirectory(pathDirectory: "$targetDirectory", clean: true); + + $slug = $slugger->slug($srcLogoFile); + $newImageName = $slugger->slug($srcLogoInfo['filename'])->toString() . '.' . $srcLogoInfo['extension'] ; + $filesystem->copy( + originFile: "$srcLogoPath", + targetFile: "$targetDirectory/$newImageName" + ); + $organization->setLogo("$newImageName"); + $organizationRepository->save($organization, true); + echo "<pre>" . print_r(pathinfo("$targetDirectory/$newImageName"), true) . "</pre>"; + } + } + exit(); + } } -- GitLab