Beispiel #1
0
 /**
  * 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);
 }
Beispiel #2
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);
 }