Date.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid\Column\Renderer;
  7. use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;
  8. /**
  9. * Backend grid item renderer date
  10. * @api
  11. * @deprecated 100.2.0 in favour of UI component implementation
  12. * @since 100.0.2
  13. */
  14. class Date extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
  15. {
  16. /**
  17. * @var int
  18. */
  19. protected $_defaultWidth = 160;
  20. /**
  21. * Date format string
  22. *
  23. * @var string
  24. */
  25. protected static $_format = null;
  26. /**
  27. * @var DateTimeFormatterInterface
  28. */
  29. protected $dateTimeFormatter;
  30. /**
  31. * @param \Magento\Backend\Block\Context $context
  32. * @param DateTimeFormatterInterface $dateTimeFormatter
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Backend\Block\Context $context,
  37. DateTimeFormatterInterface $dateTimeFormatter,
  38. array $data = []
  39. ) {
  40. parent::__construct($context, $data);
  41. $this->dateTimeFormatter = $dateTimeFormatter;
  42. }
  43. /**
  44. * Retrieve date format
  45. *
  46. * @return string
  47. * @deprecated 100.1.0
  48. */
  49. protected function _getFormat()
  50. {
  51. $format = $this->getColumn()->getFormat();
  52. if ($format === null) {
  53. if (self::$_format === null) {
  54. try {
  55. self::$_format = $this->_localeDate->getDateFormat(
  56. \IntlDateFormatter::MEDIUM
  57. );
  58. } catch (\Exception $e) {
  59. $this->_logger->critical($e);
  60. }
  61. }
  62. $format = self::$_format;
  63. }
  64. return $format;
  65. }
  66. /**
  67. * Renders grid column
  68. *
  69. * @param \Magento\Framework\DataObject $row
  70. * @return string
  71. */
  72. public function render(\Magento\Framework\DataObject $row)
  73. {
  74. $format = $this->getColumn()->getFormat();
  75. $date = $this->_getValue($row);
  76. if ($date) {
  77. if (!($date instanceof \DateTimeInterface)) {
  78. $date = new \DateTime($date);
  79. }
  80. return $this->_localeDate->formatDateTime(
  81. $date,
  82. $format ?: \IntlDateFormatter::MEDIUM,
  83. \IntlDateFormatter::NONE,
  84. null,
  85. $this->getColumn()->getTimezone() === false ? 'UTC' : null
  86. );
  87. }
  88. return $this->getColumn()->getDefault();
  89. }
  90. }