. */ /* */ /*************************************************************************************/ namespace Thelia\Form; use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\ExecutionContextInterface; use Thelia\Form\Exception\StockNotFoundException; use Thelia\Form\Exception\ProductNotFoundException; use Thelia\Model\ProductSaleElementsQuery; use Thelia\Model\ConfigQuery; use Thelia\Model\ProductQuery; class CartAdd extends BaseForm { /** * * in this function you add all the fields you need for your Form. * Form this you have to call add method on $this->formBuilder attribute : * * $this->formBuilder->add("name", "text") * ->add("email", "email", array( * "attr" => array( * "class" => "field" * ), * "label" => "email", * "constraints" => array( * new \Symfony\Component\Validator\Constraints\NotBlank() * ) * ) * ) * ->add('age', 'integer'); * * @return null */ protected function buildForm() { $this->formBuilder ->add("product", "text", array( "constraints" => array( new Constraints\NotBlank(), new Constraints\Callback(array("methods" => array( array($this, "checkProduct") ))) ) )) ->add("product_sale_elements_id", "text", array( "constraints" => array( new Constraints\Callback(array("methods" => array( array($this, "checkStockAvailability") ))) ) )) ->add("quantity", "text", array( "constraints" => array( new Constraints\NotBlank(), new Constraints\Callback(array("methods" => array( array($this, "checkStock") ))), new Constraints\GreaterThanOrEqual(array( "value" => 0 )) ) )) ->add("append", "hidden") ->add("newness", "hidden") ; } public function checkProduct($value, ExecutionContextInterface $context) { $product = ProductQuery::create()->findPk($value); if (is_null($product) || $product->getVisible() == 0) { throw new ProductNotFoundException(sprintf("this product id does not exists : %d", $value)); } } public function checkStockAvailability($value, ExecutionContextInterface $context) { if ($value) { $data = $context->getRoot()->getData(); $productSaleElements = ProductSaleElementsQuery::create() ->filterById($value) ->filterByProductId($data["product"]) ->count(); if ($productSaleElements == 0) { throw new StockNotFoundException(sprintf("This product_sale_elements_id does not exists for this product : %d", $value)); } } } public function checkStock($value, ExecutionContextInterface $context) { $data = $context->getRoot()->getData(); $productSaleElements = ProductSaleElementsQuery::create() ->filterById($data["product_sale_elements_id"]) ->filterByProductId($data["product"]) ->findOne(); if ($productSaleElements->getQuantity() < $value && ConfigQuery::read("verifyStock", 1) == 1) { $context->addViolation("quantity value is not valid"); } } /** * @return string the name of you form. This name must be unique */ public function getName() { return "thelia_cart_add"; } }