/**
  * @api {put} /products_options/:id PUT /products_options/:id
  * @apiExample Example usage:
  * curl -i -X PUT "http://apibeta.compargo.com/v1/products_options/e8227346-3a61-11e4-b18a-fe7344fb1ea4/?countryCode=ph&language=en"
  *      -H "X-COMPARE-REST-API-KEY: 1234567890"
  *      -d "areaId=0b412a0e-397a-11e4-b18a-fe7344fb1ea4&
  *          value=1&
  *          status=0"
  *      
  * @apiDescription Update a Products Option
  * @apiName  Put
  * @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} id                       Mandatory Products Option Unique ID.
  * @apiParam  {String} productId                Mandatory Product ID of the Products Option.
  * @apiParam  {String} areaId                   Mandatory Area ID of the Products Option.
  * @apiParam  {String} name                     Mandatory Name of the Products Option.
  * @apiParam  {String} value                    Mandatory Value of the Products Option.
  * @apiParam  {String} [label]                  Optional Label of the Products Option.
  * @apiParam  {String} [editable]               Optional Editable Flag of the Products Option.
  * @apiParam  {String} [visibility]             Optional Visibility Flag of the Products Option.
  * @apiParam  {String} [status]                 Optional Status of the Products Option.
  * @apiParam  {String} [createdBy]              Optional ID of the User who created the Products Option.
  * @apiParam  {String} [modifiedBy]             Optional ID of the User who modified the Products Option.
  *
  * @apiSuccessExample Success-Response:
  *     HTTP/1.1 200 OK
  *     {
  *       "id": "e8227346-3a61-11e4-b18a-fe7344fb1ea4"
  *     }
  *     
  * @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 ProductsOptionNotFound The id of the Products Option was not found.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 Not Found
  *     {
  *       "error": "ProductsOptionNotFound"
  *     }
  */
 public function put($id)
 {
     $results = array();
     $request = $this->di->get('request');
     $data = $request->getPut();
     if (!empty($data)) {
         $productsOptions = ProductsOptions::findFirstById($id);
         if (!$productsOptions) {
             throw new HTTPException("Not found", 404, array('dev' => 'Products option does not exist', 'internalCode' => 'P1000', 'more' => ''));
         } else {
             $data['productId'] = isset($data['productId']) ? $data['productId'] : $productsOptions->productId;
             $data['areaId'] = isset($data['areaId']) ? $data['areaId'] : $productsOptions->areaId;
             $data['name'] = isset($data['name']) ? $data['name'] : $productsOptions->name;
             $data['value'] = isset($data['value']) ? $data['value'] : $productsOptions->value;
             $data['label'] = isset($data['label']) ? $data['label'] : $productsOptions->label;
             $data['status'] = isset($data['status']) ? $data['status'] : $productsOptions->status;
             if (isset($data['status'])) {
                 $data['active'] = $data['status'] != ProductsOptions::ACTIVE ? 0 : 1;
                 $data['editable'] = $data['status'] != ProductsOptions::ACTIVE ? 0 : $productsOptions->editable;
                 $data['visibility'] = $data['status'] != ProductsOptions::ACTIVE ? 0 : $productsOptions->visibility;
             }
             $data['createdBy'] = isset($data['createdBy']) ? $data['createdBy'] : $productsOptions->createdBy;
             $data['modifiedBy'] = isset($data['modifiedBy']) ? $data['modifiedBy'] : $productsOptions->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;
 }
 /**
  * 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);
                 }
             }
         }
     }
 }
 /**
  * Responds with information about updated inserted products option
  *
  * @method post
  * @return json/xml data
  */
 public function put($id)
 {
     $results = array();
     $productsOption = ProductsOptions::findFirstById($id);
     if ($productsOption) {
         $productsOptionCollection = ProductsOptionsCollection::findFirst(array(array('id' => $id)));
         if ($productsOptionCollection != false) {
             $productsOptionCollection->id = $productsOption->id;
             $productsOptionCollection->productId = $productsOption->productId;
             $productsOptionCollection->areaId = $productsOption->areaId;
             $productsOptionCollection->name = $productsOption->name;
             $productsOptionCollection->value = $productsOption->value;
             $productsOptionCollection->label = $productsOption->label;
             $productsOptionCollection->status = $productsOption->status;
             $productsOptionCollection->active = $productsOption->active;
             $productsOptionCollection->editable = $productsOption->editable;
             $productsOptionCollection->visibility = $productsOption->visibility;
             $productsOptionCollection->created = $productsOption->created;
             $productsOptionCollection->modified = $productsOption->modified;
             $productsOptionCollection->createdBy = $productsOption->createdBy;
             $productsOptionCollection->modifiedBy = $productsOption->modifiedBy;
             $success = $productsOptionCollection->save();
             if ($success) {
                 if (1 == $productsOption->status) {
                     $products = Products::findFirst(array(array('id' => $productsOption->productId)));
                     if ($products) {
                         $name = $productsOption->name;
                         $products->{$name} = $productsOption->value;
                         $products->save();
                         $results['id'] = $productsOption->id;
                     }
                 }
             }
         }
     }
     return $results;
 }