diff --git a/classes/WebserviceSpecificManagementOrderreference.php b/classes/WebserviceSpecificManagementOrderreference.php new file mode 100644 index 0000000..cf9fcca --- /dev/null +++ b/classes/WebserviceSpecificManagementOrderreference.php @@ -0,0 +1,80 @@ +objectOutput = $objectOutput; + return $this; + } + + public function getObjectOutput() + { + return $this->objectOutput; + } + + public function setWsObject(WebserviceRequest $wsObject) + { + $this->wsObject = $wsObject; + return $this; + } + + public function getWsObject() + { + return $this->wsObject; + } + + public function manage() + { + $fragments = $this->wsObject ? $this->wsObject->urlFragments : []; + + if (isset($fragments['id_order'])) { + $this->result = $this->fetchByOrderId((int) $fragments['id_order']); + } elseif (isset($fragments['id_cart'])) { + $this->result = $this->fetchByCartId((int) $fragments['id_cart']); + } + + return true; + } + + private function fetchByCartId(int $cartId): ?array + { + $row = Db::getInstance()->getRow( + 'SELECT id, id_cart, order_reference FROM ' . _DB_PREFIX_ . 'order_reference WHERE id_cart = ' . (int) $cartId + ); + + return $row ?: null; + } + + private function fetchByOrderId(int $orderId): ?array + { + $idCart = Db::getInstance()->getValue( + 'SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = ' . (int) $orderId + ); + + if ($idCart) { + return $this->fetchByCartId((int) $idCart); + } + + return null; + } + + public function getContent() + { + $xml = new SimpleXMLElement(''); + + if ($this->result !== null) { + $node = $xml->addChild('order_reference'); + $node->addChild('id', (string) $this->result['id']); + $node->addChild('id_cart', (string) $this->result['id_cart']); + $node->addChild('reference', htmlspecialchars($this->result['order_reference'])); + } + + return $xml->asXML(); + } +} diff --git a/classes/config/services.yml b/classes/config/services.yml new file mode 100644 index 0000000..d3c90e7 --- /dev/null +++ b/classes/config/services.yml @@ -0,0 +1,18 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + # Repository - this makes it available via dependency injection + Module\WsOrderreference\Repository\OrderReferenceRepository: + public: true + factory: ["@doctrine.orm.default_entity_manager", getRepository] + arguments: + - Module\WsOrderreference\Entity\OrderReference + + # Database installer + Module\WsOrderreference\Database\ReferenceInstaller: + public: true + arguments: + - "@doctrine.dbal.default_connection" + - "%database_prefix%" diff --git a/composer.json b/composer.json index fd1a99a..661318d 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "Module\\WsOrderreference\\": "src/" }, "classmap": [ - "ws_orderreference.php" + "ws_orderreference.php", + "classes/" ], "exclude-from-classmap": [] }, diff --git a/config/services.yml b/config/services.yml index d3c90e7..1974f82 100644 --- a/config/services.yml +++ b/config/services.yml @@ -3,6 +3,8 @@ services: autowire: true autoconfigure: true + Module\WsOrderreference\Controller\: + resource: "../src/Controller/*" # Repository - this makes it available via dependency injection Module\WsOrderreference\Repository\OrderReferenceRepository: public: true diff --git a/src/Controller/OrderReferenceController.php b/src/Controller/OrderReferenceController.php deleted file mode 100644 index eb1518d..0000000 --- a/src/Controller/OrderReferenceController.php +++ /dev/null @@ -1,19 +0,0 @@ - $id_order, - 'customReference' => 'DUMMY-REF-12345', - 'status' => 'This is a test response' - ]); - } -} \ No newline at end of file diff --git a/src/Module/Install.php b/src/Module/Install.php index f3a325d..bf229e7 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -12,6 +12,7 @@ trait Install && parent::install() && $this->registerHook('displayShoppingCart') && $this->registerHook('displayPDFDeliverySlip') + && $this->registerHook('addWebserviceResources') && $this->registerHook('actionFrontControllerSetMedia'); } @@ -42,4 +43,4 @@ trait Install return empty($errors); } -} \ No newline at end of file +} diff --git a/src/Module/actionHooks.php b/src/Module/actionHooks.php index 2b285a0..ba8a3ad 100644 --- a/src/Module/actionHooks.php +++ b/src/Module/actionHooks.php @@ -22,4 +22,19 @@ trait actionHooks ['media' => 'all', 'priority' => 150] ); } + // Register in web service using specific management to bypass objectmodel handling and return custom data. + // I used specific_management because otherwise I'd have to make an objectmodel which conflicts with the doctrine entity I made. + // Instead of having to manage two places where the order reference is handled, I can just use the specific management to return the data from the db directly. + + // See classes/webservice/WebserviceSpecificManagementInterface.php for the interface contract. + + public function hookAddWebserviceResources($params) + { + return [ + 'order_reference' => [ + 'description' => 'Order references from the ws_orderreference module', + 'specific_management' => true, + ], + ]; + } } \ No newline at end of file diff --git a/src/Repository/OrderReferenceRepository.php b/src/Repository/OrderReferenceRepository.php index 1492597..bfd336d 100644 --- a/src/Repository/OrderReferenceRepository.php +++ b/src/Repository/OrderReferenceRepository.php @@ -27,6 +27,27 @@ class OrderReferenceRepository extends EntityRepository return $this->findOneBy(['idCart' => $cartId]); } + /** + * Find order reference entity by order ID (via cart lookup) + * + * @param int $orderId + * @return OrderReference|null + */ + public function findByOrderId(int $orderId): ?OrderReference + { + $conn = $this->getEntityManager()->getConnection(); + $sql = 'SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = :orderId'; + $stmt = $conn->prepare($sql); + $resultSet = $stmt->executeQuery(['orderId' => $orderId]); + $idCart = $resultSet->fetchOne(); + + if ($idCart) { + return $this->findByCartId((int) $idCart); + } + + return null; + } + // Create a function that accepts an order Id and uses the cart ID to check if there is a custom order reference. // Requires an order ID as a parameter. // Returns the order reference if found, otherwise returns null. @@ -39,7 +60,7 @@ class OrderReferenceRepository extends EntityRepository public function customRefByOrderId(int $orderId): ?string { $conn = $this->getEntityManager()->getConnection(); - $sql = 'SELECT id_cart FROM ps_orders WHERE id_order = :orderId'; + $sql = 'SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = :orderId'; $stmt = $conn->prepare($sql); $resultSet = $stmt->executeQuery(['orderId' => $orderId]); $idCart = $resultSet->fetchOne(); diff --git a/ws_orderreference.php b/ws_orderreference.php index 6e7dbc3..3845b2c 100644 --- a/ws_orderreference.php +++ b/ws_orderreference.php @@ -13,6 +13,8 @@ if (file_exists(__DIR__ . '/vendor/autoload.php')) { require_once __DIR__ . '/vendor/autoload.php'; } +require_once __DIR__ . '/classes/WebserviceSpecificManagementOrderreference.php'; + class Ws_OrderReference extends Module { /** @var OrderReferenceRepository */ @@ -41,4 +43,4 @@ class Ws_OrderReference extends Module $this->description = $this->l('Adds an optional order reference field to the shopping cart summary.'); } -} \ No newline at end of file +}