/** * Responds with information about newly inserted option * * @method _addProductsOptions * @param string productId // product id parameter of type string * @param string areaId // area id parameter of type string * @param string channelAlias // channel alias parameter of type string * @param array productInformation // product information parameter of type array * */ private function _addProductsOptions($productId, $areaId, $channelAlias, $productInformation) { if (!empty($productInformation)) { $productOptionData = $params = array(); if ($channelAlias) { foreach ($productInformation as $key => $value) { $key = trim($key); $value = (string) $value; $productOption = ProductsOptions::findFirst("productId = '" . $productId . "' AND name = '" . $key . "'"); if (!$productOption) { $productOption = new ProductsOptions(); $productOptionData['id'] = $productOption->id; $productOptionData['productId'] = $productId; $productOptionData['areaId'] = $areaId; $productOptionData['name'] = $key; $productOptionData['value'] = $value; $productOptionData['status'] = $productInformation['status']; if (isset($productOptionData['status'])) { $productOptionData['active'] = $productOptionData['status'] != ProductsOptions::ACTIVE ? 0 : 1; } $productOptionData['createdBy'] = isset($productInformation['createdBy']) ? $productInformation['createdBy'] : ''; $productOptionData['modifiedBy'] = isset($productInformation['modifiedBy']) ? $productInformation['modifiedBy'] : ''; $params[$key] = $value; $productOption->save($productOptionData); } } } } }
/** * @api {post} /products_options POST /products_options * @apiExample Example usage: * curl -i -X POST "http://apibeta.compargo.com/v1/products_options/?countryCode=ph&language=en" * -H "X-COMPARE-REST-API-KEY: 1234567890" * -d "productId=73a4f032-3a45-11e4-b18a-fe7344fb1ea4& * areaId=0b412a0e-397a-11e4-b18a-fe7344fb1ea4& * name=maxAge& * value=70& * status=1& * editable=1& * visibility=1" * * @apiDescription Create a new Products Options * @apiName Post * @apiGroup Products Options * * @apiHeader {String} X-COMPARE-REST-API-KEY Products Options unique access-key. * * @apiParam {String} language Mandatory Language. * @apiParam {String} countryCode Mandatory Country code. * @apiParam {String} productId Mandatory Product ID of Products Options. * @apiParam {String} areaId Mandatory Area ID of Products Options. * @apiParam {String} name Mandatory Name of the Products Options. * @apiParam {String} value Mandatory Value of the Products Options. * @apiParam {String} [label] Optional Label of the Products Options. * @apiParam {String} [editable=0] Optional Editable Flag of the Products Options. * @apiParam {String} [visibility=0] Optional Visibility Value of the Products Options. * @apiParam {String} [status=0] Optional Status of the Products Options. * @apiParam {String} [createdBy] Optional ID of the User who created the Products Options. * @apiParam {String} [modifiedBy] Optional ID of the User who modified the Products Options. * * @apiSuccess {String} id The new ProductsOptions-ID. * * @apiSuccessExample Success-Response: * HTTP/1.1 200 OK * { * "id": "3aa61eec-3c06-11e4-9a7a-90a27a7c008a" * } * * @apiError BadInputParameter The request cannot be fulfilled due to bad syntax. * * @apiErrorExample Error-Response: * HTTP/1.1 400 * { * "error": "BadInputParameter" * } * * @apiError InvalidAccessToken The access token is invalid. * * @apiErrorExample Error-Response: * HTTP/1.1 401 Unauthorized * { * "error": "InvalidAccessToken" * } * * @apiError MissingAuthenticationCredentials The authentication credentials are missing. * * @apiErrorExample Error-Response: * HTTP/1.1 401 Unauthorized * { * "error": "MissingAuthenticationCredentials" * } * * @apiError RouteNotFound That route was not found on the server. * * @apiErrorExample Error-Response: * HTTP/1.1 404 * { * "error": "RouteNotFound" * } */ public function post() { $results = array(); $request = $this->di->get('request'); $data = $request->getPost(); if (!empty($data)) { $productsOptions = new ProductsOptions(); $data['id'] = $productsOptions->id; $data['productId'] = isset($data['productId']) ? $data['productId'] : ''; $data['areaId'] = isset($data['areaId']) ? $data['areaId'] : ''; $data['name'] = isset($data['name']) ? $data['name'] : ''; $data['value'] = isset($data['value']) ? $data['value'] : ''; $data['label'] = isset($data['label']) ? $data['label'] : ''; $data['editable'] = isset($data['editable']) ? $data['editable'] : 0; $data['visibility'] = isset($data['visibility']) ? $data['visibility'] : 0; $data['status'] = isset($data['status']) ? $data['status'] : 0; if (isset($data['status'])) { $data['active'] = $data['status'] != ProductsOptions::ACTIVE ? 0 : 1; $data['editable'] = $data['status'] != ProductsOptions::ACTIVE ? 0 : $data['editable']; $data['visibility'] = $data['status'] != ProductsOptions::ACTIVE ? 0 : $data['visibility']; } $data['createdBy'] = isset($data['createdBy']) ? $data['createdBy'] : ''; $data['modifiedBy'] = isset($data['modifiedBy']) ? $data['modifiedBy'] : ''; $products = Products::findFirstById($data['productId']); if ($products) { $channels = Channels::findFirstById($products->channelId); if ($channels) { $params[$data['name']] = $data['value']; $valid = $productsOptions->validateProperty($params, $this->schemaDir . $this->getDI()->getConfig()->application->jsonSchema->{$channels}->alias); if (!empty($valid)) { $errors = implode(", ", $valid); throw new HTTPException("Request unable to be followed due to semantic errors.", 422, array('dev' => ucfirst($errors), 'internalCode' => 'P1000', 'more' => '')); } } } if ($productsOptions->save($data)) { $results['id'] = $productsOptions->id; } else { throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $productsOptions->getMessages(), 'internalCode' => 'P1000', 'more' => '')); } } else { throw new HTTPException("The request cannot be fulfilled due to bad syntax.", 400, array('dev' => 'A required field is missing.', 'internalCode' => 'P1000', 'more' => '')); } return $results; }