/
CalendarType.php
102 lines (93 loc) · 3.11 KB
/
CalendarType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Form\Type\Admin;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\Calendar;
use Eccube\Repository\CalendarRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class CalendarType
*/
class CalendarType extends AbstractType
{
/**
* @var EccubeConfig
*/
protected $eccubeConfig;
/**
* @var CalendarRepository
*/
protected $calendarRepository;
/**
* CalendarType constructor.
*
* @param EccubeConfig $eccubeConfig
* @param CalendarRepository $calendarRepository
*/
public function __construct(EccubeConfig $eccubeConfig, CalendarRepository $calendarRepository)
{
$this->eccubeConfig = $eccubeConfig;
$this->calendarRepository = $calendarRepository;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'required' => true,
'constraints' => [
new Assert\Length([
'max' => $this->eccubeConfig['eccube_stext_len'],
]),
],
])
->add('holiday', DateType::class, [
'label' => 'admin.common.create_date__start',
'required' => true,
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'placeholder' => ['year' => '----', 'month' => '--', 'day' => '--'],
'attr' => [
'class' => 'datetimepicker-input',
'data-target' => '#'.$this->getBlockPrefix().'_create_date_start',
'data-toggle' => 'datetimepicker',
],
])
;
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
// 日付重複チェック
/** @var Calendar $Calendar */
$Calendar = $event->getData();
$qb = $this->calendarRepository->createQueryBuilder('c');
$qb
->select('count(c.id)')
->where('c.holiday = :holiday')
->setParameter('holiday', $Calendar->getHoliday());
$count = $qb->getQuery()
->getSingleScalarResult();
if ($count > 0) {
$form = $event->getForm();
$form['holiday']->addError(new FormError(trans('admin.setting.shop.calendar.holiday.available_error')));
}
});
}
}