|
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use App\Entity\ProductCategory;
- use Doctrine\DBAL\Types\TextType;
- use Doctrine\ORM\EntityRepository;
- use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use FOS\UserBundle\Model\UserManagerInterface;
- use Lc\ShopBundle\Context\MerchantInterface;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\SortableInterface;
- use Lc\ShopBundle\Context\StatusInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Context\TreeInterface;
- use Lc\ShopBundle\Form\AbstractEditPositionType;
- use Lc\ShopBundle\Form\ChoiceProductCategoryType;
- use Lc\ShopBundle\Form\PriceType;
- use Lc\ShopBundle\Form\ProductType;
- use Lc\ShopBundle\Form\Widget\PriceWidgetType;
- use Lc\ShopBundle\Repository\BaseRepository;
- use Lc\ShopBundle\Repository\ProductCategoryRepository;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\Extension\Core\Type\MoneyType;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Security\Core\Security;
-
- class AdminController extends EasyAdminController
- {
- protected $security;
- protected $userManager;
-
-
- public function __construct(Security $security, UserManagerInterface $userManager)
- {
- $this->security = $security;
- $this->userManager = $userManager;
-
- }
-
-
- protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
- {
- if ($pos = strpos($dqlFilter, 'currentMerchant')) {
- $dqlFilter = sprintf(str_replace('currentMerchant', $this->getUser()->getMerchant()->getId(), $dqlFilter));
- }
-
- if (new $entityClass instanceof StatusInterface && strpos($dqlFilter, 'entity.status') === false) {
- if ($dqlFilter) $dqlFilter .= sprintf(' AND entity.status > = 0');
- else $dqlFilter .= sprintf(' entity.status > = 0');
- }
-
- $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
-
- if ($entityClass == 'App\Entity\PointSale') {
- $queryBuilder->andWhere(':currentMerchant MEMBER OF entity.merchant')
- ->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
- }
-
- return $queryBuilder;
- }
-
-
- public function renderTemplate($actionName, $templatePath, array $parameters = [])
- {
- $parameters = array_merge(
- $parameters,
- $this->getTwigExtraParameters()
- );
-
- return parent::renderTemplate($actionName, $templatePath, $parameters);
- }
-
- public function getTwigExtraParameters()
- {
- $repositoryMerchant = $this->getDoctrine()->getRepository(MerchantInterface::class);
- $merchants = $repositoryMerchant->findAll();
-
- $user = $this->security->getUser();
-
- return [
- 'merchants' => $merchants,
- 'current_merchant' => ($user && $user->getMerchant()) ? $user->getMerchant() : null,
- ];
- }
-
- protected function removeEntity($entity)
- {
- $entity->setStatus(-1);
- $this->em->persist($entity);
- $this->em->flush();
- }
-
- public function updateEntity($entity)
- {
- $this->setUpdated($entity);
- parent::updateEntity($entity);
- }
-
-
- public function persistEntity($entity)
- {
-
- if ($entity instanceof StatusInterface) {
- $entity->setStatus(1);
- }
-
- if ($entity instanceof SortableInterface) {
- if ($entity instanceof TreeInterface) {
- if ($entity->getParent()) {
- $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
- $entity->setPosition($entity->getParent()->getId() . '_' . $total);
- } else {
- $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1, 'parent' => null));
- $entity->setPosition($total);
- }
- } else {
- $total = $this->em->getRepository($this->entity['class'])->count(array('status' => 1));
- $entity->setPosition($total);
- }
- }
- if (method_exists($entity, 'setMerchant')) {
- $entity->setMerchant($this->security->getUser()->getMerchant());
- }
-
- if (method_exists($entity, 'addMerchant')) {
- if ($entity->getMerchant()->isEmpty()) {
- $entity->addMerchant($this->security->getUser()->getMerchant());
- }
- }
-
-
- if (method_exists($entity, 'setCreatedBy')) {
- $entity->setCreatedBy($this->security->getUser());
- }
-
-
- if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
- $entity->getAddress()->setCreatedBy($this->security->getUser());
- $entity->getAddress()->setCreatedAt(new \DateTime());
- }
-
- if (method_exists($entity, 'getAddresses')
- && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
- foreach($entity->getAddresses() as $address) {
- $address->setCreatedBy($this->security->getUser()) ;
- $address->setCreatedAt(new \DateTime()) ;
- }
- }
-
- $this->setUpdated($entity);
-
-
- parent::persistEntity($entity);
- }
-
- public function updateProductFamilyEntity($entity)
- {
-
- $prodductFamilyRequest = $this->request->request->get('productfamily');
-
- if ($taxRateId = intval($prodductFamilyRequest['taxRate']) > 0) {
- $repo = $this->em->getRepository(TaxRateInterface::class);
- $entity->setTaxRate($repo->find($taxRateId));
- }
-
- $this->setUpdated($entity);
- parent::updateEntity($entity);
- }
-
- public function setUpdated($entity)
- {
-
- if (method_exists($entity, 'setUpdatedBy')) {
- $entity->setUpdatedBy($this->security->getUser());
- }
-
- if (method_exists($entity, 'getAddress') && $entity->getAddress()) {
- $entity->getAddress()->setUpdatedBy($this->security->getUser());
- }
-
- if (method_exists($entity, 'getAddresses')
- && $entity->getAddresses() && count($entity->getAddresses()) > 0) {
-
- foreach($entity->getAddresses() as $address) {
- $address->setUpdatedBy($this->security->getUser()) ;
- $address->setUpdatedAt(new \DateTime()) ;
- if(!$address->getCreatedBy()) {
- $address->setCreatedBy($this->security->getUser()) ;
- $address->setCreatedAt(new \DateTime()) ;
- }
- }
- }
- }
-
- public function listChildrenAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_LIST);
-
- $id = $this->request->query->get('id');
- $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $entity = $easyadmin['item'];
-
- $fields = $this->entity['list']['fields'];
- $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->entity['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);
-
- $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
-
- $parameters = [
- 'paginator' => $paginator,
- 'fields' => $fields,
- 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
- 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
- 'entity' => $entity,
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['list', $this->entity['templates']['list'], $parameters]);
- }
-
-
- public function sortAction()
- {
-
- $this->dispatch(EasyAdminEvents::PRE_LIST);
- $entity = null;
-
- //Replace this with query builder function, do not use finAll of easyAdmin
- if ($this->request->query->get('id')) {
- $id = $this->request->query->get('id');
- $this->entity['list']['dql_filter'] = "entity.parent = " . $id;
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $entity = $easyadmin['item'];
- }
-
- if ($this->entity['list']['dql_filter']) $this->entity['list']['dql_filter'] .= sprintf(' AND entity.status = 1');
- else $this->entity['list']['dql_filter'] .= sprintf(' entity.status = 1');
-
- $fields = $this->entity['list']['fields'];
- $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), 500, $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);
-
- $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]);
-
- $positionForm = $this->createFormBuilder(array('entities', $paginator->getCurrentPageResults()))
- ->add('entities', CollectionType::class, array(
- 'required' => true,
- 'allow_add' => true,
- 'entry_type' => AbstractEditPositionType::class,
- ))
- ->getForm();
-
- $positionForm->handleRequest($this->request);
-
- if ($positionForm->isSubmitted() && $positionForm->isValid()) {
- $class = $this->entity['class'];
- $repo = $this->em->getRepository($class);
-
- $latsPos = 0;
- foreach ($positionForm->get('entities')->getData() as $elm) {
- $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
- $entity = $repo->find($elm['id']);
- $entity->setPosition($elm['position']);
- $this->em->persist($entity);
- $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
- $latsPos = $elm['position'];
-
- }
- //to do récupérer les élements hors ligne et incrémenter position
- foreach ($repo->findAllOfflineAndDelete() as $offlineEntity) {
- $latsPos++;
- $offlineEntity->setPosition($latsPos);
- $this->em->persist($offlineEntity);
- }
- $this->em->flush();
-
- $this->addFlash('success', 'Position modifié', array(), 'mweb');
-
- return $this->redirectToReferrer();
-
- }
-
- $parameters = [
- 'paginator' => $paginator,
- 'fields' => $fields,
- 'batch_form' => $this->createBatchForm($this->entity['name'])->createView(),
- 'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
- 'postion_form' => $positionForm->createView(),
- 'entity' => $entity,
- 'sortable' => true
- ];
- return $this->executeDynamicMethod('render<EntityName>Template', ['sortable', "@LcShop/backend/default/sortable.html.twig", $parameters]);
- }
-
- private $taxRateClass;
- private $choicesTaxRateParam;
-
- public function createProductFamilyEntityFormBuilder($entity, $view)
- {
-
- $formBuilder = parent::createEntityFormBuilder($entity, $view);
-
-
- $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
- $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
-
- //$formBuilder = $this->get('form.factory')->createBuilder(ProductType::class, $entity);*/
- $formBuilder->add('price', MoneyType::class, array(
- 'block_prefix' => 'lc_tax_price',
- 'attr' => array(
- 'class' => 'lc-price'
- )
- ));
- $formBuilder->add('priceWithTax', MoneyType::class, array(
- 'attr' => array(
- 'class' => 'lc-price-with-tax'
- ),
- 'required' => false,
- 'mapped' => false
- ));
-
-
- //CHOICE qui permet de sélectionner une taxe
- //ça c'est du lourd fais très attention faudra refactorer
- $this->getUser()->getMerchant()->getTaxRate();
- $choicesTaxRate['TVA valeur par défaut'] = 0;
- foreach ($this->em->getRepository($this->taxRateClass->name)->findAll() as $tax) {
- $choicesTaxRate[$tax->getTitle()] = $tax->getId();
- $this->choicesTaxRateParam[$tax->getId()] = $tax->getValue();
- }
- //là mon ami je kiffe symfo !!!!!
- $this->choicesTaxRateParam[0] = $this->getUser()->getMerchant()->getTaxRate()->getValue();
-
-
- $formBuilder->add('taxRate', ChoiceType::class, array(
- 'choices' => $choicesTaxRate,
- 'mapped' => false,
- 'choice_attr' => function ($choice, $key, $value) {
- return ['data-tax-rate-value' => $this->choicesTaxRateParam[$choice]];
- },
-
- ));
-
- $formBuilder->add('unit', ChoiceType::class, array(
- 'choices' => array(
- 'pièce' => 'piece',
- 'g' => 'g',
- 'kg' => 'kg',
- 'ml' => 'mL',
- 'l' => 'L'
-
- ),
- ));
- $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
- 'choices' => array(
- 'Gèrer le stock par déclinaison' => 'by-product',
- 'Gèrer le stock par produit' => 'by-product-family'
- ),
- 'multiple' => false,
- 'expanded' => true
-
- ));
- $formBuilder->add('productCategories', EntityType::class, array(
- 'multiple' => true,
- 'expanded' => true,
- 'class' => $class->name,
- 'query_builder' => function (EntityRepository $repo) {
- return $repo->createQueryBuilder('e')
- ->where('e.parent is NOT NULL')
- ->andWhere('e.status > 0')
- ->orderBy('e.status', "DESC");
- }
- )
- );
-
- $formBuilder->add('products', CollectionType::class, array(
- 'entry_type'=> ProductType::class,
- 'allow_add'=>true,
- 'prototype'=> true
- )
- );
-
- return $formBuilder;
- }
-
-
- public function createEntityFormBuilder($entity, $view)
- {
-
- $formBuilder = parent::createEntityFormBuilder($entity, $view);
-
- $id = (null !== $entity->getId()) ? $entity->getId() : 0;
- if ($entity instanceof TreeInterface) {
- $formBuilder->add('parent', EntityType::class, array(
- 'class' => $this->entity['class'],
- 'query_builder' => function (EntityRepository $repo) use ($id) {
- return $repo->createQueryBuilder('e')
- ->where('e.parent is NULL')
- ->andWhere('e.status >= 0')
- ->orderBy('e.status', "DESC");
- },
- 'required' => false,
- )
- );
- }
- return $formBuilder;
- }
- }
|