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.

317 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. if(isset($productFamilyRequest['taxRate'])) {
  119. $taxRateId = intval($productFamilyRequest['taxRate']);
  120. if ($taxRateId > 0) {
  121. $repo = $this->em->getRepository(TaxRateInterface::class);
  122. $entity->setTaxRate($repo->find($taxRateId));
  123. }
  124. }
  125. $unitId = intval($productFamilyRequest['unit']);
  126. if ($unitId > 0) {
  127. $repo = $this->em->getRepository(UnitInterface::class);
  128. $entity->setUnit($repo->find($unitId));
  129. }
  130. $this->processCategories($entity);
  131. $this->processProducts($entity);
  132. parent::updateEntity($entity);
  133. }
  134. public function persistEntity($entity)
  135. {
  136. $this->processCategories($entity);
  137. $this->processProducts($entity);
  138. parent::persistEntity($entity);
  139. }
  140. protected function processProducts($entity)
  141. {
  142. $repo = $this->em->getRepository(UnitInterface::class);
  143. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  144. if (count($entity->getProducts()) == 0) {
  145. $product = new Product();
  146. $product->setProductFamily($entity);
  147. $this->em->persist($product);
  148. $entity->addProduct($product);
  149. } else {
  150. foreach ($entity->getProducts() as $i=>$product) {
  151. $product->setProductFamily($entity);
  152. $this->em->persist($product);
  153. $entity->addProduct($product);
  154. // die('ncici');
  155. }
  156. }
  157. }
  158. protected function processCategories(ProductFamilyInterface $entity)
  159. {
  160. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  161. $productCategories = $entity->getProductCategories();
  162. $entity->initProductCategories();
  163. foreach ($productCategories as $key => $bool) {
  164. if (is_bool($bool) && $bool) {
  165. if (strpos($key, 'category_children_') !== false) {
  166. $idCategory = (int)str_replace('category_children_', '', $key);
  167. } else {
  168. $idCategory = (int)str_replace('category_', '', $key);
  169. }
  170. $category = $productCategoryRepository->find($idCategory);
  171. $entity->addProductCategory($category);
  172. }
  173. }
  174. }
  175. protected function editAction()
  176. {
  177. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  178. $id = $this->request->query->get('id');
  179. $easyadmin = $this->request->attributes->get('easyadmin');
  180. $entity = $easyadmin['item'];
  181. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  182. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  183. $fieldsMetadata = $this->entity['list']['fields'];
  184. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  185. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  186. }
  187. $this->updateEntityProperty($entity, $property, $newValue);
  188. // cast to integer instead of string to avoid sending empty responses for 'false'
  189. return new Response((int)$newValue);
  190. }
  191. $fields = $this->entity['edit']['fields'];
  192. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  193. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  194. $sortableProductsField = array();
  195. foreach ($editForm->get('products')->getData() as $k => $product) {
  196. $sortableProductsField[$product->getPosition()] = $k;
  197. }
  198. ksort($sortableProductsField);
  199. $editForm->handleRequest($this->request);
  200. if ($editForm->isSubmitted() && $editForm->isValid()) {
  201. $this->processUploadedFiles($editForm);
  202. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  203. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  204. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  205. return $this->redirectToReferrer();
  206. }
  207. $this->dispatch(EasyAdminEvents::POST_EDIT);
  208. $parameters = [
  209. 'form' => $editForm->createView(),
  210. 'entity_fields' => $fields,
  211. 'entity' => $entity,
  212. 'delete_form' => $deleteForm->createView(),
  213. 'sortableProductsField' => $sortableProductsField
  214. ];
  215. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  216. }
  217. protected function newAction()
  218. {
  219. $this->dispatch(EasyAdminEvents::PRE_NEW);
  220. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  221. $easyadmin = $this->request->attributes->get('easyadmin');
  222. $easyadmin['item'] = $entity;
  223. $this->request->attributes->set('easyadmin', $easyadmin);
  224. $fields = $this->entity['new']['fields'];
  225. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  226. $newForm->handleRequest($this->request);
  227. if ($newForm->isSubmitted() && $newForm->isValid()) {
  228. $this->processUploadedFiles($newForm);
  229. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  230. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  231. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  232. return $this->redirectToReferrer();
  233. }
  234. $this->dispatch(EasyAdminEvents::POST_NEW, [
  235. 'entity_fields' => $fields,
  236. 'form' => $newForm,
  237. 'entity' => $entity
  238. ]);
  239. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  240. $categories = $productCategoryRepository->findAllParents();
  241. $parameters = [
  242. 'form' => $newForm->createView(),
  243. 'entity_fields' => $fields,
  244. 'entity' => $entity,
  245. 'categories' => $categories,
  246. 'sortableProductsField' => array()
  247. ];
  248. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  249. }
  250. }