3232
3333namespace GlpiPlugin \Formcreator \Field ;
3434
35- use PluginFormcreatorAbstractField ;
3635use Html ;
3736use DateTime ;
38- use Session ;
39- use PluginFormcreatorFormAnswer ;
40- use GlpiPlugin \Formcreator \Exception \ComparisonException ;
41- use Glpi \Application \View \TemplateRenderer ;
4237
43- class DateField extends PluginFormcreatorAbstractField
38+ class DateField extends DatetimeField
4439{
4540 const DATE_FORMAT = 'Y-m-d ' ;
46-
47- public function isPrerequisites (): bool {
48- return true ;
49- }
50-
51- public function showForm (array $ options ): void {
52- $ template = '@formcreator/field/ ' . $ this ->question ->fields ['fieldtype ' ] . 'field.html.twig ' ;
53-
54- $ this ->question ->fields ['default_values ' ] = Html::entities_deep ($ this ->question ->fields ['default_values ' ]);
55- $ this ->deserializeValue ($ this ->question ->fields ['default_values ' ]);
56- TemplateRenderer::getInstance ()->display ($ template , [
57- 'item ' => $ this ->question ,
58- 'params ' => $ options ,
59- 'no_header ' => true ,
60- ]);
61- }
41+ const DATE_ZERO = '0000-00-00 ' ;
6242
6343 public function getRenderedHtml ($ domain , $ canEdit = true ): string {
6444 if (!$ canEdit ) {
@@ -82,137 +62,25 @@ public function getRenderedHtml($domain, $canEdit = true): string {
8262 return $ html ;
8363 }
8464
85- public function serializeValue (PluginFormcreatorFormAnswer $ formanswer ): string {
86- return $ this ->value ;
87- }
88-
89- public function deserializeValue ($ value ) {
90- $ this ->value = $ value ;
91- }
92-
93- public function getValueForDesign (): string {
94- return $ this ->value ;
95- }
96-
97- public function getValueForTargetText ($ domain , $ richText ): ?string {
98- return Html::convDate ($ this ->value );
99- }
100-
101- public function hasInput ($ input ): bool {
102- return isset ($ input ['formcreator_field_ ' . $ this ->question ->getID ()]);
103- }
104-
105- public function moveUploads () {
106- }
107-
108- public function getDocumentsForTarget (): array {
109- return [];
110- }
111-
112- public function isValid (): bool {
113- // If the field is required it can't be empty
114- if ($ this ->isRequired () && (strtotime ($ this ->value ) == '' )) {
115- Session::addMessageAfterRedirect (
116- sprintf (__ ('A required field is empty: %s ' , 'formcreator ' ), $ this ->getTtranslatedLabel ()),
117- false ,
118- ERROR
119- );
120- return false ;
121- }
122-
123- // All is OK
124- return $ this ->isValidValue ($ this ->value );
125- }
126-
127- public function isValidValue ($ value ): bool {
128- if (!$ this ->isRequired () && empty ($ value )) {
129- return true ;
130- }
131-
132- $ check = DateTime::createFromFormat (self ::DATE_FORMAT , $ value );
133- return $ check !== false ;
134- }
135-
13665 public static function getName (): string {
13766 return __ ('Date ' );
13867 }
13968
140- public static function canRequire (): bool {
141- return true ;
142- }
143-
144- public function equals ($ value ): bool {
145- if ($ this ->value === '' ) {
146- $ answer = '0000-00-00 ' ;
147- } else {
148- $ answer = $ this ->value ;
149- }
150- $ answerDatetime = DateTime::createFromFormat (self ::DATE_FORMAT , $ answer );
151- $ answerDatetime ->setTime (0 , 0 , 0 , 0 );
152- $ compareDatetime = DateTime::createFromFormat (self ::DATE_FORMAT , $ value );
153- $ compareDatetime ->setTime (0 , 0 , 0 , 0 );
154- return $ answerDatetime == $ compareDatetime ;
155- }
156-
157- public function notEquals ($ value ): bool {
158- return !$ this ->equals ($ value );
159- }
160-
161- public function greaterThan ($ value ): bool {
162- if (empty ($ this ->value )) {
163- $ answer = '0000-00-00 ' ;
164- } else {
165- $ answer = $ this ->value ;
166- }
167- $ answerDatetime = DateTime::createFromFormat (self ::DATE_FORMAT , $ answer );
168- $ answerDatetime ->setTime (0 , 0 , 0 , 0 );
169- $ compareDatetime = DateTime::createFromFormat (self ::DATE_FORMAT , $ value );
170- $ compareDatetime ->setTime (0 , 0 , 0 , 0 );
171- return $ answerDatetime > $ compareDatetime ;
172- }
173-
174- public function lessThan ($ value ): bool {
175- return !$ this ->greaterThan ($ value ) && !$ this ->equals ($ value );
176- }
177-
178- public function regex ($ value ): bool {
179- throw new ComparisonException ('Meaningless comparison ' );
180- }
181-
182- public function parseAnswerValues ($ input , $ nonDestructive = false ): bool {
183- $ key = 'formcreator_field_ ' . $ this ->question ->getID ();
184- if (!isset ($ input [$ key ])) {
185- $ input [$ key ] = '' ;
69+ /**
70+ * Convert a string value into DateTime object
71+ *
72+ * @param string $value
73+ * @return false|DateTime
74+ */
75+ protected function getDateFromValue (string $ value ) {
76+ if (empty ($ value )) {
77+ $ value = self ::DATE_ZERO ;
18678 }
187-
188- if (! is_string ( $ input [ $ key ]) ) {
189- return false ;
79+ $ datetime = DateTime:: createFromFormat ( self :: DATE_FORMAT , $ value );
80+ if ($ datetime !== false ) {
81+ $ datetime -> setTime ( 0 , 0 , 0 , 0 ) ;
19082 }
191-
192- if ($ input [$ key ] != ''
193- && DateTime::createFromFormat (self ::DATE_FORMAT , $ input [$ key ]) === false
194- ) {
195- return false ;
196- }
197-
198- $ this ->value = $ input [$ key ];
199- return true ;
200- }
201-
202- public function isPublicFormCompatible (): bool {
203- return true ;
204- }
205-
206- public function getHtmlIcon (): string {
207- return '<i class="fa fa-calendar" aria-hidden="true"></i> ' ;
208- }
209-
210- public function isVisibleField (): bool {
211- return true ;
212- }
213-
214- public function isEditableField (): bool {
215- return true ;
83+ return $ datetime ;
21684 }
21785
21886 public function getValueForApi () {
0 commit comments