From ed9c9d8654333b405e36747a0a31e083f5da6fdd Mon Sep 17 00:00:00 2001 From: "John.R" Date: Wed, 22 Nov 2023 10:58:09 +0100 Subject: [PATCH] feat: sync translations --- controllers/front/apiTranslations.php | 25 ++++++ src/Config/Config.php | 1 + src/Decorator/TranslationDecorator.php | 54 ++++++++++++ src/Provider/TranslationDataProvider.php | 91 ++++++++++++++++++++ src/Repository/TranslationRepository.php | 104 +++++++++++++++++++++++ 5 files changed, 275 insertions(+) create mode 100755 controllers/front/apiTranslations.php create mode 100755 src/Decorator/TranslationDecorator.php create mode 100755 src/Provider/TranslationDataProvider.php create mode 100755 src/Repository/TranslationRepository.php diff --git a/controllers/front/apiTranslations.php b/controllers/front/apiTranslations.php new file mode 100755 index 00000000..2528c2c5 --- /dev/null +++ b/controllers/front/apiTranslations.php @@ -0,0 +1,25 @@ +module->getService(TranslationDataProvider::class); + + $response = $this->handleDataSync($translationDataProvider); + + $this->exitWithResponse($response); + } +} diff --git a/src/Config/Config.php b/src/Config/Config.php index 9c0916d7..98ba5187 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -56,6 +56,7 @@ class Config public const COLLECTION_SUPPLIERS = 'suppliers'; public const COLLECTION_PRODUCT_SUPPLIERS = 'product_suppliers'; public const COLLECTION_LANGUAGES = 'languages'; + public const COLLECTION_TRANSLATIONS = 'translations'; /** * @param mixed $message diff --git a/src/Decorator/TranslationDecorator.php b/src/Decorator/TranslationDecorator.php new file mode 100755 index 00000000..048d6f07 --- /dev/null +++ b/src/Decorator/TranslationDecorator.php @@ -0,0 +1,54 @@ +timezone = (string) $configurationRepository->get('PS_TIMEZONE'); + $this->createdAt = $shopRepository->getCreatedAt(); + } + + /** + * @param array $translations + * + * @return void + */ + public function decorateTranslations(array &$translations) + { + foreach ($translations as &$translation) { + $this->castPropertyValues($translation); + } + } + + /** + * @param array $translation + * + * @return void + */ + private function castPropertyValues(array &$translation) + { + $translation['id_translation'] = (int) $translation['id_translation']; + $translation['id_lang'] = (int) $translation['id_lang']; + $translation['key'] = (string) $translation['key']; + $translation['translation'] = (string) $translation['translation']; + $translation['domain'] = (string) $translation['domain']; + $translation['theme'] = (string) $translation['theme']; + } +} diff --git a/src/Provider/TranslationDataProvider.php b/src/Provider/TranslationDataProvider.php new file mode 100755 index 00000000..83cbb57c --- /dev/null +++ b/src/Provider/TranslationDataProvider.php @@ -0,0 +1,91 @@ +translationRepository = $translationRepository; + $this->translationDecorator = $translationDecorator; + } + + /** + * @param int $offset + * @param int $limit + * @param string $langIso + * + * @return array + * + * @throws \PrestaShopDatabaseException + */ + public function getFormattedData($offset, $limit, $langIso) + { + $translations = $this->translationRepository->getTranslationsSync($offset, $limit); + + if (!is_array($translations)) { + return []; + } + $this->translationDecorator->decorateTranslations($translations); + + return array_map(function ($translation) { + return [ + 'id' => $translation['id_lang'], + 'collection' => Config::COLLECTION_TRANSLATIONS, + 'properties' => $translation, + ]; + }, $translations); + } + + /** + * @param int $offset + * @param string $langIso + * + * @return int + */ + public function getRemainingObjectsCount($offset, $langIso) + { + return (int) $this->translationRepository->getRemainingTranslationsCount($offset); + } + + /** + * @param int $limit + * @param string $langIso + * @param array $objectIds + * + * @return array + * + * @throws \PrestaShopDatabaseException + */ + public function getFormattedDataIncremental($limit, $langIso, $objectIds) + { + $translations = $this->translationRepository->getTranslationsIncremental($limit, $objectIds); + + if (!is_array($translations)) { + return []; + } + $this->translationDecorator->decorateTranslations($translations); + + return array_map(function ($translation) { + return [ + 'id' => $translation['id_lang'], + 'collection' => Config::COLLECTION_TRANSLATIONS, + 'properties' => $translation, + ]; + }, $translations); + } +} diff --git a/src/Repository/TranslationRepository.php b/src/Repository/TranslationRepository.php new file mode 100755 index 00000000..fe2e9a3e --- /dev/null +++ b/src/Repository/TranslationRepository.php @@ -0,0 +1,104 @@ +db = $db; + $this->context = $context; + } + + /** + * @param int $shopId + * + * @return \DbQuery + */ + public function getBaseQuery($shopId) + { + $query = new \DbQuery(); + $query->from('translation', 'c') + ->where('c.id_shop = ' . (int) $shopId); + + return $query; + } + + /** + * @param int $offset + * @param int $limit + * + * @return array|bool|\mysqli_result|\PDOStatement|resource|null + * + * @throws \PrestaShopDatabaseException + */ + public function getTranslations($offset, $limit) + { + /** @var int $shopId */ + $shopId = $this->context->shop->id; + $query = $this->getBaseQuery($shopId); + + $this->addSelectParameters($query); + + $query->limit($limit, $offset); + + return $this->db->executeS($query); + } + + /** + * @param int $offset + * + * @return int + */ + public function getRemainingTranslationsCount($offset) + { + /** @var int $shopId */ + $shopId = $this->context->shop->id; + $query = $this->getBaseQuery($shopId) + ->select('(COUNT(c.id_translation) - ' . (int) $offset . ') as count'); + + return (int) $this->db->getValue($query); + } + + /** + * @param int $limit + * @param array $translationIds + * + * @return array|bool|\mysqli_result|\PDOStatement|resource|null + * + * @throws \PrestaShopDatabaseException + */ + public function getTranslationsIncremental($limit, $translationIds) + { + /** @var int $shopId */ + $shopId = $this->context->shop->id; + $query = $this->getBaseQuery($shopId); + + $this->addSelectParameters($query); + + $query->where('c.id_translation IN(' . implode(',', array_map('intval', $translationIds)) . ')') + ->limit($limit); + + return $this->db->executeS($query); + } + + /** + * @param \DbQuery $query + * + * @return void + */ + private function addSelectParameters(\DbQuery $query) + { + $query->select('c.id_translation, c.id_lang, c.key, c.translation, c.domain, c.theme'); + } +}