Browse Source

Section resolver exepction

feature/ticket
Fab 3 years ago
parent
commit
a4a9d5c364
16 changed files with 302 additions and 47 deletions
  1. +10
    -0
      Controller/Dashboard/DashboardAdminController.php
  2. +37
    -0
      Controller/Merchant/SwitchMerchantController.php
  3. +39
    -0
      Controller/Section/SwitchSectionAdminController.php
  4. +33
    -7
      Form/Merchant/SwitchMerchantFormType.php
  5. +81
    -0
      Form/Section/SwitchSectionFormType.php
  6. +5
    -5
      Model/User/UserModel.php
  7. +1
    -1
      Repository/Section/SectionRepository.php
  8. +2
    -3
      Resolver/SectionResolver.php
  9. +2
    -0
      Resources/assets/app/adminlte/common/app.common.js
  10. +0
    -0
      Resources/assets/app/adminlte/common/common.scss
  11. +7
    -0
      Resources/assets/app/switchmerchant/app.switchmerchant.js
  12. +11
    -0
      Resources/assets/app/switchmerchant/switchmerchant.js
  13. +25
    -0
      Resources/assets/app/switchmerchant/switchmerchant_admin.scss
  14. +8
    -0
      Resources/config/routes.yaml
  15. +9
    -3
      Resources/views/adminlte/layout.html.twig
  16. +32
    -28
      Twig/TwigExtension.php

+ 10
- 0
Controller/Dashboard/DashboardAdminController.php View File



namespace Lc\CaracoleBundle\Controller\Dashboard; namespace Lc\CaracoleBundle\Controller\Dashboard;


use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use Lc\SovBundle\Controller\Dashboard\DashboardController as SovDashboardController ; use Lc\SovBundle\Controller\Dashboard\DashboardController as SovDashboardController ;


return $crud ; return $crud ;
} }


public function configureAssets(): Assets
{
$assets = parent::configureAssets(); // TODO: Change the autogenerated stub

$assets->addWebpackEncoreEntry('common-switch-merchant') ;

return $assets ;
}

} }

+ 37
- 0
Controller/Merchant/SwitchMerchantController.php View File

<?php

namespace Lc\CaracoleBundle\Controller\Merchant;

use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\SovBundle\Doctrine\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

class SwitchMerchantController extends AbstractController
{

public function switchMerchant(Request $request)
{
$form = $this->createForm(SwitchMerchantFormType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$merchant = $form->get('merchant')->getData();
$context = $form->get('context')->getData();

if ($merchant) {
$url = $merchant->getMerchantConfig('url');

if($context == 'admin') {
$url .= 'admin' ;
}

if ($url) {
return $this->redirect($url);
}
}
}
}
}

+ 39
- 0
Controller/Section/SwitchSectionAdminController.php View File

<?php

namespace Lc\CaracoleBundle\Controller\Section;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Form\Section\SwitchSectionFormType;
use Lc\CaracoleBundle\Repository\Section\SectionRepository;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class SwitchSectionAdminController extends AbstractController
{

public function switchSection(
Request $request,
EntityManagerInterface $em,
MerchantResolver $merchantResolver,
SectionRepository $sectionRepository
) {
$form = $this->createForm(SwitchSectionFormType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$idSection = $form->get('id_section')->getData();
$section = $sectionRepository->find($idSection);
$userMerchant = $merchantResolver->getUserMerchant();

if ($section && $userMerchant) {
$userMerchant->setCurrentAdminSection($section);
$em->update($section);
$em->flush();
}

$referer = $request->headers->get('referer');
return $this->redirect($referer);
}
}
}

+ 33
- 7
Form/Merchant/SwitchMerchantFormType.php View File

namespace Lc\CaracoleBundle\Form\Merchant; namespace Lc\CaracoleBundle\Form\Merchant;


use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\SovBundle\DataTransformer\FileManagerTypeToDataTransformer;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\SovBundle\Doctrine\EntityManager; use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Translation\TranslatorAdmin; use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;


class SwitchMerchantFormType extends AbstractType class SwitchMerchantFormType extends AbstractType
{ {
protected $em; protected $em;
protected $translatorAdmin; protected $translatorAdmin;
protected $merchantResolver;


public function __construct(EntityManager $em, TranslatorAdmin $translatorAdmin)
public function __construct(EntityManager $em, TranslatorAdmin $translatorAdmin, MerchantResolver $merchantResolver)
{ {
$this->em = $em; $this->em = $em;
$this->translatorAdmin = $translatorAdmin; $this->translatorAdmin = $translatorAdmin;
$this->merchantResolver = $merchantResolver;
} }


public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$entityName = $this->em->getEntityName(MerchantInterface::class);

$builder->add( $builder->add(
'merchants',
'merchant',
EntityType::class, EntityType::class,
[ [
'class' => $entityName,
'choice_label' => 'title'
'class' => $this->em->getEntityName(MerchantInterface::class),
'choice_label' => 'title',
'data' => $options['current_merchant']
]
);

$builder->add(
'context',
HiddenType::class,
[
'data' => $options['context']
] ]
); );
} }

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'context' => 'frontend',
'current_merchant' => $this->merchantResolver->getCurrent(),
]
);
}

} }

+ 81
- 0
Form/Section/SwitchSectionFormType.php View File

<?php

namespace Lc\CaracoleBundle\Form\Section;

use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Repository\Section\SectionRepository;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\SectionResolver;
use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SwitchSectionFormType extends AbstractType
{
protected $em;
protected $translatorAdmin;
protected $sectionResolver;

public function __construct(
EntityManager $em,
TranslatorAdmin $translatorAdmin,
SectionResolver $sectionResolver
) {
$this->em = $em;
$this->translatorAdmin = $translatorAdmin;
$this->sectionResolver = $sectionResolver;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$section = $options['section'];
$currentSection = $this->sectionResolver->getCurrent();

$builder->add(
'id_section',
HiddenType::class,
[
'data' => $section ? $section->getId() : null
]
);

$styleButton = '';
$classButton = 'btn-section';
if ($section == $currentSection) {
$classButton .= ' btn-section-current';
$styleButton = 'background-color: ' . $currentSection->getColor() . ';';
}

$builder->add(
'submit',
SubmitType::class,
[
'label' => $section ? $section->getTitle() : '',
'attr' => [
'class' => $classButton,
'style' => $styleButton,
]
]
);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'section' => null,
]
);
}

}

+ 5
- 5
Model/User/UserModel.php View File

/** /**
* @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Merchant\MerchantInterface") * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Merchant\MerchantInterface")
*/ */
protected $merchant;
protected $favoriteMerchant;


/** /**
* @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Address\AddressInterface", mappedBy="user", cascade={"persist"}) * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Address\AddressInterface", mappedBy="user", cascade={"persist"})
return $this; return $this;
} }


public function getMerchant(): ?MerchantInterface
public function getFavoriteMerchant(): ?MerchantInterface
{ {
return $this->merchant;
return $this->favoriteMerchant;
} }


public function setMerchant(?MerchantInterface $merchant): self
public function setFavoriteMerchant(?MerchantInterface $merchant): self
{ {
$this->merchant = $merchant;
$this->favoriteMerchant = $merchant;


return $this; return $this;
} }

+ 1
- 1
Repository/Section/SectionRepository.php View File

namespace Lc\CaracoleBundle\Repository\Section; namespace Lc\CaracoleBundle\Repository\Section;


use Lc\CaracoleBundle\Model\Section\SectionInterface; use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\SovBundle\Repository\AbstractRepository;
use Lc\CaracoleBundle\Repository\AbstractRepository;


/** /**
* @method SectionInterface|null find($id, $lockMode = null, $lockVersion = null) * @method SectionInterface|null find($id, $lockMode = null, $lockVersion = null)

+ 2
- 3
Resolver/SectionResolver.php View File

protected $sectionRepository; protected $sectionRepository;
protected $merchantResolver; protected $merchantResolver;


public function __construct(MerchantResolver $merchantResolver, SectionRepository $sectionRepository)
public function __construct(MerchantResolver $merchantResolver, EntityManagerInterface $em)
{ {
$this->sectionRepository = $sectionRepository;
$this->em = $em;
$this->merchantResolver = $merchantResolver; $this->merchantResolver = $merchantResolver;
} }


throw new \ErrorException('Aucune section par défaut définie pour ce merchant'); throw new \ErrorException('Aucune section par défaut définie pour ce merchant');
} }
} }

return $currentAdminSection; return $currentAdminSection;
} }



+ 2
- 0
Resources/assets/app/adminlte/common/app.common.js View File


import './common.scss' ;

+ 0
- 0
Resources/assets/app/adminlte/common/common.scss View File


+ 7
- 0
Resources/assets/app/switchmerchant/app.switchmerchant.js View File


import './switchmerchant.js' ;
import './switchmerchant_admin.scss' ;





+ 11
- 0
Resources/assets/app/switchmerchant/switchmerchant.js View File


$(document).ready(function() {
initSwitchMerchant() ;
}) ;

function initSwitchMerchant() {
let $form = $('form.switch-merchant') ;
$form.find('select').change(function() {
$form.submit() ;
}) ;
}

+ 25
- 0
Resources/assets/app/switchmerchant/switchmerchant_admin.scss View File


body.admin {
.nav-switch-merchant {
width: 220px ;
text-align: right ;

.fa {
position: relative;
top: -4px;
right: 7px;
}

form.switch-merchant {
display: inline-block ;
width: 180px ;
position: relative ;
top: 7px ;
text-align: left ;

label, .select2-selection__clear {
display: none ;
}
}
}
}

+ 8
- 0
Resources/config/routes.yaml View File

carac_switch_merchant:
path: /switch-merchant
controller: Lc\CaracoleBundle\Controller\Merchant\SwitchMerchantController::switchMerchant

carac_switch_section:
path: /switch-section
controller: Lc\CaracoleBundle\Controller\Section\SwitchSectionAdminController::switchSection


+ 9
- 3
Resources/views/adminlte/layout.html.twig View File

<ul class="navbar-nav"> <ul class="navbar-nav">
{% for section in carac_get_sections() %} {% for section in carac_get_sections() %}
<li class="nav-item d-none d-sm-inline-block"> <li class="nav-item d-none d-sm-inline-block">
<a href="#" class="nav-link">{{ section.title }}</a>
{% set form_switch_section = carac_get_form_switch_section(section) %}
{% form_theme form_switch_section '@LcSov/adminlte/crud/form_theme.html.twig' %}
{{ form_start(form_switch_section) }}
{{ form(form_switch_section) }}
{{ form_end(form_switch_section) }}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
<li class="nav-item">
{% set form_switch_merchant = carac_get_form_switch_merchhant() %}
<li class="nav-item nav-switch-merchant">
<i class="fa fa-store"></i>
{% set form_switch_merchant = carac_get_form_switch_merchant('admin') %}
{% form_theme form_switch_merchant '@LcSov/adminlte/crud/form_theme.html.twig' %}
{{ form_start(form_switch_merchant) }} {{ form_start(form_switch_merchant) }}
{{ form(form_switch_merchant) }} {{ form(form_switch_merchant) }}
{{ form_end(form_switch_merchant) }} {{ form_end(form_switch_merchant) }}

+ 32
- 28
Twig/TwigExtension.php View File



use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType; use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType;
use Lc\CaracoleBundle\Form\Section\SwitchSectionFormType;
use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository; use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
use Lc\CaracoleBundle\Repository\Section\SectionRepository; use Lc\CaracoleBundle\Repository\Section\SectionRepository;
use Lc\SovBundle\Translation\TranslatorAdmin; use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;


class TwigExtension extends AbstractExtension class TwigExtension extends AbstractExtension
{ {
protected $em;
protected $kernel;
protected $parameterBag;
protected $cacheManager;
protected $requestStack;
protected $router;
protected $translator;
protected $translatorAdmin;
protected $merchantRepository; protected $merchantRepository;
protected $sectionRepository; protected $sectionRepository;
protected $formFactory; protected $formFactory;
protected $urlGenerator;


public function __construct( public function __construct(
KernelInterface $kernel,
ParameterBagInterface $parameterBag,
CacheManager $cacheManager,
EntityManagerInterface $entityManager,
RequestStack $requestStack,
UrlGeneratorInterface $router,
TranslatorInterface $translator,
TranslatorAdmin $translatorAdmin,
MerchantRepository $merchantRepository, MerchantRepository $merchantRepository,
SectionRepository $sectionRepository, SectionRepository $sectionRepository,
FormFactoryInterface $formFactory
FormFactoryInterface $formFactory,
UrlGeneratorInterface $urlGenerator
) { ) {
$this->kernel = $kernel;
$this->parameterBag = $parameterBag;
$this->cacheManager = $cacheManager;
$this->em = $entityManager;
$this->requestStack = $requestStack;
$this->router = $router;
$this->translator = $translator;
$this->translatorAdmin = $translatorAdmin;
$this->merchantRepository = $merchantRepository; $this->merchantRepository = $merchantRepository;
$this->sectionRepository = $sectionRepository; $this->sectionRepository = $sectionRepository;
$this->formFactory = $formFactory; $this->formFactory = $formFactory;
$this->urlGenerator = $urlGenerator;
} }


public function getFunctions() public function getFunctions()
{ {
return array( return array(
new TwigFunction('carac_get_sections', [$this, 'getSections']), new TwigFunction('carac_get_sections', [$this, 'getSections']),
new TwigFunction('carac_get_form_switch_merchhant', [$this, 'getFormSwitchMerchant']),
new TwigFunction('carac_get_form_switch_merchant', [$this, 'getFormSwitchMerchant']),
new TwigFunction('carac_get_form_switch_section', [$this, 'getFormSwitchSection']),
); );
} }


return $this->merchantRepository->findAll(); return $this->merchantRepository->findAll();
} }


public function getFormSwitchMerchant()
public function getFormSwitchMerchant($context = 'front')
{ {
$form = $this->formFactory->create(SwitchMerchantFormType::class, null, ['action' => '#']);
$form = $this->formFactory->create(
SwitchMerchantFormType::class,
null,
[
'action' => $this->urlGenerator->generate('carac_switch_merchant'),
'attr' => ['class' => 'switch-merchant'],
'context' => $context
]
);
return $form->createView();
}

public function getFormSwitchSection($section)
{
$form = $this->formFactory->create(
SwitchSectionFormType::class,
null,
[
'action' => $this->urlGenerator->generate('carac_switch_section'),
'attr' => ['class' => 'switch-section'],
'section' => $section,
]
);
return $form->createView(); return $form->createView();
} }
} }

Loading…
Cancel
Save