|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class DatePicker extends AControl |
|
7
|
|
|
{ |
|
8
|
|
|
protected string $templateInput = '<input type="text" value="%1$s"%2$s />%3$s'; |
|
9
|
|
|
protected string $dateFormat = 'Y-m-d'; |
|
10
|
|
|
protected string $dateClass = 'datepicker'; |
|
11
|
|
|
|
|
12
|
5 |
|
public function set(string $alias, ?string $originalValue = null, string $label = ''): self |
|
13
|
|
|
{ |
|
14
|
5 |
|
$this->setEntry($alias, $originalValue, $label); |
|
15
|
5 |
|
$this->setAttribute('class', $this->dateClass); |
|
16
|
5 |
|
$this->setAttribute('id', $this->getKey()); |
|
17
|
5 |
|
return $this; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function setDateFormat(string $dateFormat): self |
|
21
|
|
|
{ |
|
22
|
1 |
|
$this->dateFormat = $dateFormat; |
|
23
|
1 |
|
return $this; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public function getDateFormat(): string |
|
27
|
|
|
{ |
|
28
|
1 |
|
return $this->dateFormat; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function setValue($value): void |
|
32
|
|
|
{ |
|
33
|
1 |
|
if (is_numeric($value)) { |
|
34
|
1 |
|
$this->value = $value; |
|
35
|
1 |
|
} elseif (!empty($value)) { |
|
36
|
1 |
|
$this->value = strtotime(strval($value)); |
|
37
|
|
|
} |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function renderInput($attributes = null): string |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->addAttributes($attributes); |
|
43
|
2 |
|
if (is_numeric($this->value)) { |
|
44
|
1 |
|
$value = date($this->getDateFormat(), intval($this->value)); |
|
45
|
|
|
} else { |
|
46
|
2 |
|
$value = ''; |
|
47
|
|
|
} |
|
48
|
2 |
|
$this->setAttribute('name', $this->getKey()); |
|
49
|
2 |
|
return $this->wrapIt(sprintf($this->templateInput, $this->escaped(strval($value)), $this->renderAttributes(), $this->renderChildren()), $this->wrappersInput); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|