feat(main): Task #5 is done

This commit is contained in:
2026-06-22 14:27:47 +02:00
parent 649bf62430
commit 06a860cdd0
9 changed files with 235 additions and 4 deletions
+1 -1
View File
@@ -44,4 +44,4 @@ Todo:
{/if}
```
5.) Make paper choice an entity
5.) ✅ Make paper choice an entity
+9
View File
@@ -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" }
+10
View File
@@ -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 }
+1 -2
View File
@@ -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']));
@@ -0,0 +1,38 @@
<?php
namespace Websmid\GiftDropdown\Command\CommandHandler;
use Websmid\GiftDropdown\Command\SaveGiftDropdownCommand;
use Websmid\GiftDropdown\Entity\GiftDropdown;
use DateTime;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
final class SaveGiftDropdownCommandHandler
{
public function __invoke(SaveGiftDropdownCommand $command)
{
return $this->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;
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace Websmid\GiftDropdown\Command;
class SaveGiftDropdownCommand
{
private $idCart;
private $paperChoice;
public function __construct(int $idCart, string $paperChoice)
{
$this->idCart = $idCart;
$this->paperChoice = $paperChoice;
}
public function getIdCart(): int
{
return $this->idCart;
}
public function getPaperChoice(): string
{
return $this->paperChoice;
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
namespace Websmid\GiftDropdown\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="ws_gift_dropdown")
* @ORM\Entity()
*/
class GiftDropdown
{
/**
* @ORM\Id
* @ORM\Column(name="id_ws_gift_dropdown", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="id_cart", type="integer")
*/
private $idCart;
/**
* @ORM\Column(name="id_order", type="integer")
*/
private $idOrder;
/**
* @ORM\Column(name="paper_choice", type="string", length=64)
*/
private $paperChoice;
/**
* @ORM\Column(name="date_add", type="datetime")
*/
private $dateAdd;
public function getId(): ?int
{
return $this->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,
];
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace Websmid\GiftDropdown\Entity;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
class TablePrefixSubscriber implements EventSubscriber
{
private $prefix;
public function __construct(string $prefix)
{
$this->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]);
}
}
+8 -1
View File
@@ -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;