src/Eccube/Form/Type/Admin/NewsType.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Form\Type\Admin;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\News;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  19. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  20. use Symfony\Component\Form\Extension\Core\Type\FileType;
  21. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  22. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  23. use Symfony\Component\Form\Extension\Core\Type\TextType;
  24. use Symfony\Component\Form\FormBuilderInterface;
  25. use Symfony\Component\Form\FormError;
  26. use Symfony\Component\Form\FormEvent;
  27. use Symfony\Component\Form\FormEvents;
  28. use Symfony\Component\Form\FormInterface;
  29. use Symfony\Component\OptionsResolver\OptionsResolver;
  30. use Symfony\Component\Validator\Constraints as Assert;
  31. class NewsType extends AbstractType
  32. {
  33.     /**
  34.      * @var EccubeConfig
  35.      */
  36.     protected $eccubeConfig;
  37.     public function __construct(EccubeConfig $eccubeConfig)
  38.     {
  39.         $this->eccubeConfig $eccubeConfig;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function buildForm(FormBuilderInterface $builder, array $options)
  45.     {
  46.         $builder
  47.             ->add('publish_date'DateTimeType::class, [
  48.                 'widget' => 'single_text',
  49.                 'input' => 'datetime',
  50.                 'years' => range($this->eccubeConfig['eccube_news_start_year'], date('Y') + 3),
  51.                 'with_seconds' => true,
  52.                 'constraints' => [
  53.                     new Assert\NotBlank(),
  54.                     new Assert\Range([
  55.                         'min'=> '0003-01-01',
  56.                         'minMessage' => 'form_error.out_of_range',
  57.                     ]),
  58.                 ],
  59.             ])
  60.             ->add('title'TextType::class, [
  61.                 'required' => true,
  62.                 'constraints' => [
  63.                     new Assert\NotBlank(),
  64.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_mtext_len']]),
  65.                 ],
  66.             ])
  67.             ->add('url'TextType::class, [
  68.                 'required' => false,
  69.                 'constraints' => [
  70.                     new Assert\Url(),
  71.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_mtext_len']]),
  72.                 ],
  73.             ])
  74.             ->add('link_method'CheckboxType::class, [
  75.                 'required' => false,
  76.                 'label' => 'admin.content.news.new_window',
  77.                 'value' => '1',
  78.             ])
  79.             // 画像
  80.             ->add('news_image'FileType::class, [
  81.                 'multiple' => true,
  82.                 'required' => false,
  83.                 'mapped' => false,
  84.             ])
  85.             ->add('images'CollectionType::class, [
  86.                 'entry_type' => HiddenType::class,
  87.                 'prototype' => true,
  88.                 'mapped' => false,
  89.                 'allow_add' => true,
  90.                 'allow_delete' => true,
  91.             ])
  92.             ->add('add_images'CollectionType::class, [
  93.                 'entry_type' => HiddenType::class,
  94.                 'prototype' => true,
  95.                 'mapped' => false,
  96.                 'allow_add' => true,
  97.                 'allow_delete' => true,
  98.             ])
  99.             ->add('delete_images'CollectionType::class, [
  100.                 'entry_type' => HiddenType::class,
  101.                 'prototype' => true,
  102.                 'mapped' => false,
  103.                 'allow_add' => true,
  104.                 'allow_delete' => true,
  105.             ])
  106.             ->add('description'TextareaType::class, [
  107.                 'required' => false,
  108.                 'purify_html' => true,
  109.                 'attr' => [
  110.                     'rows' => 8,
  111.                 ],
  112.                 'constraints' => [
  113.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_ltext_len']]),
  114.                 ],
  115.             ])
  116.             ->add('visible'ChoiceType::class, [
  117.                 'label' => false,
  118.                 'choices' => ['admin.content.news.display_status__show' => true'admin.content.news.display_status__hide' => false],
  119.                 'required' => true,
  120.                 'expanded' => false,
  121.             ]);
  122.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  123.             /** @var FormInterface $form */
  124.             $form $event->getForm();
  125.             $saveImgDir $this->eccubeConfig['eccube_save_image_dir'];
  126.             $tempImgDir $this->eccubeConfig['eccube_temp_image_dir'];
  127.             $this->validateFilePath($form->get('delete_images'), [$saveImgDir$tempImgDir]);
  128.             $this->validateFilePath($form->get('add_images'), [$tempImgDir]);
  129.         });
  130.     }
  131.     /**
  132.      * 指定された複数ディレクトリのうち、いずれかのディレクトリ以下にファイルが存在するかを確認。
  133.      *
  134.      * @param $form FormInterface
  135.      * @param $dirs array
  136.      */
  137.     private function validateFilePath($form$dirs)
  138.     {
  139.         foreach ($form->getData() as $fileName) {
  140.             if (strpos($fileName'..') !== false) {
  141.                 $form->getRoot()['product_image']->addError(new FormError(trans('admin.product.image__invalid_path')));
  142.                 break;
  143.             }
  144.             $fileInDir array_filter($dirs, function ($dir) use ($fileName) {
  145.                 $filePath realpath($dir.'/'.$fileName);
  146.                 $topDirPath realpath($dir);
  147.                 return strpos($filePath$topDirPath) === && $filePath !== $topDirPath;
  148.             });
  149.             if (!$fileInDir) {
  150.                 $form->getRoot()['product_image']->addError(new FormError(trans('admin.product.image__invalid_path')));
  151.             }
  152.         }
  153.     }
  154.     /**
  155.      * {@inheritdoc}
  156.      */
  157.     public function configureOptions(OptionsResolver $resolver)
  158.     {
  159.         $resolver->setDefaults([
  160.             'data_class' => News::class,
  161.         ]);
  162.     }
  163.     /**
  164.      * {@inheritdoc}
  165.      */
  166.     public function getBlockPrefix()
  167.     {
  168.         return 'admin_news';
  169.     }
  170. }