Using specific management and the WebserviceSpecificManagementInterface contract to tap into the webservice api.
81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
// You can do api/order_reference?id_order=12 or api/order_reference?id_cart=12
|
|
|
|
class WebserviceSpecificManagementOrderreference implements WebserviceSpecificManagementInterface
|
|
{
|
|
private $objectOutput;
|
|
private $wsObject;
|
|
private ?array $result = null;
|
|
|
|
public function setObjectOutput(WebserviceOutputBuilder $objectOutput)
|
|
{
|
|
$this->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('<order_references/>');
|
|
|
|
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();
|
|
}
|
|
}
|