em->getClassMetadata(ProductCategoryInterface::class); $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class); $formBuilder->add('productCategories', ProductFamilyCategoriesType::class); //$formBuilder->add('reductionCatalog', ReductionCatalogType::class); /*$formBuilder->add('taxRate', EntityType::class, array( 'class' => $this->em->getClassMetadata(TaxRateInterface::class)->name, 'required' =>false, 'placeholder'=> 'Tva par défaut ('.$this->getUser()->getMerchant()->getTaxRate()->getValue().'%)' ));*/ $formBuilder->add('warningMessageType', ChoiceType::class, array( 'choices' => array( 'field.default.warningMessageTypeOptions.success' => 'success', 'field.default.warningMessageTypeOptions.error' => 'error', 'field.default.warningMessageTypeOptions.warning' => 'warning', 'field.default.warningMessageTypeOptions.info' => 'info' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'expanded' => false, 'required' => false )); $formBuilder->add('behaviorCountStock', ChoiceType::class, array( 'empty_data' => 'by-product-family', 'choices' => array( 'field.ProductFamily.behaviorCountStockOptions.byQuantity' => 'by-quantity', 'field.ProductFamily.behaviorCountStockOptions.byProductFamily' => 'by-product-family', 'field.ProductFamily.behaviorCountStockOptions.byProduct' => 'by-product' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'expanded' => true )); $formBuilder->add('behaviorAddToCart', ChoiceType::class, array( 'empty_data' => 'by-product-family', 'choices' => array( 'field.ProductFamily.behaviorAddToCartOptions.simple' => 'simple', 'field.ProductFamily.behaviorAddToCartOptions.multiple' => 'multiple' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'required'=>false, 'expanded' => true )); $formBuilder->add('behaviorBuyingPrice', ChoiceType::class, array( 'empty_data' => 'by-piece', 'choices' => array( 'field.ProductFamily.behaviorBuyingPriceOptions.byPiece' => 'by-piece', 'field.ProductFamily.behaviorBuyingPriceOptions.byRefUnit' => 'by-reference-unit' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'expanded' => true )); $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array( 'choices' => array( 'field.ProductFamily.organicLabelOptions.ab' => 'ab', 'field.ProductFamily.organicLabelOptions.natureProgres' => 'nature-progres', 'field.ProductFamily.organicLabelOptions.hVE' => 'hve' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'expanded' => false, 'required' => false )); $formBuilder->add('typeExpirationDate', ChoiceType::class, array( 'choices' => array( 'field.default.dlc' => 'dlc', 'field.default.ddm' => 'ddm', 'field.default.dluo' => 'dluo' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'expanded' => true, 'required'=>false )); $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array( 'choices' => array( 'field.ProductFamily.behaviorExpirationDateOptions.productFamily' => 'by-product-family', 'field.ProductFamily.behaviorExpirationDateOptions.product' => 'by-product' ), 'translation_domain' => 'lcshop', 'multiple' => false, 'expanded' => true, 'required'=>false )); $formBuilder->add('products', CollectionType::class, array( 'label' => 'Déclinaisons', 'entry_type' => ProductType::class, 'entry_options' => ['label' => false], 'allow_add' => true, 'allow_delete' => true, 'required' => true ) ); return $formBuilder; } public function updateEntity($entity) { $productFamilyRequest = $this->request->request->get('productfamily'); if(isset($productFamilyRequest['taxRate'])) { $taxRateId = intval($productFamilyRequest['taxRate']); if ($taxRateId > 0) { $repo = $this->em->getRepository(TaxRateInterface::class); $entity->setTaxRate($repo->find($taxRateId)); } } $unitId = intval($productFamilyRequest['unit']); if ($unitId > 0) { $repo = $this->em->getRepository(UnitInterface::class); $entity->setUnit($repo->find($unitId)); } //$reductionCatalogInfo = $productFamilyRequest['reductionCatalog']; $this->processCategories($entity); $this->processProducts($entity); /* dump($reductionCatalog); dump($productFamilyRequest);*/ parent::updateEntity($entity); } public function persistEntity($entity) { $this->processCategories($entity); $this->processProducts($entity); parent::persistEntity($entity); } protected function processProducts($entity) { $repo = $this->em->getRepository(UnitInterface::class); //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien if (count($entity->getProducts()) == 0) { $product = new Product(); $product->setProductFamily($entity); $this->em->persist($product); $entity->addProduct($product); } else { foreach ($entity->getProducts() as $i=>$product) { $product->setProductFamily($entity); $this->em->persist($product); $entity->addProduct($product); // die('ncici'); } } } protected function processCategories(ProductFamilyInterface $entity) { $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class); $productCategories = $entity->getProductCategories(); $entity->initProductCategories(); foreach ($productCategories as $key => $bool) { if (is_bool($bool) && $bool) { if (strpos($key, 'category_children_') !== false) { $idCategory = (int)str_replace('category_children_', '', $key); } else { $idCategory = (int)str_replace('category_', '', $key); } $category = $productCategoryRepository->find($idCategory); $entity->addProductCategory($category); } } } protected function editAction() { $this->dispatch(EasyAdminEvents::PRE_EDIT); $id = $this->request->query->get('id'); $easyadmin = $this->request->attributes->get('easyadmin'); $entity = $easyadmin['item']; if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) { $newValue = 'true' === mb_strtolower($this->request->query->get('newValue')); $fieldsMetadata = $this->entity['list']['fields']; if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) { throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property)); } $this->updateEntityProperty($entity, $property, $newValue); // cast to integer instead of string to avoid sending empty responses for 'false' return new Response((int)$newValue); } $fields = $this->entity['edit']['fields']; $editForm = $this->executeDynamicMethod('createEditForm', [$entity, $fields]); $deleteForm = $this->createDeleteForm($this->entity['name'], $id); $sortableProductsField = array(); foreach ($editForm->get('products')->getData() as $k => $product) { $sortableProductsField[$product->getPosition()] = $k; } ksort($sortableProductsField); $editForm->handleRequest($this->request); if ($editForm->isSubmitted() && $editForm->isValid()) { $this->processUploadedFiles($editForm); $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]); $this->executeDynamicMethod('updateEntity', [$entity, $editForm]); $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]); return $this->redirectToReferrer(); } $this->dispatch(EasyAdminEvents::POST_EDIT); $parameters = [ 'form' => $editForm->createView(), 'entity_fields' => $fields, 'entity' => $entity, 'delete_form' => $deleteForm->createView(), 'sortableProductsField' => $sortableProductsField ]; return $this->executeDynamicMethod('renderTemplate', ['edit', $this->entity['templates']['edit'], $parameters]); } protected function newAction() { $this->dispatch(EasyAdminEvents::PRE_NEW); $entity = $this->executeDynamicMethod('createNewEntity'); $easyadmin = $this->request->attributes->get('easyadmin'); $easyadmin['item'] = $entity; $this->request->attributes->set('easyadmin', $easyadmin); $fields = $this->entity['new']['fields']; $newForm = $this->executeDynamicMethod('createNewForm', [$entity, $fields]); $newForm->handleRequest($this->request); if ($newForm->isSubmitted() && $newForm->isValid()) { $this->processUploadedFiles($newForm); $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]); $this->executeDynamicMethod('persistEntity', [$entity, $newForm]); $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]); return $this->redirectToReferrer(); } $this->dispatch(EasyAdminEvents::POST_NEW, [ 'entity_fields' => $fields, 'form' => $newForm, 'entity' => $entity ]); $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class); $categories = $productCategoryRepository->findAllParents(); $parameters = [ 'form' => $newForm->createView(), 'entity_fields' => $fields, 'entity' => $entity, 'categories' => $categories, 'sortableProductsField' => array() ]; return $this->executeDynamicMethod('renderTemplate', ['new', $this->entity['templates']['new'], $parameters]); } }