Exemplo n.º 1
0
 /**
  * Build combinations from the combination output builder
  */
 public function buildCombinationsAction()
 {
     // Check current user authorization
     if (null !== ($response = $this->checkAuth($this->resourceCode, array(), AccessManager::UPDATE))) {
         return $response;
     }
     $changeForm = $this->createForm(AdminForm::PRODUCT_COMBINATION_GENERATION);
     try {
         // Check the form against constraints violations
         $form = $this->validateForm($changeForm, "POST");
         // Get the form field values
         $data = $form->getData();
         // Rework attributes_av array, to build an array which contains all combinations,
         // in the form combination[] = array of combination attributes av IDs
         //
         // First, create an array of attributes_av ID in the form $attributes_av_list[$attribute_id] = array of attributes_av ID
         // from the list of attribute_id:attributes_av ID from the form.
         $combinations = $attributes_av_list = $tmp = array();
         foreach ($data['attribute_av'] as $item) {
             list($attribute_id, $attribute_av_id) = explode(':', $item);
             if (!isset($attributes_av_list[$attribute_id])) {
                 $attributes_av_list[$attribute_id] = array();
             }
             $attributes_av_list[$attribute_id][] = $attribute_av_id;
         }
         // Next, recursively combine array
         $this->combine($attributes_av_list, $combinations, $tmp);
         // Create event
         $event = new ProductCombinationGenerationEvent($this->getExistingObject(), $data['currency'], $combinations);
         $event->setReference($data['reference'] == null ? '' : $data['reference'])->setPrice($data['price'] == null ? 0 : $data['price'])->setWeight($data['weight'] == null ? 0 : $data['weight'])->setQuantity($data['quantity'] == null ? 0 : $data['quantity'])->setSalePrice($data['sale_price'] == null ? 0 : $data['sale_price'])->setOnsale($data['onsale'] == null ? false : $data['onsale'])->setIsnew($data['isnew'] == null ? false : $data['isnew'])->setEanCode($data['ean_code'] == null ? '' : $data['ean_code']);
         $this->dispatch(TheliaEvents::PRODUCT_COMBINATION_GENERATION, $event);
         // Log object modification
         $this->adminLogAppend($this->resourceCode, AccessManager::CREATE, sprintf("Combination generation for product reference %s", $event->getProduct()->getRef()), $event->getProduct()->getId());
         // Redirect to the success URL
         return $this->generateSuccessRedirect($changeForm);
     } catch (FormValidationException $ex) {
         // Form cannot be validated
         $error_msg = $this->createStandardFormValidationErrorMessage($ex);
     } catch (\Exception $ex) {
         // Any other error
         $error_msg = $ex->getMessage();
     }
     $this->setupFormErrorContext($this->getTranslator()->trans("Combination builder"), $error_msg, $changeForm, $ex);
     // At this point, the form has errors, and should be redisplayed.
     return $this->renderEditionTemplate();
 }
Exemplo n.º 2
0
 /**
  * Generate combinations. All existing combinations for the product are deleted.
  *
  * @param  ProductCombinationGenerationEvent $event
  * @throws \Exception
  */
 public function generateCombinations(ProductCombinationGenerationEvent $event)
 {
     $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         // Delete all product's productSaleElement
         ProductSaleElementsQuery::create()->filterByProductId($event->product->getId())->delete();
         $isDefault = true;
         // Create all combinations
         foreach ($event->getCombinations() as $combinationAttributesAvIds) {
             // Create the PSE
             $saleElement = $event->getProduct()->createProductSaleElement($con, $event->getWeight(), $event->getPrice(), $event->getSalePrice(), $event->getCurrencyId(), $isDefault, $event->getOnsale(), $event->getIsnew(), $event->getQuantity(), $event->getEanCode(), $event->getReference());
             $isDefault = false;
             $this->createCombination($con, $saleElement, $combinationAttributesAvIds);
         }
         // Store all the stuff !
         $con->commit();
     } catch (\Exception $ex) {
         $con->rollback();
         throw $ex;
     }
 }