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.

320 lines
14KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\Product;
  4. use Doctrine\DBAL\Types\FloatType;
  5. use Doctrine\ORM\EntityRepository;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  7. use Lc\ShopBundle\Context\ProductCategoryInterface;
  8. use Lc\ShopBundle\Context\ProductFamilyInterface;
  9. use Lc\ShopBundle\Context\ProductInterface;
  10. use Lc\ShopBundle\Context\TaxRateInterface;
  11. use Lc\ShopBundle\Context\UnitInterface;
  12. use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
  13. use Lc\ShopBundle\Form\ProductType;
  14. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  18. use Symfony\Component\Form\Extension\Core\Type\DateType;
  19. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  20. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  21. use Symfony\Component\HttpFoundation\Response;
  22. class ProductFamilyController extends AdminController
  23. {
  24. private $taxRateClass;
  25. private $choicesTaxRateParam;
  26. private $choicesSupplierTaxRateParam;
  27. public function createEntityFormBuilder($entity, $view)
  28. {
  29. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  30. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  31. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  32. $formBuilder->add('productCategories', ProductFamilyCategoriesType::class);
  33. /*$formBuilder->add('taxRate', EntityType::class, array(
  34. 'class' => $this->em->getClassMetadata(TaxRateInterface::class)->name,
  35. 'required' =>false,
  36. 'placeholder'=> 'Tva par défaut ('.$this->getUser()->getMerchant()->getTaxRate()->getValue().'%)'
  37. ));*/
  38. $formBuilder->add('warningMessageType', ChoiceType::class, array(
  39. 'choices' => array(
  40. 'field.default.warningMessageTypeOptions.success' => 'success',
  41. 'field.default.warningMessageTypeOptions.error' => 'error',
  42. 'field.default.warningMessageTypeOptions.warning' => 'warning',
  43. 'field.default.warningMessageTypeOptions.info' => 'info'
  44. ),
  45. 'translation_domain' => 'lcshop',
  46. 'multiple' => false,
  47. 'expanded' => false,
  48. 'required' => false
  49. ));
  50. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  51. 'empty_data' => 'by-product-family',
  52. 'choices' => array(
  53. 'field.ProductFamily.behaviorCountStockOptions.byQuantity' => 'by-quantity',
  54. 'field.ProductFamily.behaviorCountStockOptions.byProductFamily' => 'by-product-family',
  55. 'field.ProductFamily.behaviorCountStockOptions.byProduct' => 'by-product'
  56. ),
  57. 'translation_domain' => 'lcshop',
  58. 'multiple' => false,
  59. 'expanded' => true
  60. ));
  61. $formBuilder->add('behaviorAddToCart', ChoiceType::class, array(
  62. 'empty_data' => 'by-product-family',
  63. 'choices' => array(
  64. 'field.ProductFamily.behaviorAddToCartOptions.simple' => 'simple',
  65. 'field.ProductFamily.behaviorAddToCartOptions.multiple' => 'multiple'
  66. ),
  67. 'translation_domain' => 'lcshop',
  68. 'multiple' => false,
  69. 'required'=>false,
  70. 'expanded' => true
  71. ));
  72. $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
  73. 'choices' => array(
  74. 'field.ProductFamily.organicLabelOptions.ab' => 'ab',
  75. 'field.ProductFamily.organicLabelOptions.natureProgres' => 'nature-progres',
  76. 'field.ProductFamily.organicLabelOptions.hVE' => 'hve'
  77. ),
  78. 'translation_domain' => 'lcshop',
  79. 'multiple' => false,
  80. 'expanded' => false,
  81. 'required' => false
  82. ));
  83. $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
  84. 'choices' => array(
  85. 'field.default.dlc' => 'dlc',
  86. 'field.default.ddm' => 'ddm',
  87. 'field.default.dluo' => 'dluo'
  88. ),
  89. 'translation_domain' => 'lcshop',
  90. 'multiple' => false,
  91. 'expanded' => true,
  92. 'required'=>false
  93. ));
  94. $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
  95. 'choices' => array(
  96. 'field.ProductFamily.behaviorExpirationDateOptions.productFamily' => 'by-product-family',
  97. 'field.ProductFamily.behaviorExpirationDateOptions.product' => 'by-product'
  98. ),
  99. 'translation_domain' => 'lcshop',
  100. 'multiple' => false,
  101. 'expanded' => true,
  102. 'required'=>false
  103. ));
  104. $formBuilder->add('products', CollectionType::class, array(
  105. 'label' => 'Déclinaisons',
  106. 'entry_type' => ProductType::class,
  107. 'entry_options' => ['label' => false],
  108. 'allow_add' => true,
  109. 'allow_delete' => true,
  110. 'required' => true
  111. )
  112. );
  113. return $formBuilder;
  114. }
  115. public function updateEntity($entity)
  116. {
  117. $productFamilyRequest = $this->request->request->get('productfamily');
  118. $taxRateId = intval($productFamilyRequest['taxRate']);
  119. if ($taxRateId > 0) {
  120. $repo = $this->em->getRepository(TaxRateInterface::class);
  121. $entity->setTaxRate($repo->find($taxRateId));
  122. }
  123. $unitId = intval($productFamilyRequest['unit']);
  124. if ($unitId > 0) {
  125. $repo = $this->em->getRepository(UnitInterface::class);
  126. $entity->setUnit($repo->find($unitId));
  127. }
  128. $this->processCategories($entity);
  129. $this->processProducts($entity);
  130. parent::updateEntity($entity);
  131. }
  132. public function persistEntity($entity)
  133. {
  134. $this->processCategories($entity);
  135. $this->processProducts($entity);
  136. parent::persistEntity($entity);
  137. }
  138. protected function processProducts($entity)
  139. {
  140. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  141. if (count($entity->getProducts()) == 0) {
  142. $product = new Product();
  143. $product->setCreatedBy($this->getUser());
  144. $product->setUpdatedBy($this->getUser());
  145. $product->setProductFamily($entity);
  146. $this->em->persist($product);
  147. $entity->addProduct($product);
  148. } else {
  149. foreach ($entity->getProducts() as $product) {
  150. $product->setProductFamily($entity);
  151. $product->setCreatedBy($this->getUser());
  152. $product->setUpdatedBy($this->getUser());
  153. $this->em->persist($product);
  154. $entity->addProduct($product);
  155. // die('ncici');
  156. }
  157. }
  158. }
  159. protected function processCategories(ProductFamilyInterface $entity)
  160. {
  161. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  162. $productCategories = $entity->getProductCategories();
  163. $entity->initProductCategories();
  164. foreach ($productCategories as $key => $bool) {
  165. if (is_bool($bool) && $bool) {
  166. if (strpos($key, 'category_children_') !== false) {
  167. $idCategory = (int)str_replace('category_children_', '', $key);
  168. } else {
  169. $idCategory = (int)str_replace('category_', '', $key);
  170. }
  171. $category = $productCategoryRepository->find($idCategory);
  172. $entity->addProductCategory($category);
  173. }
  174. }
  175. }
  176. protected function editAction()
  177. {
  178. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  179. $id = $this->request->query->get('id');
  180. $easyadmin = $this->request->attributes->get('easyadmin');
  181. $entity = $easyadmin['item'];
  182. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  183. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  184. $fieldsMetadata = $this->entity['list']['fields'];
  185. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  186. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  187. }
  188. $this->updateEntityProperty($entity, $property, $newValue);
  189. // cast to integer instead of string to avoid sending empty responses for 'false'
  190. return new Response((int)$newValue);
  191. }
  192. $fields = $this->entity['edit']['fields'];
  193. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  194. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  195. $sortableProductsField = array();
  196. foreach ($editForm->get('products')->getData() as $k => $product) {
  197. $sortableProductsField[$product->getPosition()] = $k;
  198. }
  199. ksort($sortableProductsField);
  200. $editForm->handleRequest($this->request);
  201. if ($editForm->isSubmitted() && $editForm->isValid()) {
  202. $this->processUploadedFiles($editForm);
  203. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  204. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  205. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  206. return $this->redirectToReferrer();
  207. }
  208. $this->dispatch(EasyAdminEvents::POST_EDIT);
  209. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  210. $categories = $productCategoryRepository->findAllParents();
  211. $parameters = [
  212. 'form' => $editForm->createView(),
  213. 'entity_fields' => $fields,
  214. 'entity' => $entity,
  215. 'delete_form' => $deleteForm->createView(),
  216. 'categories' => $categories,
  217. 'sortableProductsField' => $sortableProductsField
  218. ];
  219. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  220. }
  221. protected function newAction()
  222. {
  223. $this->dispatch(EasyAdminEvents::PRE_NEW);
  224. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  225. $easyadmin = $this->request->attributes->get('easyadmin');
  226. $easyadmin['item'] = $entity;
  227. $this->request->attributes->set('easyadmin', $easyadmin);
  228. $fields = $this->entity['new']['fields'];
  229. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  230. $newForm->handleRequest($this->request);
  231. if ($newForm->isSubmitted() && $newForm->isValid()) {
  232. $this->processUploadedFiles($newForm);
  233. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  234. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  235. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  236. return $this->redirectToReferrer();
  237. }
  238. $this->dispatch(EasyAdminEvents::POST_NEW, [
  239. 'entity_fields' => $fields,
  240. 'form' => $newForm,
  241. 'entity' => $entity
  242. ]);
  243. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  244. $categories = $productCategoryRepository->findAllParents();
  245. $parameters = [
  246. 'form' => $newForm->createView(),
  247. 'entity_fields' => $fields,
  248. 'entity' => $entity,
  249. 'categories' => $categories,
  250. 'sortableProductsField' => array()
  251. ];
  252. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  253. }
  254. }