예제 #1
0
 public function getProductDefinition(CommentDefinitionEvent $event)
 {
     $config = $event->getConfig();
     $event->setRating(true);
     $product = ProductQuery::create()->findPk($event->getRefId());
     if (null === $product) {
         throw new InvalidDefinitionException($this->translator->trans("Product %id does not exist", ['%ref' => $event->getRef()], CommentModule::MESSAGE_DOMAIN));
     }
     // is comment is authorized on this product
     $commentProductActivated = MetaDataQuery::getVal(Comment::META_KEY_ACTIVATED, \Thelia\Model\MetaData::PRODUCT_KEY, $product->getId());
     // not defined, get the global config
     if ("1" !== $commentProductActivated) {
         if ("0" === $commentProductActivated || false === $config['activated']) {
             throw new InvalidDefinitionException($this->translator->trans("Comment not activated on this element.", ['%ref' => $event->getRef()], CommentModule::MESSAGE_DOMAIN));
         }
     }
     $verified = false;
     if (null !== $event->getCustomer()) {
         // customer has bought the product
         $productBoughtCount = OrderProductQuery::getSaleStats($product->getRef(), null, null, [2, 3, 4], $event->getCustomer()->getId());
         if ($config['only_verified']) {
             if (0 === $productBoughtCount) {
                 throw new InvalidDefinitionException($this->translator->trans("Only customers who have bought this product can publish comment", [], CommentModule::MESSAGE_DOMAIN), false);
             }
         }
         $verified = 0 !== $productBoughtCount;
     } else {
         $verified = false;
     }
     $event->setVerified($verified);
 }