1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\Components\Columns;
13
14 15 16 17 18 19 20 21 22
23 class Date extends Editable
24 {
25 const FORMAT_TEXT = 'd M Y';
26 const FORMAT_DATE = 'd.m.Y';
27 const FORMAT_DATETIME = 'd.m.Y H:i:s';
28
29
30 protected $dateFormat = self::FORMAT_DATE;
31
32 33 34 35 36 37
38 public function __construct($grid, $name, $label, $dateFormat = NULL)
39 {
40 parent::__construct($grid, $name, $label);
41
42 if ($dateFormat !== NULL) {
43 $this->dateFormat = $dateFormat;
44 }
45 }
46
47 48 49 50
51 public function setDateFormat($format)
52 {
53 $this->dateFormat = $format;
54 return $this;
55 }
56
57 58 59
60 public function getDateFormat()
61 {
62 return $this->dateFormat;
63 }
64
65 66 67 68
69 protected function formatValue($value)
70 {
71 if ($value === NULL || is_bool($value)) {
72 return $this->applyReplacement($value);
73 } elseif (is_scalar($value)) {
74 $value = \Nette\Templating\Helpers::escapeHtml($value);
75 $replaced = $this->applyReplacement($value);
76 if ($value !== $replaced && is_scalar($replaced)) {
77 return $replaced;
78 }
79 }
80
81 return $value instanceof \DateTime
82 ? $value->format($this->dateFormat)
83 : date($this->dateFormat, is_numeric($value) ? $value : strtotime($value));
84 }
85
86 87 88 89 90
91 public function renderExport($row)
92 {
93 if (is_callable($this->customRenderExport)) {
94 return callback($this->customRenderExport)->invokeArgs(array($row));
95 }
96
97 $value = $this->getValue($row);
98 return $this->formatValue($value);
99 }
100 }
101