|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
-
- namespace Lc\ShopBundle\Controller\Backend;
-
- use App\Entity\Product;
- use Doctrine\ORM\EntityManagerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use FOS\UserBundle\Model\UserManagerInterface;
- use Lc\ShopBundle\Context\ImageInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ProductInterface;
- use Lc\ShopBundle\Context\ReductionCatalogInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Form\Backend\Common\AbstractEditPositionType;
- use Lc\ShopBundle\Form\Backend\Common\ReductionCatalogType;
- use Lc\ShopBundle\Form\Backend\ProductFamily\ProductType;
- use Lc\ShopBundle\Model\ProductFamily;
- use Lc\ShopBundle\Services\UtilsManager;
- use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\Extension\Core\Type\HiddenType;
- use Symfony\Component\Form\Extension\Core\Type\NumberType;
- use Symfony\Component\Form\FormError;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Contracts\Translation\TranslatorInterface;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
-
- class ProductFamilyController extends AdminController
- {
- private $taxRateClass;
- private $choicesTaxRateParam;
- private $choicesSupplierTaxRateParam;
- private $parameterBag ;
- private $productFamilyUtils ;
-
- public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator, SessionInterface $session, ParameterBagInterface $parameterBag)
- {
- parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator, $session);
- $this->parameterBag = $parameterBag ;
- $this->productFamilyUtils = $utilsManager->getProductFamilyUtils() ;
- }
-
- public function createEntityFormBuilder($entity, $view, $override = true)
- {
-
- $formBuilder = parent::createEntityFormBuilder($entity, $view, false);
-
- $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
- $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
-
- //$formBuilder->add('productCategories', ProductFamilyCategoriesType::class);
-
-
- $reductionCatalogRepo = $this->em->getRepository(ReductionCatalogInterface::class);
-
- $reductionCatalogClass = $this->em->getClassMetadata(ReductionCatalogInterface::class);
- $reductionCatalog = $reductionCatalogRepo->findOneBy(array('status' => false, 'productFamily' => $entity));
-
- if ($reductionCatalog == null) $reductionCatalog = new $reductionCatalogClass->name;
- $formBuilder->add('reductionCatalog', ReductionCatalogType::class, array(
- 'mapped' => false,
- 'data' => $reductionCatalog
- ));
-
- $formBuilder->add('stayOnPage', HiddenType::class, array(
- 'required' => false,
- 'mapped' => false,
- ));
- $formBuilder->add('warningMessageType', ChoiceType::class, array(
- 'choices' => array(
- 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_SUCCESS => ProductFamily::WARNING_MESSAGE_TYPE_SUCCESS,
- 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_ERROR => ProductFamily::WARNING_MESSAGE_TYPE_ERROR,
- 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_WARNING => ProductFamily::WARNING_MESSAGE_TYPE_WARNING,
- 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_INFO => ProductFamily::WARNING_MESSAGE_TYPE_INFO
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => false,
- 'required' => false
- ));
-
- $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
- 'empty_data' => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
- 'choices' => array(
- 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE,
- 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
- 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT,
- 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED => ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => true
- ));
-
- $formBuilder->add('behaviorAddToCart', ChoiceType::class, array(
- 'data' => $entity->getBehaviorAddToCart() ? $entity->getBehaviorAddToCart() : 'simple',
- 'choices' => array(
- 'field.ProductFamily.behaviorAddToCartOptions.' . ProductFamily::BEHAVIOR_ADD_TO_CART_SIMPLE => ProductFamily::BEHAVIOR_ADD_TO_CART_SIMPLE,
- 'field.ProductFamily.behaviorAddToCartOptions.' . ProductFamily::BEHAVIOR_ADD_TO_CART_MULTIPLE => ProductFamily::BEHAVIOR_ADD_TO_CART_MULTIPLE
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => true
- ));
-
- $formBuilder->add('behaviorPrice', ChoiceType::class, array(
- 'empty_data' => 'by-piece',
- 'choices' => array(
- 'field.ProductFamily.behaviorPriceOptions.' . ProductFamily::BEHAVIOR_PRICE_BY_PIECE => ProductFamily::BEHAVIOR_PRICE_BY_PIECE,
- 'field.ProductFamily.behaviorPriceOptions.' . ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT => ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => true
- ));
-
- $formBuilder->add('multiplyingFactor', NumberType::class, array(
- 'mapped' => false,
- 'data' => floatval($this->merchantUtils->getMerchantConfig('multiplying-factor')),
- ));
-
- $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
- 'choices' => array(
- 'field.ProductFamily.organicLabelOptions.' . ProductFamily::PROPERTY_ORGANIC_LABEL_AB => ProductFamily::PROPERTY_ORGANIC_LABEL_AB,
- 'field.ProductFamily.organicLabelOptions.' . ProductFamily::PROPERTY_ORGANIC_LABEL_NP => ProductFamily::PROPERTY_ORGANIC_LABEL_NP,
- 'field.ProductFamily.organicLabelOptions.' . ProductFamily::PROPERTY_ORGANIC_LABEL_HVE => ProductFamily::PROPERTY_ORGANIC_LABEL_HVE
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => false,
- 'required' => false
- ));
-
- $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
- 'choices' => array(
- 'field.default.' . ProductFamily::TYPE_EXPIRATION_DATE_DLC => ProductFamily::TYPE_EXPIRATION_DATE_DLC,
- 'field.default.' . ProductFamily::TYPE_EXPIRATION_DATE_DDM => ProductFamily::TYPE_EXPIRATION_DATE_DDM,
- 'field.default.' . ProductFamily::TYPE_EXPIRATION_DATE_DLUO => ProductFamily::TYPE_EXPIRATION_DATE_DLUO
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => true,
- 'required' => false
- ));
-
- $formBuilder->add('behaviorStockWeek', ChoiceType::class, array(
- 'choices' => array(
- 'field.ProductFamily.behaviorStockWeekOptions.' . ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE => ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE,
- 'field.ProductFamily.behaviorStockWeekOptions.' . ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE_VALIDATION => ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE_VALIDATION,
- 'field.ProductFamily.behaviorStockWeekOptions.' . ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE => ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE
- ),
- 'translation_domain' => 'lcshop',
- 'multiple' => false,
- 'expanded' => true,
- 'required' => true
- ));
-
- $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
- 'choices' => array(
- 'field.ProductFamily.behaviorExpirationDateOptions.' . ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY => ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
- 'field.ProductFamily.behaviorExpirationDateOptions.' . ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT => ProductFamily::BEHAVIOR_EXPIRATION_DATE_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
- )
- );
-
- $formBuilder = $this->overrideFormBuilder($formBuilder, $entity, $view);
-
- return $formBuilder;
- }
-
- public function updateProductFamilyEntity($entity, $editForm = false)
- {
- if ($editForm) {
- $entity = $this->productFamilyUtils->processBeforePersistProductFamily($entity, $editForm);
- }
-
- parent::updateEntity($entity);
-
- $this->orderUtils->updatePriceByProductFamily($entity);
- }
-
- public function persistProductFamilyEntity($entity, $newForm)
- {
- $entity = $this->productFamilyUtils->processBeforePersistProductFamily($entity, $newForm);
-
- $this->em->persist($entity);
- $this->em->flush();
- }
-
- 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);
- $this->utils->addFlash('success', 'success.common.fieldChange');
-
- $response['flashMessages'] = $this->utils->getFlashMessages();
- return new Response(json_encode($response));
- }
-
- $fields = $this->entity['edit']['fields'];
-
- $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
- $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
-
- $sortableProductsField = array();
-
- foreach ($editForm->get('products')->getData() as $k => $product) {
- if($product->getOriginProduct() == false) {
- $sortableProductsField[$product->getPosition()] = $k;
- }else{
- $sortableProductsField[-1] = $k;
- }
-
-
- }
- ksort($sortableProductsField);
-
- $editForm->handleRequest($this->request);
-
- if ($editForm->isSubmitted() && count($entity->getProductCategories()) == 0) {
- $editForm->get('productCategories')->addError(new FormError('Vous devez choisir au moins une catégorie'));
- }
- if ($editForm->isSubmitted() && $editForm->isValid() && count($entity->getProductCategories()) > 0) {
-
- $this->processUploadedFiles($editForm);
-
- $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
- $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
- $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
-
- $this->utils->addFlash('success', 'Produit sauvegardé');
-
- if ($editForm->get('stayOnPage')->getData() != "false" || $this->request->get('submitAndStay') !== null) {
- $refererUrl = $this->request->query->get('referer', '');
- return $this->redirectToRoute('easyadmin', ['entity' => 'ProductFamily', 'action' => 'edit', 'id' => $id, 'referer' => $refererUrl, 'ajax' => $this->request->query->get('ajax')]);
- } else {
- return $this->redirectToReferrer();
- }
-
- }
-
- $this->dispatch(EasyAdminEvents::POST_EDIT);
-
- $parameters = [
- 'form' => $editForm->createView(),
- 'entity_fields' => $fields,
- 'entity' => $entity,
- 'delete_form' => $deleteForm->createView(),
- 'sortableProductsField' => $sortableProductsField,
- 'totalProductOrdered' => $this->orderUtils->getTotalProductOrderedLastWeeks($entity)
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
- }
-
- protected function newAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_NEW);
-
- $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $easyadmin['item'] = $entity;
- $this->request->attributes->set('easyadmin', $easyadmin);
-
- $fields = $this->entity['new']['fields'];
-
- $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
-
- $newForm->handleRequest($this->request);
-
-
- if ($newForm->isSubmitted() && array_search(true, $newForm->get('productCategories')->getData()->toArray()) === false) {
- $newForm->get('productCategories')->addError(new FormError('Vous devez choisir au moins une catégorie'));
- }
-
- if ($newForm->isSubmitted() && $newForm->isValid() && array_search(true, $newForm->get('productCategories')->getData()->toArray()) !== false) {
- $this->processUploadedFiles($newForm);
-
- $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
- $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
- $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
-
- if ($newForm->get('stayOnPage')->getData() != "false" || $this->request->get('submitAndStay') !== null) {
- return $this->redirectToRoute('easyadmin', ['entity' => 'ProductFamily', 'action' => 'edit', 'id' => $entity->getId()]);
- } else {
- 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(),
- 'totalProductOrdered' => array('total'=>0)
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
-
-
- //hack utilisé pour filter sur les catégories lors du tri des produits par sous cat
- //A améliorer à l'occas
- protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
- {
- $productCategoryId = false;
-
- if (isset($dqlFilter['productCategoryId'])) $productCategoryId = $dqlFilter['productCategoryId'];
- if(isset($dqlFilter['filter']))$dqlFilter = $dqlFilter['filter'];
-
- $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
-
- $queryBuilder->leftJoin('entity.productCategories', 'product_categories');
-
- if ($productCategoryId) {
- $queryBuilder->andWhere('product_categories.id = :cat');
- $queryBuilder->setParameter('cat', $productCategoryId);
- }
-
- return $queryBuilder;
- }
-
- public function sortByProductCategoryAction(){
- $this->dispatch(EasyAdminEvents::PRE_LIST);
- $entity = null;
-
- //Replace this with query builder function, do not use finAll of easyAdmin
- if ($this->request->query->get('productCategoryId')) {
- if(!is_array($this->entity['list']['dql_filter'])) {
- $this->entity['list']['dql_filter'] = array('filter' => $this->entity['list']['dql_filter']);
- }
- $this->entity['list']['dql_filter']['productCategoryId'] = $this->request->query->get('productCategoryId');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $entity = $easyadmin['item'];
- }else{
- throw new \ErrorException('Action impossible') ;
- }
-
- if ($this->entity['list']['dql_filter']['filter']) $this->entity['list']['dql_filter']['filter'] .= sprintf(' AND entity.status = 1');
- else $this->entity['list']['dql_filter']['filter'] .= sprintf(' entity.status = 1');
-
- $fields = $this->entity['list']['fields'];
-
- $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), 500, 'position', 'asc', $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'];
-
- }
-
- //die();
- //to do récupérer les élements hors ligne et incrémenter position
- /*foreach ($repo->findBy(array('status'=> false)) 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]);
- }
- }
-
|