1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
<?php
/* --------------------------------------------------------------
OrdersItemsAttributesApiV2Controller.inc.php 2016-09-20
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('HttpApiV2Controller');
/**
* Class OrdersItemsAttributesApiV2Controller
*
* Notice: This controller is a sub-resource of the OrdersItemsApiV2Controller.
*
* @category System
* @package ApiV2Controllers
*/
class OrdersItemsAttributesApiV2Controller extends HttpApiV2Controller
{
/**
* Order write service.
*
* @var OrderWriteService
*/
protected $orderWriteService;
/**
* Order read service.
*
* @var OrderReadService
*/
protected $orderReadService;
/**
* Order JSON serializer.
*
* @var OrderJsonSerializer
*/
protected $orderJsonSerializer;
/**
* Initializes API Controller
*
* @throws HttpApiV2Exception On missing or invalid order ID and order item ID.
*/
protected function __initialize()
{
// Check if the order ID was provided
if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Order record ID was not provided in the resource URL.', 400);
}
// Check if the order item ID was provided
if(!isset($this->uri[3]) || !is_numeric($this->uri[3]))
{
throw new HttpApiV2Exception('Order item record ID was not provided in the resource URL.', 400);
}
$this->orderWriteService = StaticGXCoreLoader::getService('OrderWrite');
$this->orderReadService = StaticGXCoreLoader::getService('OrderRead');
$this->orderJsonSerializer = MainFactory::create('OrderJsonSerializer');
}
/**
* @api {post} /orders/:id/items/:id/attributes Create Order Item Attribute
* @apiVersion 2.1.0
* @apiName CreateOrderItemAttribute
* @apiGroup Orders
*
* @apiDescription
* Use this method to create a new order item attribute to an existing order item. The order item attribute JSON
* object is the same as the one included in the full order representation. There are two different order item
* variation systems in the shop, the "attributes" and the "properties". Both of them define a variation of an
* order item (e.g. color, size etc). You must always use only one of them for a single order item.
*
* @apiParamExample {json} Request-Example
* {
* "name": "Color",
* "value": "blue",
* "price": 0.00,
* "priceType": "+",
* "optionId": 1,
* "optionValueId": 1,
* "combisId": null
* }
*
* @apiParam {String} name Attribute Name.
* @apiParam {String} value Attribute Value.
* @apiParam {Number} price Attribute Price as float.
* @apiParam {String} priceType Must contain one of the existing price types of the shop.
* @apiParam {Number} optionId Only attribute-records need this value.
* @apiParam {Number} optionValueId Only attribute-records need this value.
* @apiParam {Number} combisId Only property-records need this value.
*
* @apiSuccess (Success 201) Response-Body If successful, this method returns a complete Order Item Attribute
* resource in the response body.
*
* @apiError 400-BadRequest The request body is empty.
* @apiErrorExample Error-Response
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order item attribute data were not provided."
* }
*/
public function post()
{
$orderItemAttributeJsonString = $this->api->request->getBody();
if(empty($orderItemAttributeJsonString))
{
throw new HttpApiV2Exception('Order item attribute data were not provided.', 400);
}
$orderItemId = new IdType($this->uri[3]);
$orderItemAttributeJsonObject = json_decode($orderItemAttributeJsonString);
$attributeClassName = ($orderItemAttributeJsonObject->combisId
!== null) ? 'OrderItemProperty' : 'OrderItemAttribute';
$orderItemAttribute = $this->orderJsonSerializer->deserializeAttribute($orderItemAttributeJsonObject);
$orderItemAttributeId = $this->orderWriteService->addOrderItemAttribute($orderItemId, $orderItemAttribute);
$orderItemAttributes = $this->orderReadService->getOrderItemById($orderItemId)->getAttributes();
foreach($orderItemAttributes->getArray() as $storedOrderItemAttribute)
{
/** @var StoredOrderItemAttributeInterface $storedOrderItemAttribute */
if($storedOrderItemAttribute->getOrderItemAttributeId() === $orderItemAttributeId
&& is_a($storedOrderItemAttribute, $attributeClassName)
)
{
$response = $this->orderJsonSerializer->serializeAttribute($storedOrderItemAttribute);
}
}
$this->_writeResponse($response, 201);
}
/**
* @api {put} /orders/:id/items/:id/attributes/:id Update Order Item Attribute/Property
* @apiVersion 2.1.0
* @apiName UpdateOrderIteAttribute
* @apiGroup Orders
*
* @apiDescription
* Use this method to update an existing order item attribute record. It uses the same attribute JSON format
* as in the "Create Order Item Attribute" method.
*
* @apiSuccess Response-Body If successful, this method returns the updated Order Item Attribute resource in the
* response body.
*
* @apiError 400-BadRequest The request body was empty or the order item attribute record ID was not provided or is
* invalid.
* @apiErrorExample Error-Response (Missing ID)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order item attribute data were not provided."
* }
*
* @apiErrorExample Error-Response (Empty request body)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order item attribute data were not provided."
* }
*/
public function put()
{
if(!isset($this->uri[5]) || !is_numeric($this->uri[5]))
{
throw new HttpApiV2Exception('Order item attribute record ID was not provided or is invalid: '
. gettype($this->uri[5]), 400);
}
$orderItemAttributeJsonString = $this->api->request->getBody();
if(empty($orderItemAttributeJsonString))
{
throw new HttpApiV2Exception('Order item attribute data were not provided.', 400);
}
$orderItemId = new IdType($this->uri[3]);
$orderItemAttributeId = new IdType($this->uri[5]);
$attributeClassName = ($this->uri[4] === 'properties') ? 'OrderItemProperty' : 'OrderItemAttribute';
$orderItemAttributes = $this->orderReadService->getOrderItemById($orderItemId)->getAttributes();
$baseOrderItemAttribute = null;
foreach($orderItemAttributes->getArray() as $orderItemAttribute)
{
/** @var StoredOrderItemAttributeInterface $orderItemAttribute */
if($orderItemAttribute->getOrderItemAttributeId() === $orderItemAttributeId->asInt()
&& is_a($orderItemAttribute, $attributeClassName)
)
{
$baseOrderItemAttribute = $orderItemAttribute;
break;
}
}
// Ensure that the order item attribute has the correct order item attribute id of the request url
$orderItemAttributeJsonString = $this->_setJsonValue($orderItemAttributeJsonString, 'id',
$orderItemAttributeId->asInt());
$storedOrderItemAttribute = $this->orderJsonSerializer->deserializeAttribute(json_decode($orderItemAttributeJsonString),
$baseOrderItemAttribute);
$this->orderWriteService->updateOrderItemAttribute($storedOrderItemAttribute);
$response = $this->orderJsonSerializer->serializeAttribute($storedOrderItemAttribute);
$this->_writeResponse($response);
}
/**
* @api {delete} /orders/:id/items/:id/attributes/:id Delete Order Item Attribute/Property
* @apiVersion 2.1.0
* @apiName DeleteOrderItemAttribute
* @apiGroup Orders
*
* @apiDescription
* Removes a single order item attribute/property entry from an existing order item record.
*
* @apiExample {curl} Delete Attribute with ID = 3
* curl -X DELETE --user admin@shop.de:12345
* http://shop.de/api.php/v2/orders/400953/items/1/attributes/3
*
* @apiExample {curl} Delete Property with ID = 84
* curl -X DELETE --user admin@shop.de:12345
* http://shop.de/api.php/v2/orders/400953/items/1/properties/84
*
* @apiSuccessExample {json} Success-Response
* {
* "code": 200,
* "status": "success",
* "action": "delete",
* "resource": "Order",
* "orderId": 400953,
* "orderItemId": 1,
* "orderItemAttributeId": 3
* }
*
* @apiError 400-BadRequest The order item attribute ID in the URI was not provided or is invalid.
* @apiErrorExample Error-Response (Missing ID)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Order item attribute record ID was not provided in the resource URL."
* }
*
* @apiError 404-NotFound The order item attribute was not found.
* @apiErrorExample Error-Response (Not found)
* HTTP/1.1 404 Not Found
* {
* "code": 404,
* "status": "error",
* "message": "The order item attribute was not found."
* }
*/
public function delete()
{
// Check if record ID was provided.
if(!isset($this->uri[5]) || !is_numeric($this->uri[5]))
{
throw new HttpApiV2Exception('Order item attribute record ID was not provided in the resource URL.', 400);
}
$orderItemId = new IdType($this->uri[3]);
$orderItemAttributeId = new IdType($this->uri[5]);
$attributeClassName = ($this->uri[4] === 'properties') ? 'OrderItemProperty' : 'OrderItemAttribute';
$orderItemAttributes = $this->orderReadService->getOrderItemById($orderItemId)->getAttributes();
$storedOrderItemAttribute = null;
foreach($orderItemAttributes->getArray() as $orderItemAttribute)
{
/** @var StoredOrderItemAttributeInterface $orderItemAttribute */
if($orderItemAttribute->getOrderItemAttributeId() === $orderItemAttributeId->asInt()
&& is_a($orderItemAttribute, $attributeClassName)
)
{
$storedOrderItemAttribute = $orderItemAttribute;
}
}
$this->orderWriteService->removeOrderItemAttribute($storedOrderItemAttribute);
// Return response JSON.
$response = array(
'code' => 200,
'status' => 'success',
'action' => 'delete',
'resource' => $attributeClassName,
'orderId' => (int)$this->uri[1],
'orderItemId' => (int)$this->uri[3],
'orderItemAttributeId' => (int)$this->uri[5]
);
$this->_writeResponse($response);
}
/**
* @api {get} /orders/:id/items/:id/attributes/:id Get Order Item Attribute/Property
* @apiVersion 2.1.0
* @apiName GetOrderItemAttribute
* @apiGroup Orders
*
* @apiDescription
* Returns multiple or a single order item attribute/property records through a GET request. This method supports
* all the GET parameters that are mentioned in the "Introduction" section of this documentation.
*
* @apiExample {curl} Get All Order Item Attributes
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/items/3/attributes
*
* @apiExample {curl} Get All Order Item Properties
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/items/3/properties
*
* @apiExample {curl} Get Attribute With ID = 2
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/items/3/attributes/2
*
* @apiExample {curl} Get Property With ID = 54
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/orders/400210/items/3/properties/54
*/
public function get()
{
$response = array();
$orderItemId = new IdType($this->uri[3]);
$orderItemAttributes = $this->orderReadService->getOrderItemById($orderItemId)->getAttributes();
foreach($orderItemAttributes->getArray() as $orderItemAttribute)
{
$response[] = $this->orderJsonSerializer->serializeAttribute($orderItemAttribute);
}
if(isset($this->uri[5]))
{
foreach($response as $item)
{
$itemType = ($item['combisId'] !== null) ? 'properties' : 'attributes';
if($item['id'] === (int)$this->uri[5] && $this->uri[4] == $itemType)
{
$response = $item;
break;
}
}
}
else if($this->api->request->get('q'))
{
$this->_searchResponse($response, $this->api->request->get('q'));
}
$this->_sortResponse($response);
$this->_paginateResponse($response);
$this->_minimizeResponse($response);
$this->_linkResponse($response);
$this->_writeResponse($response);
}
}