1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use SleepingOwl\Admin\Display\TableColumn; |
9
|
|
|
use Illuminate\Database\Eloquent\Collection; |
10
|
|
|
use Illuminate\Support\Collection as SuportCollection; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; |
13
|
|
|
|
14
|
|
|
abstract class NamedColumn extends TableColumn implements NamedColumnInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Column field name. |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $small; |
27
|
|
|
protected $smallString = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $orderable = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $isolated = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $isSearchable = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* NamedColumn constructor. |
46
|
|
|
* @param $name |
47
|
|
|
* @param $label string |
48
|
|
|
* @param $small string |
49
|
|
|
*/ |
50
|
|
|
public function __construct($name, $label = null, $small = null) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($label); |
53
|
|
|
$this->setName($name); |
54
|
|
|
$this->setSmall($small); |
55
|
|
|
|
56
|
|
|
$this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class()))); |
57
|
|
|
|
58
|
|
|
if ($this->orderable) { |
59
|
|
|
$this->setOrderable(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getName() |
67
|
|
|
{ |
68
|
|
|
return $this->name; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setName($name) |
77
|
|
|
{ |
78
|
|
|
$this->name = $name; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getSmall() |
87
|
|
|
{ |
88
|
|
|
if ($this->smallString) { |
89
|
|
|
return $this->small; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->getValueFromObject($this->getModel(), $this->small); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $small |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setSmall($small, $smallString = false) |
101
|
|
|
{ |
102
|
|
|
$this->small = $small; |
103
|
|
|
$this->smallString = $smallString; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function getModelValue() |
112
|
|
|
{ |
113
|
|
|
return $this->getValueFromObject($this->getModel(), $this->getName()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param OrderByClauseInterface|bool $orderable |
118
|
|
|
* @return TableColumn |
119
|
|
|
*/ |
120
|
|
|
public function setOrderable($orderable = true) |
121
|
|
|
{ |
122
|
|
|
if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { |
123
|
|
|
if (! is_string($orderable) && ! $orderable instanceof Closure) { |
|
|
|
|
124
|
|
|
$orderable = $this->getName(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return parent::setOrderable($orderable); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param bool $isolated |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setIsolated($isolated) |
137
|
|
|
{ |
138
|
|
|
$this->isolated = $isolated; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getIsolated() |
147
|
|
|
{ |
148
|
|
|
return $this->isolated; |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the instance as an array. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function toArray() |
157
|
|
|
{ |
158
|
|
|
$model_value_small = $this->getSmall(); |
159
|
|
|
if ($this->isolated) { |
160
|
|
|
$model_value_small = htmlspecialchars($model_value_small); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return parent::toArray() + [ |
164
|
|
|
'name' => $this->getName(), |
165
|
|
|
'small' => $model_value_small, |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get column value from instance. |
171
|
|
|
* |
172
|
|
|
* @param Collection|Model|Closure $instance |
173
|
|
|
* @param string $name |
174
|
|
|
* |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
protected function getValueFromObject($instance, $name) |
178
|
|
|
{ |
179
|
|
|
if ($name instanceof Closure) { |
|
|
|
|
180
|
|
|
return $name($instance); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/* |
184
|
|
|
* Implement json parsing |
185
|
|
|
*/ |
186
|
|
|
if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
187
|
|
|
$casts = collect($instance->getCasts()); |
|
|
|
|
188
|
|
|
$jsonParts = collect(explode('->', $name)); |
189
|
|
|
|
190
|
|
|
$jsonAttr = $instance->{$jsonParts->first()}; |
191
|
|
|
|
192
|
|
|
$cast = $casts->get($jsonParts->first(), false); |
193
|
|
|
|
194
|
|
|
if ($cast == 'object') { |
195
|
|
|
$jsonAttr = json_decode(json_encode($jsonAttr), true); |
196
|
|
|
} elseif ($cast != 'array') { |
197
|
|
|
$jsonAttr = json_decode($jsonAttr); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$parts = explode('.', $name); |
204
|
|
|
$part = array_shift($parts); |
205
|
|
|
|
206
|
|
|
if ($instance instanceof Collection) { |
207
|
|
|
$instance = $instance->pluck($part); |
208
|
|
|
} elseif ($instance instanceof SuportCollection) { |
|
|
|
|
209
|
|
|
$instance = $instance->first(); |
210
|
|
|
if ($instance instanceof Collection) { |
211
|
|
|
$instance = $instance->pluck($part); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if ($instance === null) { |
215
|
|
|
$instance = collect(); |
216
|
|
|
} |
217
|
|
|
} elseif (! is_null($instance)) { |
218
|
|
|
$instance = $instance->getAttribute($part); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (! empty($parts) && ! is_null($instance)) { |
222
|
|
|
return $this->getValueFromObject($instance, implode('.', $parts)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $instance; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|