diff --git a/Api/GuaranteedOpinionClient.php b/Api/GuaranteedOpinionClient.php index 70250cf..abacd7e 100644 --- a/Api/GuaranteedOpinionClient.php +++ b/Api/GuaranteedOpinionClient.php @@ -12,7 +12,10 @@ namespace GuaranteedOpinion\Api; +use Exception; use GuaranteedOpinion\GuaranteedOpinion; +use JsonException; +use RuntimeException; /** * Class GuaranteedOpinionClient @@ -31,11 +34,12 @@ class GuaranteedOpinionClient * * @param string $scope 'site' or productId * @return array - * @throws \JsonException + * @throws JsonException + * @throws RuntimeException */ public function getReviewsFromApi(string $scope = 'site'): array { - $url = self::URL_API . "/" . self::URL_API_REVIEW . "/" . GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_REVIEW) . "/" . $scope; + $url = self::URL_API . "/" . self::URL_API_REVIEW . "/" . GuaranteedOpinion::getConfigValue(GuaranteedOpinion::API_REVIEW_CONFIG_KEY) . "/" . $scope; $ch = curl_init($url); @@ -45,18 +49,24 @@ public function getReviewsFromApi(string $scope = 'site'): array curl_close($ch); - return json_decode($response, false, 512, JSON_THROW_ON_ERROR)->reviews; + $jsonResponse = json_decode($response, true, 512, JSON_THROW_ON_ERROR); + + if (isset($jsonResponse['data']) && $jsonResponse['data'] !== 200) { + throw new RuntimeException($jsonResponse['message']); + } + + return $jsonResponse; } /** - * @throws \JsonException + * @throws JsonException */ public function sendOrder($jsonOrder) { $url = self::URL_API . "/" . self::URL_API_ORDER; $request = [ - 'api_key' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_ORDER), + 'api_key' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::API_ORDER_CONFIG_KEY), 'orders' => $jsonOrder ]; diff --git a/Command/GetProductReviewCommand.php b/Command/GetProductReviewCommand.php index c94fc2c..87e8883 100644 --- a/Command/GetProductReviewCommand.php +++ b/Command/GetProductReviewCommand.php @@ -37,10 +37,12 @@ public function execute(InputInterface $input, OutputInterface $output): int $output->write("Product Review synchronization start \n"); foreach ($products as $product) { - $productReviews = $this->client->getReviewsFromApi($product->getId()); + $apiResponse = $this->client->getReviewsFromApi($product->getId()); - if ($productReviews !== []) { - foreach ($productReviews as $productRow) { + if ($apiResponse['reviews'] !== []) { + $this->productReviewService->addGuaranteedOpinionProductRating($product->getId(), $apiResponse['ratings']); + + foreach ($apiResponse['reviews'] as $productRow) { if ($rowsTreated % 100 === 0) { $output->write("Rows treated : " . $rowsTreated . "\n"); } diff --git a/Command/GetSiteReviewCommand.php b/Command/GetSiteReviewCommand.php index 251b1b8..79c7fe3 100644 --- a/Command/GetSiteReviewCommand.php +++ b/Command/GetSiteReviewCommand.php @@ -4,6 +4,7 @@ use Exception; use GuaranteedOpinion\Api\GuaranteedOpinionClient; +use GuaranteedOpinion\GuaranteedOpinion; use GuaranteedOpinion\Service\SiteReviewService; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -30,7 +31,12 @@ public function execute(InputInterface $input, OutputInterface $output): int $siteReviewsAdded = 0; try { - $siteReviews = $this->client->getReviewsFromApi(); + $apiResponse = $this->client->getReviewsFromApi(); + + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_RATING_TOTAL_CONFIG_KEY, $apiResponse['ratings']['total']); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_RATING_AVERAGE_CONFIG_KEY, $apiResponse['ratings']['average']); + + $siteReviews = $apiResponse['reviews']; $output->write("Site Review synchronization start \n"); @@ -44,6 +50,7 @@ public function execute(InputInterface $input, OutputInterface $output): int } } catch (Exception $exception) { $output->write($exception->getMessage()); + return 0; } $output->write("End of Site Review synchronization\n"); diff --git a/Config/TheliaMain.sql b/Config/TheliaMain.sql index 8c7e5a1..dbd3e19 100644 --- a/Config/TheliaMain.sql +++ b/Config/TheliaMain.sql @@ -63,5 +63,19 @@ CREATE TABLE `guaranteed_opinion_site_review` UNIQUE INDEX `guaranteed_opinion_site_review_id_unique` (`site_review_id`) ) ENGINE=InnoDB; +-- --------------------------------------------------------------------- +-- guaranteed_opinion_product_rating +-- --------------------------------------------------------------------- + +DROP TABLE IF EXISTS `guaranteed_opinion_product_rating`; + +CREATE TABLE `guaranteed_opinion_product_rating` +( + `product_id` INTEGER NOT NULL, + `total` INTEGER, + `average` VARCHAR(255), + PRIMARY KEY (`product_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 625d4ef..07409ee 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,7 +13,7 @@ en_US fr_FR - 1.0.3 + 1.0.4 Chabreuil Antoine diff --git a/Config/schema.xml b/Config/schema.xml index b4c763c..a2d6d14 100644 --- a/Config/schema.xml +++ b/Config/schema.xml @@ -45,5 +45,11 @@ + + + + +
+ diff --git a/Config/update/1.0.4.sql b/Config/update/1.0.4.sql new file mode 100644 index 0000000..4efd6f9 --- /dev/null +++ b/Config/update/1.0.4.sql @@ -0,0 +1,18 @@ +SET FOREIGN_KEY_CHECKS = 0; + +-- --------------------------------------------------------------------- +-- guaranteed_opinion_product_rating +-- --------------------------------------------------------------------- + +DROP TABLE IF EXISTS `guaranteed_opinion_product_rating`; + +CREATE TABLE `guaranteed_opinion_product_rating` +( + `product_id` INTEGER NOT NULL, + `total` INTEGER, + `average` VARCHAR(255), + PRIMARY KEY (`product_id`) +) ENGINE=InnoDB; + +# This restores the fkey checks, after having unset them earlier +SET FOREIGN_KEY_CHECKS = 1; \ No newline at end of file diff --git a/Controller/FrontController.php b/Controller/FrontController.php new file mode 100644 index 0000000..fd38dbd --- /dev/null +++ b/Controller/FrontController.php @@ -0,0 +1,79 @@ +setLimit($limit) + ->setOffset($limit * ($offset -1)) + ->find() + ; + + foreach ($siteReviews as $review) { + $reviews[] = [ + 'rate' => $review->getRate(), + 'name' => $review->getName(), + 'date' => $review->getReviewDate()?->format('Y-m-d'), + 'message' => $review->getReview() + ]; + } + + return new JsonResponse([ + 'total' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_RATING_TOTAL_CONFIG_KEY), + 'average' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_RATING_AVERAGE_CONFIG_KEY), + 'reviews' => $reviews + ]); + } + + /** + * @throws PropelException + */ + #[Route(path: "/product_reviews/{id}/offset/{offset}/limit/{limit}", name: "product_reviews", methods: "GET")] + public function productReviews(int $id, int $offset, int $limit): JsonResponse + { + $reviews = []; + + $productRating = GuaranteedOpinionProductRatingQuery::create() + ->findOneByProductId($id); + + $productReviews = GuaranteedOpinionProductReviewQuery::create() + ->filterByProductId($id) + ->setLimit($limit) + ->setOffset($limit * ($offset -1)) + ->find(); + + foreach ($productReviews as $review) { + $reviews[] = [ + 'rate' => $review->getRate(), + 'name' => $review->getName(), + 'date' => $review->getReviewDate()?->format('Y-m-d'), + 'message' => $review->getReview() + ]; + } + + return new JsonResponse([ + 'total' => $productRating?->getTotal(), + 'average' => $productRating?->getAverage(), + 'reviews' => $reviews + ]); + } +} \ No newline at end of file diff --git a/Controller/GuaranteedOpinionConfigController.php b/Controller/GuaranteedOpinionConfigController.php index 43ef102..064b7e3 100644 --- a/Controller/GuaranteedOpinionConfigController.php +++ b/Controller/GuaranteedOpinionConfigController.php @@ -22,23 +22,23 @@ public function saveConfiguration(ParserContext $parserContext) : RedirectRespon try { $data = $this->validateForm($form)->getData(); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::CONFIG_API_REVIEW, $data["api_key_review"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::CONFIG_API_ORDER, $data["api_key_order"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SHOW_RATING_URL, $data["show_rating_url"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::API_REVIEW_CONFIG_KEY, $data["api_key_review"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::API_ORDER_CONFIG_KEY, $data["api_key_order"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SHOW_RATING_URL_CONFIG_KEY, $data["show_rating_url"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STORE_URL, $data["store_url"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT, implode(',', $data["status_to_export"])); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::EMAIL_DELAY, $data["email_delay"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STORE_URL_CONFIG_KEY, $data["store_url"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY, implode(',', $data["status_to_export"])); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::EMAIL_DELAY_CONFIG_KEY, $data["email_delay"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY, $data["site_review_hook_display"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY, (bool)$data["site_review_display"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY_CONFIG_KEY, $data["site_review_hook_display"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY_CONFIG_KEY, (bool)$data["site_review_display"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY, $data["product_review_hook_display"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY, (bool)$data["product_review_display"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY, (bool)$data["product_review_tab_display"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY_CONFIG_KEY, $data["product_review_hook_display"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY_CONFIG_KEY, (bool)$data["product_review_display"]); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY_CONFIG_KEY, (bool)$data["product_review_tab_display"]); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET, htmlspecialchars($data["site_review_widget"])); - GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME, htmlspecialchars($data["site_review_widget_iframe"])); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_CONFIG_KEY, htmlspecialchars($data["site_review_widget"])); + GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME_CONFIG_KEY, htmlspecialchars($data["site_review_widget_iframe"])); return $this->generateSuccessRedirect($form); diff --git a/EventListeners/OrderListener.php b/EventListeners/OrderListener.php index ff5fac5..bcd9eb4 100644 --- a/EventListeners/OrderListener.php +++ b/EventListeners/OrderListener.php @@ -2,6 +2,7 @@ namespace GuaranteedOpinion\EventListeners; +use DateTime; use GuaranteedOpinion\GuaranteedOpinion; use GuaranteedOpinion\Model\GuaranteedOpinionOrderQueue; use GuaranteedOpinion\Model\GuaranteedOpinionOrderQueueQuery; @@ -11,7 +12,6 @@ use Symfony\Component\HttpFoundation\RequestStack; use Thelia\Core\Event\Order\OrderEvent; use Thelia\Core\Event\TheliaEvents; -use Thelia\Model\OrderStatusQuery; class OrderListener implements EventSubscriberInterface { @@ -32,7 +32,7 @@ public function sendOrderToQueue(OrderEvent $orderEvent): void return; } - $orderStatuses = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT)); + $orderStatuses = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY)); foreach ($orderStatuses as $orderStatus) { @@ -42,7 +42,7 @@ public function sendOrderToQueue(OrderEvent $orderEvent): void $guaranteedOpinionOrderQueue ->setOrderId($orderEvent->getOrder()->getId()) - ->setTreatedAt(new \DateTime('')) + ->setTreatedAt(new DateTime('')) ->setStatus($status) ; diff --git a/Form/ConfigurationForm.php b/Form/ConfigurationForm.php index 61d9422..4e548e3 100644 --- a/Form/ConfigurationForm.php +++ b/Form/ConfigurationForm.php @@ -41,36 +41,36 @@ protected function buildForm(): void /** @var OrderStatus $item */ foreach ($list as $item) { - $item->setLocale($this->getRequest()->getSession()->getLang()->getLocale()); + $item->setLocale($this->getRequest()->getSession()?->getLang()->getLocale()); $orderStatus[$item->getTitle()] = $item->getId(); } $this->formBuilder /* API */ ->add("api_key_review", TextType::class, array( - "label" => $translator->trans("Api key review", [], GuaranteedOpinion::DOMAIN_NAME), + "label" => $translator?->trans("Api key review", [], GuaranteedOpinion::DOMAIN_NAME), "label_attr" => ["for" => "api_key_review"], "required" => true, "constraints" => array( new NotBlank(), ), - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_REVIEW) + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::API_REVIEW_CONFIG_KEY) )) ->add("api_key_order", TextType::class, array( - "label" => $translator->trans("Api key order", [], GuaranteedOpinion::DOMAIN_NAME), + "label" => $translator?->trans("Api key order", [], GuaranteedOpinion::DOMAIN_NAME), "label_attr" => ["for" => "api_key_order"], "required" => true, "constraints" => array( new NotBlank(), ), - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_ORDER) + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::API_ORDER_CONFIG_KEY) )) ->add( 'store_url', TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STORE_URL), - "label"=>$translator->trans("Store url", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STORE_URL_CONFIG_KEY), + "label"=>$translator?->trans("Store url", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] ) @@ -78,8 +78,8 @@ protected function buildForm(): void 'email_delay', TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::EMAIL_DELAY, 10), - "label"=>$translator->trans("Reviews email delay (in days):", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::EMAIL_DELAY_CONFIG_KEY, 10), + "label"=>$translator?->trans("Reviews email delay (in days):", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false ] ) @@ -87,8 +87,8 @@ protected function buildForm(): void 'show_rating_url', TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SHOW_RATING_URL) ?: "https://www.societe-des-avis-garantis.fr/", - "label"=>Translator::getInstance()->trans("Show all opinions url", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SHOW_RATING_URL_CONFIG_KEY) ?: "https://www.societe-des-avis-garantis.fr/", + "label"=>$translator?->trans("Show all opinions url", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false ] ) @@ -96,8 +96,8 @@ protected function buildForm(): void 'status_to_export', ChoiceType::class, [ - "data" => explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT)), - "label"=>$translator->trans("Order status to export", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY)), + "label"=>$translator?->trans("Order status to export", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, 'multiple' => true, 'choices' => $orderStatus @@ -109,41 +109,41 @@ protected function buildForm(): void "site_review_hook_display", TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY), - "label"=>$translator->trans("Site review hook display", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY_CONFIG_KEY), + "label"=>$translator?->trans("Site review hook display", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] ) ->add("site_review_display", CheckboxType::class, array( - "label" => $translator->trans("Show site review", [], GuaranteedOpinion::DOMAIN_NAME), - "data" => (bool)GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY, true), + "label" => $translator?->trans("Show site review", [], GuaranteedOpinion::DOMAIN_NAME), + "data" => (bool)GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY_CONFIG_KEY, true), 'required' => false )) ->add( "product_review_hook_display", TextType::class, [ - "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY), - "label"=>$translator->trans("Product review hook display", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY_CONFIG_KEY), + "label"=>$translator?->trans("Product review hook display", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] ) ->add("product_review_display", CheckboxType::class, array( - "label" => $translator->trans("Show product review", [], GuaranteedOpinion::DOMAIN_NAME), - "data" => (bool)GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY, true), + "label" => $translator?->trans("Show product review", [], GuaranteedOpinion::DOMAIN_NAME), + "data" => (bool)GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY_CONFIG_KEY, true), 'required' => false )) ->add("product_review_tab_display", CheckboxType::class, array( - "label" => $translator->trans("Show product review tab", [], GuaranteedOpinion::DOMAIN_NAME), - "data" => (bool)GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY, true), + "label" => $translator?->trans("Show product review tab", [], GuaranteedOpinion::DOMAIN_NAME), + "data" => (bool)GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY_CONFIG_KEY, true), 'required' => false )) ->add( "site_review_widget", TextareaType::class, [ - "data" => htmlspecialchars_decode(GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET)), - "label"=>$translator->trans("Site review widget code", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => htmlspecialchars_decode(GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_CONFIG_KEY)), + "label"=>$translator?->trans("Site review widget code", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] ) @@ -151,8 +151,8 @@ protected function buildForm(): void "site_review_widget_iframe", TextareaType::class, [ - "data" => htmlspecialchars_decode(GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME)), - "label"=>$translator->trans("Site review widget iframe code", array(), GuaranteedOpinion::DOMAIN_NAME), + "data" => htmlspecialchars_decode(GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME_CONFIG_KEY)), + "label"=>$translator?->trans("Site review widget iframe code", array(), GuaranteedOpinion::DOMAIN_NAME), "required" => false, ] ) diff --git a/GuaranteedOpinion.php b/GuaranteedOpinion.php index 0cff731..47dca8a 100644 --- a/GuaranteedOpinion.php +++ b/GuaranteedOpinion.php @@ -14,24 +14,28 @@ class GuaranteedOpinion extends BaseModule /** @var string */ public const DOMAIN_NAME = 'guaranteedopinion'; - public const CONFIG_API_REVIEW = "guaranteedopinion.api.review"; - public const CONFIG_API_ORDER = "guaranteedopinion.api.order"; + public const API_REVIEW_CONFIG_KEY = "guaranteedopinion.api.review"; + public const API_ORDER_CONFIG_KEY = "guaranteedopinion.api.order"; - public const STATUS_TO_EXPORT = "guaranteedopinion.status_to_export"; - public const EMAIL_DELAY = "guaranteedopinion.email_delay"; - public const STORE_URL = "guaranteedopinion.store_url"; + public const STATUS_TO_EXPORT_CONFIG_KEY = "guaranteedopinion.status_to_export"; + public const EMAIL_DELAY_CONFIG_KEY = "guaranteedopinion.email_delay"; + public const STORE_URL_CONFIG_KEY = "guaranteedopinion.store_url"; - public const SHOW_RATING_URL = "guaranteedopinion.show_rating_url"; + public const SHOW_RATING_URL_CONFIG_KEY = "guaranteedopinion.show_rating_url"; - public const SITE_REVIEW_DISPLAY = "guaranteedopinion.site_review_display"; - public const SITE_REVIEW_HOOK_DISPLAY = "guaranteedopinion.site_review_hook_display"; + public const SITE_REVIEW_DISPLAY_CONFIG_KEY = "guaranteedopinion.site_review_display"; + public const SITE_REVIEW_HOOK_DISPLAY_CONFIG_KEY = "guaranteedopinion.site_review_hook_display"; - public const SITE_REVIEW_WIDGET = "guaranteedopinion.site_review_widget"; - public const SITE_REVIEW_WIDGET_IFRAME = "guaranteedopinion.site_review_widget_iframe"; + public const SITE_REVIEW_WIDGET_CONFIG_KEY = "guaranteedopinion.site_review_widget"; + public const SITE_REVIEW_WIDGET_IFRAME_CONFIG_KEY = "guaranteedopinion.site_review_widget_iframe"; + + public const PRODUCT_REVIEW_DISPLAY_CONFIG_KEY = "guaranteedopinion.product_review_display"; + public const PRODUCT_REVIEW_HOOK_DISPLAY_CONFIG_KEY = "guaranteedopinion.product_review_hook_display"; + public const PRODUCT_REVIEW_TAB_DISPLAY_CONFIG_KEY = "guaranteedopinion.product_review_tab_display"; + + public const SITE_RATING_TOTAL_CONFIG_KEY = "guaranteedopinion.site_rating_total"; + public const SITE_RATING_AVERAGE_CONFIG_KEY = "guaranteedopinion.site_rating_average"; - public const PRODUCT_REVIEW_DISPLAY = "guaranteedopinion.product_review_display"; - public const PRODUCT_REVIEW_HOOK_DISPLAY = "guaranteedopinion.product_review_hook_display"; - public const PRODUCT_REVIEW_TAB_DISPLAY = "guaranteedopinion.product_review_tab_display"; /* * You may now override BaseModuleInterface methods, such as: * install, destroy, preActivation, postActivation, preDeactivation, postDeactivation @@ -106,7 +110,7 @@ public function postActivation(ConnectionInterface $con = null): void self::setConfigValue('is_initialized', true); } - self::setConfigValue(self::SITE_REVIEW_HOOK_DISPLAY, 'main.content-bottom'); - self::setConfigValue(self::PRODUCT_REVIEW_HOOK_DISPLAY, 'product.bottom'); + self::setConfigValue(self::SITE_REVIEW_HOOK_DISPLAY_CONFIG_KEY, 'main.content-bottom'); + self::setConfigValue(self::PRODUCT_REVIEW_HOOK_DISPLAY_CONFIG_KEY, 'product.bottom'); } } diff --git a/Hook/FrontHook.php b/Hook/FrontHook.php index 489ef34..897cf2f 100644 --- a/Hook/FrontHook.php +++ b/Hook/FrontHook.php @@ -14,11 +14,11 @@ class FrontHook extends BaseHook public static function getSubscribedHooks(): array { return [ - GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY) => [ + GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY_CONFIG_KEY) => [ "type" => "front", "method" => "displaySiteWidget" ], - GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY) => [ + GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY_CONFIG_KEY) => [ "type" => "front", "method" => "displayProductWidget" ], @@ -31,7 +31,7 @@ public static function getSubscribedHooks(): array public function displaySiteWidget(HookRenderEvent $event): void { - if (!GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY)) { + if (!GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY_CONFIG_KEY)) { return; } @@ -40,9 +40,9 @@ public function displaySiteWidget(HookRenderEvent $event): void "site/site-review.html", [ "site_reviews_widget" => htmlspecialchars_decode( - GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET)), + GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_CONFIG_KEY)), "site_reviews_widget_iframe" => htmlspecialchars_decode( - GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME)), + GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME_CONFIG_KEY)), ] ) ); @@ -50,7 +50,7 @@ public function displaySiteWidget(HookRenderEvent $event): void public function displayProductTab(HookRenderBlockEvent $event): void { - if (!GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY)) { + if (!GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY_CONFIG_KEY)) { return; } @@ -68,7 +68,7 @@ public function displayProductTab(HookRenderBlockEvent $event): void "product/product-review-tab.html", [ "product_reviews" => $productReviews, - "show_rating_url" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SHOW_RATING_URL), + "show_rating_url" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SHOW_RATING_URL_CONFIG_KEY), ] ) ]); @@ -76,7 +76,7 @@ public function displayProductTab(HookRenderBlockEvent $event): void public function displayProductWidget(HookRenderEvent $event): void { - if (!GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY)) { + if (!GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY_CONFIG_KEY)) { return; } @@ -89,7 +89,7 @@ public function displayProductWidget(HookRenderEvent $event): void $this->render( "product/product-review.html", [ "product_reviews" => $productReviews, - "show_rating_url" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SHOW_RATING_URL), + "show_rating_url" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SHOW_RATING_URL_CONFIG_KEY), ] ) ); diff --git a/Model/GuaranteedOpinionProductRating.php b/Model/GuaranteedOpinionProductRating.php new file mode 100644 index 0000000..c14d3b6 --- /dev/null +++ b/Model/GuaranteedOpinionProductRating.php @@ -0,0 +1,19 @@ +find(); - - if ($guaranteedOpinionOrders === null) - { - return; - } - $jsonOrder = []; + $guaranteedOpinionOrders = GuaranteedOpinionOrderQueueQuery::create()->find(); + foreach ($guaranteedOpinionOrders as $guaranteedOpinionOrder) { $jsonOrder[] = $this->orderToJsonObject($guaranteedOpinionOrder); } + if (empty($jsonOrder)) { + throw new RuntimeException('No Order found'); + } + return json_encode($jsonOrder, JSON_THROW_ON_ERROR); } + /** + * @throws PropelException + */ private function orderToJsonObject(GuaranteedOpinionOrderQueue $guaranteedOpinionOrder): array { $order = OrderQuery::create()->findOneById($guaranteedOpinionOrder->getOrderId()); @@ -50,7 +54,7 @@ private function orderToJsonObject(GuaranteedOpinionOrderQueue $guaranteedOpinio return [ 'id_order' => $order?->getId(), - 'order_date' => $order?->getCreatedAt()->format('Y-m-d H:i:s'), + 'order_date' => $order?->getCreatedAt()?->format('Y-m-d H:i:s'), 'firstname' => $order?->getCustomer()->getFirstname(), 'lastname' => $order?->getCustomer()->getLastname(), 'email' => $order?->getCustomer()->getEmail(), @@ -59,9 +63,14 @@ private function orderToJsonObject(GuaranteedOpinionOrderQueue $guaranteedOpinio ]; } + /** + * @throws PropelException + */ private function productToJsonObject(OrderProduct $orderProduct): array { - $pse = ProductSaleElementsQuery::create()->findOneById($orderProduct->getProductSaleElementsId()); + if (null === $pse = ProductSaleElementsQuery::create()->findOneById($orderProduct->getProductSaleElementsId())) { + return []; + } $category = GuaranteedOpinionOrderQueueQuery::getCategoryByProductSaleElements($pse); @@ -76,7 +85,7 @@ private function productToJsonObject(OrderProduct $orderProduct): array 'ean13' => $pse?->getEanCode(), 'sku' => null, 'upc' => null, - 'url' => GuaranteedOpinion::STORE_URL . '/'. + 'url' => GuaranteedOpinion::STORE_URL_CONFIG_KEY . '/'. GuaranteedOpinionOrderQueueQuery::getProductUrl($pse?->getProductId())->getUrl(), ]; } diff --git a/Service/ProductReviewService.php b/Service/ProductReviewService.php index 27d80ef..ba6a18d 100644 --- a/Service/ProductReviewService.php +++ b/Service/ProductReviewService.php @@ -3,6 +3,8 @@ namespace GuaranteedOpinion\Service; use GuaranteedOpinion\GuaranteedOpinion; +use GuaranteedOpinion\Model\GuaranteedOpinionProductRating; +use GuaranteedOpinion\Model\GuaranteedOpinionProductRatingQuery; use GuaranteedOpinion\Model\GuaranteedOpinionProductReview; use GuaranteedOpinion\Model\GuaranteedOpinionProductReviewQuery; use Propel\Runtime\Exception\PropelException; @@ -20,7 +22,7 @@ public function addGuaranteedOpinionProductReviews(array $productReviews, int $p public function addGuaranteedOpinionProductRow($row, int $productId): bool { $review = GuaranteedOpinionProductReviewQuery::create() - ->findOneByProductReviewId($row->id); + ->findOneByProductReviewId($row['id']); if (null !== $review) { return false; @@ -30,20 +32,20 @@ public function addGuaranteedOpinionProductRow($row, int $productId): bool try { $review - ->setProductReviewId($row->id) - ->setName($row->c) - ->setReview($row->txt) - ->setReviewDate($row->date) - ->setRate($row->r) - ->setOrderId($row->o) - ->setOrderDate($row->odate) + ->setProductReviewId($row['id']) + ->setName($row['c']) + ->setReview($row['txt']) + ->setReviewDate($row['date']) + ->setRate($row['r']) + ->setOrderId($row['o']) + ->setOrderDate($row['odate']) ->setProductId($productId) ; - if ($row->reply !== "" && $row->rdate !== "") { + if ($row['reply'] !== "" && $row['rdate'] !== "") { $review - ->setReply($row->reply) - ->setReplyDate($row->rdate) + ->setReply($row['reply']) + ->setReplyDate($row['rdate']) ; } @@ -98,4 +100,20 @@ protected function normalizeSimpleXML($obj, &$result): void $result = $data; } } + + /** + * @throws PropelException + */ + public function addGuaranteedOpinionProductRating(int $productId, array $ratings): void + { + if (null === $productRating = GuaranteedOpinionProductRatingQuery::create()->findOneByProductId($productId)) { + $productRating = new GuaranteedOpinionProductRating(); + } + + $productRating + ->setProductId($productId) + ->setTotal($ratings['total']) + ->setAverage($ratings['average']) + ->save(); + } } \ No newline at end of file diff --git a/Service/SiteReviewService.php b/Service/SiteReviewService.php index b9c091e..d9a608e 100644 --- a/Service/SiteReviewService.php +++ b/Service/SiteReviewService.php @@ -24,7 +24,7 @@ public function addGuaranteedOpinionSiteReviews($siteReviews): void public function addGuaranteedOpinionSiteRow($row): bool { $review = GuaranteedOpinionSiteReviewQuery::create() - ->findOneBySiteReviewId($row->id); + ->findOneBySiteReviewId($row["id"]); if (null !== $review) { return false; @@ -34,19 +34,19 @@ public function addGuaranteedOpinionSiteRow($row): bool $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) + ->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 !== "") { + if ($row["reply"] !== "" && $row["rdate"] !== "") { $review - ->setReply($row->reply) - ->setReplyDate($row->rdate) + ->setReply($row["reply"]) + ->setReplyDate($row["rdate"]) ; }