/
Date.php
179 lines (165 loc) · 4.74 KB
/
Date.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Magento data selector form element
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Framework\Data\Form\Element;
use Magento\Framework\Escaper;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class Date extends AbstractElement
{
/**
* @var \DateTime
*/
protected $_value;
/**
* @var TimezoneInterface
*/
protected $localeDate;
/**
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param TimezoneInterface $localeDate
* @param array $data
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
TimezoneInterface $localeDate,
$data = []
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
$this->setType('text');
$this->setExtType('textfield');
if (isset($data['value'])) {
$this->setValue($data['value']);
}
$this->localeDate = $localeDate;
}
/**
* If script executes on x64 system, converts large
* numeric values to timestamp limit
*
* @param int $value
* @return int
*/
protected function _toTimestamp($value)
{
$value = (int)$value;
if ($value > 3155760000) {
$value = 0;
}
return $value;
}
/**
* Set date value
*
* @param mixed $value
* @return $this
*/
public function setValue($value)
{
if (empty($value)) {
$this->_value = '';
return $this;
}
if ($value instanceof \DateTimeInterface) {
$this->_value = $value;
return $this;
}
if (preg_match('/^[0-9]+$/', $value)) {
$this->_value = (new \DateTime())->setTimestamp($this->_toTimestamp($value));
return $this;
}
try {
$this->_value = new \DateTime($value);
} catch (\Exception $e) {
$this->_value = '';
}
return $this;
}
/**
* Get date value as string.
* Format can be specified, or it will be taken from $this->getFormat()
*
* @param string $format (compatible with \DateTime)
* @return string
*/
public function getValue($format = null)
{
if (empty($this->_value)) {
return '';
}
if (null === $format) {
$format = $this->getDateFormat();
$format .= ($format && $this->getTimeFormat()) ? ' ' : '';
$format .= $this->getTimeFormat() ? $this->getTimeFormat() : '';
}
return $this->localeDate->formatDateTime($this->_value, null, null, null, null, $format);
}
/**
* Get value instance, if any
*
* @return \DateTime
*/
public function getValueInstance()
{
if (empty($this->_value)) {
return null;
}
return $this->_value;
}
/**
* Output the input field and assign calendar instance to it.
* In order to output the date:
* - the value must be instantiated (\DateTime)
* - output format must be set (compatible with \DateTime)
*
* @throws \Exception
* @return string
*/
public function getElementHtml()
{
$this->addClass('input-text');
$dateFormat = $this->getDateFormat() ?: $this->getFormat();
$timeFormat = $this->getTimeFormat();
if (empty($dateFormat)) {
throw new \Exception(
'Output format is not specified. ' .
'Please, specify "format" key in constructor, or set it using setFormat().'
);
}
$dataInit = 'data-mage-init="' . $this->_escape(
json_encode(
[
'calendar' => [
'dateFormat' => $dateFormat,
'showsTime' => !empty($timeFormat),
'timeFormat' => $timeFormat,
'buttonImage' => $this->getImage(),
'buttonText' => 'Select Date',
'disabled' => $this->getDisabled(),
],
]
)
) . '"';
$html = sprintf(
'<input name="%s" id="%s" value="%s" %s %s />',
$this->getName(),
$this->getHtmlId(),
$this->_escape($this->getValue()),
$this->serialize($this->getHtmlAttributes()),
$dataInit
);
$html .= $this->getAfterElementHtml();
return $html;
}
}