1 <?php
2
3 /* --------------------------------------------------------------
4 ProductsLinksApiV2Controller.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 ProductsLinksApiV2Controller
17 *
18 * Provides an API interface for managing the product links.
19 *
20 * Notice: This controller is a subresource of the ProductsApiV2Controller.
21 *
22 * @category System
23 * @package ApiV2Controllers
24 */
25 class ProductsLinksApiV2Controller extends HttpApiV2Controller
26 {
27 /**
28 * Product write service.
29 *
30 * @var ProductWriteService
31 */
32 protected $productWriteService;
33
34 /**
35 * Product read service.
36 *
37 * @var ProductReadService
38 */
39 protected $productReadService;
40
41
42 /**
43 * Initialize API Controller
44 *
45 * @throws HttpApiV2Exception
46 */
47 protected function __initialize()
48 {
49 if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
50 {
51 throw new HttpApiV2Exception('Product record ID was not provided or is invalid: ' . gettype($this->uri[1]),
52 400);
53 }
54
55 $this->productWriteService = StaticGXCoreLoader::getService('ProductWrite');
56 $this->productReadService = StaticGXCoreLoader::getService('ProductRead');
57 }
58
59
60 /**
61 * @api {post} /products/:id/links Create Product Link
62 * @apiVersion 2.1.0
63 * @apiName CreateProductLink
64 * @apiGroup Products
65 *
66 * @apiDescription
67 * Creates a new 'product to category' record in the system.
68 *
69 * @apiParamExample {json} Request-Body
70 * {
71 * "categoryId": 1
72 * }
73 *
74 * @apiSuccessExample {json} Success-Response
75 * {
76 * "code": 201,
77 * "status": "success",
78 * "action": "create",
79 * "resource": "ProductLink",
80 * "productId": 1,
81 * "categoryId": 1
82 * }
83 */
84 public function post()
85 {
86 $json = json_decode($this->api->request->getBody());
87
88 $productId = new IdType($this->uri[1]);
89 $categoryId = new IdType($json->categoryId);
90
91 $this->productWriteService->linkProduct($productId, $categoryId);
92
93 $response = array(
94 'code' => 201,
95 'status' => 'success',
96 'action' => 'create',
97 'resource' => 'ProductLink',
98 'productId' => (int)$this->uri[1],
99 'categoryId' => (int)$json->categoryId
100 );
101
102 $this->_writeResponse($response, 201);
103 }
104
105
106 /**
107 * @api {put} /products/:id/links Update Product Link
108 * @apiVersion 2.1.0
109 * @apiName ChangeProductLink
110 * @apiGroup Products
111 *
112 * @apiDescription
113 * Changes an existing 'product to category' link.
114 *
115 * @apiParamExample {json} Request-Body
116 * {
117 * "oldCategoryId": 1,
118 * "newCategoryId": 2
119 * }
120 *
121 * @apiSuccessExample {json} Success-Response
122 * {
123 * "code": 200,
124 * "status": "success",
125 * "action": "update",
126 * "resource": "ProductLink",
127 * "productId": 1,
128 * "oldCategoryId": 1,
129 * "newCategoryId": 2
130 * }
131 */
132 public function put()
133 {
134 $json = json_decode($this->api->request->getBody());
135
136 $productId = new IdType($this->uri[1]);
137 $oldCategoryId = new IdType($json->oldCategoryId);
138 $newCategoryId = new IdType($json->newCategoryId);
139
140 $this->productWriteService->changeProductLink($productId, $oldCategoryId, $newCategoryId);
141
142 $response = array(
143 'code' => 200,
144 'status' => 'success',
145 'action' => 'update',
146 'resource' => 'ProductLink',
147 'productId' => (int)$this->uri[1],
148 'oldCategoryId' => (int)$json->oldCategoryId,
149 'newCategoryId' => (int)$json->newCategoryId
150 );
151
152 $this->_writeResponse($response);
153 }
154
155
156 /**
157 * @api {delete} /products/:id/links Delete Product Link
158 * @apiVersion 2.1.0
159 * @apiName CreateProductLink
160 * @apiGroup Products
161 *
162 * @apiDescription
163 * Deletes an existing 'product to category' link.
164 * If there is no categoryId property set, all the product links will be removed.
165 *
166 * @apiExample {curl} Delete product link for Product with ID = 1 in Category with ID = 1
167 * curl -X DELETE --user admin@shop.de:12345 http://shop.de/api.php/v2/products/1/links
168 *
169 * @apiSuccessExample {json} Success-Response
170 * {
171 * "code": 200,
172 * "status": "success",
173 * "action": "delete",
174 * "resource": "ProductLink",
175 * "productId": 1
176 * }
177 */
178 public function delete()
179 {
180 $json = json_decode($this->api->request->getBody());
181
182 $productId = new IdType($this->uri[1]);
183
184 if($json->categoryId !== null)
185 {
186 $categoryId = new IdType($json->categoryId);
187 $this->productWriteService->deleteProductLink($productId, $categoryId);
188 }
189 else
190 {
191 $this->productWriteService->deleteProductLinks($productId);
192 }
193
194 $response = array(
195 'code' => 200,
196 'status' => 'success',
197 'action' => 'delete',
198 'resource' => 'ProductLink',
199 'productId' => (int)$this->uri[1]
200 );
201
202 $this->_writeResponse($response);
203 }
204
205
206 /**
207 * @api {get} /products/:id/links Get Product Links
208 * @apiVersion 2.1.0
209 * @apiName CreateProductLink
210 * @apiGroup Products
211 *
212 * @apiDescription
213 * Get all 'product to category' links for associated with a specific product.
214 *
215 * @apiExample {curl} Get product links for product with ID = 1
216 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/products/1/links
217 *
218 * @apiSuccessExample {json} Success-Response
219 * [
220 * 0,
221 * 1
222 * ]
223 */
224 public function get()
225 {
226 $productId = new IdType($this->uri[1]);
227
228 $response = array();
229 $categoriesArray = $this->productReadService->getProductLinks($productId)->getArray();
230
231 foreach($categoriesArray as $categoryId)
232 {
233 $response[] = $categoryId->asInt();
234 }
235
236 $this->_writeResponse($response);
237 }
238 }
239