示例#1
0
 private function save(Product $product)
 {
     ClassLoader::import('application.model.presentation.CategoryPresentation');
     $validator = $this->buildValidator($product);
     if ($validator->isValid()) {
         $product->loadRequestData($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();
             }
         }
         // $product->loadRequestData($this->request);
         // $product->save();
         if ($this->isQuickEdit == false) {
             BackendToolbarItem::registerLastViewedProduct($product);
         }
         $response = $this->productForm($product);
         $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')));
     }
 }
 protected function processRecord(Product $product)
 {
     $act = $this->getAction();
     $field = $this->getField();
     if ('manufacturer' == $act) {
         $product->manufacturer->set($this->params['manufacturer']);
     } else {
         if ('price' == $act) {
             $product->setPrice($this->params['baseCurrency'], $this->params['price']);
         } else {
             if (in_array($act, array('inc_price', 'multi_price', 'div_price'))) {
                 $actions = array('inc_price' => 'increasePriceByPercent', 'multi_price' => 'multiplyPrice', 'div_price' => 'dividePrice');
                 $action = $actions[$act];
                 $pricing = $product->getPricingHandler();
                 foreach ($this->params['currencies'] as $currency) {
                     if ($pricing->isPriceSet($currency)) {
                         $p = $pricing->getPrice($currency);
                         $p->{$action}($this->params['inc_price_value'], $this->params['inc_quant_price']);
                         $p->save();
                     }
                 }
             } else {
                 if ('inc_stock' == $act) {
                     $product->stockCount->set($product->stockCount->get() + $this->request->get($act));
                 } else {
                     if ('addRelated' == $act) {
                         $product->addRelatedProduct($this->params['relatedProduct']);
                     } else {
                         if ('copy' == $act) {
                             $cloned = clone $product;
                             $cloned->category->set($this->params['category']);
                             $cloned->save();
                         } else {
                             if ('addCat' == $act) {
                                 // check if the product is not assigned to this category already
                                 $relation = ActiveRecordModel::getInstanceByIdIfExists('ProductCategory', array('productID' => $product->getID(), 'categoryID' => $this->params['category']->getID()));
                                 if (!$relation->isExistingRecord() && $product->category->get() !== $category) {
                                     $relation->save();
                                 }
                             } else {
                                 if ('theme' == $act) {
                                     $instance = CategoryPresentation::getInstance($product);
                                     $instance->theme->set($this->params['theme']);
                                     $instance->save();
                                 } else {
                                     if ('shippingClass' == $act) {
                                         $product->shippingClass->set(ActiveRecordModel::getInstanceByIDIfExists('ShippingClass', $this->params['shippingClass'], false));
                                     } else {
                                         if ('taxClass' == $act) {
                                             $product->taxClass->set(ActiveRecordModel::getInstanceByIDIfExists('TaxClass', $this->params['taxClass'], false));
                                         } else {
                                             if (substr($act, 0, 13) == 'set_specField') {
                                                 $this->params['request']->remove('manufacturer');
                                                 $product->loadRequestData($this->params['request']);
                                             } else {
                                                 if (substr($act, 0, 16) == 'remove_specField') {
                                                     $this->params['request']->remove('manufacturer');
                                                     if ($this->params['field']->isMultiValue->get()) {
                                                         // remove only selected multi-select options
                                                         $product->loadRequestData($this->params['request']);
                                                     } else {
                                                         $product->removeAttribute($this->params['field']);
                                                     }
                                                 } else {
                                                     parent::processRecord($product);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
示例#3
0
 /**
  * Updates a category record
  *
  * @role !category.update
  *
  * @return ActionRedirectResponse
  */
 public function update()
 {
     ClassLoader::import('application.model.presentation.CategoryPresentation');
     $categoryNode = Category::getInstanceByID($this->request->get("id"), Category::LOAD_DATA);
     $validator = $this->buildValidator($categoryNode);
     if ($validator->isValid()) {
         $categoryNode->loadRequestData($this->request);
         $categoryNode->save();
         // presentation
         $instance = CategoryPresentation::getInstance($categoryNode);
         $instance->loadRequestData($this->request);
         $instance->save();
         return new JSONResponse($categoryNode->toFlatArray(), 'success', $this->translate('_category_succsessfully_saved'));
     }
 }
示例#4
0
 protected function processRecord(Product $product)
 {
     $act = $this->getAction();
     $field = $this->getField();
     if ('manufacturer' == $act) {
         $product->manufacturer->set($this->params['manufacturer']);
     } else {
         if ('price' == $act) {
             $product->setPrice($this->params['baseCurrency'], $this->params['price']);
         } else {
             if ('inc_price' == $act) {
                 $pricing = $product->getPricingHandler();
                 foreach ($this->params['currencies'] as $currency) {
                     if ($pricing->isPriceSet($currency)) {
                         $p = $pricing->getPrice($currency);
                         $p->increasePriceByPercent($this->params['inc_price_value'], $this->params['inc_quant_price']);
                         $p->save();
                     }
                 }
             } else {
                 if ('inc_stock' == $act) {
                     $product->stockCount->set($product->stockCount->get() + $this->request->get($act));
                 } else {
                     if ('addRelated' == $act) {
                         $product->addRelatedProduct($this->params['relatedProduct']);
                     } else {
                         if ('copy' == $act) {
                             $cloned = clone $product;
                             $cloned->category->set($this->params['category']);
                             $cloned->save();
                         } else {
                             if ('addCat' == $act) {
                                 // check if the product is not assigned to this category already
                                 $relation = ActiveRecordModel::getInstanceByIdIfExists('ProductCategory', array('productID' => $product->getID(), 'categoryID' => $this->params['category']->getID()));
                                 if (!$relation->isExistingRecord() && $product->category->get() !== $category) {
                                     $relation->save();
                                 }
                             } else {
                                 if ('theme' == $act) {
                                     $instance = CategoryPresentation::getInstance($product);
                                     $instance->theme->set($this->params['theme']);
                                     $instance->save();
                                 } else {
                                     if ('shippingClass' == $act) {
                                         $product->shippingClass->set(ActiveRecordModel::getInstanceByIDIfExists('ShippingClass', $this->params['shippingClass'], false));
                                     } else {
                                         if ('taxClass' == $act) {
                                             $product->taxClass->set(ActiveRecordModel::getInstanceByIDIfExists('TaxClass', $this->params['taxClass'], false));
                                         } else {
                                             parent::processRecord($product);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }