/
DateType.php
177 lines (159 loc) · 4.9 KB
/
DateType.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* This file is part of the GenemuFormBundle package.
*
* (c) Olivier Chauvel <olivier@generation-multiple.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Genemu\Bundle\FormBundle\Form\JQuery\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\DateType as BaseDateType;
/**
* DateType
*
* @author Olivier Chauvel <olivier@generation-multiple.com>
*/
class DateType extends AbstractType
{
private $options;
/**
* Constructs
*
* @param array $options
*/
public function __construct(array $options)
{
$this->options = $options;
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$configs = $options['configs'];
$years = $options['years'];
$configs['dateFormat'] = 'yy-mm-dd';
if ('single_text' === $options['widget']) {
$dateFormat = is_int($options['format']) ? $options['format'] : BaseDateType::DEFAULT_FORMAT;
$timeFormat = \IntlDateFormatter::NONE;
$calendar = \IntlDateFormatter::GREGORIAN;
$pattern = is_string($options['format']) ? $options['format'] : null;
$formatter = new \IntlDateFormatter(
\Locale::getDefault(),
$dateFormat,
$timeFormat,
'UTC',
$calendar,
$pattern
);
$formatter->setLenient(false);
$configs['dateFormat'] = $this->getJavascriptPattern($formatter);
}
$view->vars = array_replace($view->vars, array(
'min_year' => min($years),
'max_year' => max($years),
'configs' => $configs,
'culture' => $options['culture'],
));
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$configs = $this->options;
$resolver
->setDefaults(array(
'culture' => \Locale::getPrimaryLanguage(\Locale::getDefault()),
'widget' => 'choice',
'years' => range(date('Y') - 5, date('Y') + 5),
'configs' => array(
'dateFormat' => null,
),
))
->setNormalizers(array(
'configs' => function (Options $options, $value) use ($configs) {
$result = array_merge($configs, $value);
if ('single_text' !== $options['widget'] || isset($result['buttonImage'])) {
$result['showOn'] = 'button';
}
return $result;
}
));
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'date';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'genemu_jquerydate';
}
/**
* Create pattern Date Javascript
*
* @param IntlDateFormatter $formatter
*
* @return string pattern date of Javascript
*/
protected function getJavascriptPattern(\IntlDateFormatter $formatter)
{
$pattern = $formatter->getPattern();
$patterns = preg_split('([\\\/.:_;,\s-\ ]{1})', $pattern);
$exits = array();
// Transform pattern for JQuery ui datepicker
foreach ($patterns as $index => $val) {
switch ($val) {
case 'yy':
$exits[$val] = 'y';
break;
case 'y':
case 'yyyy':
$exits[$val] = 'yy';
break;
case 'M':
$exits[$val] = 'm';
break;
case 'MM':
case 'L':
case 'LL':
$exits[$val] = 'mm';
break;
case 'MMM':
case 'LLL':
$exits[$val] = 'M';
break;
case 'MMMM':
case 'LLLL':
$exits[$val] = 'MM';
break;
case 'D':
$exits[$val] = 'o';
break;
case 'E':
case 'EE':
case 'EEE':
case 'eee':
$exits[$val] = 'D';
break;
case 'EEEE':
case 'eeee':
$exits[$val] = 'DD';
break;
}
}
return str_replace(array_keys($exits), array_values($exits), $pattern);
}
}