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
|
|
|
* NamedColumn constructor. |
36
|
|
|
* @param $name |
37
|
|
|
* @param $label string |
38
|
|
|
* @param $small string |
39
|
|
|
*/ |
40
|
|
|
public function __construct($name, $label = null, $small = null) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($label); |
43
|
|
|
$this->setName($name); |
44
|
|
|
$this->setSmall($small); |
45
|
|
|
|
46
|
|
|
$this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class()))); |
47
|
|
|
|
48
|
|
|
if ($this->orderable) { |
49
|
|
|
$this->setOrderable(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getName() |
57
|
|
|
{ |
58
|
|
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setName($name) |
67
|
|
|
{ |
68
|
|
|
$this->name = $name; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getSmall() |
77
|
|
|
{ |
78
|
|
|
if ($this->smallString) { |
79
|
|
|
return $this->small; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->getValueFromObject($this->getModel(), $this->small); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $small |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setSmall($small, $smallString = false) |
91
|
|
|
{ |
92
|
|
|
$this->small = $small; |
93
|
|
|
$this->smallString = $smallString; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function getModelValue() |
102
|
|
|
{ |
103
|
|
|
return $this->getValueFromObject($this->getModel(), $this->getName()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param OrderByClauseInterface|bool $orderable |
108
|
|
|
* @return TableColumn |
109
|
|
|
*/ |
110
|
|
|
public function setOrderable($orderable = true) |
111
|
|
|
{ |
112
|
|
|
if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { |
113
|
|
|
if (! is_string($orderable) && ! $orderable instanceof Closure) { |
|
|
|
|
114
|
|
|
$orderable = $this->getName(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return parent::setOrderable($orderable); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the instance as an array. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function toArray() |
127
|
|
|
{ |
128
|
|
|
return parent::toArray() + [ |
129
|
|
|
'name' => $this->getName(), |
130
|
|
|
'small' => htmlspecialchars($this->getSmall()), |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get column value from instance. |
136
|
|
|
* |
137
|
|
|
* @param Collection|Model|Closure $instance |
138
|
|
|
* @param string $name |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
protected function getValueFromObject($instance, $name) |
143
|
|
|
{ |
144
|
|
|
if ($name instanceof Closure) { |
|
|
|
|
145
|
|
|
return $name($instance); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* Implement json parsing |
150
|
|
|
*/ |
151
|
|
|
if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
152
|
|
|
$casts = collect($instance->getCasts()); |
|
|
|
|
153
|
|
|
$jsonParts = collect(explode('->', $name)); |
154
|
|
|
|
155
|
|
|
$jsonAttr = $instance->{$jsonParts->first()}; |
156
|
|
|
|
157
|
|
|
$cast = $casts->get($jsonParts->first(), false); |
158
|
|
|
|
159
|
|
|
if ($cast == 'object') { |
160
|
|
|
$jsonAttr = json_decode(json_encode($jsonAttr), true); |
161
|
|
|
} elseif ($cast != 'array') { |
162
|
|
|
$jsonAttr = json_decode($jsonAttr); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$parts = explode('.', $name); |
169
|
|
|
$part = array_shift($parts); |
170
|
|
|
|
171
|
|
|
if ($instance instanceof Collection) { |
172
|
|
|
$instance = $instance->pluck($part); |
173
|
|
|
} elseif ($instance instanceof SuportCollection) { |
|
|
|
|
174
|
|
|
$instance = $instance->first(); |
175
|
|
|
if ($instance instanceof Collection) { |
176
|
|
|
$instance = $instance->pluck($part); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($instance === null) { |
180
|
|
|
$instance = collect(); |
181
|
|
|
} |
182
|
|
|
} elseif (! is_null($instance)) { |
183
|
|
|
$instance = $instance->getAttribute($part); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (! empty($parts) && ! is_null($instance)) { |
187
|
|
|
return $this->getValueFromObject($instance, implode('.', $parts)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $instance; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|