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.

85 lines
2.6KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Form\Section;
  3. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  4. use Lc\CaracoleBundle\Repository\Section\SectionRepository;
  5. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  6. use Lc\CaracoleBundle\Resolver\SectionResolver;
  7. use Lc\CaracoleBundle\Solver\Section\SectionSolver;
  8. use Lc\SovBundle\Doctrine\EntityManager;
  9. use Lc\SovBundle\Model\User\UserInterface;
  10. use Lc\SovBundle\Translation\TranslatorAdmin;
  11. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  14. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  15. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. class SwitchSectionFormType extends AbstractType
  19. {
  20. protected EntityManager $em;
  21. protected TranslatorAdmin $translatorAdmin;
  22. protected SectionResolver $sectionResolver;
  23. protected SectionSolver $sectionSolver;
  24. public function __construct(
  25. EntityManager $em,
  26. TranslatorAdmin $translatorAdmin,
  27. SectionResolver $sectionResolver,
  28. SectionSolver $sectionSolver
  29. ) {
  30. $this->em = $em;
  31. $this->translatorAdmin = $translatorAdmin;
  32. $this->sectionResolver = $sectionResolver;
  33. $this->sectionSolver = $sectionSolver;
  34. }
  35. public function buildForm(FormBuilderInterface $builder, array $options)
  36. {
  37. $section = $options['section'];
  38. $currentSection = $this->sectionResolver->getCurrent();
  39. $builder->add(
  40. 'id_section',
  41. HiddenType::class,
  42. [
  43. 'data' => $section ? $section->getId() : null
  44. ]
  45. );
  46. $styleButton = '';
  47. $classButton = 'btn-section';
  48. if ($section == $currentSection) {
  49. $classButton .= ' btn-section-current '.$this->sectionSolver->getHtmlClass($currentSection);
  50. }
  51. $builder->add(
  52. 'submit',
  53. SubmitType::class,
  54. [
  55. 'label' => $section ? $section->getTitle() : 'Tout afficher',
  56. 'attr' => [
  57. 'class' => $classButton,
  58. 'style' => $styleButton,
  59. ]
  60. ]
  61. );
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function configureOptions(OptionsResolver $resolver)
  67. {
  68. $resolver->setDefaults(
  69. [
  70. 'section' => null,
  71. ]
  72. );
  73. }
  74. }