This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathDateTimeColumn.php
More file actions
276 lines (236 loc) · 6.88 KB
/
DateTimeColumn.php
File metadata and controls
276 lines (236 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sg\DatatablesBundle\Datatable\Column;
use Sg\DatatablesBundle\Datatable\Filter\TextFilter;
use Sg\DatatablesBundle\Datatable\Editable\EditableInterface;
use Sg\DatatablesBundle\Datatable\Helper;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Exception;
/**
* Class DateTimeColumn
*
* @package Sg\DatatablesBundle\Datatable\Column
*/
class DateTimeColumn extends AbstractColumn
{
/**
* This Column is editable.
*/
use EditableTrait;
/**
* The Column is filterable.
*/
use FilterableTrait;
/**
* Moment.js date format.
* Default: 'lll'
*
* @link http://momentjs.com/
*
* @var string
*/
protected $dateFormat;
/**
* Use the time ago format.
* Default: false
*
* @var bool
*/
protected $timeago;
//-------------------------------------------------
// ColumnInterface
//-------------------------------------------------
/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
{
$path = Helper::getDataPropertyPath($this->data);
if (true === $this->isEditableContentRequired($row)) {
$content = $this->renderTemplate($this->accessor->getValue($row, $path), $row[$this->editable->getPk()]);
} else {
try {
$content = $this->renderTemplate($this->accessor->getValue($row, $path));
}
catch (\Exception $ex) {
$content = $this->renderTemplate(null);
}
}
try {
$this->accessor->setValue($row, $path, $content);
}
catch (\Exception $ex) {
}
return $this;
}
/**
* {@inheritdoc}
*/
public function renderToMany(array &$row)
{
$value = null;
$path = Helper::getDataPropertyPath($this->data, $value);
$entries = $this->accessor->getValue($row, $path);
if (count($entries) > 0) {
foreach ($entries as $key => $entry) {
$currentPath = $path.'['.$key.']'.$value;
$currentObjectPath = Helper::getPropertyPathObjectNotation($path, $key, $value);
if (true === $this->isEditableContentRequired($row)) {
$content = $this->renderTemplate(
$this->accessor->getValue($row, $currentPath),
$row[$this->editable->getPk()],
$currentObjectPath
);
} else {
$content = $this->renderTemplate($this->accessor->getValue($row, $currentPath));
}
$this->accessor->setValue($row, $currentPath, $content);
}
} else {
// no placeholder - leave this blank
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getCellContentTemplate()
{
return 'SgDatatablesBundle:render:datetime.html.twig';
}
/**
* {@inheritdoc}
*/
public function renderPostCreateDatatableJsContent()
{
if ($this->editable instanceof EditableInterface) {
return $this->twig->render(
'SgDatatablesBundle:column:column_post_create_dt.js.twig',
array(
'column_class_editable_selector' => $this->getColumnClassEditableSelector(),
'editable_options' => $this->editable,
'entity_class_name' => $this->getEntityClassName(),
'column_dql' => $this->dql,
'original_type_of_field' => $this->getOriginalTypeOfField(),
)
);
}
return null;
}
//-------------------------------------------------
// Options
//-------------------------------------------------
/**
* Config options.
*
* @param OptionsResolver $resolver
*
* @return $this
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults(array(
'date_format' => 'lll',
'timeago' => false,
'filter' => array(TextFilter::class, array()),
'editable' => null,
));
$resolver->setAllowedTypes('date_format', 'string');
$resolver->setAllowedTypes('timeago', 'bool');
$resolver->setAllowedTypes('filter', 'array');
$resolver->setAllowedTypes('editable', array('null', 'array'));
return $this;
}
//-------------------------------------------------
// Getters && Setters
//-------------------------------------------------
/**
* Get date format.
*
* @return string
*/
public function getDateFormat()
{
return $this->dateFormat;
}
/**
* Set date format.
*
* @param string $dateFormat
*
* @return $this
* @throws Exception
*/
public function setDateFormat($dateFormat)
{
if (empty($dateFormat) || !is_string($dateFormat)) {
throw new Exception('DateTimeColumn::setDateFormat(): A non-empty string is expected.');
}
$this->dateFormat = $dateFormat;
return $this;
}
/**
* Get timeago.
*
* @return bool
*/
public function isTimeago()
{
return $this->timeago;
}
/**
* Set timeago.
*
* @param bool $timeago
*
* @return $this
*/
public function setTimeago($timeago)
{
$this->timeago = $timeago;
return $this;
}
//-------------------------------------------------
// Helper
//-------------------------------------------------
/**
* Render template.
*
* @param string|null $data
* @param string|null $pk
* @param string|null $path
*
* @return mixed|string
*/
private function renderTemplate($data, $pk = null, $path = null)
{
$renderVars = array(
'data' => $data,
'default_content' => $this->getDefaultContent(),
'date_format' => $this->dateFormat,
'timeago' => $this->timeago,
'datatable_name' => $this->getDatatableName(),
'row_id' => Helper::generateUniqueID(),
);
// editable vars
if (null !== $pk) {
$renderVars = array_merge($renderVars, array(
'column_class_editable_selector' => $this->getColumnClassEditableSelector(),
'pk' => $pk,
'path' => $path,
));
}
return $this->twig->render(
$this->getCellContentTemplate(),
$renderVars
);
}
}