Date.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  8. * Date element block
  9. */
  10. class Date extends \Magento\Framework\View\Element\Template
  11. {
  12. /**
  13. * Render block HTML
  14. *
  15. * @return string
  16. * @SuppressWarnings(PHPMD.NPathComplexity)
  17. */
  18. protected function _toHtml()
  19. {
  20. $html = '<input type="text" name="' . $this->getName() . '" id="' . $this->getId() . '" ';
  21. $html .= 'value="' . $this->escapeHtml($this->getValue()) . '" ';
  22. $html .= 'class="' . $this->getClass() . '" ' . $this->getExtraParams() . '/> ';
  23. $calendarYearsRange = $this->getYearsRange();
  24. $changeMonth = $this->getChangeMonth();
  25. $changeYear = $this->getChangeYear();
  26. $maxDate = $this->getMaxDate();
  27. $showOn = $this->getShowOn();
  28. $firstDay = $this->getFirstDay();
  29. $html .= '<script type="text/javascript">
  30. require(["jquery", "mage/calendar"], function($){
  31. $("#' .
  32. $this->getId() .
  33. '").calendar({
  34. showsTime: ' .
  35. ($this->getTimeFormat() ? 'true' : 'false') .
  36. ',
  37. ' .
  38. ($this->getTimeFormat() ? 'timeFormat: "' .
  39. $this->getTimeFormat() .
  40. '",' : '') .
  41. '
  42. dateFormat: "' .
  43. $this->getDateFormat() .
  44. '",
  45. buttonImage: "' .
  46. $this->getImage() .
  47. '",
  48. ' .
  49. ($calendarYearsRange ? 'yearRange: "' .
  50. $calendarYearsRange .
  51. '",' : '') .
  52. '
  53. buttonText: "' .
  54. (string)new \Magento\Framework\Phrase(
  55. 'Select Date'
  56. ) .
  57. '"' . ($maxDate ? ', maxDate: "' . $maxDate . '"' : '') .
  58. ($changeMonth === null ? '' : ', changeMonth: ' . $changeMonth) .
  59. ($changeYear === null ? '' : ', changeYear: ' . $changeYear) .
  60. ($showOn ? ', showOn: "' . $showOn . '"' : '') .
  61. ($firstDay ? ', firstDay: ' . $firstDay : '') .
  62. '})
  63. });
  64. </script>';
  65. return $html;
  66. }
  67. /**
  68. * Convert special characters to HTML entities
  69. *
  70. * @return string
  71. */
  72. public function getEscapedValue()
  73. {
  74. if ($this->getFormat() && $this->getValue()) {
  75. return strftime($this->getFormat(), strtotime($this->getValue()));
  76. }
  77. return htmlspecialchars($this->getValue());
  78. }
  79. /**
  80. * Produce and return block's html output
  81. *
  82. * {@inheritdoc}
  83. *
  84. * @return string
  85. */
  86. public function getHtml()
  87. {
  88. return $this->toHtml();
  89. }
  90. }