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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
<?php
/* --------------------------------------------------------------
ProductsApiV2Controller.inc.php 2016-09-06
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 ProductsApiV2Controller
*
* Provides a gateway to the ProductWriteService and ProductReadService classes, which handle the shop
* product resources.
*
* @category System
* @package ApiV2Controllers
*/
class ProductsApiV2Controller extends HttpApiV2Controller
{
/**
* Product write service.
*
* @var ProductWriteService
*/
protected $productWriteService;
/**
* Product read service.
*
* @var ProductReadService
*/
protected $productReadService;
/**
* Product JSON serializer.
*
* @var ProductJsonSerializer
*/
protected $productJsonSerializer;
/**
* Product list item JSON serializer.
*
* @var ProductListItemJsonSerializer
*/
protected $productListItemJsonSerializer;
/**
* Sub resources.
*
* @var array
*/
protected $subresource;
/**
* Initializes API Controller
*/
protected function __initialize()
{
$this->productWriteService = StaticGXCoreLoader::getService('ProductWrite');
$this->productReadService = StaticGXCoreLoader::getService('ProductRead');
$this->productJsonSerializer = MainFactory::create('ProductJsonSerializer');
$this->productListItemJsonSerializer = MainFactory::create('ProductListItemJsonSerializer');
$this->subresource = array(
'links' => 'ProductsLinksApiV2Controller'
);
}
/**
* @api {post} /products Create Product
* @apiVersion 2.1.0
* @apiName CreateProduct
* @apiGroup Products
*
* @apiDescription
* Creates a new product record in the system. To see an example usage take a look at
* `docs/REST/samples/product-service/create_product.php`
*
* @apiParamExample {json} Request-Body
* {
* "isActive": false,
* "sortOrder": 0,
* "orderedCount": 1,
* "productModel": "ABC123",
* "ean": "",
* "price": 16.7983,
* "discountAllowed": 0,
* "taxClassId": 1,
* "quantity": 998,
* "weight": 0,
* "shippingCosts": 0,
* "shippingTimeId": 1,
* "productTypeId": 1,
* "manufacturerId": 0,
* "isFsk18": false,
* "isVpeActive": false,
* "vpeID": 0,
* "vpeValue": 0,
* "name": {
* "en": "test article",
* "de": "Testartikel"
* },
* "description": {
* "en": "[TAB:Page 1] Test Product Description (Page 1) [TAB: Page 2] Test Product Description (Page 2)",
* "de": "[TAB:Seite 1] Testartikel Beschreibung (Seite 1) [TAB:Seite 2] Testartikel Beschreibung (Seite 2)"
* },
* "shortDescription": {
* "en": "<p>Test product short description.</p>",
* "de": "<p>Testartikel Kurzbeschreibung</p>"
* },
* "keywords": {
* "en": "",
* "de": ""
* },
* "metaTitle": {
* "en": "",
* "de": ""
* },
* "metaDescription": {
* "en": "",
* "de": ""
* },
* "metaKeywords": {
* "en": "",
* "de": ""
* },
* "url": {
* "en": "",
* "de": ""
* },
* "urlKeywords": {
* "en": "test-article",
* "de": "Testartikel"
* },
* "checkoutInformation": {
* "en": "",
* "de": ""
* },
* "viewedCount": {
* "en": 0,
* "de": 32
* },
* "images": [
* {
* "filename": "artikelbild_1_1.jpg",
* "isPrimary": false,
* "isVisible": true,
* "imageAltText": {
* "en": "",
* "de": ""
* }
* },
* {
* "filename": "artikelbild_1_2.jpg",
* "isPrimary": false,
* "isVisible": true,
* "imageAltText": {
* "en": "",
* "de": ""
* }
* },
* {
* "filename": "artikelbild_1_3.jpg",
* "isPrimary": false,
* "isVisible": true,
* "imageAltText": {
* "en": "",
* "de": ""
* }
* }
* ],
* "settings": {
* "detailsTemplate": "standard.html",
* "optionsDetailsTemplate": "product_options_dropdown.html",
* "optionsListingTemplate": "product_options_dropdown.html",
* "showOnStartpage": false,
* "showQuantityInfo": true,
* "showWeight": false,
* "showPriceOffer": true,
* "showAddedDateTime": false,
* "priceStatus": 0,
* "minOrder": 1,
* "graduatedQuantity": 1,
* "onSitemap": true,
* "sitemapPriority": "0.5",
* "sitemapChangeFrequency": "daily",
* "propertiesDropdownMode": "dropdown_mode_1",
* "startpageSortOrder": 0,
* "showPropertiesPrice": true,
* "usePropertiesCombisQuantity": false,
* "usePropertiesCombisShippingTime": true,
* "usePropertiesCombisWeight": false
* },
* "addonValues": {
* "productsImageWidth": "0",
* "productsImageHeight": "0"
* }
* }
*
* @apiParam {Boolean} isActive Whether the product is active.
* @apiParam {Number} sortOrder The sort order of the product.
* @apiParam {Number} orderedCount How many times the product was ordered.
* @apiParam {String} productModel Product's Model.
* @apiParam {String} ean European Article Number.
* @apiParam {Number} price Product's Price as float value.
* @apiParam {Number} discountAllowed Percentage of the allowed discount as float value.
* @apiParam {Number} taxClassId The tax class ID.
* @apiParam {Number} quantity Quantity in stock as float value.
* @apiParam {Number} weight The weight of the product as float value.
* @apiParam {Number} shippingCosts Additional shipping costs as float value.
* @apiParam {Number} shippingTimeId Must match a record from the shipping time entries.
* @apiParam {Number} productTypeId Must match a record from the product type entries.
* @apiParam {Number} manufacturerId Must match the ID of the manufacturer record.
* @apiParam {Boolean} isFsk18 Whether the product is FSK18.
* @apiParam {Boolean} isVpeActive Whether VPE is active.
* @apiParam {Number} vpeID The VPE ID of the product.
* @apiParam {Number} vpeValue The VPE value of the product as float value.
* @apiParam {Object} name Language specific object with the product's name.
* @apiParam {Object} description Language specific object with the product's description.
* @apiParam {Object} shortDescription Language specific object with the product's short description.
* @apiParam {Object} keywords Language specific object with the product's keywords.
* @apiParam {Object} metaTitle Language specific object with the product's meta title.
* @apiParam {Object} metaDescription Language specific object with the product's meta description.
* @apiParam {Object} metaKeywords Language specific object with the product's meta keywords.
* @apiParam {Object} url Language specific object with the product's url.
* @apiParam {Object} urlKeywords Language specific object with the product's url keywords.
* @apiParam {Object} checkoutInformation Language specific object with the product's checkout information.
* @apiParam {Object} viewedCount Language specific object with the product's viewed count.
* @apiParam {Array} images Contains the product images information.
* @apiParam {String} images.filename The product image file name (provide only the file name and not the whole
* path).
* @apiParam {Boolean} images.isPrimary Whether the image is the primary one.
* @apiParam {Boolean} images.isVisible Whether the image will be visible.
* @apiParam {Object} images.imageAltText Language specific object with the image alternative text.
* @apiParam {Object} settings Contains various product settings.
* @apiParam {String} settings.detailsTemplate Filename of the details HTML template.
* @apiParam {String} settings.optionsDetailsTemplate Filename of the options details HTML template.
* @apiParam {String} settings.optionsListingTemplate Filename of the options listing HTML template.
* @apiParam {Boolean} settings.showOnStartpage Whether to show the product on startpage.
* @apiParam {Boolean} settings.showQuantityInfo Whether to show quantity information.
* @apiParam {Boolean} settings.showWeight Whether to show the products weight.
* @apiParam {Boolean} settings.showPriceOffer Whether to show price offer.
* @apiParam {Boolean} settings.showAddedDateTime Whether to show the creation date-time of the product.
* @apiParam {Number} settings.priceStatus Must match a record from the price status entries.
* @apiParam {Number} settings.minOrder The minimum order of the product.
* @apiParam {Number} settings.graduatedQuantity Product's graduated quantity.
* @apiParam {Boolean} settings.onSitemap Whether to include the product in the sitemap.
* @apiParam {String} settings.sitemapPriority The sitemap priority (provide a decimal value as a string).
* @apiParam {String} settings.sitemapChangeFrequency Possible values can contain the `always`, `hourly`, `daily`,
* `weekly`, `monthly`, `yearly`, `never`.
* @apiParam {String} settings.propertiesDropdownMode Provide one of the following values: "" >> Default - all
* values are always selectable, `dropdown_mode_1` >> Any order, only possible values are selectable,
* `dropdown_mode_2` >> Specified order, only possible values are selectable.
* @apiParam {Number} settings.startpageSortOrder The sort order in the startpage.
* @apiParam {Boolean} settings.showPropertiesPrice Whether to show properties price.
* @apiParam {Boolean} settings.usePropertiesCombisQuantity Whether to use properties combis quantitity.
* @apiParam {Boolean} settings.usePropertiesCombisShippingTime Whether to use properties combis shipping time.
* @apiParam {Boolean} settings.usePropertiesCombisWeight Whether to use properties combis weight.
* @apiParam {Object} addonValues Contains some extra addon values.
* @apiParam {String} addonValues.productsImageWidth The CSS product image width (might contain size metrics).
* @apiParam {String} addonValues.productsImageHeight The CSS product image height (might contain size metrics).
*
* @apiSuccess (Success 201) Response-Body If successful, this method returns a complete Product resource in the
* response body.
*
* @apiError 400-BadRequest The body of the request was empty.
*
* @apiErrorExample Error-Response
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "The body of the request was empty."
* }
*/
public function post()
{
if($this->_mapResponse($this->subresource))
{
return;
}
$productJsonString = $this->api->request->getBody();
if(isset($this->uri[1]) && is_numeric($this->uri[1])) // Duplicate Product
{
$productJsonObject = json_decode($productJsonString);
if($productJsonObject->categoryId === null || !is_numeric($productJsonObject->categoryId))
{
$productJsonObject = new stdClass;
$productJsonObject->categoryId = 0; // Default category value.
}
$productId = $this->productWriteService->duplicateProduct(new IdType($this->uri[1]),
new IdType($productJsonObject->categoryId));
}
else // Create New Product
{
$product = $this->productJsonSerializer->deserialize($productJsonString);
$productId = $this->productWriteService->createProduct($product);
}
$storedProduct = $this->productReadService->getProductById(new IdType($productId));
$response = $this->productJsonSerializer->serialize($storedProduct, false);
$this->_linkResponse($response);
$this->_locateResource('products', $productId);
$this->_writeResponse($response, 201);
}
/**
* @api {put} /products/:id Update Product
* @apiVersion 2.1.0
* @apiName ProductCategory
* @apiGroup Products
*
* @apiDescription
* Use this method to update an existing product record. Take a look in the POST method for more detailed
* explanation on every resource property. To see an example usage consider
* `docs/REST/samples/product-service/update_product.php`
*
* @apiSuccess Response-Body If successful, this method returns the updated Product resource in the response body.
*
* @apiError 400-BadRequest Product data were not provided.
* @apiErrorExample Error-Response (No data)
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Product data were not provided."
* }
*
* @todo Error status code on not found entries should be 404 and not 400.
*/
public function put()
{
if($this->_mapResponse($this->subresource))
{
return;
}
if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Product record ID was not provided or is invalid: ' . gettype($this->uri[1]),
400);
}
$productJsonString = $this->api->request->getBody();
if(empty($productJsonString))
{
throw new HttpApiV2Exception('Product data were not provided.', 400);
}
$productId = new IdType($this->uri[1]);
// Ensure that the product has the correct product id of the request url
$productJsonString = $this->_setJsonValue($productJsonString, 'id', $productId->asInt());
$product = $this->productJsonSerializer->deserialize($productJsonString,
$this->productReadService->getProductById($productId));
$this->productWriteService->updateProduct($product);
$response = $this->productJsonSerializer->serialize($this->productReadService->getProductById($productId),
false);
$this->_linkResponse($response);
$this->_writeResponse($response, 200);
}
/**
* @api {delete} /products/:id Delete Product
* @apiVersion 2.1.0
* @apiName DeleteProduct
* @apiGroup Products
*
* @apiDescription
* Removes a product record from the database. To see an example usage take a look at
* `docs/REST/samples/product-service/remove_product.php`
*
* @apiExample {curl} Delete Product With ID = 24
* curl -X DELETE --user admin@shop.de:12345 http://shop.de/api.php/v2/products/24
*
* @apiSuccessExample {json} Success-Response
* {
* "code": 200,
* "status": "success",
* "action": "delete",
* "resource": "Product",
* "productId": 24
* }
*
* @apiError 400-BadRequest Product record ID was not provided in the resource URL.
* @apiErrorExample Error-Response
* HTTP/1.1 400 Bad Request
* {
* "code": 400,
* "status": "error",
* "message": "Product record ID was not provided in the resource URL."
* }
*/
public function delete()
{
if($this->_mapResponse($this->subresource))
{
return;
}
// Check if record ID was provided.
if(!isset($this->uri[1]) || !is_numeric($this->uri[1]))
{
throw new HttpApiV2Exception('Product record ID was not provided in the resource URL.', 400);
}
// Remove product record from database.
$this->productWriteService->deleteProductById(new IdType($this->uri[1]));
// Return response JSON.
$response = array(
'code' => 200,
'status' => 'success',
'action' => 'delete',
'resource' => 'Product',
'productId' => (int)$this->uri[1]
);
$this->_writeResponse($response);
}
/**
* @api {get} /products/:id Get Products
* @apiVersion 2.1.0
* @apiName GetProduct
* @apiGroup Products
*
* @apiDescription
* Get multiple or a single product records through a GET request. This method supports all the GET parameters
* that are mentioned in the "Introduction" section of this documentation. To see an example usage take a look at
* `docs/REST/samples/product-service/remove_product.php`
*
* @apiExample {curl} Get All Products
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/products
*
* @apiExample {curl} Get Product With ID = 24
* curl -i --user admin@shop.de:12345 http://shop.de/api.php/v2/products/24
*
* @apiError 404-NotFound Product does not exist.
* @apiErrorExample Error-Response
* HTTP/1.1 404 Not Found
* {
* "code": 404,
* "status": "error",
* "message": "Product does not exist."
* }
*/
public function get()
{
if($this->_mapResponse($this->subresource))
{
return;
}
if($this->uri[1] && is_numeric($this->uri[1])) // Get Single Record
{
try
{
$products = array($this->productReadService->getProductById(new IdType($this->uri[1])));
}
catch(UnexpectedValueException $e)
{
throw new HttpApiV2Exception('Product does not exist.', 404);
}
}
else
{
$langParameter = ($this->api->request->get('lang') !== null) ? $this->api->request->get('lang') : 'de';
$languageCode = new LanguageCode(new NonEmptyStringType($langParameter));
$products = $this->productReadService->getProductList($languageCode)->getArray();
}
$response = array();
foreach($products as $product)
{
if($product instanceof ProductInterface)
{
$serialized = $this->productJsonSerializer->serialize($product, false);
}
else
{
$serialized = $this->productListItemJsonSerializer->serialize($product, false);
}
$response[] = $serialized;
}
if($this->api->request->get('q') !== null)
{
$this->_searchResponse($response, $this->api->request->get('q'));
}
$this->_sortResponse($response);
$this->_paginateResponse($response);
$this->_minimizeResponse($response);
$this->_linkResponse($response);
// Return single resource to client and not array.
if(isset($this->uri[1]) && is_numeric($this->uri[1]) && count($response) > 0)
{
$response = $response[0];
}
$this->_writeResponse($response);
}
}