Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Date.php
Go to the documentation of this file.
1 <?php
13 
16 
17 class Date extends AbstractElement
18 {
22  protected $_value;
23 
27  protected $localeDate;
28 
36  public function __construct(
37  Factory $factoryElement,
38  CollectionFactory $factoryCollection,
39  Escaper $escaper,
41  $data = []
42  ) {
43  $this->localeDate = $localeDate;
44  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
45  $this->setType('text');
46  $this->setExtType('textfield');
47  if (isset($data['value'])) {
48  $this->setValue($data['value']);
49  }
50  }
51 
59  protected function _toTimestamp($value)
60  {
61  $value = (int)$value;
62  if ($value > 3155760000) {
63  $value = 0;
64  }
65 
66  return $value;
67  }
68 
75  public function setValue($value)
76  {
77  if (empty($value)) {
78  $this->_value = '';
79  return $this;
80  }
81  if ($value instanceof \DateTimeInterface) {
82  $this->_value = $value;
83  return $this;
84  }
85  if (preg_match('/^[0-9]+$/', $value)) {
86  $this->_value = (new \DateTime())->setTimestamp($this->_toTimestamp($value));
87  return $this;
88  }
89 
90  try {
91  $this->_value = new \DateTime($value, new \DateTimeZone($this->localeDate->getConfigTimezone()));
92  } catch (\Exception $e) {
93  $this->_value = '';
94  }
95  return $this;
96  }
97 
105  public function getValue($format = null)
106  {
107  if (empty($this->_value)) {
108  return '';
109  }
110  if (null === $format) {
111  $format = $this->getDateFormat();
112  $format .= ($format && $this->getTimeFormat()) ? ' ' : '';
113  $format .= $this->getTimeFormat() ? $this->getTimeFormat() : '';
114  }
115  return $this->localeDate->formatDateTime(
116  $this->_value,
117  null,
118  null,
119  null,
120  $this->_value->getTimezone(),
121  $format
122  );
123  }
124 
130  public function getValueInstance()
131  {
132  if (empty($this->_value)) {
133  return null;
134  }
135  return $this->_value;
136  }
137 
147  public function getElementHtml()
148  {
149  $this->addClass('admin__control-text input-text');
150  $dateFormat = $this->getDateFormat() ?: $this->getFormat();
151  $timeFormat = $this->getTimeFormat();
152  if (empty($dateFormat)) {
153  throw new \Exception(
154  'Output format is not specified. ' .
155  'Please specify "format" key in constructor, or set it using setFormat().'
156  );
157  }
158 
159  $dataInit = 'data-mage-init="' . $this->_escape(
160  json_encode(
161  [
162  'calendar' => [
163  'dateFormat' => $dateFormat,
164  'showsTime' => !empty($timeFormat),
165  'timeFormat' => $timeFormat,
166  'buttonImage' => $this->getImage(),
167  'buttonText' => 'Select Date',
168  'disabled' => $this->getDisabled(),
169  'minDate' => $this->getMinDate(),
170  'maxDate' => $this->getMaxDate(),
171  ],
172  ]
173  )
174  ) . '"';
175 
176  $html = sprintf(
177  '<input name="%s" id="%s" value="%s" %s %s />',
178  $this->getName(),
179  $this->getHtmlId(),
180  $this->_escape($this->getValue()),
181  $this->serialize($this->getHtmlAttributes()),
182  $dataInit
183  );
184  $html .= $this->getAfterElementHtml();
185  return $html;
186  }
187 }
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, TimezoneInterface $localeDate, $data=[])
Definition: Date.php:36
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
serialize($attributes=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')