예제 #1
0
파일: Product.php 프로젝트: savvagee/thelia
 /**
  * Update the value of a product feature.
  *
  * @param FeatureProductUpdateEvent $event
  */
 public function updateFeatureProductValue(FeatureProductUpdateEvent $event)
 {
     // Prepare the FeatureAv's ID
     $featureAvId = $event->getFeatureValue();
     // Search for existing FeatureProduct
     $featureProductQuery = FeatureProductQuery::create()->filterByProductId($event->getProductId())->filterByFeatureId($event->getFeatureId());
     // If it's not a free text value, we can filter by the event's featureValue (which is an ID)
     if ($event->getFeatureValue() !== null && $event->getIsTextValue() === false) {
         $featureProductQuery->filterByFeatureAvId($featureAvId);
     }
     $featureProduct = $featureProductQuery->findOne();
     // If the FeatureProduct does not exist, create it
     if ($featureProduct === null) {
         $featureProduct = new FeatureProduct();
         $featureProduct->setDispatcher($event->getDispatcher())->setProductId($event->getProductId())->setFeatureId($event->getFeatureId());
         // If it's a free text value, create a FeatureAv to handle i18n
         if ($event->getIsTextValue() === true) {
             $featureProduct->setFreeTextValue(true);
             $createFeatureAvEvent = new FeatureAvCreateEvent();
             $createFeatureAvEvent->setFeatureId($event->getFeatureId())->setLocale($event->getLocale())->setTitle($event->getFeatureValue());
             $event->getDispatcher()->dispatch(TheliaEvents::FEATURE_AV_CREATE, $createFeatureAvEvent);
             $featureAvId = $createFeatureAvEvent->getFeatureAv()->getId();
         }
     } elseif ($featureProduct !== null && $event->getIsTextValue() === true) {
         // Get the FeatureAv
         $freeTextFeatureAv = FeatureAvQuery::create()->filterByFeatureProduct($featureProduct)->findOneByFeatureId($event->getFeatureId());
         // Get the FeatureAvI18n by locale
         $freeTextFeatureAvI18n = FeatureAvI18nQuery::create()->filterById($freeTextFeatureAv->getId())->findOneByLocale($event->getLocale());
         // Nothing found for this lang and the new value is not empty : create FeatureAvI18n
         if ($freeTextFeatureAvI18n === null && !empty($featureAvId)) {
             $featureAvI18n = new FeatureAvI18n();
             $featureAvI18n->setId($freeTextFeatureAv->getId())->setLocale($event->getLocale())->setTitle($event->getFeatureValue())->save();
             $featureAvId = $featureAvI18n->getId();
         } elseif ($freeTextFeatureAvI18n !== null && empty($featureAvId)) {
             $freeTextFeatureAvI18n->delete();
             // Check if there are still some FeatureAvI18n for this FeatureAv
             $freeTextFeatureAvI18ns = FeatureAvI18nQuery::create()->findById($freeTextFeatureAv->getId());
             // If there are no more FeatureAvI18ns for this FeatureAv, remove the corresponding FeatureProduct & FeatureAv
             if (count($freeTextFeatureAvI18ns) == 0) {
                 $deleteFeatureProductEvent = new FeatureProductDeleteEvent($event->getProductId(), $event->getFeatureId());
                 $event->getDispatcher()->dispatch(TheliaEvents::PRODUCT_FEATURE_DELETE_VALUE, $deleteFeatureProductEvent);
                 $deleteFeatureAvEvent = new FeatureAvDeleteEvent($freeTextFeatureAv->getId());
                 $event->getDispatcher()->dispatch(TheliaEvents::FEATURE_AV_DELETE, $deleteFeatureAvEvent);
                 return;
             }
         } elseif ($freeTextFeatureAvI18n !== null && !empty($featureAvId)) {
             $freeTextFeatureAvI18n->setTitle($featureAvId);
             $freeTextFeatureAvI18n->save();
             $featureAvId = $freeTextFeatureAvI18n->getId();
         }
     } else {
         $featureAvId = $event->getFeatureValue();
     }
     $featureProduct->setFeatureAvId($featureAvId);
     $featureProduct->save();
     $event->setFeatureProduct($featureProduct);
 }
예제 #2
0
 /**
  * Update product attributes and features
  */
 public function updateAttributesAndFeaturesAction($productId)
 {
     $product = ProductQuery::create()->findPk($productId);
     if ($product != null) {
         $featureTemplate = FeatureTemplateQuery::create()->filterByTemplateId($product->getTemplateId())->find();
         if ($featureTemplate !== null) {
             // Get all features for the template attached to this product
             $allFeatures = FeatureQuery::create()->filterByFeatureTemplate($featureTemplate)->find();
             $updatedFeatures = array();
             // Update all features values, starting with feature av. values
             $featureValues = $this->getRequest()->get('feature_value', array());
             foreach ($featureValues as $featureId => $featureValueList) {
                 // Delete all features av. for this feature.
                 $event = new FeatureProductDeleteEvent($productId, $featureId);
                 $this->dispatch(TheliaEvents::PRODUCT_FEATURE_DELETE_VALUE, $event);
                 // Add then all selected values
                 foreach ($featureValueList as $featureValue) {
                     $event = new FeatureProductUpdateEvent($productId, $featureId, $featureValue);
                     $this->dispatch(TheliaEvents::PRODUCT_FEATURE_UPDATE_VALUE, $event);
                 }
                 $updatedFeatures[] = $featureId;
             }
             // Update then features text values
             $featureTextValues = $this->getRequest()->get('feature_text_value', array());
             foreach ($featureTextValues as $featureId => $featureValue) {
                 // Check if a FeatureProduct exists for this product and this feature (for another lang)
                 $freeTextFeatureProduct = FeatureProductQuery::create()->filterByProductId($productId)->filterByFreeTextValue(true)->findOneByFeatureId($featureId);
                 // If no corresponding FeatureProduct exists AND if the feature_text_value is empty, do nothing
                 if (is_null($freeTextFeatureProduct) && empty($featureValue)) {
                     continue;
                 }
                 $event = new FeatureProductUpdateEvent($productId, $featureId, $featureValue, true);
                 $event->setLocale($this->getCurrentEditionLocale());
                 $this->dispatch(TheliaEvents::PRODUCT_FEATURE_UPDATE_VALUE, $event);
                 $updatedFeatures[] = $featureId;
             }
             // Delete features which don't have any values
             /** @var Feature $feature */
             foreach ($allFeatures as $feature) {
                 if (!in_array($feature->getId(), $updatedFeatures)) {
                     $event = new FeatureProductDeleteEvent($productId, $feature->getId());
                     $this->dispatch(TheliaEvents::PRODUCT_FEATURE_DELETE_VALUE, $event);
                 }
             }
         }
     }
     // If we have to stay on the same page, do not redirect to the successUrl,
     // just redirect to the edit page again.
     if ($this->getRequest()->get('save_mode') == 'stay') {
         return $this->redirectToEditionTemplate($this->getRequest());
     }
     // Redirect to the category/product list
     return $this->redirectToListTemplate();
 }
예제 #3
0
 /**
  * Update the value of a product feature.
  *
  * @param FeatureProductUpdateEvent $event
  */
 public function updateFeatureProductValue(FeatureProductUpdateEvent $event)
 {
     // If the feature is not free text, it may have one ore more values.
     // If the value exists, we do not change it
     // If the value does not exists, we create it.
     //
     // If the feature is free text, it has only a single value.
     // Etiher create or update it.
     $featureProductQuery = FeatureProductQuery::create()->filterByFeatureId($event->getFeatureId())->filterByProductId($event->getProductId());
     if ($event->getIsTextValue() !== true) {
         $featureProductQuery->filterByFeatureAvId($event->getFeatureValue());
     }
     $featureProduct = $featureProductQuery->findOne();
     if ($featureProduct == null) {
         $featureProduct = new FeatureProduct();
         $featureProduct->setDispatcher($event->getDispatcher())->setProductId($event->getProductId())->setFeatureId($event->getFeatureId());
     }
     if ($event->getIsTextValue() == true) {
         $featureProduct->setFreeTextValue($event->getFeatureValue());
     } else {
         $featureProduct->setFeatureAvId($event->getFeatureValue());
     }
     $featureProduct->save();
     $event->setFeatureProduct($featureProduct);
 }