Date.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Listing\Columns;
  7. use Magento\Framework\Stdlib\BooleanUtils;
  8. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  9. use Magento\Framework\View\Element\UiComponentFactory;
  10. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  11. /**
  12. * Date format column
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Date extends Column
  18. {
  19. /**
  20. * @var TimezoneInterface
  21. */
  22. protected $timezone;
  23. /**
  24. * @var BooleanUtils
  25. */
  26. private $booleanUtils;
  27. /**
  28. * @param ContextInterface $context
  29. * @param UiComponentFactory $uiComponentFactory
  30. * @param TimezoneInterface $timezone
  31. * @param BooleanUtils $booleanUtils
  32. * @param array $components
  33. * @param array $data
  34. */
  35. public function __construct(
  36. ContextInterface $context,
  37. UiComponentFactory $uiComponentFactory,
  38. TimezoneInterface $timezone,
  39. BooleanUtils $booleanUtils,
  40. array $components = [],
  41. array $data = []
  42. ) {
  43. $this->timezone = $timezone;
  44. $this->booleanUtils = $booleanUtils;
  45. parent::__construct($context, $uiComponentFactory, $components, $data);
  46. }
  47. /**
  48. * @inheritdoc
  49. * @since 101.1.1
  50. */
  51. public function prepare()
  52. {
  53. $config = $this->getData('config');
  54. $config['filter'] = [
  55. 'filterType' => 'dateRange',
  56. 'templates' => [
  57. 'date' => [
  58. 'options' => [
  59. 'dateFormat' => $this->timezone->getDateFormatWithLongYear()
  60. ]
  61. ]
  62. ]
  63. ];
  64. $this->setData('config', $config);
  65. parent::prepare();
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function prepareDataSource(array $dataSource)
  71. {
  72. if (isset($dataSource['data']['items'])) {
  73. foreach ($dataSource['data']['items'] as & $item) {
  74. if (isset($item[$this->getData('name')])
  75. && $item[$this->getData('name')] !== "0000-00-00 00:00:00"
  76. ) {
  77. $date = $this->timezone->date(new \DateTime($item[$this->getData('name')]));
  78. $timezone = isset($this->getConfiguration()['timezone'])
  79. ? $this->booleanUtils->convert($this->getConfiguration()['timezone'])
  80. : true;
  81. if (!$timezone) {
  82. $date = new \DateTime($item[$this->getData('name')]);
  83. }
  84. $item[$this->getData('name')] = $date->format('Y-m-d H:i:s');
  85. }
  86. }
  87. }
  88. return $dataSource;
  89. }
  90. }