1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Column; |
5
|
|
|
|
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Sco\Attributes\HasOriginalAndAttributesTrait; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
abstract class Column |
14
|
|
|
{ |
15
|
|
|
use HasOriginalAndAttributesTrait; |
16
|
|
|
|
17
|
|
|
protected $defaultsAttributes = [ |
18
|
|
|
'title' => '', |
19
|
|
|
'sortable' => false, |
20
|
|
|
'width' => 0, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
protected $defaults = []; |
26
|
|
|
|
27
|
|
|
protected $configFactory; |
28
|
|
|
|
29
|
|
|
protected $model; |
30
|
|
|
|
31
|
|
|
public function __construct($name, $attributes) |
32
|
|
|
{ |
33
|
|
|
//$this->configFactory = $factory; |
|
|
|
|
34
|
|
|
$this->name = $name; |
35
|
|
|
$this->setAttribute(array_merge($this->getDefaults(), $attributes)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function getDefaults() |
39
|
|
|
{ |
40
|
|
|
return array_merge($this->defaultsAttributes, $this->defaults); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getName() |
44
|
|
|
{ |
45
|
|
|
return $this->name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getModel() |
49
|
|
|
{ |
50
|
|
|
return $this->model; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setModel(Model $model) |
54
|
|
|
{ |
55
|
|
|
$this->model = $model; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function toArray() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'key' => $this->name, |
63
|
|
|
'title' => $this->getAttribute('title'), |
64
|
|
|
'sortable' => $this->getAttribute('sortable'), |
65
|
|
|
'width' => $this->getAttribute('width'), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function isRelationship() |
70
|
|
|
{ |
71
|
|
|
return $this->hasAttribute('relationship'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function getRelationValue() |
75
|
|
|
{ |
76
|
|
|
$model = $this->getModel(); |
77
|
|
|
$relation = $model->{$this->getAttribute('relationship')}(); |
78
|
|
|
$fields = $this->getAttribute('fields', $this->name); |
79
|
|
|
$results = $relation->getResults(); |
80
|
|
|
if (strpos($fields, ',') === false) { |
81
|
|
|
if ($results instanceof Model) { |
82
|
|
|
return $results->{$fields}; |
83
|
|
|
} |
84
|
|
|
if ($results instanceof Collection) { |
85
|
|
|
return $results->pluck($fields); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$values = collect(); |
90
|
|
|
$fields = is_array($fields) ? $fields : explode(',', $fields); |
91
|
|
|
|
92
|
|
|
if ($results instanceof Model) { |
93
|
|
|
foreach ($fields as $field) { |
94
|
|
|
$values->put($field, $results->{$field}); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($results instanceof Collection) { |
99
|
|
|
$values = $results->map(function ($item) use ($fields) { |
100
|
|
|
$map = collect(); |
101
|
|
|
foreach ($fields as $field) { |
102
|
|
|
$map->put($field, $item->{$field}); |
103
|
|
|
} |
104
|
|
|
return $map; |
105
|
|
|
}); |
106
|
|
|
} |
107
|
|
|
return $values; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function render() |
111
|
|
|
{ |
112
|
|
|
if (isset($this->getModel()->{$this->name})) { |
113
|
|
|
$value = $this->getModel()->{$this->name}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($this->isRelationship()) { |
117
|
|
|
$value = $this->getRelationValue(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($value instanceof Carbon) { |
121
|
|
|
if ($this->hasAttribute('format')) { |
122
|
|
|
if ($this->getAttribute('format') == 'humans') { |
123
|
|
|
$value = $value->diffForHumans(); |
|
|
|
|
124
|
|
|
} else { |
125
|
|
|
$value = $value->format($this->getAttribute('format')); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function __toString() |
134
|
|
|
{ |
135
|
|
|
return $this->toJson(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.