You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
11KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\Product;
  4. use Doctrine\ORM\EntityRepository;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  6. use Lc\ShopBundle\Context\ProductCategoryInterface;
  7. use Lc\ShopBundle\Context\ProductFamilyInterface;
  8. use Lc\ShopBundle\Context\ProductInterface;
  9. use Lc\ShopBundle\Context\TaxRateInterface;
  10. use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
  11. use Lc\ShopBundle\Form\ProductType;
  12. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  13. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  14. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  15. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  16. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  17. use Symfony\Component\HttpFoundation\Response;
  18. class ProductFamilyController extends AdminController
  19. {
  20. private $taxRateClass;
  21. private $choicesTaxRateParam;
  22. public function createEntityFormBuilder($entity, $view)
  23. {
  24. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  25. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  26. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  27. //$formBuilder = $this->get('form.factory')->createBuilder(ProductType::class, $entity);*/
  28. $formBuilder->add('price', MoneyType::class, array(
  29. 'block_prefix' => 'lc_tax_price',
  30. 'attr' => array(
  31. 'class' => 'lc-price'
  32. )
  33. ));
  34. $formBuilder->add('priceWithTax', MoneyType::class, array(
  35. 'attr' => array(
  36. 'class' => 'lc-price-with-tax'
  37. ),
  38. 'required' => false,
  39. 'mapped' => false
  40. ));
  41. //CHOICE qui permet de sélectionner une taxe
  42. //ça c'est du lourd fais très attention faudra refactorer
  43. $this->getUser()->getMerchant()->getTaxRate();
  44. $choicesTaxRate['TVA valeur par défaut'] = 0;
  45. foreach ($this->em->getRepository($this->taxRateClass->name)->findAll() as $tax) {
  46. $choicesTaxRate[$tax->getTitle()] = $tax->getId();
  47. $this->choicesTaxRateParam[$tax->getId()] = $tax->getValue();
  48. }
  49. //là mon ami je kiffe symfo !!!!!
  50. $this->choicesTaxRateParam[0] = $this->getUser()->getMerchant()->getTaxRate()->getValue();
  51. $formBuilder->add('taxRate', ChoiceType::class, array(
  52. 'choices' => $choicesTaxRate,
  53. 'mapped' => false,
  54. 'choice_attr' => function ($choice, $key, $value) {
  55. return ['data-tax-rate-value' => $this->choicesTaxRateParam[$choice]];
  56. },
  57. ));
  58. $formBuilder->add('unit', ChoiceType::class, array(
  59. 'choices' => array(
  60. 'pièce' => 'piece',
  61. 'g' => 'g',
  62. 'kg' => 'kg',
  63. 'ml' => 'mL',
  64. 'l' => 'L'
  65. ),
  66. ));
  67. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  68. 'choices' => array(
  69. 'Gèrer le stock par déclinaison' => 'by-product',
  70. 'Gèrer le stock par produit' => 'by-product-family'
  71. ),
  72. 'multiple' => false,
  73. 'expanded' => true
  74. ));
  75. $formBuilder->add('productCategories', ProductFamilyCategoriesType::class) ;
  76. $formBuilder->add('products', CollectionType::class, array(
  77. 'label' => 'Catégories',
  78. 'entry_type' => ProductType::class,
  79. 'allow_add' => true,
  80. 'prototype' => true
  81. )
  82. );
  83. return $formBuilder;
  84. }
  85. public function updateEntity($entity)
  86. {
  87. $prodductFamilyRequest = $this->request->request->get('productfamily');
  88. if ($taxRateId = intval($prodductFamilyRequest['taxRate']) > 0) {
  89. $repo = $this->em->getRepository(TaxRateInterface::class);
  90. $entity->setTaxRate($repo->find($taxRateId));
  91. }
  92. $this->processCategories($entity) ;
  93. $this->processProducts($entity) ;
  94. $this->setUpdated($entity);
  95. parent::updateEntity($entity);
  96. }
  97. public function persistEntity($entity)
  98. {
  99. $this->processCategories($entity) ;
  100. $this->processProducts($entity) ;
  101. parent::persistEntity($entity) ;
  102. }
  103. protected function processProducts($entity)
  104. {
  105. if(count($entity->getProducts()) == 0) {
  106. $product = new Product();
  107. $product->setTitle($entity->getTitle()) ;
  108. $product->setCreatedBy($this->getUser()) ;
  109. $product->setUpdatedBy($this->getUser()) ;
  110. $this->em->persist($product);
  111. $entity->addProduct($product) ;
  112. }
  113. }
  114. protected function processCategories(ProductFamilyInterface $entity)
  115. {
  116. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class) ;
  117. $productCategories = $entity->getProductCategories() ;
  118. $entity->initProductCategories() ;
  119. foreach($productCategories as $key => $bool) {
  120. if(is_bool($bool) && $bool) {
  121. if(strpos($key, 'category_children_') !== false) {
  122. $idCategory = (int) str_replace('category_children_', '', $key) ;
  123. }
  124. else {
  125. $idCategory = (int) str_replace('category_', '', $key) ;
  126. }
  127. $category = $productCategoryRepository->find($idCategory) ;
  128. $entity->addProductCategory($category) ;
  129. }
  130. }
  131. }
  132. protected function editAction()
  133. {
  134. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  135. $id = $this->request->query->get('id');
  136. $easyadmin = $this->request->attributes->get('easyadmin');
  137. $entity = $easyadmin['item'];
  138. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  139. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  140. $fieldsMetadata = $this->entity['list']['fields'];
  141. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  142. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  143. }
  144. $this->updateEntityProperty($entity, $property, $newValue);
  145. // cast to integer instead of string to avoid sending empty responses for 'false'
  146. return new Response((int) $newValue);
  147. }
  148. $fields = $this->entity['edit']['fields'];
  149. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  150. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  151. $editForm->handleRequest($this->request);
  152. if ($editForm->isSubmitted() && $editForm->isValid()) {
  153. $this->processUploadedFiles($editForm);
  154. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  155. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  156. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  157. return $this->redirectToReferrer();
  158. }
  159. $this->dispatch(EasyAdminEvents::POST_EDIT);
  160. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  161. $categories = $productCategoryRepository->findAllParents();
  162. $parameters = [
  163. 'form' => $editForm->createView(),
  164. 'entity_fields' => $fields,
  165. 'entity' => $entity,
  166. 'delete_form' => $deleteForm->createView(),
  167. 'categories' => $categories
  168. ];
  169. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  170. }
  171. protected function newAction()
  172. {
  173. $this->dispatch(EasyAdminEvents::PRE_NEW);
  174. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  175. $easyadmin = $this->request->attributes->get('easyadmin');
  176. $easyadmin['item'] = $entity;
  177. $this->request->attributes->set('easyadmin', $easyadmin);
  178. $fields = $this->entity['new']['fields'];
  179. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  180. $newForm->handleRequest($this->request);
  181. if ($newForm->isSubmitted() && $newForm->isValid()) {
  182. $this->processUploadedFiles($newForm);
  183. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  184. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  185. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  186. return $this->redirectToReferrer();
  187. }
  188. $this->dispatch(EasyAdminEvents::POST_NEW, [
  189. 'entity_fields' => $fields,
  190. 'form' => $newForm,
  191. 'entity' => $entity,
  192. ]);
  193. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  194. $categories = $productCategoryRepository->findAllParents();
  195. $parameters = [
  196. 'form' => $newForm->createView(),
  197. 'entity_fields' => $fields,
  198. 'entity' => $entity,
  199. 'categories' => $categories,
  200. ];
  201. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  202. }
  203. }