From 7667757d73323d8e56b5ed07153d9b103afa7170 Mon Sep 17 00:00:00 2001 From: Isabelle Date: Mon, 22 Jun 2026 14:27:47 +0200 Subject: [PATCH] feat(main): Task #5 is done --- README.md | 2 - config/command.yml | 9 ++ config/services.yml | 10 ++ controllers/front/savepaper.php | 3 +- .../SaveGiftDropdownCommandHandler.php | 38 +++++++ src/Command/SaveGiftDropdownCommand.php | 25 +++++ src/Entity/GiftDropdown.php | 104 ++++++++++++++++++ src/Entity/TablePrefixSubscriber.php | 39 +++++++ src/Module/DisplayHooks.php | 9 +- 9 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 config/command.yml create mode 100644 config/services.yml create mode 100644 src/Command/CommandHandler/SaveGiftDropdownCommandHandler.php create mode 100644 src/Command/SaveGiftDropdownCommand.php create mode 100644 src/Entity/GiftDropdown.php create mode 100644 src/Entity/TablePrefixSubscriber.php diff --git a/README.md b/README.md index 33cc821..d265b0d 100644 --- a/README.md +++ b/README.md @@ -43,5 +43,3 @@ Todo: {/if} ``` - -5.) Make paper choice an entity diff --git a/config/command.yml b/config/command.yml new file mode 100644 index 0000000..a83a033 --- /dev/null +++ b/config/command.yml @@ -0,0 +1,9 @@ +services: + websmid.giftdropdown.command_handler.save_gift_dropdown_handler: + class: 'Websmid\GiftDropdown\Command\CommandHandler\SaveGiftDropdownCommandHandler' + tags: + - { + name: "tactician.handler", + command: 'Websmid\GiftDropdown\Command\SaveGiftDropdownCommand', + } + - { name: "messenger.message_handler" } diff --git a/config/services.yml b/config/services.yml new file mode 100644 index 0000000..b2cb7b4 --- /dev/null +++ b/config/services.yml @@ -0,0 +1,10 @@ +imports: + - { resource: command.yml } + +services: + websmid.giftdropdown.entity.table_prefix_subscriber: + class: 'Websmid\GiftDropdown\Entity\TablePrefixSubscriber' + arguments: + - "%database_prefix%" + tags: + - { name: doctrine.event_subscriber } diff --git a/controllers/front/savepaper.php b/controllers/front/savepaper.php index 9c6a33b..bd299cf 100644 --- a/controllers/front/savepaper.php +++ b/controllers/front/savepaper.php @@ -2,7 +2,6 @@ use Websmid\GiftDropdown\Config\Config; use Configuration; -use Context; use Db; use Tools; @@ -31,7 +30,7 @@ class Ws_GiftDropdownSavepaperModuleFrontController extends ModuleFrontControlle exit; } - $idCart = (int) Context::getContext()->cart->id; + $idCart = (int) $this->context->cart->id; if (!$idCart) { $this->ajaxRender(json_encode(['success' => false, 'error' => 'No cart'])); diff --git a/src/Command/CommandHandler/SaveGiftDropdownCommandHandler.php b/src/Command/CommandHandler/SaveGiftDropdownCommandHandler.php new file mode 100644 index 0000000..ea80eee --- /dev/null +++ b/src/Command/CommandHandler/SaveGiftDropdownCommandHandler.php @@ -0,0 +1,38 @@ +handle($command); + } + + public function handle(SaveGiftDropdownCommand $command) + { + $entityManager = SymfonyContainer::getInstance()->get('doctrine.orm.entity_manager'); + $repo = $entityManager->getRepository(GiftDropdown::class); + + $entity = $repo->findOneBy(['idCart' => $command->getIdCart()]); + + if (!$entity) { + $entity = new GiftDropdown(); + $entity->setIdCart($command->getIdCart()); + $entity->setIdOrder(0); + $entity->setDateAdd(new DateTime()); + } + + $entity->setPaperChoice($command->getPaperChoice()); + + $entityManager->persist($entity); + $entityManager->flush(); + + return true; + } +} diff --git a/src/Command/SaveGiftDropdownCommand.php b/src/Command/SaveGiftDropdownCommand.php new file mode 100644 index 0000000..bfef0c1 --- /dev/null +++ b/src/Command/SaveGiftDropdownCommand.php @@ -0,0 +1,25 @@ +idCart = $idCart; + $this->paperChoice = $paperChoice; + } + + public function getIdCart(): int + { + return $this->idCart; + } + + public function getPaperChoice(): string + { + return $this->paperChoice; + } +} diff --git a/src/Entity/GiftDropdown.php b/src/Entity/GiftDropdown.php new file mode 100644 index 0000000..3932f82 --- /dev/null +++ b/src/Entity/GiftDropdown.php @@ -0,0 +1,104 @@ +id; + } + + public function getIdCart(): int + { + return $this->idCart; + } + + public function setIdCart(int $idCart): self + { + $this->idCart = $idCart; + + return $this; + } + + public function getIdOrder(): int + { + return $this->idOrder; + } + + public function setIdOrder(int $idOrder): self + { + $this->idOrder = $idOrder; + + return $this; + } + + public function getPaperChoice(): string + { + return $this->paperChoice; + } + + public function setPaperChoice(string $paperChoice): self + { + $this->paperChoice = $paperChoice; + + return $this; + } + + public function getDateAdd(): ?DateTime + { + return $this->dateAdd; + } + + public function setDateAdd(DateTime $dateAdd): self + { + $this->dateAdd = $dateAdd; + + return $this; + } + + public function toArray(): array + { + return [ + 'id_ws_gift_dropdown' => $this->id, + 'id_cart' => $this->idCart, + 'id_order' => $this->idOrder, + 'paper_choice' => $this->paperChoice, + 'date_add' => $this->dateAdd ? $this->dateAdd->format('Y-m-d H:i:s') : null, + ]; + } +} diff --git a/src/Entity/TablePrefixSubscriber.php b/src/Entity/TablePrefixSubscriber.php new file mode 100644 index 0000000..dc9e328 --- /dev/null +++ b/src/Entity/TablePrefixSubscriber.php @@ -0,0 +1,39 @@ +prefix = $prefix; + } + + public function getSubscribedEvents(): array + { + return [Events::loadClassMetadata]; + } + + public function loadClassMetadata(LoadClassMetadataEventArgs $args): void + { + $classMetadata = $args->getClassMetadata(); + + if (strpos($classMetadata->getName(), 'Websmid\\GiftDropdown\\') !== 0) { + return; + } + + $tableName = $classMetadata->getTableName(); + + if (strpos($tableName, $this->prefix) === 0) { + return; + } + + $classMetadata->setPrimaryTable(['name' => $this->prefix . $tableName]); + } +} diff --git a/src/Module/DisplayHooks.php b/src/Module/DisplayHooks.php index bb0ce51..cd6d427 100644 --- a/src/Module/DisplayHooks.php +++ b/src/Module/DisplayHooks.php @@ -89,9 +89,16 @@ trait DisplayHooks private function loadGiftPaperChoice(int $idOrder): ?string { + $idCart = (int) Db::getInstance()->getValue( + 'SELECT `id_cart` FROM `' . _DB_PREFIX_ . 'orders` WHERE `id_order` = ' . $idOrder + ); + if (!$idCart) { + return null; + } + $value = Db::getInstance()->getValue( 'SELECT `paper_choice` FROM `' . _DB_PREFIX_ . 'ws_gift_dropdown` - WHERE `id_order` = ' . $idOrder + WHERE `id_cart` = ' . $idCart ); return $value !== false ? (string) $value : null;