Date.php 5.1 KB

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