diff --git a/Command/GetProductReviewCommand.php b/Command/GetProductReviewCommand.php index 94845ed..c94fc2c 100644 --- a/Command/GetProductReviewCommand.php +++ b/Command/GetProductReviewCommand.php @@ -28,23 +28,36 @@ public function configure(): void public function execute(InputInterface $input, OutputInterface $output): int { + $productReviewsAdded = 0; + $rowsTreated = 0; + try { $products = ProductQuery::create()->findByVisible(1); + $output->write("Product Review synchronization start \n"); foreach ($products as $product) { $productReviews = $this->client->getReviewsFromApi($product->getId()); - if ($productReviews !== []) - { - $this->productReviewService->addGuaranteedOpinionProductReviews($productReviews, $product->getId()); + if ($productReviews !== []) { + foreach ($productReviews as $productRow) { + if ($rowsTreated % 100 === 0) { + $output->write("Rows treated : " . $rowsTreated . "\n"); + } + if ($this->productReviewService->addGuaranteedOpinionProductRow($productRow, $product->getId())) { + $productReviewsAdded ++; + } + $rowsTreated++; + } } } - } catch (Exception $exception) { $output->write($exception->getMessage()); } + $output->write("End of Product Review synchronization\n"); + $output->write("Product Reviews Added : " .$productReviewsAdded. "\n"); + return 1; } } \ No newline at end of file diff --git a/Command/GetSiteReviewCommand.php b/Command/GetSiteReviewCommand.php new file mode 100644 index 0000000..251b1b8 --- /dev/null +++ b/Command/GetSiteReviewCommand.php @@ -0,0 +1,54 @@ +setName('module:GuaranteedOpinion:GetSiteReview') + ->setDescription('Get site review from API Avis-Garantis'); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $siteReviewsAdded = 0; + + try { + $siteReviews = $this->client->getReviewsFromApi(); + + $output->write("Site Review synchronization start \n"); + + foreach ($siteReviews as $key => $siteRow) { + if ($key % 100 === 0) { + $output->write("Rows treated : " . $key . "\n"); + } + if ($this->siteReviewService->addGuaranteedOpinionSiteRow($siteRow)) { + $siteReviewsAdded ++; + } + } + } catch (Exception $exception) { + $output->write($exception->getMessage()); + } + + $output->write("End of Site Review synchronization\n"); + $output->write("Site Review Added : " .$siteReviewsAdded. "\n"); + + return 1; + } +} \ No newline at end of file diff --git a/Config/TheliaMain.sql b/Config/TheliaMain.sql index 2a3eb8c..8c7e5a1 100644 --- a/Config/TheliaMain.sql +++ b/Config/TheliaMain.sql @@ -41,5 +41,27 @@ CREATE TABLE `guaranteed_opinion_order_queue` PRIMARY KEY (`id`) ) ENGINE=InnoDB; +-- --------------------------------------------------------------------- +-- guaranteed_opinion_site_review +-- --------------------------------------------------------------------- + +DROP TABLE IF EXISTS `guaranteed_opinion_site_review`; + +CREATE TABLE `guaranteed_opinion_site_review` +( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `site_review_id` INTEGER NOT NULL, + `name` VARCHAR(255), + `rate` DECIMAL(2,1) DEFAULT 0, + `review` VARBINARY(10000), + `review_date` DATETIME, + `order_id` VARCHAR(255), + `order_date` DATETIME, + `reply` VARCHAR(255), + `reply_date` DATETIME, + PRIMARY KEY (`id`,`site_review_id`), + UNIQUE INDEX `guaranteed_opinion_site_review_id_unique` (`site_review_id`) +) ENGINE=InnoDB; + # This restores the fkey checks, after having unset them earlier SET FOREIGN_KEY_CHECKS = 1; diff --git a/Config/module.xml b/Config/module.xml index 564c6bd..8d08a7a 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,7 +13,7 @@ en_US fr_FR - 1.0.1 + 1.0.2 Chabreuil Antoine diff --git a/Config/schema.xml b/Config/schema.xml index 3d66f14..b4c763c 100644 --- a/Config/schema.xml +++ b/Config/schema.xml @@ -28,5 +28,22 @@ + + + + + + + + + + + + + + + +
+ diff --git a/Config/update/1.0.2.sql b/Config/update/1.0.2.sql new file mode 100644 index 0000000..b65660a --- /dev/null +++ b/Config/update/1.0.2.sql @@ -0,0 +1,66 @@ +# This is a fix for InnoDB in MySQL >= 4.1.x +# It "suspends judgement" for fkey relationships until are tables are set. +SET FOREIGN_KEY_CHECKS = 0; + +-- --------------------------------------------------------------------- +-- guaranteed_opinion_product_review +-- --------------------------------------------------------------------- + +DROP TABLE IF EXISTS `guaranteed_opinion_product_review`; + +CREATE TABLE `guaranteed_opinion_product_review` +( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `product_review_id` VARCHAR(55) NOT NULL, + `name` VARCHAR(255), + `rate` DECIMAL(2,1) DEFAULT 0, + `review` VARBINARY(10000), + `review_date` DATETIME, + `product_id` INTEGER, + `order_id` VARCHAR(255), + `order_date` DATETIME, + `reply` VARCHAR(255), + `reply_date` DATETIME, + PRIMARY KEY (`id`), + UNIQUE INDEX `guaranteed_opinion_product_review_id_unique` (`product_review_id`) +) ENGINE=InnoDB; + +-- --------------------------------------------------------------------- +-- guaranteed_opinion_order_queue +-- --------------------------------------------------------------------- + +DROP TABLE IF EXISTS `guaranteed_opinion_order_queue`; + +CREATE TABLE `guaranteed_opinion_order_queue` +( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `order_id` INTEGER NOT NULL, + `treated_at` DATETIME, + `status` INTEGER, + PRIMARY KEY (`id`) +) ENGINE=InnoDB; + +-- --------------------------------------------------------------------- +-- guaranteed_opinion_site_review +-- --------------------------------------------------------------------- + +DROP TABLE IF EXISTS `guaranteed_opinion_site_review`; + +CREATE TABLE `guaranteed_opinion_site_review` +( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `site_review_id` INTEGER NOT NULL, + `name` VARCHAR(255), + `rate` DECIMAL(2,1) DEFAULT 0, + `review` VARBINARY(10000), + `review_date` DATETIME, + `order_id` VARCHAR(255), + `order_date` DATETIME, + `reply` VARCHAR(255), + `reply_date` DATETIME, + PRIMARY KEY (`id`,`site_review_id`), + UNIQUE INDEX `guaranteed_opinion_site_review_id_unique` (`site_review_id`) +) ENGINE=InnoDB; + +# This restores the fkey checks, after having unset them earlier +SET FOREIGN_KEY_CHECKS = 1; diff --git a/Form/ConfigurationForm.php b/Form/ConfigurationForm.php index e11834b..61d9422 100644 --- a/Form/ConfigurationForm.php +++ b/Form/ConfigurationForm.php @@ -109,7 +109,7 @@ protected function buildForm(): void "site_review_hook_display", TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY) ?: "main.content-bottom", + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY), "label"=>$translator->trans("Site review hook display", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] @@ -123,7 +123,7 @@ protected function buildForm(): void "product_review_hook_display", TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY) ?: "product.bottom", + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY), "label"=>$translator->trans("Product review hook display", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] diff --git a/GuaranteedOpinion.php b/GuaranteedOpinion.php index 64e0eeb..ac705c4 100644 --- a/GuaranteedOpinion.php +++ b/GuaranteedOpinion.php @@ -96,4 +96,10 @@ public static function log($msg): void ); $logger->addAlert("MESSAGE => " . print_r($msg, true)); } + + public function postActivation(ConnectionInterface $con = null): void + { + self::setConfigValue(self::SITE_REVIEW_HOOK_DISPLAY, 'main.content-bottom'); + self::setConfigValue(self::PRODUCT_REVIEW_HOOK_DISPLAY, 'product.bottom'); + } } diff --git a/Loop/GuaranteedProductLoop.php b/Loop/GuaranteedProductLoop.php index b122366..902d0d0 100644 --- a/Loop/GuaranteedProductLoop.php +++ b/Loop/GuaranteedProductLoop.php @@ -18,7 +18,6 @@ */ class GuaranteedProductLoop extends BaseLoop implements PropelSearchLoopInterface { - public function parseResults(LoopResult $loopResult): LoopResult { foreach ($loopResult->getResultDataCollection() as $review) { @@ -50,21 +49,27 @@ public function buildModelCriteria(): GuaranteedOpinionProductReviewQuery|ModelC $search = GuaranteedOpinionProductReviewQuery::create(); if (null !== $productId = $this->getProduct()) { - $search->filterByProductId($productId, Criteria::IN); + $search->filterByProductId($productId); } if (null !== $minRate = $this->getMinRate()) { $search->filterByRate($minRate, Criteria::GREATER_EQUAL); } + if ((null !== $page = $this->getPage()) && (null !== $limit = $this->getLimit())) { + $search->paginate($page, $limit); + } + return $search; } protected function getArgDefinitions(): ArgumentCollection { return new ArgumentCollection( - Argument::createIntListTypeArgument('product'), - Argument::createIntListTypeArgument('min_rate') + Argument::createIntTypeArgument('product'), + Argument::createIntTypeArgument('min_rate'), + Argument::createIntTypeArgument('limit', 5), + Argument::createIntTypeArgument('page', 0) ); } } \ No newline at end of file diff --git a/Loop/GuaranteedSiteLoop.php b/Loop/GuaranteedSiteLoop.php new file mode 100644 index 0000000..77dc9e4 --- /dev/null +++ b/Loop/GuaranteedSiteLoop.php @@ -0,0 +1,71 @@ +getResultDataCollection() as $review) { + $loopResultRow = new LoopResultRow($review); + + $loopResultRow + ->set('ID', $review->getId()) + ->set('PRODUCT_REVIEW_ID', $review->getSiteReviewId()) + ->set('NAME', $review->getName()) + ->set('RATE', $review->getRate()) + ->set('REVIEW', $review->getReview()) + ->set('REVIEW_DATE', $review->getReviewDate()?->format('Y-m-d')) + ->set('ORDER_ID', $review->getOrderId()) + ->set('ORDER_DATE', $review->getOrderDate()?->format('Y-m-d')) + ->set('REPLY', $review->getReply()) + ->set('REPLY_DATE', $review->getReplyDate()?->format('Y-m-d')) + ; + $this->addOutputFields($loopResultRow, $review); + + $loopResult->addRow($loopResultRow); + } + + return $loopResult; + } + + public function buildModelCriteria(): GuaranteedOpinionProductReviewQuery|ModelCriteria + { + $search = GuaranteedOpinionSiteReviewQuery::create(); + + if (null !== $minRate = $this->getMinRate()) { + $search->filterByRate($minRate, Criteria::GREATER_EQUAL); + } + + if ((null !== $page = $this->getPage()) && (null !== $limit = $this->getLimit())) { + $search->paginate($page, $limit); + } + + return $search; + } + + protected function getArgDefinitions(): ArgumentCollection + { + return new ArgumentCollection( + Argument::createIntTypeArgument('min_rate'), + Argument::createIntTypeArgument('limit', 5), + Argument::createIntTypeArgument('page', 0) + ); + } +} \ No newline at end of file diff --git a/Model/GuaranteedOpinionSiteReview.php b/Model/GuaranteedOpinionSiteReview.php new file mode 100644 index 0000000..f7c5fd0 --- /dev/null +++ b/Model/GuaranteedOpinionSiteReview.php @@ -0,0 +1,19 @@ +findOneByProductReviewId($row->id); if (null !== $review) { - return; + return false; } $review = new GuaranteedOpinionProductReview(); @@ -51,7 +51,10 @@ public function addGuaranteedOpinionProductRow($row, int $productId): void } catch (PropelException $e) { GuaranteedOpinion::log($e->getMessage()); + return false; } + + return true; } /** diff --git a/Service/SiteReviewService.php b/Service/SiteReviewService.php new file mode 100644 index 0000000..b9c091e --- /dev/null +++ b/Service/SiteReviewService.php @@ -0,0 +1,62 @@ +addGuaranteedOpinionSiteRow($siteRow); + } + } + + /** + * @param $row + * @return bool + */ + public function addGuaranteedOpinionSiteRow($row): bool + { + $review = GuaranteedOpinionSiteReviewQuery::create() + ->findOneBySiteReviewId($row->id); + + if (null !== $review) { + return false; + } + + try { + $review = new GuaranteedOpinionSiteReview(); + + $review + ->setSiteReviewId($row->id) + ->setName($row->c) + ->setReview($row->txt) + ->setReviewDate($row->date) + ->setRate($row->r) + ->setOrderId($row->o) + ->setOrderDate($row->odate) + ; + + if ($row->reply !== "" && $row->rdate !== "") { + $review + ->setReply($row->reply) + ->setReplyDate($row->rdate) + ; + } + + $review->save(); + + } catch (PropelException $e) { + GuaranteedOpinion::log($e->getMessage()); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/templates/backOffice/default/module_configuration.html b/templates/backOffice/default/module_configuration.html index f188250..2dc55c5 100644 --- a/templates/backOffice/default/module_configuration.html +++ b/templates/backOffice/default/module_configuration.html @@ -19,6 +19,10 @@

{intl l="Guaranteed Opinion Review access"
{render_form_field field="api_key_review"} +
+

{intl l="Import Guaranteed Opinion Site Review : " d='guaranteedopinion.bo.default'}

+

php Thelia module:GuaranteedOpinion:GetSiteReview

+

{intl l="Import Guaranteed Opinion Product Review : " d='guaranteedopinion.bo.default'}

php Thelia module:GuaranteedOpinion:GetProductReview