1
+ <?php
2
+
3
+ namespace Xthiago \FormExtraBundle \Form \Type ;
4
+
5
+ use Symfony \Component \Form \AbstractType ;
6
+ use Symfony \Component \OptionsResolver \OptionsResolverInterface ;
7
+ use Symfony \Component \OptionsResolver \Options ;
8
+ use Symfony \Component \Form \FormView ;
9
+ use Symfony \Component \Form \FormView \FormInterface ;
10
+ use Symfony \Component \Form \FormBuilderInterface ;
11
+
12
+ use Symfony \Component \Form \Extension \Core \DataTransformer \DateTimeToStringTransformer ;
13
+
14
+
15
+ class DatetimeType extends AbstractType
16
+ {
17
+ public function buildForm (FormBuilderInterface $ builder , array $ options )
18
+ {
19
+ if ($ options ['returnDatetime ' ] === true ) {
20
+
21
+ //$transformer = new DatetimeToStringTransformer($options['format']);
22
+ $ transformer = new DateTimeToStringTransformer (null , null , $ options ['format ' ]);
23
+ $ builder ->addModelTransformer ($ transformer );
24
+ }
25
+ }
26
+
27
+ public function setDefaultOptions (OptionsResolverInterface $ resolver )
28
+ {
29
+ $ resolver ->setDefaults (
30
+ array (
31
+ 'format ' => 'd/m/Y ' , // formato da data (exemplo: d/m/Y H:i)
32
+ 'autoclose ' => true , // fecha a picker ao selecionar data
33
+ 'todayBtn ' => true , // mostra botão 'hoje'
34
+ 'startDate ' => null , // primeira data que pode ser selecionada (deve estar no mesmo formato de 'format') - ex: '05/11/2013 00:00'
35
+ 'endDate ' => null , // última data que pode ser selecionada (deve estar no mesmo formato de 'format') - ex: '15/11/2013 23:59'
36
+ 'minuteStep ' => 30 , // incremento usado para exibir a seleção de minutos
37
+ 'language ' => 'pt-BR ' , // idioma do picker
38
+ 'startView ' => null , // janela que aparece quando o datepicker é aberto
39
+ 'minView ' => 2 , // última janela a ser exibida para concluir a seleção (0 -minutos, 1-hora, 2 - dias, 3 - meses, 4- anos)
40
+ 'maxView ' => null ,
41
+ 'todayHighlight ' => true , // destaca o dia atual na listagem de dias
42
+ 'keyboardNavigation ' => true , // permite navegar pelo teclado
43
+ 'daysOfWeekDisabled ' => null , // dias da semana que devem ser desabilitado (ex: '0,6')
44
+ 'weekStart ' => 0 , // primeiro dia da semana
45
+
46
+ 'returnDatetime ' => true , // true para retornar \DateTime e false para string
47
+
48
+ 'dateType ' => 'date ' , // tipos possíveis: date (d/m/Y), datetime (d/m/Y H:i), month (m/Y), year (Y)
49
+
50
+ 'onchange ' => null ,
51
+ 'datepicker ' => true , // false para desabilitar o datepicker
52
+ )
53
+ );
54
+ }
55
+
56
+ protected function convertFormatToJavascriptDate ($ formato )
57
+ {
58
+ $ padroes = // key: php - value: javascript
59
+ array (
60
+ 'y ' => 'yy ' ,
61
+ 'Y ' => 'yyyy ' ,
62
+ 'm ' => 'mm ' ,
63
+ 'd ' => 'dd ' ,
64
+ 'H ' => 'hh ' ,
65
+ 'i ' => 'ii ' ,
66
+ 's ' => 'ss ' ,
67
+ )
68
+ ;
69
+
70
+ return str_replace (array_keys ($ padroes ), array_values ($ padroes ), $ formato );
71
+ }
72
+
73
+ public function buildView (\Symfony \Component \Form \FormView $ view , \Symfony \Component \Form \FormInterface $ form , array $ options )
74
+ {
75
+ /*if($options['format'] === null) {
76
+
77
+ $type = $options['type'];
78
+
79
+ if($type == 'date') {
80
+
81
+ $options['format'] = 'd/m/Y';
82
+ $options['minView'] = 2;
83
+
84
+ } else if($type == 'datetime') {
85
+
86
+ $options['format'] = 'd/m/Y H:i';
87
+ $options['minView'] = 0;
88
+
89
+ } else if($type == 'month') {
90
+
91
+ $options['format'] = 'm/Y';
92
+ $options['minView'] = 3;
93
+ $options['startView'] = 3;
94
+
95
+ } else if($type == 'year') {
96
+
97
+ $options['format'] = 'Y';
98
+ $options['minView'] = 4;
99
+ $options['startView'] = 4;
100
+
101
+ }
102
+ }*/
103
+
104
+ $ formato = array_key_exists ('format ' , $ options ) ? $ options ['format ' ] : 'd/m/Y H:i:s ' ;
105
+ $ view ->vars ['format ' ] = $ this ->convertFormatToJavascriptDate ($ formato );
106
+
107
+ $ view ->vars ['readonly ' ] = array_key_exists ('readonly ' , $ options ) ? $ options ['readonly ' ] : null ;
108
+ $ view ->vars ['autoclose ' ] = array_key_exists ('autoclose ' , $ options ) ? $ options ['autoclose ' ] : null ;
109
+ $ view ->vars ['todayBtn ' ] = array_key_exists ('todayBtn ' , $ options ) ? $ options ['todayBtn ' ] : null ;
110
+ $ view ->vars ['startDate ' ] = array_key_exists ('startDate ' , $ options ) ? $ options ['startDate ' ] : null ;
111
+ $ view ->vars ['endDate ' ] = array_key_exists ('endDate ' , $ options ) ? $ options ['endDate ' ] : null ;
112
+ $ view ->vars ['minuteStep ' ] = array_key_exists ('minuteStep ' , $ options ) ? $ options ['minuteStep ' ] : null ;
113
+ $ view ->vars ['language ' ] = array_key_exists ('language ' , $ options ) ? $ options ['language ' ] : null ;
114
+ $ view ->vars ['minView ' ] = array_key_exists ('minView ' , $ options ) ? $ options ['minView ' ] : null ;
115
+ $ view ->vars ['maxView ' ] = array_key_exists ('maxView ' , $ options ) ? $ options ['maxView ' ] : null ;
116
+ $ view ->vars ['todayHighlight ' ] = array_key_exists ('todayHighlight ' , $ options ) ? $ options ['todayHighlight ' ] : null ;
117
+ $ view ->vars ['keyboardNavigation ' ] = array_key_exists ('keyboardNavigation ' , $ options ) ? $ options ['keyboardNavigation ' ] : null ;
118
+ $ view ->vars ['daysOfWeekDisabled ' ] = array_key_exists ('daysOfWeekDisabled ' , $ options ) ? $ options ['daysOfWeekDisabled ' ] : null ;
119
+ $ view ->vars ['weekStart ' ] = array_key_exists ('weekStart ' , $ options ) ? $ options ['weekStart ' ] : null ;
120
+ $ view ->vars ['startView ' ] = array_key_exists ('startView ' , $ options ) ? $ options ['startView ' ] : null ;
121
+
122
+ $ view ->vars ['onchange ' ] = array_key_exists ('onchange ' , $ options ) ? $ options ['onchange ' ] : null ;
123
+
124
+ $ view ->vars ['dateType ' ] = array_key_exists ('dateType ' , $ options ) ? $ options ['dateType ' ] : 'date ' ;
125
+
126
+ $ view ->vars ['datepicker ' ] = array_key_exists ('datepicker ' , $ options ) ? $ options ['datepicker ' ] : true ;
127
+ }
128
+
129
+ public function getParent ()
130
+ {
131
+ return 'text ' ;
132
+ }
133
+
134
+ public function getName ()
135
+ {
136
+ return 'xthiago_datetime ' ;
137
+ }
138
+ }
0 commit comments