Browse Source

Modal switch merchant

feature/ticket
Guillaume 3 years ago
parent
commit
9518a1f2ba
14 changed files with 188 additions and 149 deletions
  1. +40
    -0
      Controller/Merchant/FavoriteMerchantController.php
  2. +11
    -4
      Controller/Merchant/SwitchMerchantController.php
  3. +1
    -0
      Controller/Setting/SettingAdminController.php
  4. +30
    -0
      Definition/MerchantSettingDefinition.php
  5. +0
    -60
      Form/Merchant/SwitchMerchantButtonAdminFormType.php
  6. +9
    -0
      Form/Merchant/SwitchMerchantFormType.php
  7. +14
    -15
      Form/Setting/BaseSettingType.php
  8. +16
    -23
      Resources/assets/app/adminlte/common/common.scss
  9. +8
    -2
      Resources/assets/app/switchmerchant/switchmerchant_admin.js
  10. +29
    -17
      Resources/assets/app/switchmerchant/switchmerchant_admin.scss
  11. +4
    -0
      Resources/config/routes.yaml
  12. +11
    -0
      Resources/translations/admin.fr.yaml
  13. +13
    -13
      Resources/views/adminlte/layout.html.twig
  14. +2
    -15
      Twig/TwigExtension.php

+ 40
- 0
Controller/Merchant/FavoriteMerchantController.php View File

<?php

namespace Lc\CaracoleBundle\Controller\Merchant;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

class FavoriteMerchantController extends AbstractController
{
public function favoriteMerchant(Request $request, Security $security, EntityManagerInterface $em)
{
$user = $security->getUser() ;

if($user) {
$form = $this->createForm(SwitchMerchantFormType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$merchant = $form->get('merchant')->getData();

if ($merchant) {
$user->setFavoriteMerchant($merchant) ;
$em->update($user) ;
$em->flush() ;

$url = $merchant->getSettingValue(MerchantSettingDefinition::SETTING_URL).'admin';

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

+ 11
- 4
Controller/Merchant/SwitchMerchantController.php View File



namespace Lc\CaracoleBundle\Controller\Merchant; namespace Lc\CaracoleBundle\Controller\Merchant;


use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType; use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType;
use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;


class SwitchMerchantController extends AbstractController class SwitchMerchantController extends AbstractController
{ {


public function switchMerchant(Request $request)
public function switchMerchant(Request $request, MerchantRepository $merchantRepository)
{ {
$form = $this->createForm(SwitchMerchantFormType::class); $form = $this->createForm(SwitchMerchantFormType::class);
$form->handleRequest($request); $form->handleRequest($request);
$context = $form->get('context')->getData(); $context = $form->get('context')->getData();


if ($merchant) { if ($merchant) {
$url = $merchant->getMerchantConfig('url');
$url = $merchant->getSettingValue(MerchantSettingDefinition::SETTING_URL);


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


if ($url) { if ($url) {
} }
} }
} }

public function visitMerchant()
{

}
} }

+ 1
- 0
Controller/Setting/SettingAdminController.php View File

$entity = $resolver->getCurrent(); $entity = $resolver->getCurrent();


if ($entity) { if ($entity) {

$form = $this->createForm($formClass, $entity); $form = $this->createForm($formClass, $entity);


$form->handleRequest($request); $form->handleRequest($request);

+ 30
- 0
Definition/MerchantSettingDefinition.php View File

<?php

namespace Lc\CaracoleBundle\Definition;


class MerchantSettingDefinition extends AbstractSettingDefinition implements MerchantSettingDefinitionInterface
{
const CATEGORY_GENERAL = 'general';

const SETTING_URL = 'url';

public function __construct()
{
$this
->addSettingText(
[
'name' => self::SETTING_URL,
'category' => self::CATEGORY_GENERAL,
]
) ;
}

public function getCategories()
{
return [
self::CATEGORY_GENERAL,
];
}

}

+ 0
- 60
Form/Merchant/SwitchMerchantButtonAdminFormType.php View File

<?php

namespace Lc\CaracoleBundle\Form\Merchant;

use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
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\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

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

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

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'merchant',
HiddenType::class,
[
'data' => $options['merchant']
]
);

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

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

}

+ 9
- 0
Form/Merchant/SwitchMerchantFormType.php View File

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\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;


'data' => $options['context'] 'data' => $options['context']
] ]
); );

$builder->add(
'submit',
SubmitType::class,
[
'label' => $this->translatorAdmin->transAction('favorite')
]
);
} }


/** /**

+ 14
- 15
Form/Setting/BaseSettingType.php View File

abstract class BaseSettingType extends AbstractType abstract class BaseSettingType extends AbstractType
{ {
protected $em; protected $em;
protected $merchantSetup;
protected $sectionSetup;
protected $merchantSettingDefinition;
protected $sectionSettingDefinition;


public function __construct( public function __construct(
EntityManagerInterface $entityManager, EntityManagerInterface $entityManager,
MerchantSettingDefinitionInterface $merchantSetup,
SectionSettingDefinitionInterface $sectionSetup
MerchantSettingDefinitionInterface $merchantSettingDefinition,
SectionSettingDefinitionInterface $sectionSettingDefinition
) { ) {
$this->em = $entityManager; $this->em = $entityManager;
$this->merchantSetup = $merchantSetup;
$this->sectionSetup = $sectionSetup;
$this->merchantSettingDefinition = $merchantSettingDefinition;
$this->sectionSettingDefinition = $sectionSettingDefinition;
} }


public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$merchantSetup = $this->merchantSetup;
$sectionSetup = $this->sectionSetup;
$merchantSettingDefinition = $this->merchantSettingDefinition;
$sectionSettingDefinition = $this->sectionSettingDefinition;


$builder->addEventListener( $builder->addEventListener(
FormEvents::PRE_SET_DATA, FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($merchantSetup, $sectionSetup) {
function (FormEvent $event) use ($merchantSettingDefinition, $sectionSettingDefinition) {
$form = $event->getForm(); $form = $event->getForm();


$settingEntity = $event->getData(); $settingEntity = $event->getData();
$setup = null ; $setup = null ;
if($settingEntity instanceof MerchantSettingInterface) { if($settingEntity instanceof MerchantSettingInterface) {
$transCategory = 'merchant' ; $transCategory = 'merchant' ;
$setup = $merchantSetup ;
$settingDefinition = $merchantSettingDefinition ;
} }
elseif($settingEntity instanceof SectionSettingInterface) { elseif($settingEntity instanceof SectionSettingInterface) {
$transCategory = 'section' ; $transCategory = 'section' ;
$setup = $sectionSetup ;
$settingDefinition = $sectionSettingDefinition ;
} }
$label = 'setting_definition.'.$transCategory.'.settings.'.$settingName ; $label = 'setting_definition.'.$transCategory.'.settings.'.$settingName ;


if($setup) {
$setting = $setup->getSettingByName($settingName);
$settingType = $setup->getSettingType($settingName);

if($settingDefinition) {
$setting = $settingDefinition->getSettingByName($settingName);
$settingType = $settingDefinition->getSettingType($settingName);


if ($settingType == 'text') { if ($settingType == 'text') {
$form->add( $form->add(

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



body.admin {
// navbar carac
nav.navbar.carac {
background-color: white;
padding-top: 0px;
padding-bottom: 0px;
border-width: 2px;


nav.navbar.carac {
background-color: white ;
padding-top: 0px ;
padding-bottom: 0px ;
border-width: 2px;
ul.left {
position: relative;
top: 16px;


ul.left {
position: relative ;
top: 16px ;
li {
form.switch-section {
button {
border-radius: 7px 7px 0px 0px;


li {
form.switch-section {
button {
border-radius: 7px 7px 0px 0px ;

&:hover {
color: gray ;
}
&:hover {
color: gray;
} }
} }
} }
} }
} }

.content-header {
padding-top: 20px ;
}

}
}

+ 8
- 2
Resources/assets/app/switchmerchant/switchmerchant_admin.js View File

}) ; }) ;


function initSwitchMerchantAdmin() { function initSwitchMerchantAdmin() {
let idMerchant = $('#switch_merchant_form_merchant').val() ;
let nameVisitMerchantSessionStorage = 'visit_merchant_'+idMerchant ;
let $modalSwitchMerchant = $('#carac-modal-switch-merchant') ; let $modalSwitchMerchant = $('#carac-modal-switch-merchant') ;


if($modalSwitchMerchant.length) {
//$('#carac-modal-switch-merchant').modal('show') ;
if($modalSwitchMerchant.length && !sessionStorage.getItem(nameVisitMerchantSessionStorage)) {
$('#carac-modal-switch-merchant').modal('show') ;
} }

$('#carac-button-visit-merchant').click(function() {
sessionStorage.setItem(nameVisitMerchantSessionStorage, true);
}) ;
} }

+ 29
- 17
Resources/assets/app/switchmerchant/switchmerchant_admin.scss View File



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


.fa {
position: relative;
top: -4px;
right: 7px;
.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 ;
} }


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


label, .select2-selection__clear {
display: none ;
}
// modal
#carac-modal-switch-merchant {
form.switch-merchant {
.form-group:first-child {
display: none ;
} }
} }
} }

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

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


carac_favorite_merchant:
path: /favorite-merchant
controller: Lc\CaracoleBundle\Controller\Merchant\FavoriteMerchantController::favoriteMerchant

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

+ 11
- 0
Resources/translations/admin.fr.yaml View File

flash_message: flash_message:
settings_saved: Paramètres mis à jour settings_saved: Paramètres mis à jour


action:
favorite: Définir comme favoris

setting_definition:
merchant:
categories:
general: Général
settings:
url: URL absolue


entity: entity:
default: default:
fields: fields:

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

{% set merchant_current = merchant_resolver.getCurrent() %} {% set merchant_current = merchant_resolver.getCurrent() %}


{% if(user.favoriteMerchant != merchant_current) %} {% if(user.favoriteMerchant != merchant_current) %}
{# modal affichée uniquement si la sessionStorage.visit_merchant n'est pas défini (js) #}
{% embed '@LcSov/adminlte/embed/modal.twig' %} {% embed '@LcSov/adminlte/embed/modal.twig' %}
{% block id %}carac-modal-switch-merchant{% endblock %} {% block id %}carac-modal-switch-merchant{% endblock %}
{% block size %}modal-lg{% endblock %}
{% block title %} {% block title %}
Switch merchant
Bienvenue sur le marchand <i>{{ merchant_current.getTitle() }}</i>
{% endblock %} {% endblock %}
{% block body %} {% block body %}
{% if not user.favoriteMerchant %} {% if not user.favoriteMerchant %}
<p>Vous n'avez pas de marchand favoris.</p> <p>Vous n'avez pas de marchand favoris.</p>
{% endif %} {% endif %}
{% if user.favoriteMerchant and user.favoriteMerchant != merchant_current %} {% if user.favoriteMerchant and user.favoriteMerchant != merchant_current %}
<p>Vous n'êtes pas sur votre marchand par défaut.</p>
<p>Vous n'êtes pas sur votre marchand par favoris.</p>
{% endif %} {% endif %}
<p>Vous pouvez soit définir le marchand <strong>{{ merchant_current.getTitle() }}</strong>
comme marchand favoris ou simplement indiquer que vous visitez ce marchand pour aujourd'hui.</p>
{% endblock %} {% endblock %}
{% block footer %} {% block footer %}
{% set form_switch_merchant_button = carac_get_form_switch_merchant_button() %}
{% form_theme form_switch_merchant_button '@LcSov/adminlte/crud/form_theme.html.twig' %}
{{ form_start(form_switch_merchant_button) }}
{{ form(form_switch_merchant_button) }}
{{ form_end(form_switch_merchant_button) }}


<button type="button" class="btn btn-primary"
data-dismiss="modal">{{ 'action.switch_merchant'|trans }}</button>
<button type="button" class="btn btn-default"
data-dismiss="modal">{{ 'action.visit'|trans }}</button>
{% set form_switch_merchant = carac_get_form_switch_merchant('admin', 'carac_favorite_merchant') %}
{% form_theme form_switch_merchant '@LcSov/adminlte/crud/form_theme.html.twig' %}
{{ form_start(form_switch_merchant) }}
{{ form(form_switch_merchant) }}
{{ form_end(form_switch_merchant) }}
<button id="carac-button-visit-merchant" type="button" class="btn btn-default"
data-dismiss="modal">Visiter</button>
{% endblock %} {% endblock %}
{% endembed %} {% endembed %}
{% endif %} {% endif %}

+ 2
- 15
Twig/TwigExtension.php View File

return array( return array(
new TwigFunction('carac_get_sections', [$this, 'getSections']), new TwigFunction('carac_get_sections', [$this, 'getSections']),
new TwigFunction('carac_get_form_switch_merchant', [$this, 'getFormSwitchMerchant']), new TwigFunction('carac_get_form_switch_merchant', [$this, 'getFormSwitchMerchant']),
new TwigFunction('carac_get_form_switch_merchant_button', [$this, 'getFormSwitchMerchantButton']),
new TwigFunction('carac_get_form_switch_section', [$this, 'getFormSwitchSection']), new TwigFunction('carac_get_form_switch_section', [$this, 'getFormSwitchSection']),
); );
} }
return $this->merchantRepository->findAll(); return $this->merchantRepository->findAll();
} }


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


public function getFormSwitchMerchantButton()
{
$form = $this->formFactory->create(
SwitchMerchantButtonAdminFormType::class,
null,
[
'action' => $this->urlGenerator->generate('carac_switch_merchant'),
]
);
return $form->createView();
}

public function getFormSwitchSection($section) public function getFormSwitchSection($section)
{ {
$form = $this->formFactory->create( $form = $this->formFactory->create(

Loading…
Cancel
Save