Calendar.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\Html;
  7. use Magento\Framework\Locale\Bundle\DataBundle;
  8. /**
  9. * Calendar block for page header
  10. *
  11. * Prepares localization data for calendar
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Calendar extends \Magento\Framework\View\Element\Template
  17. {
  18. /**
  19. * Date model
  20. *
  21. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  22. */
  23. protected $_date;
  24. /**
  25. * JSON Encoder
  26. *
  27. * @var \Magento\Framework\Json\EncoderInterface
  28. */
  29. protected $encoder;
  30. /**
  31. * @var \Magento\Framework\Locale\ResolverInterface
  32. */
  33. protected $_localeResolver;
  34. /**
  35. * Constructor
  36. *
  37. * @param \Magento\Framework\View\Element\Template\Context $context
  38. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  39. * @param \Magento\Framework\Json\EncoderInterface $encoder
  40. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Framework\View\Element\Template\Context $context,
  45. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  46. \Magento\Framework\Json\EncoderInterface $encoder,
  47. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  48. array $data = []
  49. ) {
  50. $this->_date = $date;
  51. $this->encoder = $encoder;
  52. $this->_localeResolver = $localeResolver;
  53. parent::__construct($context, $data);
  54. }
  55. /**
  56. * Render block HTML
  57. *
  58. * @return string
  59. */
  60. protected function _toHtml()
  61. {
  62. $localeData = (new DataBundle())->get($this->_localeResolver->getLocale());
  63. // get days names
  64. $daysData = $localeData['calendar']['gregorian']['dayNames'];
  65. $this->assign(
  66. 'days',
  67. [
  68. 'wide' => $this->encoder->encode(array_values(iterator_to_array($daysData['format']['wide']))),
  69. 'abbreviated' => $this->encoder->encode(
  70. array_values(iterator_to_array($daysData['format']['abbreviated']))
  71. ),
  72. ]
  73. );
  74. /**
  75. * Month names in abbreviated format values was added to ICU Data tables
  76. * starting ICU library version 52.1. For some OS, like CentOS, default
  77. * installation version of ICU library is 50.1.2, which not contain
  78. * 'abbreviated' key, and that may cause a PHP fatal error when passing
  79. * as an argument of function 'iterator_to_array'. This issue affects
  80. * locales like ja_JP, ko_KR etc.
  81. *
  82. * @see http://source.icu-project.org/repos/icu/tags/release-50-1-2/icu4c/source/data/locales/ja.txt
  83. * @see http://source.icu-project.org/repos/icu/tags/release-52-1/icu4c/source/data/locales/ja.txt
  84. * @var \ResourceBundle $monthsData
  85. */
  86. $monthsData = $localeData['calendar']['gregorian']['monthNames'];
  87. $this->assign(
  88. 'months',
  89. [
  90. 'wide' => $this->encoder->encode(array_values(iterator_to_array($monthsData['format']['wide']))),
  91. 'abbreviated' => $this->encoder->encode(
  92. array_values(
  93. iterator_to_array(
  94. null !== $monthsData->get('format')->get('abbreviated')
  95. ? $monthsData['format']['abbreviated']
  96. : $monthsData['format']['wide']
  97. )
  98. )
  99. ),
  100. ]
  101. );
  102. // get "today" and "week" words
  103. $this->assign('today', $this->encoder->encode($localeData['fields']['day']['relative']['0']));
  104. $this->assign('week', $this->encoder->encode($localeData['fields']['week']['dn']));
  105. // get "am" & "pm" words
  106. $this->assign('am', $this->encoder->encode($localeData['calendar']['gregorian']['AmPmMarkers']['0']));
  107. $this->assign('pm', $this->encoder->encode($localeData['calendar']['gregorian']['AmPmMarkers']['1']));
  108. // get first day of week and weekend days
  109. $this->assign(
  110. 'firstDay',
  111. (int)$this->_scopeConfig->getValue(
  112. 'general/locale/firstday',
  113. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  114. )
  115. );
  116. $this->assign(
  117. 'weekendDays',
  118. $this->encoder->encode(
  119. (string)$this->_scopeConfig->getValue(
  120. 'general/locale/weekend',
  121. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  122. )
  123. )
  124. );
  125. // define default format and tooltip format
  126. $this->assign(
  127. 'defaultFormat',
  128. $this->encoder->encode(
  129. $this->_localeDate->getDateFormat(\IntlDateFormatter::MEDIUM)
  130. )
  131. );
  132. $this->assign(
  133. 'toolTipFormat',
  134. $this->encoder->encode(
  135. $this->_localeDate->getDateFormat(\IntlDateFormatter::LONG)
  136. )
  137. );
  138. // get days and months for en_US locale - calendar will parse exactly in this locale
  139. $englishMonths = (new DataBundle())->get('en_US')['calendar']['gregorian']['monthNames'];
  140. $enUS = new \stdClass();
  141. $enUS->m = new \stdClass();
  142. $enUS->m->wide = array_values(iterator_to_array($englishMonths['format']['wide']));
  143. $enUS->m->abbr = array_values(iterator_to_array($englishMonths['format']['abbreviated']));
  144. $this->assign('enUS', $this->encoder->encode($enUS));
  145. return parent::_toHtml();
  146. }
  147. /**
  148. * Return offset of current timezone with GMT in seconds
  149. *
  150. * @return int
  151. */
  152. public function getTimezoneOffsetSeconds()
  153. {
  154. return $this->_date->getGmtOffset();
  155. }
  156. /**
  157. * Getter for store timestamp based on store timezone settings
  158. *
  159. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  160. * @return int
  161. */
  162. public function getStoreTimestamp($store = null)
  163. {
  164. return $this->_localeDate->scopeTimeStamp($store);
  165. }
  166. /**
  167. * Getter for yearRange option in datepicker
  168. *
  169. * @return string
  170. */
  171. public function getYearRange()
  172. {
  173. return (new \DateTime())->modify('- 100 years')->format('Y')
  174. . ':' . (new \DateTime())->modify('+ 100 years')->format('Y');
  175. }
  176. }