/
Column.php
executable file
·271 lines (235 loc) · 7.44 KB
/
Column.php
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
<?php
namespace Frozennode\Administrator\DataTable\Columns;
use Frozennode\Administrator\Validator;
use Frozennode\Administrator\Config\ConfigInterface;
use Illuminate\Database\DatabaseManager as DB;
use Illuminate\Database\Eloquent\Model;
class Column
{
/**
* The validator instance.
*
* @var \Frozennode\Administrator\Validator
*/
protected $validator;
/**
* The config instance.
*
* @var \Frozennode\Administrator\Config\ConfigInterface
*/
protected $config;
/**
* The config instance.
*
* @var \Illuminate\Database\DatabaseManager
*/
protected $db;
/**
* The options array.
*
* @var array
*/
protected $options;
/**
* The originally-supplied options array.
*
* @var array
*/
protected $suppliedOptions;
/**
* The default configuration options.
*
* @var array
*/
protected $baseDefaults = array(
'relationship' => false,
'sortable' => true,
'select' => false,
'output' => '(:value)',
'sort_field' => null,
'nested' => array(),
'is_related' => false,
'is_computed' => false,
'is_included' => false,
'external' => false,
'belongs_to_many' => false,
'visible' => true,
'raw_output' => false,
);
/**
* The specific defaults for subclasses to override.
*
* @var array
*/
protected $defaults = array();
/**
* The base rules that all fields need to pass.
*
* @var array
*/
protected $baseRules = array(
'column_name' => 'required|string',
'title' => 'string',
'relationship' => 'string',
'select' => 'required_with:relationship|string',
);
/**
* The specific rules for subclasses to override.
*
* @var array
*/
protected $rules = array();
/**
* The immediate relationship object for this column.
*
* @var Relationship
*/
protected $relationshipObject = null;
/**
* The table prefix.
*
* @var string
*/
protected $tablePrefix = '';
/**
* Create a new action Factory instance.
*
* @param \Frozennode\Administrator\Validator $validator
* @param \Frozennode\Administrator\Config\ConfigInterface $config
* @param \Illuminate\Database\DatabaseManager $db
* @param array $options
*/
public function __construct(Validator $validator, ConfigInterface $config, DB $db, array $options)
{
$this->config = $config;
$this->validator = $validator;
$this->db = $db;
$this->suppliedOptions = $options;
}
/**
* Validates the supplied options.
*/
public function validateOptions()
{
//override the config
$this->validator->override($this->suppliedOptions, $this->getRules());
//if the validator failed, throw an exception
if ($this->validator->fails()) {
throw new \InvalidArgumentException("There are problems with your '".$this->suppliedOptions['column_name']."' column in the ".
$this->config->getOption('name').' model: '.implode('. ', $this->validator->messages()->all()));
}
}
/**
* Builds the necessary fields on the object.
*/
public function build()
{
$model = $this->config->getDataModel();
$options = $this->suppliedOptions;
$this->tablePrefix = $this->db->getTablePrefix();
//set some options-based defaults
$options['title'] = $this->validator->arrayGet($options, 'title', $options['column_name']);
$options['sort_field'] = $this->validator->arrayGet($options, 'sort_field', $options['column_name']);
//if the supplied item is an accessor, make this unsortable for the moment
if (method_exists($model, \Str::camel('get_'.$options['column_name'].'_attribute')) && $options['column_name'] === $options['sort_field']) {
$options['sortable'] = false;
}
//however, if this is not a relation and the select option was supplied, str_replace the select option and make it sortable again
if ($select = $this->validator->arrayGet($options, 'select')) {
$options['select'] = str_replace('(:table)', $this->tablePrefix.$model->getTable(), $select);
}
//now we do some final organization to categorize these columns (useful later in the sorting)
if (method_exists($model, \Str::camel('get_'.$options['column_name'].'_attribute')) || $select) {
$options['is_computed'] = true;
} else {
$options['is_included'] = true;
}
//run the visible property closure if supplied
$visible = $this->validator->arrayGet($options, 'visible');
if (is_callable($visible)) {
$options['visible'] = $visible($this->config->getDataModel()) ? true : false;
}
$this->suppliedOptions = $options;
}
/**
* Adds selects to a query.
*
* @param array $selects
*/
public function filterQuery(&$selects)
{
if ($select = $this->getOption('select')) {
$selects[] = $this->db->raw($select.' AS '.$this->db->getQueryGrammar()->wrap($this->getOption('column_name')));
}
}
/**
* Gets all user options.
*
* @return array
*/
public function getOptions()
{
//make sure the supplied options have been merged with the defaults
if (empty($this->options)) {
//validate the options and build them
$this->validateOptions();
$this->build();
$this->options = array_merge($this->getDefaults(), $this->suppliedOptions);
}
return $this->options;
}
/**
* Gets a field's option.
*
* @param string $key
*
* @return mixed
*/
public function getOption($key)
{
$options = $this->getOptions();
if (!array_key_exists($key, $options)) {
throw new \InvalidArgumentException("An invalid option was searched for in the '".$options['column_name']."' column");
}
return $options[$key];
}
/**
* Takes a column output string and renders the column with it (replacing '(:value)' with the column's field value).
*
* @param $value string $value
* @param \Illuminate\Database\Eloquent\Model $item
*
* @return string
*/
public function renderOutput($value, $item = null)
{
$output = $this->getOption('output');
// default is xss secured untill u open `raw_output` option
// e() is laravel blade `{{ }}` for printing data
if ( ! $this->getOption('raw_output')) {
$value = e($value);
}
if (is_callable($output)) {
return $output($value, $item);
}
return str_replace('(:value)', $value, $output);
}
/**
* Gets all rules.
*
* @return array
*/
public function getRules()
{
return array_merge($this->baseRules, $this->rules);
}
/**
* Gets all default values.
*
* @return array
*/
public function getDefaults()
{
return array_merge($this->baseDefaults, $this->defaults);
}
}