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