/**
  * Responds with information about updated inserted brand
  *
  * @method put
  * @return json/xml data
  */
 public function put($id)
 {
     $results = array();
     $brand = Brands::findFirstById($id);
     if ($brand) {
         $brandCollection = BrandsCollection::findFirst(array(array('id' => $id)));
         if ($brandCollection != false) {
             $brandCollection->id = $brand->id;
             $brandCollection->companyId = $brand->companyId;
             $brandCollection->name = $brand->name;
             $brandCollection->alias = $brand->alias;
             $brandCollection->logo = $brand->logo;
             $brandCollection->link = $brand->link;
             $brandCollection->description = $brand->description;
             $brandCollection->language = $brand->language;
             $brandCollection->revenueValue = $brand->revenueValue;
             $brandCollection->active = $brand->active;
             $brandCollection->created = $brand->created;
             $brandCollection->modified = $brand->modified;
             $brandCollection->createdBy = $brand->createdBy;
             $brandCollection->modifiedBy = $brand->modifiedBy;
             $brandCollection->save();
             $results['id'] = $brand->id;
         }
     }
     return $results;
 }
 /**
  * @api {post} /products/import/ POST /products/import/
  * @apiExample Example usage:
  * curl -i -X POST "http://apibeta.compargo.com/v1/products/import/?language=en&countryCode=ph
  *      -H "X-COMPARE-REST-API-KEY: 1234567890"
  *      -d "channelId=a1d14206-1ea9-11e4-b32d-eff91066cccf&
  *          data=[{"areaName": "NCR Luzon, Visayas and Mindanao",
  *            	   "brandName": "Bank of Commerce",
  *            	   "companyName": "Bank of Commerce",
  *            	   "productName": "Visa Classic",
  *            	   "productImage": "bank-of-commerce-visa-classic.jpg",
  *            	   "linkInformation": "http://www.bankcom.com.ph/percc.php#vc",
  *            	   "linkApplication": "http://www.bankcom.com.ph/img/ccaf.pdf",
  *            	   "featured": 0,
  *            	   "hasApplyButton": 0,
  *            	   "overlayOrClickthrough": 0,
  *            	   "phoneNumber": 0,
  *            	   "status": 1,
  *            	   "providerCard": 1,
  *            	   "premiumCard": 0,
  *            	   "octopusCard": 0,
  *            	   "islamicCard": 0,
  *            	   "businessCard": 0,
  *            	   "studentCard": 0,
  *            	   "travelCard": 0,
  *            	   "shoppingCard": 0,
  *            	   "specialtyCard": 0,
  *            	   "onlineShoppingCard": 0,
  *            	   "coBranded": 0,
  *            	   "greatFor1": "Get a separate credit limit for your local and foreign transactions",
  *            	   "greatFor2": "Get up to 30% of your credit limit as cash advance",
  *            	   "greatFor3": "Purchase new appliances, gadgets or other high-ticket items by installment",
  *            	   "bewareThat1": 0,
  *            	   "bewareThat2": 0,
  *            	   "bewareThat3": 0,
  *            	   "promoPicture": 0,
  *            	   "promoTitle": 0,
  *            	   "promoApplyPicture": 0,
  *            	   "promoApplyContent": 0,
  *            	   "ribbonBest": 0,
  *            	   "rewardConversion": "P50 spent = 1 point",
  *            	   "rewardConversionCondition": "Only principal cardholders can redeem reward points",
  *            	   "rewardMultiplier": "No points multiplier",
  *            	   "rewardMultiplierCondition": 0,
  *            	   "rewardSpendingShopping": "SM P500 Gift Card (3,711 points)",
  *            	   "rewardSpendingEntertainment": "SM Cinema Tickets for Two (3,149 points)",
  *            	   "rewardSpendingDining": 0,
  *            	   "rewardSpendingOther": 0,
  *            	   "earningPointsOctopus": 0,
  *            	   "earningPointsAutomaticTransaction": 0,
  *            	   "earningPointsOnlineBillPayments": 0,
  *            	   "earningPointsOnlineShopping": 0,
  *            	   "earningPointsInstallment": 0,
  *            	   "cashbackDining": 0,
  *            	   "cashbackDiningCondition": 0,
  *            	   "cashbackShopping": 0,
  *            	   "cashbackShoppingCondition": 0,
  *            	   "cashbackGroceries": 0,
  *            	   "cashbackGroceriesCondition": 0,
  *            	   "cashbackEntertainment": 0,
  *            	   "cashbackEntertainmentCondition": 0,
  *            	   "cashbackPetrol": 0,
  *            	   "cashbackPetrolCondition": 0,
  *            	   "cashbackLocalRetail": 0,
  *            	   "cashbackLocalRetailCondition": 0,
  *            	   "cashbackOverseasSpending": 0,
  *            	   "cashbackOverseasCondition": 0,
  *            	   "cashbackGeneral": 0,
  *            	   "cashbackGeneralCondition": 0,
  *            	   "cashbackOther": 0,
  *            	   "cashbackOtherCondition": 0,
  *            	   "cashbackMetaCondition": 0,
  *            	   "cbeLocalRetails": 0,
  *            	   "cbeLocalRetailsCondition": 0,
  *            	   "cbeLocalDining": 0,
  *            	   "cbeLocalDiningCondition": 0,
  *            	   "cbeOverseasTransaction": 0,
  *            	   "cbeOverseasTransactionCondition": 0,
  *            	   "cbeOnlineShopping": 0,
  *            	   "cbeOnlineShoppingCondition": 0,
  *            	   "cbeOnlineBillPayment": 0,
  *            	   "cbeOnlineBillPaymentCondition": 0,
  *            	   "cbeOctopusAavs": 0,
  *            	   "cbeOctopusAavsCondition": 0,
  *            	   "cbeAutomaticTransaction": 0,
  *            	   "cbeAutomaticTransactionCondition": 0,
  *            	   "cbeInstalment": 0,
  *            	   "cbeInstalmentConditi": 0,
  *            	   "cbeSpecialCondition": 0,
  *            	   "cbsAllNewTransactions": 0,
  *            	   "cbsCashCoupons": 0,
  *            	   "cbsAutopusAavs": 0,
  *            	   "cbsPetrol": 0,
  *            	   "cbsDining": 0,
  *            	   "cbsShopping": 0,
  *            	   "cbsEntertainment": 0,
  *            	   "cbsTravel": 0,
  *            	   "cbsSpecialCondition": 0,
  *            	   "discountsDining": 0,
  *            	   "discountsDiningCondition": 0,
  *            	   "discountsShopping": 0,
  *            	   "discountsShoppingCondition": 0,
  *            	   "discountsGroceries": 0,
  *            	   "discountsGroceriesCondition": 0,
  *            	   "discountsEntertainment": 0,
  *            	   "discountsEntertainmentCondition": 0,
  *            	   "discountsPetrol": 0,
  *            	   "discountsPetrolCondition": 0,
  *            	   "discountsOther": 0,
  *            	   "discountsOtherCondition": 0,
  *            	   "discountsMetaCondition": 0,
  *            	   "milesConversionLocal": 0,
  *            	   "milesConversionConditionLocal": 0,
  *            	   "milesConversionOverseas": 0,
  *            	   "milesConversionConditionOverseas": 0,
  *            	   "milesPogram": 0,
  *            	   "insurance": 0,
  *            	   "airportLounge": 0,
  *            	   "travelOther": 0,
  *            	   "fraudProtection": 0,
  *            	   "installmentPlan": 0,
  *            	   "personalAssistant": 0,
  *            	   "parking": 0,
  *            	   "creditLimit": 0,
  *            	   "purchaseInterest": 3.5,
  *            	   "purchaseApr": 0,
  *            	   "cashAdvanceInterest": 0,
  *            	   "cashAdvanceApr": 0,
  *            	   "interestFreePeriod": 21,
  *            	   "delinquencyRetailPurchaseApr": 0,
  *            	   "delinquencyCashAdvanceApr": 0,
  *            	   "annualFee": 1500,
  *            	   "annualFeePromo": 0,
  *            	   "annualFeeWaiver": "Request for a reversal by calling Customer Care and spend P5,000 within the specified period",
  *            	   "foreignTransactionFee": "2% of the converted amount",
  *            	   "annualFeeSupplementary": "P750",
  *            	   "annualFeeSupplementaryCondition": 0,
  *            	   "cashAdvanceFee": "5% of the amount withdrawn ",
  *            	   "cashAdvanceFee2": 0,
  *            	   "cardReplacementFee": "P300",
  *            	   "annualFeeAfterFirst": 1500,
  *            	   "partlyWaivedCondition": 0,
  *            	   "minimumRepayment": "5% of the amount due",
  *            	   "minimumRepayment2": "P500, whichever is higher",
  *            	   "latePayment": "2% of the overdue amount",
  *            	   "latePayment2": 0,
  *            	   "creditOverLimit": 0,
  *            	   "balanceTransferLowest": 0.88,
  *            	   "balanceTransferMonth": 18,
  *            	   "balanceTransferLongest": 0,
  *            	   "balanceTransferHighlight": 0,
  *            	   "balanceTransferAware": 0,
  *            	   "monthlyIncomeLocals": 10000,
  *            	   "monthlyIncomeForeigners": 0,
  *            	   "minimumEmploymentSalaried": 0,
  *            	   "minimumEmploymentSelfEmployed": 0,
  *            	   "minimumAge": 21,
  *            	   "minimumAgeSupplementary": 18,
  *            	   "existingCardHolder": 1,
  *            	   "nationality": 1,
  *            	   "residence": 0,
  *            	   "other1": 0,
  *            	   "other2": 0,
  *            	   "minimumAnnualIncome": 10000,
  *            	   "maxAge": 65,
  *            	   "language": "en"
  *               }]"
  * 
  *      
  * @apiDescription Bulk import of Products
  * @apiName        Import
  * @apiGroup       Products
  *
  * @apiHeader  {String} X-COMPARE-REST-API-KEY   Products unique access-key.
  *
  * @apiParam   {String} language                 Mandatory Language.
  * @apiParam   {String} countryCode              Mandatory Country Code.
  * @apiParam   {String} id                       Mandatory Product Unique ID.
  * @apiParam   {String} channelId                Mandatory ChannelId of the Product.
  * @apiParam   {String} date                     Mandatory Data of the Product.
  *
  * @apiSuccess {String} id                       The ID of Product.
  *
  * @apiSuccessExample Success-Response:
  *     HTTP/1.1 200 OK
  *     {
  *       "id": "96b3d052-3716-11e4-b18a-fe7344fb1ea4"
  *     } 
  *     
  * @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 ProductNotFound The id of the Product was not found.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 Not Found
  *     {
  *       "error": "ProductNotFound"
  *     }
  *     
  * @apiError RouteNotFound That route was not found on the server.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 
  *     {
  *       "error": "RouteNotFound"
  *     }    
  */
 public function bulkImport()
 {
     $results = array();
     $request = $this->di->get('request');
     $data = $request->getPost();
     $processCompany = $processBrand = $processChannel = $processArea = 1;
     if (!empty($data)) {
         $product = new Products();
         $productId = $product->id;
         $metaData = new Memory();
         $columns = $metaData->getColumnMap($product);
         $channelId = isset($data['channelId']) ? $data['channelId'] : '';
         $upload = isset($data['data']) ? $data['data'] : '';
         $channel = Channels::findFirstById($channelId);
         if (!$channel) {
             throw new HTTPException("Not found", 404, array('dev' => 'The requested channel does not exist.', 'internalCode' => 'P1000', 'more' => ''));
         }
         if (isJson($upload) && !empty($channelId)) {
             $productData = $brandData = $areaData = $companyData = array();
             $companyParams = $brandParams = $channelParams = $productParams = array();
             $channelAlias = $channel->alias;
             $errors = '';
             $upload = json_decode($upload, true);
             if (!empty($upload)) {
                 for ($i = 0; $i < count($upload); $i++) {
                     $productInformation = $upload[$i];
                     $company = Companies::findFirstByName($productInformation['companyName']);
                     if ($company) {
                         $companyId = $company->id;
                         $processCompany = 0;
                     } else {
                         $company = new Companies();
                         $schemaFile = $this->schemaDir . $this->getDI()->getConfig()->application->jsonSchema->companies;
                         $output = readJsonFromFile($schemaFile, $this);
                         if (!empty($output)) {
                             $properties = $output['properties'];
                             foreach ($properties as $key => $value) {
                                 switch ($key) {
                                     case 'options':
                                         continue 2;
                                         break;
                                     case 'name':
                                         $companyParams[$key] = $productInformation['companyName'];
                                         break;
                                     default:
                                         $companyParams[$key] = !empty($productInformation[$key]) ? $productInformation[$key] : '';
                                         break;
                                 }
                             }
                         }
                         $valid = $company->validateProperty($companyParams, $schemaFile);
                         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' => ''));
                         }
                         $companyParams['id'] = $company->id;
                         $companyParams['createdBy'] = isset($productInformation['createdBy']) ? $productInformation['createdBy'] : '';
                         $companyParams['modifiedBy'] = isset($productInformation['modifiedBy']) ? $productInformation['modifiedBy'] : '';
                         $processCompany = 1;
                     }
                     $brandName = !empty($productInformation['brandName']) ? $productInformation['brandName'] : $productInformation['companyName'];
                     if (isset($brandName)) {
                         $brand = Brands::findFirstByName($brandName);
                         if ($brand) {
                             $brandId = $brand->id;
                             $processBrand = 0;
                         } else {
                             $brand = new Brands();
                             $schemaFile = $this->schemaDir . $this->getDI()->getConfig()->application->jsonSchema->brands;
                             $output = readJsonFromFile($schemaFile, $this);
                             if (!empty($output)) {
                                 $properties = $output['properties'];
                                 foreach ($properties as $key => $value) {
                                     switch ($key) {
                                         case 'options':
                                             continue 2;
                                             break;
                                         case 'name':
                                             $brandParams['name'] = $brandName;
                                             break;
                                         default:
                                             $brandParams[$key] = !empty($productInformation[$key]) ? $productInformation[$key] : '';
                                             break;
                                     }
                                 }
                             }
                             $valid = $brand->validateProperty($brandParams, $schemaFile);
                             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' => ''));
                             }
                             $brandParams['id'] = $brand->id;
                             $brandParams['createdBy'] = isset($productInformation['createdBy']) ? $productInformation['createdBy'] : '';
                             $brandParams['modifiedBy'] = isset($productInformation['modifiedBy']) ? $productInformation['modifiedBy'] : '';
                             $processBrand = 1;
                         }
                     }
                     $areaName = $productInformation['areaName'];
                     if (isset($areaName)) {
                         $area = Areas::findFirstByName($areaName);
                         if ($area) {
                             $areaId = $area->id;
                         } else {
                             $area = new Areas();
                             $schemaFile = $this->schemaDir . $this->getDI()->getConfig()->application->jsonSchema->areas;
                             $output = readJsonFromFile($schemaFile, $this);
                             if (!empty($output)) {
                                 $properties = $output['properties'];
                                 foreach ($properties as $key => $value) {
                                     switch ($key) {
                                         case 'options':
                                             continue 2;
                                             break;
                                         case 'name':
                                             $areaParams['name'] = $areaName;
                                             break;
                                         default:
                                             $areaParams[$key] = !empty($productInformation[$key]) ? $productInformation[$key] : '';
                                             break;
                                     }
                                 }
                             }
                             $valid = $area->validateProperty($areaParams, $schemaFile);
                             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' => ''));
                             }
                             $areaParams['id'] = $area->id;
                             $areaParams['name'] = $areaName;
                             $areaParams['language'] = $productInformation['language'];
                             $areaParams['status'] = $productInformation['status'];
                             if (isset($areaParams['status'])) {
                                 $areaParams['active'] = $productInformation['status'] != Areas::ACTIVE ? 0 : 1;
                             }
                             $areaParams['createdBy'] = isset($productInformation['createdBy']) ? $productInformation['createdBy'] : '';
                             $areaParams['modifiedBy'] = isset($productInformation['modifiedBy']) ? $productInformation['modifiedBy'] : '';
                             $processArea = 1;
                         }
                     }
                     $productName = !empty($productInformation['productName']) ? $productInformation['productName'] : '';
                     if (isset($productName)) {
                         $product = Products::findFirstByName($productName);
                         if ($product) {
                             $productId = $product->id;
                             $processChannel = 0;
                         } else {
                             $params['productName'] = $productInformation['productName'];
                             $params['featured'] = isset($productInformation['featured']) ? $productInformation['featured'] : 0;
                             $params['productImage'] = isset($productInformation['productImage']) ? $productInformation['productImage'] : '';
                             $product = new Products();
                             $schemaFile = $this->schemaDir . $this->getDI()->getConfig()->application->jsonSchema->{$channelAlias};
                             $output = readJsonFromFile($schemaFile, $this);
                             if (!empty($output)) {
                                 $properties = $output['properties'];
                                 foreach ($properties as $key => $value) {
                                     switch ($key) {
                                         case 'options':
                                             continue 2;
                                             break;
                                         case 'name':
                                             $productData['name'] = $productInformation['productName'];
                                             break;
                                         case 'id':
                                             $productData['id'] = $product->id;
                                             break;
                                         default:
                                             $productData[$key] = !empty($productInformation[$key]) ? $productInformation[$key] : '';
                                             break;
                                     }
                                 }
                             }
                             $valid = $product->validateProperty($productData, $schemaFile);
                             if (!empty($valid)) {
                                 $errors .= implode(", ", $valid);
                                 throw new HTTPException("Request unable to be followed due to semantic errors.", 422, array('dev' => $errors, 'internalCode' => 'P1000', 'more' => ''));
                             }
                             $productData['id'] = $product->id;
                             $productData['channelId'] = $channelId;
                             $productData['name'] = $productInformation['productName'];
                             $productData['alias'] = '';
                             $productData['language'] = $productInformation['language'];
                             $productData['featured'] = $productInformation['featured'];
                             $productData['icon'] = $productInformation['productImage'];
                             $productData['status'] = $productInformation['status'];
                             if (isset($productData['status'])) {
                                 $productData['active'] = $productInformation['status'] != Products::ACTIVE ? 0 : 1;
                             }
                             $productData['createdBy'] = isset($productInformation['createdBy']) ? $productInformation['createdBy'] : '';
                             $productData['modifiedBy'] = isset($productInformation['modifiedBy']) ? $productInformation['modifiedBy'] : '';
                             $processChannel = 1;
                         }
                         // only save the records when all data is valid
                         if ($processCompany) {
                             $companyParams['id'] = $company->id;
                             if ($company->create($companyParams)) {
                                 $companyId = $company->id;
                             } else {
                                 throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $company->getMessages(), 'internalCode' => 'P1000', 'more' => ''));
                             }
                         }
                         if ($processBrand) {
                             $brandParams['companyId'] = $company->id;
                             if ($brand->create($brandParams)) {
                                 $brandId = $brand->id;
                             } else {
                                 throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $brand->getMessages(), 'internalCode' => 'P1000', 'more' => ''));
                             }
                         }
                         if ($processArea) {
                             if ($area->create($areaParams)) {
                                 $areaId = $area->id;
                             } else {
                                 throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $area->getMessages(), 'internalCode' => 'P1000', 'more' => ''));
                             }
                         }
                         if ($processChannel) {
                             $productData['id'] = $product->id;
                             $productData['brandId'] = $brandId;
                             if ($product->create($productData)) {
                                 $productId = $product->id;
                             } else {
                                 throw new HTTPException("Request unable to be followed due to semantic errors", 422, array('dev' => $product->getMessages(), 'internalCode' => 'P1000', 'more' => ''));
                             }
                         }
                         unset($productInformation['productName']);
                         unset($productInformation['areaName']);
                         unset($productInformation['brandName']);
                         unset($productInformation['companyName']);
                         unset($productInformation['language']);
                         unset($productInformation['featured']);
                         unset($productInformation['productImage']);
                         $this->_addProductsOptions($productId, $areaId, $channelAlias, $productInformation);
                     }
                 }
                 return $results;
             } else {
                 throw new HTTPException('Could not return results in specified format', 403, array('dev' => 'Could not understand type specified by type parameter in query string.', 'internalCode' => 'NF1000', 'more' => 'Type may not be implemented.'));
             }
         } else {
             throw new HTTPException('Could not return results in specified format', 403, array('dev' => 'Could not understand type specified by type parameter in query string.', 'internalCode' => 'NF1000', 'more' => 'Type may not be implemented.'));
         }
     } else {
         throw new HTTPException("The request cannot be fulfilled due to bad syntax.", 400, array('dev' => 'A required field is missing.', 'internalCode' => 'P1000', 'more' => ''));
     }
 }
 /**
  * @api {put} /brands/upload/:id PUT /brands/upload/:id
  * @apiExample Example usage:
  * curl --upload-file /home/moneymax/brands/bpi-family.png "http://apibeta.compargo.com/v1/brands/upload/0d6b45a6-3eea-11e4-9a7a-90a27a7c008a/?language=en&countryCode=ph
  *      -H "X-COMPARE-REST-API-KEY: 1234567890"
  *
  * @apiDescription Upload a Brand Image
  * @apiName        Delete
  * @apiGroup       Brands
  *
  * @apiHeader  {String} X-COMPARE-REST-API-KEY   Brands unique access-key.
  *
  * @apiParam   {String} language                 Mandatory Language.
  * @apiParam   {String} countryCode              Mandatory Country Code.
  * @apiParam   {String} id                       Mandatory Company Unique ID.
  * @apiParam   {String} file                     Mandatory File.
  *
  * @apiSuccess {String} id                       The ID of Brand.
  *
  * @apiSuccessExample Success-Response:
  *     HTTP/1.1 200 OK
  *     {
  *       "id": "0d6b45a6-3eea-11e4-9a7a-90a27a7c008a"
  *     }
  *
  * @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 BrandNotFound The id of the Brand was not found.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404 Not Found
  *     {
  *       "error": "BrandNotFound"
  *     }
  *
  * @apiError RouteNotFound That route was not found on the server.
  *
  * @apiErrorExample Error-Response:
  *     HTTP/1.1 404
  *     {
  *       "error": "RouteNotFound"
  *     }
  */
 public function upload($id)
 {
     $parameters = $results = array();
     $requestBody = file_get_contents("php://input");
     $request = $this->di->get('request');
     $language = $request->get()['language'];
     if (!empty($requestBody)) {
         $brand = Brands::findFirstById($id);
         if ($brand) {
             $width = isset($request->get()['width']) ? $request->get()['width'] : '';
             $height = isset($request->get()['height']) ? $request->get()['height'] : '';
             $quality = isset($request->get()['quality']) ? $request->get()['quality'] : 85;
             $parameters = array('prefix' => $id . '_', 'basename' => $brand->name, 'uploadpath' => $language . '/brands/', 'max_width' => $width, 'max_height' => $height, 'quality' => $quality);
             $image = new ImageUpload();
             if ($filename = $image->save($requestBody, $parameters)) {
                 $brand->logo = $filename;
                 if ($brand->save()) {
                     $brandCollection = BrandsCollection::findFirst(array('condition' => array('id' => $id)));
                     $brandCollection->logo = $filename;
                     if ($brandCollection->save()) {
                         $results[] = $brandCollection;
                     }
                 }
             }
         }
     } 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;
 }