1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Sco\Admin\Contracts\Form\Elements\ElementInterface; |
8
|
|
|
|
9
|
|
|
abstract class Element implements ElementInterface |
10
|
|
|
{ |
11
|
|
|
protected $type; |
12
|
|
|
|
13
|
|
|
protected $name; |
14
|
|
|
protected $title; |
15
|
|
|
|
16
|
|
|
protected $disabled = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $defaultValue; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
25
|
|
|
*/ |
26
|
|
|
protected $model; |
27
|
|
|
|
28
|
|
|
public function __construct($name, $title) |
29
|
|
|
{ |
30
|
|
|
$this->name = $name; |
31
|
|
|
$this->title = $title; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getName() |
35
|
|
|
{ |
36
|
|
|
return $this->name; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setName($value) |
40
|
|
|
{ |
41
|
|
|
$this->name = $value; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getTitle() |
47
|
|
|
{ |
48
|
|
|
return $this->title; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setTitle($value) |
52
|
|
|
{ |
53
|
|
|
$this->title = $value; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function save(Request $request) |
59
|
|
|
{ |
60
|
|
|
$this->setModelAttribute( |
61
|
|
|
$this->getValueFromRequest($request) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function setModelAttribute($value) |
66
|
|
|
{ |
67
|
|
|
$model = $this->getModel(); |
68
|
|
|
$model->setAttribute( |
69
|
|
|
$this->getName(), |
70
|
|
|
$this->prepareValue($value) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function getValueFromRequest(Request $request) |
75
|
|
|
{ |
76
|
|
|
return $request->input($this->getName()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $value |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
protected function prepareValue($value) |
85
|
|
|
{ |
86
|
|
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getModel() |
90
|
|
|
{ |
91
|
|
|
return $this->model; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setModel(Model $model) |
95
|
|
|
{ |
96
|
|
|
$this->model = $model; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getValue() |
105
|
|
|
{ |
106
|
|
|
return $this->getValueFromModel(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getValueFromModel() |
110
|
|
|
{ |
111
|
|
|
$model = $this->getModel(); |
112
|
|
|
$value = $this->getDefaultValue(); |
113
|
|
|
if (is_null($model) || !$model->exists) { |
114
|
|
|
return $value; |
115
|
|
|
} |
116
|
|
|
return $model->getAttribute($this->getName()); |
117
|
|
|
|
118
|
|
|
/*$relations = explode('.', $this->getName(), 2); |
|
|
|
|
119
|
|
|
$count = count($relations); |
120
|
|
|
|
121
|
|
|
if ($count == 1) { |
122
|
|
|
return $model->getAttribute($this->getName()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach ($relations as $relation) { |
126
|
|
|
if ($model->{$relation} instanceof Model) { |
127
|
|
|
$model = $model->{$relation}; |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $model->getAttribute($relation); |
132
|
|
|
}*/ |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function getDefaultValue() |
136
|
|
|
{ |
137
|
|
|
return $this->defaultValue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function isDisabled() |
141
|
|
|
{ |
142
|
|
|
return $this->disabled; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function setDisabled() |
146
|
|
|
{ |
147
|
|
|
$this->disabled = true; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function toArray() |
153
|
|
|
{ |
154
|
|
|
return [ |
155
|
|
|
'key' => $this->name, |
156
|
|
|
'title' => $this->title, |
157
|
|
|
'type' => $this->type, |
158
|
|
|
'disabled' => $this->isDisabled(), |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function jsonSerialize() |
163
|
|
|
{ |
164
|
|
|
return $this->toArray(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function toJson($options = 0) |
168
|
|
|
{ |
169
|
|
|
return json_encode($this->jsonSerialize(), $options); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function __toString() |
173
|
|
|
{ |
174
|
|
|
return $this->toJson(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.