1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\ManageBlock;
13
14 use ICanBoogie\ActiveRecord\Query;
15 use ICanBoogie\DateTime;
16 use Icybee\ManageBlock;
17
18 19 20
21 class DateTimeColumn extends Column
22 {
23 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=array())
24 {
25 parent::__construct
26 (
27 $manager, $id, $options + array
28 (
29 'class' => 'date',
30 'default_order' => -1,
31 'discreet' => true
32 )
33 );
34 }
35
36 public function alter_query_with_filter(Query $query, $filter_value)
37 {
38 if ($filter_value)
39 {
40 $field = $this->id;
41
42 list($year, $month, $day) = explode('-', $filter_value) + array(0, 0, 0);
43
44 if ($year)
45 {
46 $query->where("YEAR(`$field`) = ?", (int) $year);
47 }
48
49 if ($month)
50 {
51 $query->where("MONTH(`$field`) = ?", (int) $month);
52 }
53
54 if ($day)
55 {
56 $query->where("DAY(`$field`) = ?", (int) $day);
57 }
58 }
59
60 return $query;
61 }
62
63 private $discreet_value;
64
65 66 67 68 69 70
71 public function render_cell($record)
72 {
73 $date = $record->{ $this->id };
74
75 if (!$date)
76 {
77 return;
78 }
79
80 if (!($date instanceof DateTime))
81 {
82 $date = new DateTime($date, 'utc');
83 }
84
85 if ($date->is_empty)
86 {
87 return;
88 }
89
90 if ($this->discreet && $this->discreet_value == $date->as_date)
91 {
92 $rendered_date = ManageBlock::DISCREET_PLACEHOLDER;
93 }
94 else
95 {
96 $rendered_date = $this->render_cell_date($date, $this->id);
97
98 $this->discreet_value = $date->as_date;
99 }
100
101 $rendered_time = $this->render_cell_time($date, $this->id);
102
103 return $rendered_date . ($rendered_time ? ' <span class="small light">' . $rendered_time . '</span>' : '');
104 }
105
106 107 108 109 110 111 112 113
114 protected function render_cell_time($date, $property)
115 {
116 return $date->local->format('H:i');
117 }
118
119 120 121 122 123 124 125 126
127 protected function render_cell_date($date, $property)
128 {
129 $tag = $property;
130
131 $year = $date->year;
132 $month = $date->month;
133 $day = $date->day;
134
135 $filtering = false;
136 $filter = null;
137
138 if ($this->manager->is_filtering($property))
139 {
140 $filtering = true;
141 $filter = $this->manager->options->filters[$property];
142 }
143
144 $parts = array
145 (
146 array($year, $year),
147 array($date->format('m'), $date->format('Y-m')),
148 array($date->format('d'), $date->as_date)
149 );
150
151 $today = new DateTime('now', 'utc');
152 $today_year = $today->year;
153 $today_month = $today->month;
154 $today_day = $today->day;
155 $today_formatted = $today->as_date;
156
157 $select = $parts[2][1];
158 $diff_days = $day - $today_day;
159
160 if ($year == $today_year && $month == $today_month && $day <= $today_day && $day > $today_day - 6)
161 {
162 $label = \ICanBoogie\I18n\date_period($date);
163 $label = ucfirst($label);
164
165 if ($filtering && $filter == $today_formatted)
166 {
167 $rc = $label;
168 }
169 else
170 {
171 $ttl = $this->manager->t('Display only: :identifier', array(':identifier' => $label));
172
173 $rc = <<<EOT
174 <a href="?$property=$select" title="$ttl" class="filter">$label</a>
175 EOT;
176 }
177 }
178 else
179 {
180 $rc = '';
181
182 foreach ($parts as $i => $part)
183 {
184 list($value, $select) = $part;
185
186 if ($filtering && $filter == $select)
187 {
188 $rc .= $value;
189 }
190 else
191 {
192 $ttl = $this->manager->t('Display only: :identifier', array(':identifier' => $select));
193
194 $rc .= <<<EOT
195 <a class="filter" href="?$property=$select" title="$ttl">$value</a>
196 EOT;
197 }
198
199 if ($i < 2)
200 {
201 $rc .= '–';
202 }
203 }
204 }
205
206 return $rc;
207 }
208 }
209
210 211 212
213 class DateColumn extends DateTimeColumn
214 {
215 protected function render_cell_time($date, $property)
216 {
217 return;
218 }
219 }