Skip to content

Commit b19f573

Browse files
committed
Merge remote-tracking branch 'origin/support/2.13.0' into support/2.14.0
2 parents 0553a3a + 20e4a83 commit b19f573

4 files changed

Lines changed: 59 additions & 162 deletions

File tree

inc/field/datefield.class.php

Lines changed: 15 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,13 @@
3232

3333
namespace GlpiPlugin\Formcreator\Field;
3434

35-
use PluginFormcreatorAbstractField;
3635
use Html;
3736
use 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() {

inc/field/datetimefield.class.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class DatetimeField extends PluginFormcreatorAbstractField
4646
protected $fields = null;
4747

4848
const DATE_FORMAT = 'Y-m-d H:i:s';
49+
const DATE_ZERO = '0000-00-00 00:00:00';
4950

5051
public function isPrerequisites(): bool {
5152
return true;
@@ -132,7 +133,7 @@ public function isValidValue($value): bool {
132133
return true;
133134
}
134135

135-
$check = DateTime::createFromFormat(self::DATE_FORMAT, $value);
136+
$check = $this->getDateFromValue($value);
136137
return $check !== false;
137138
}
138139

@@ -144,14 +145,25 @@ public static function canRequire(): bool {
144145
return true;
145146
}
146147

148+
/**
149+
* Convert a string value into DateTime object
150+
*
151+
* @param string $value
152+
* @return false|DateTime
153+
*/
154+
protected function getDateFromValue(string $value) {
155+
if (empty($value)) {
156+
$value = self::DATE_ZERO;
157+
}
158+
return DateTime::createFromFormat(self::DATE_FORMAT, $value);
159+
}
160+
147161
public function equals($value): bool {
148-
if ($this->value === '') {
149-
$answer = '0000-00-00 00:00:00';
150-
} else {
151-
$answer = $this->value;
162+
$answerDatetime = $this->getDateFromValue($this->value ?? '');
163+
$compareDatetime = $this->getDateFromValue($value);
164+
if ($compareDatetime === false) {
165+
return false;
152166
}
153-
$answerDatetime = DateTime::createFromFormat(self::DATE_FORMAT, $answer);
154-
$compareDatetime = DateTime::createFromFormat(self::DATE_FORMAT, $value);
155167
return $answerDatetime == $compareDatetime;
156168
}
157169

@@ -160,13 +172,11 @@ public function notEquals($value): bool {
160172
}
161173

162174
public function greaterThan($value): bool {
163-
if (empty($this->value)) {
164-
$answer = '0000-00-00 00:00:00';
165-
} else {
166-
$answer = $this->value;
175+
$answerDatetime = $this->getDateFromValue($this->value ?? '');
176+
$compareDatetime = $this->getDateFromValue($value);
177+
if ($compareDatetime === false) {
178+
return true;
167179
}
168-
$answerDatetime = DateTime::createFromFormat(self::DATE_FORMAT, $answer);
169-
$compareDatetime = DateTime::createFromFormat(self::DATE_FORMAT, $value);
170180
return $answerDatetime > $compareDatetime;
171181
}
172182

@@ -188,7 +198,7 @@ public function parseAnswerValues($input, $nonDestructive = false): bool {
188198
}
189199

190200
if ($input[$key] != ''
191-
&& DateTime::createFromFormat(self::DATE_FORMAT, $input[$key]) === false
201+
&& $this->getDateFromValue($input[$key]) === false
192202
) {
193203
return false;
194204
}

tests/3-unit/GlpiPlugin/Formcreator/Field/DateField.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ public function providerEquals() {
176176
'answer' => '2019-01-01',
177177
'expected' => true,
178178
],
179+
[
180+
'value' => '',
181+
'answer' => '2019-01-01',
182+
'expected' => false,
183+
],
179184
];
180185
}
181186

@@ -206,7 +211,11 @@ public function providerNotEquals() {
206211
'answer' => '2019-01-01',
207212
'expected' => false,
208213
],
209-
214+
[
215+
'value' => '',
216+
'answer' => '2019-01-01',
217+
'expected' => true,
218+
],
210219
];
211220
}
212221

tests/3-unit/GlpiPlugin/Formcreator/Field/DatetimeField.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ public function providerEquals() {
236236
'answer' => '2019-01-01 03:00:00',
237237
'expected' => true,
238238
],
239+
[
240+
'value' => '',
241+
'answer' => '2019-01-01 03:00:00',
242+
'expected' => false,
243+
],
239244
];
240245
}
241246

@@ -266,6 +271,11 @@ public function providerNotEquals() {
266271
'answer' => '2019-01-01 03:00:00',
267272
'expected' => false,
268273
],
274+
[
275+
'value' => '',
276+
'answer' => '2019-01-01 03:00:00',
277+
'expected' => true,
278+
],
269279
];
270280
}
271281

0 commit comments

Comments
 (0)