app/Plugin/CustomerSupportPro42/Form/Extension/Front/ContactTypeExtension.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the ContactManagement Plugin
  4.  *
  5.  * Copyright (C) 2020 Diezon.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\CustomerSupportPro42\Form\Extension\Front;
  11. use Eccube\Form\Type\Front\ContactType;
  12. use Plugin\CustomerSupportPro42\Form\Type\AllCharactersKanaType;
  13. use Plugin\CustomerSupportPro42\Form\Type\Master\ContactPurposeType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  15. use Symfony\Component\Form\AbstractTypeExtension;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormEvent;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. use Plugin\CustomerSupportPro42\Entity\Master\ContactPurpose;
  20. use Plugin\CustomerSupportPro42\Repository\Master\ContactPurposeRepository;
  21. use Plugin\CustomerSupportPro42\Form\Type\ContactCommentType;
  22. use Plugin\CustomerSupportPro42\Entity\Contact;
  23. use Symfony\Component\Form\FormEvents;
  24. use Symfony\Component\Validator\Constraints as Assert;
  25. use Eccube\Common\EccubeConfig;
  26. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  27. use Eccube\Entity\Customer;
  28. class ContactTypeExtension extends AbstractTypeExtension
  29. {
  30.     /**
  31.      * @var ContactPurposeRepository
  32.      */
  33.     protected $contactPurposeRepository;
  34.     /**
  35.      * @var EccubeConfig
  36.      */
  37.     protected $eccubeConfig;
  38.     /**
  39.      * @var TokenStorageInterface
  40.      */
  41.     protected $tokenStorage;
  42.     public function __construct(
  43.         ContactPurposeRepository $contactPurposeRepository,
  44.         EccubeConfig $eccubeConfig,
  45.         TokenStorageInterface $tokenStorage
  46.     ) {
  47.         $this->contactPurposeRepository $contactPurposeRepository;
  48.         $this->eccubeConfig $eccubeConfig;
  49.         $this->tokenStorage $tokenStorage;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function buildForm(FormBuilderInterface $builder, array $options)
  55.     {
  56.         $builder
  57.             ->add('ContactPurpose'ContactPurposeType::class, [
  58.                 'data_class' => null,
  59.                 'label' => 'front.contact.purpose',
  60.                 'expanded' => true,
  61.                 'required' => true,
  62.                 'data' => $this->contactPurposeRepository->find(ContactPurpose::DEFAULT),
  63.                 'choice_label' => function ($choice) {
  64.                     return 'front.contact.purpose_text.' $choice->getId();
  65.                 },
  66.                 'choice_translation_domain' => 'messages',
  67.             ])
  68.             ->remove('contents')
  69.             ->add('contents'TextAreaType::class, [
  70.                 'mapped' => false,
  71.                 'constraints' => [
  72.                     new Assert\NotBlank(),
  73.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_ltextarea_len']]),
  74.                 ],
  75.             ])
  76.             ->add('ContactComment'ContactCommentType::class, [
  77.                 'mapped' => false,
  78.             ]);
  79.         $builder->addEventListener(FormEvents::POST_SET_DATA, [$this'setCustomerData']);
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function configureOptions(OptionsResolver $resolver)
  85.     {
  86.         $resolver->setDefaults([
  87.             'data_class' => Contact::class,
  88.         ]);
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      */
  93.     public function getExtendedType()
  94.     {
  95.         return ContactType::class;
  96.     }
  97.     /**
  98.      * Return the class of the type being extended.
  99.      */
  100.     public static function getExtendedTypes(): iterable
  101.     {
  102.         return [ContactType::class];
  103.     }
  104.     /**
  105.      * @param FormEvent $event
  106.      */
  107.     public function setCustomerData(FormEvent $event)
  108.     {
  109.         $token $this->tokenStorage->getToken();
  110.         $Customer $token $token->getUser() : null;
  111.         if ($Customer instanceof Customer && $Customer->getId()) {
  112.             $form $event->getForm();
  113.             $form['name']['name01']->setData($Customer->getName01());
  114.             $form['name']['name02']->setData($Customer->getName02());
  115.             $form['kana']['kana01']->setData($Customer->getKana01());
  116.             $form['kana']['kana02']->setData($Customer->getKana02());
  117.             $form['postal_code']->setData($Customer->getPostalCode());
  118.             $form['address']['pref']->setData($Customer->getPref());
  119.             $form['address']['addr01']->setData($Customer->getAddr01());
  120.             $form['address']['addr02']->setData($Customer->getAddr02());
  121.             $form['phone_number']->setData($Customer->getPhoneNumber());
  122.             $form['email']->setData($Customer->getEmail());
  123.         }
  124.     }
  125. }