1 <?php
2
3 /* --------------------------------------------------------------
4 CategoryIconsApiV2Controller.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('AbstractImagesApiV2Controller');
14
15 /**
16 * Class CategoryIconsApiV2Controller
17 *
18 * This controller handles the Category Icon file operations. The clients of the API can list, upload, rename or delete
19 * files that exist in the server.
20 *
21 * @category System
22 * @package ApiV2Controllers
23 */
24 class CategoryIconsApiV2Controller extends AbstractImagesApiV2Controller
25 {
26 /**
27 * Initializes API Controller
28 */
29 protected function __initialize()
30 {
31 $this->writeService = StaticGXCoreLoader::getService('CategoryWrite');
32 }
33
34
35 /**
36 * Returns the absolute path where the image files are located.
37 *
38 * @return string Image folder name.
39 */
40 protected function _getImageFolderName()
41 {
42 return DIR_FS_CATALOG . DIR_WS_IMAGES . 'categories/icons/';
43 }
44
45
46 /**
47 * @api {post} /category_icons Upload Category Icon
48 * @apiVersion 2.1.0
49 * @apiName UploadCategoryIcon
50 * @apiGroup Categories
51 *
52 * @apiDescription
53 * Upload an icon image for the categories. Make this request without the "Content-Type: application/json". Except
54 * from the file the POST request must also contain a "filename" value with the final file name.
55 *
56 * @apiSuccess (Success 201) Response-Body Contains information about the uploaded file.
57 *
58 * @apiSuccessExample {json} Success-Response
59 * {
60 * "code": 201,
61 * "status": "success",
62 * "action": "upload",
63 * "filename": "my-icon-file.png"
64 * }
65 *
66 * @apiError 400-BadRequest No image file or filename parameter have been provided.
67 * @apiErrorExample Error-Response (No image file)
68 * HTTP/1.1 400 Bad Request
69 * {
70 * "code": 400,
71 * "status": "error",
72 * "message": "No image file was provided."
73 * }
74 *
75 * @apiErrorExample Error-Response (No filename)
76 * HTTP/1.1 400 Bad Request
77 * {
78 * "code": 400,
79 * "status": "error",
80 * "message": "The 'filename' parameter is required and was not provided with the request."
81 * }
82 */
83 public function post()
84 {
85 if(!isset($_FILES) || empty($_FILES))
86 {
87 throw new HttpApiV2Exception('No image file was provided.', 400);
88 }
89
90 $file = array_shift($_FILES);
91
92 $existingFile = new ExistingFile(new NonEmptyStringType($file['tmp_name']));
93
94 if($this->api->request->post('filename') === null)
95 {
96 throw new HttpApiV2Exception('The "filename" parameter is required and was not provided with the request. '
97 . 'Check the documentation on how to properly use the API.', 400);
98 }
99
100 $filename = new FilenameStringType($this->api->request->post('filename'));
101
102 $filename = $this->writeService->importCategoryIconFile($existingFile, $filename);
103
104 // Return success response to client.
105 $response = array(
106 'code' => 201,
107 'status' => 'success',
108 'action' => 'upload',
109 'filename' => (string)$filename
110 );
111
112 $this->_writeResponse($response, 201);
113 }
114
115
116 /**
117 * @api {put} /category_icons Rename Icon File
118 * @apiVersion 2.1.0
119 * @apiName RenameIconFile
120 * @apiGroup Categories
121 *
122 * @apiDescription
123 * Use this method to rename an existing icon file.
124 *
125 * @apiExample {json} Request-Body
126 * {
127 * "oldFilename": "my-old-icon.png",
128 * "newFilename": "my-new-icon.png"
129 * }
130 *
131 * @apiSuccess Response-Body Contains information about the executed operation.
132 *
133 * @apiSuccessExample {json} Response-Body
134 * {
135 * "code": 200,
136 * "status": "success",
137 * "action": "rename",
138 * "oldFilename": "my-old-icon.png",
139 * "newFilename": "my-new-icon.png"
140 * }
141 *
142 * @apiError 400-BadRequest The body of the request was empty or the request body did not contain the oldFilename or newFilename properties, or their
143 * values were invalid.
144 *
145 * @apiErrorExample Error-Response (Empty request body)
146 * HTTP/1.1 400 Bad Request
147 * {
148 * "code": 400,
149 * "status": "error",
150 * "message": "Category icon data were not provided."
151 * }
152 *
153 * @apiErrorExample Error-Response (Missing parameters)
154 * HTTP/1.1 400 Bad Request
155 * {
156 * "code": 400,
157 * "status": "error",
158 * "message": "This operation requires a JSON object with 'oldFilename' and 'newFilename' properties set."
159 * }
160 */
161 public function put()
162 {
163 $iconJsonString = $this->api->request->getBody();
164
165 if(empty($iconJsonString))
166 {
167 throw new HttpApiV2Exception('Category icon data were not provided.', 400);
168 }
169
170 $iconJsonObject = json_decode($iconJsonString);
171
172 if($iconJsonObject->oldFilename === null || $iconJsonObject->newFilename === null)
173 {
174 throw new HttpApiV2Exception('This operation requires a JSON object with "oldFilename" and "newFilename" '
175 . 'properties set. Check the documentation on how to properly use the API.',
176 400);
177 }
178
179 $oldFilename = new FilenameStringType($iconJsonObject->oldFilename);
180 $newFilename = new FilenameStringType($iconJsonObject->newFilename);
181
182 $this->writeService->renameCategoryIconFile($oldFilename, $newFilename);
183
184 $response = array(
185 'code' => 200,
186 'status' => 'success',
187 'action' => 'rename',
188 'oldFilename' => $iconJsonObject->oldFilename,
189 'newFilename' => $iconJsonObject->newFilename
190 );
191
192 $this->_writeResponse($response);
193 }
194
195
196 /**
197 * @api {delete} /category_icon Delete Category Icon
198 * @apiVersion 2.1.0
199 * @apiName DeleteCategoryIcon
200 * @apiGroup Categories
201 *
202 * @apiDescription
203 * Removes the category icon file from the server. This method will always provide a successful response even if
204 * the image file was not found.
205 *
206 * @apiExample {json} Request-Body
207 * {
208 * "filename": "file-to-be-deleted.png"
209 * }
210 *
211 * @apiSuccessExample {json} Success-Response
212 * {
213 * "code": 200,
214 * "status": "success",
215 * "action": "delete",
216 * "filename": "file-to-be-deleted.png"
217 * }
218 *
219 * @apiError 400-BadRequest Category icon data were not provided or the request body did not contain the
220 * filename or its value was invalid.
221 *
222 * @apiErrorExample Error-Response (Empty request body)
223 * HTTP/1.1 400 Bad Request
224 * {
225 * "code": 400,
226 * "status": "error",
227 * "message": "Category icon data were not provided."
228 * }
229 *
230 * @apiErrorExample Error-Response (Missing parameters)
231 * HTTP/1.1 400 Bad Request
232 * {
233 * "code": 400,
234 * "status": "error",
235 * "message": "This operation requires a JSON object with 'filename' properties set."
236 * }
237 */
238 public function delete()
239 {
240 $iconJsonString = $this->api->request->getBody();
241
242 if(empty($iconJsonString))
243 {
244 throw new HttpApiV2Exception('Category icon data were not provided.', 400);
245 }
246
247 $iconJsonObject = json_decode($iconJsonString);
248
249 if($iconJsonObject->filename === null)
250 {
251 throw new HttpApiV2Exception('This operation requires a JSON object with "filename" properties set. '
252 . 'Check the documentation on how to properly use the API.', 400);
253 }
254
255 // The CategoryWriteService will not throw an exception if the image file does not exist.
256 $this->writeService->deleteCategoryIconFile(new FilenameStringType($iconJsonObject->filename));
257
258 $response = array(
259 'code' => 200,
260 'status' => 'success',
261 'action' => 'delete',
262 'filename' => $iconJsonObject->filename
263 );
264
265 $this->_writeResponse($response);
266 }
267
268
269 /**
270 * @api {get} /category_icon
271 * @apiVersion 2.1.0
272 * @apiName GetCategoryIcons
273 * @apiGroup Categories
274 *
275 * @apiDescription
276 * Returns a list of all category icon files which exists in the server's filesystem through a GET request.
277 *
278 * @apiExample {curl} Get All Category Icons
279 * curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/category_icon
280 */
281 public function get()
282 {
283 parent::get();
284 }
285 }
286