1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Alxarafe. Development of PHP applications in a flash! |
4
|
|
|
* Copyright (C) 2018-2020 Alxarafe <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Alxarafe\Core\Renders\Twig\Components; |
8
|
|
|
|
9
|
|
|
use DateTime; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class InputComponent |
13
|
|
|
* |
14
|
|
|
* @package Alxarafe\Core\Renders\Twig\Components |
15
|
|
|
*/ |
16
|
|
|
class InputComponent extends AbstractComponent |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Contains component type. |
20
|
|
|
* |
21
|
|
|
* @doc https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Contains component value. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $value; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Contains component minimum value. |
36
|
|
|
* |
37
|
|
|
* @var float |
38
|
|
|
*/ |
39
|
|
|
public $min; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Contains component maximum value. |
43
|
|
|
* |
44
|
|
|
* @var float |
45
|
|
|
*/ |
46
|
|
|
public $max; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Contains component pattern for regex validation. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $pattern; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Contains component placeholder. |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
public $placeholder; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* InputComponent constructor. |
64
|
|
|
* |
65
|
|
|
* @param $parameters |
66
|
|
|
*/ |
67
|
|
|
public function __construct($parameters) |
68
|
|
|
{ |
69
|
|
|
// TODO: Verify all fields |
70
|
|
|
switch ($parameters['type']) { |
71
|
|
|
case 'datetime': |
72
|
|
|
if (isset($parameters['value']) && $parameters['value'] != '') { |
73
|
|
|
if ($parameters['value'] === 'CURRENT_TIMESTAMP') { |
74
|
|
|
$parameters['value'] = date('Y-m-d\TH:i:s'); |
75
|
|
|
} |
76
|
|
|
$dt = new DateTime($parameters['value']); |
77
|
|
|
$parameters['value'] = $dt->format('Y-m-d\TH:i:s'); |
78
|
|
|
} |
79
|
|
|
break; |
80
|
|
|
case 'checkbox': |
81
|
|
|
$parameters['value'] = $parameters['value'] == 1 ? 1 : 0; |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
parent::__construct($parameters); |
86
|
|
|
// TESTED: checkbox, number, datetime-local, text |
87
|
|
|
// NOT TESTED: button, color, date, email, file, hidden, image, month, password, radio, range, reset, search, submit, tel, time, url, week |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function toHtml($readOnly = false): string |
91
|
|
|
{ |
92
|
|
|
/** |
93
|
|
|
* TODO: Entiendo toHtml debería de existir tanto para editar como para mostrar. |
94
|
|
|
* Lo debajo comentado, entiendo que debería de estar en la opción de mostrar, |
95
|
|
|
* de manera que muestre los datos de forma entendible por el usuario. |
96
|
|
|
* |
97
|
|
|
* Tanto la opción de listar un controlador, como en la de mostrar un registro, |
98
|
|
|
* campos del tipo fecha aparecen con la fecha mal formada YYYY-MM-DD HH:MM_SS, |
99
|
|
|
* y campos de tipo lógico aparecen con un 1 o un 0, en lugar de algo más entendible. |
100
|
|
|
* |
101
|
|
|
* Incluso en al mostrar una fecha, se muestra de la forma YYYY-MM-DDTHH:MM_SS, que |
102
|
|
|
* es la que entiende el componente de edición de timestamps. |
103
|
|
|
* |
104
|
|
|
* TODO: Otro problema está en la edición de timestamp nulos, que pone el instante actual. |
105
|
|
|
*/ |
106
|
|
|
switch ($this->type) { |
107
|
|
|
case 'datetime': |
108
|
|
|
// $dt = new \DateTime($this->value); |
109
|
|
|
// $this->value = $dt->format('d-m-Y H:i:s'); |
110
|
|
|
break; |
111
|
|
|
case 'checkbox': |
112
|
|
|
// if ($this->value == 1) { |
113
|
|
|
// $this->value = '<i class="fa fa-check-square" aria-hidden="true"></i>'; |
114
|
|
|
// } else { |
115
|
|
|
// $this->value = '<i class="fa fa-square" aria-hidden="true"></i>'; |
116
|
|
|
// } |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
return parent::toHtml($readOnly); // TODO: Change the autogenerated stub |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the template path to render this component. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getTemplatePath(): string |
128
|
|
|
{ |
129
|
|
|
return '@Core/components/input.html'; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|