/**
  * Responds with information about updated inserted company option
  *
  * @method put
  * @return json/xml data
  */
 public function put($id)
 {
     $results = array();
     $companiesOption = CompaniesOptions::findFirstById($id);
     if ($companiesOption) {
         $companiesOptionCollection = CompaniesOptionsCollection::findFirst(array(array('id' => $id)));
         if ($companiesOptionCollection != false) {
             $companiesOptionCollection->id = $companiesOption->id;
             $companiesOptionCollection->modelId = $companiesOption->modelId;
             $companiesOptionCollection->category = $companiesOption->category;
             $companiesOptionCollection->name = $companiesOption->name;
             $companiesOptionCollection->value = $companiesOption->value;
             $companiesOptionCollection->label = $companiesOption->label;
             $companiesOptionCollection->status = $companiesOption->status;
             $companiesOptionCollection->active = $companiesOption->active;
             $companiesOptionCollection->editable = $companiesOption->editable;
             $companiesOptionCollection->visibility = $companiesOption->visibility;
             $companiesOptionCollection->created = $companiesOption->created;
             $companiesOptionCollection->modified = $companiesOption->modified;
             $companiesOptionCollection->createdBy = $companiesOption->createdBy;
             $companiesOptionCollection->modifiedBy = $companiesOption->modifiedBy;
             $companiesOptionCollection->save();
             $success = $companiesOptionCollection->save();
             if ($success) {
                 if ('company' == $companiesOption->category && 1 == $companiesOption->status) {
                     $collection = CompaniesCollection::findFirst(array(array('id' => $companiesOption->modelId)));
                 } else {
                     if ('brand' == $companiesOption->category && 1 == $companiesOption->status) {
                         $collection = BrandsCollection::findFirst(array(array('id' => $companiesOption->modelId)));
                     }
                 }
                 if ($collection) {
                     $name = $companiesOption->name;
                     $collection->{$name} = $companiesOption->value;
                     $collection->save();
                     $results['id'] = $companiesOption->id;
                 }
             }
         }
     }
     return $results;
 }
 /**
  * @api {put} /companies_options/:id PUT /companies_options/:id
  * @apiExample Example usage:
  * curl -i -X PUT "http://apibeta.compargo.com/v1/companies_options/8a048714-2791-11e4-bd33-17609cecca2f/?countryCode=ph&language=en"
  *      -H "X-COMPARE-REST-API-KEY: 1234567890"
  *      -d "category=brand&
  *          value=1234567&
  *          editable=1&
  *          visibility=1&
  *          status=0"
  *      
  * @apiDescription Update a Companies Option
  * @apiName  Put
  * @apiGroup Companies Options
  *
  * @apiHeader {String} X-COMPARE-REST-API-KEY   Companies Options unique access-key.
  *
  * @apiParam  {String} language                 Mandatory Language.
  * @apiParam  {String} countryCode              Mandatory Country Code.
  * @apiParam  {String} id                       Mandatory Companies Option Unique ID.
  * @apiParam  {String} modelId                  Mandatory Company ID/Brand ID of the Companies Option.
  * @apiParam  {String} category                 Mandatory Company/Model ID of the Companies Option.
  * @apiParam  {String} name                     Mandatory Name of the Companies Option.
  * @apiParam  {String} value                    Mandatory Value of the Companies Option.
  * @apiParam  {String} [label]                  Optional Label of the Companies Option.
  * @apiParam  {String} [editable]               Optional Editable Flag of the Companies Option.
  * @apiParam  {String} [visibility]             Optional Visibility Flag of the Companies Option.
  * @apiParam  {String} [status]                 Optional Status of the Companies Option.
  * @apiParam  {String} [createdBy]              Optional ID of the User who created the Companies Option.
  * @apiParam  {String} [modifiedBy]             Optional ID of the User who modified the Companies Option.
  *
  * @apiSuccessExample Success-Response:
  *     HTTP/1.1 200 OK
  *     {
  *       "id": "8a048714-2791-11e4-bd33-17609cecca2f"
  *     }
  *     
  * @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 CompaniesOptionNotFound The id of the Companies Option was not found.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 Not Found
  *     {
  *       "error": "CompaniesOptionNotFound"
  *     }
  */
 public function put($id)
 {
     $results = $params = array();
     $request = $this->di->get('request');
     $data = $request->getPut();
     if (!empty($data)) {
         $companiesOptions = CompaniesOptions::findFirstById($id);
         if (!$companiesOptions) {
             throw new HTTPException("Not found", 404, array('dev' => 'Companies option does not exist', 'internalCode' => 'P1000', 'more' => ''));
         } else {
             $data['modelId'] = isset($data['modelId']) ? $data['modelId'] : $companiesOptions->modelId;
             $data['category'] = isset($data['category']) ? $data['category'] : $companiesOptions->category;
             $data['name'] = isset($data['name']) ? $data['name'] : $companiesOptions->name;
             $data['value'] = isset($data['value']) ? $data['value'] : $companiesOptions->value;
             $data['label'] = isset($data['label']) ? $data['label'] : $companiesOptions->label;
             $data['status'] = isset($data['status']) ? $data['status'] : $companiesOptions->status;
             if (isset($data['status'])) {
                 $data['active'] = $data['status'] != CompaniesOptions::ACTIVE ? 0 : 1;
                 $data['editable'] = $data['status'] != CompaniesOptions::ACTIVE ? 0 : $companiesOptions->editable;
                 $data['visibility'] = $data['status'] != CompaniesOptions::ACTIVE ? 0 : $companiesOptions->visibility;
             }
             $data['createdBy'] = isset($data['createdBy']) ? $data['createdBy'] : $companiesOptions->createdBy;
             $data['modifiedBy'] = isset($data['modifiedBy']) ? $data['modifiedBy'] : $companiesOptions->modifiedBy;
             if ($companiesOptions->save($data)) {
                 $results['id'] = $companiesOptions->id;
             } else {
                 throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $companiesOptions->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;
 }