Example #1
0
 private function save(Product $product)
 {
     ClassLoader::import('application.model.presentation.CategoryPresentation');
     $validator = $this->buildValidator(true);
     if ($validator->isModelValid()) {
         $product->loadRequestModel($this->request);
         foreach (array('ShippingClass' => 'shippingClassID', 'TaxClass' => 'taxClassID') as $class => $field) {
             $value = $this->request->get($field, null);
             $instance = $value ? ActiveRecordModel::getInstanceByID($class, $value) : null;
             $product->setFieldValue($field, $instance);
         }
         $product->save();
         // presentation
         $instance = CategoryPresentation::getInstance($product);
         $instance->loadRequestData($this->request);
         $instance->save();
         // save pricing
         $product->loadSpecification();
         $product->loadPricing();
         if ($quantities = $this->request->get('quantityPricing')) {
             foreach ($product->getRelatedRecordSet('ProductPrice', new ARSelectFilter()) as $price) {
                 $id = $price->currency->get()->getID();
                 $prices = array();
                 if (!empty($quantities[$id])) {
                     $values = json_decode($quantities[$id], true);
                     $prices = array();
                     // no group selected - set all customers
                     if ('' == $values['group'][0]) {
                         $values['group'][0] = 0;
                     }
                     $quantCount = count($values['quant']);
                     foreach ($values['group'] as $groupIndex => $group) {
                         foreach ($values['quant'] as $quantIndex => $quant) {
                             $pr = $values['price'][$groupIndex * $quantCount + $quantIndex];
                             if (strlen($pr) != 0) {
                                 $prices[$quant][$group] = (double) $pr;
                             }
                         }
                     }
                 }
                 ksort($prices);
                 $price->serializedRules->set(serialize($prices));
                 $price->save();
             }
         }
         // save product images
         $inputImages = $this->request->get('productImage');
         $tmpImages = array();
         if (is_array($inputImages)) {
             $dir = ClassLoader::getRealPath('public.upload.tmpimage.');
             foreach ($inputImages as $tmpImage) {
                 if (strlen(trim($tmpImage)) == 0 || strpos($tmpImage, '/')) {
                     continue;
                 }
                 if (file_exists($dir . $tmpImage)) {
                     $tmpImages[] = $dir . $tmpImage;
                     $productImage = ProductImage::getNewInstance($product);
                     $productImage->save();
                     $productImage->setFile($dir . $tmpImage);
                 }
             }
         }
         $response = $this->productForm(true);
         $response->setHeader('Cache-Control', 'no-cache, must-revalidate');
         $response->setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
         $response->setHeader('Content-type', 'text/javascript');
         return $response;
     } else {
         // reset validator data (as we won't need to restore the form)
         $validator->restore();
         return new JSONResponse(array('errors' => $validator->getErrorList(), 'failure', $this->translate('_could_not_save_product_information')));
     }
 }