1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\View\View; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use KodiComponents\Support\HtmlAttributes; |
9
|
|
|
use SleepingOwl\Admin\Contracts\Display\ControlButtonInterface; |
10
|
|
|
use SleepingOwl\Admin\Traits\Renderable; |
11
|
|
|
|
12
|
|
|
class ControlLink implements ControlButtonInterface |
13
|
|
|
{ |
14
|
|
|
use HtmlAttributes, Renderable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Closure |
18
|
|
|
*/ |
19
|
|
|
protected $url; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Closure |
23
|
|
|
*/ |
24
|
|
|
protected $attributeCondition; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $position; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|View |
33
|
|
|
*/ |
34
|
|
|
protected $view = 'column.control_link'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $text; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $hideText = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $icon; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Model |
53
|
|
|
*/ |
54
|
|
|
protected $model; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Closure |
58
|
|
|
*/ |
59
|
|
|
protected $condition; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Closure $url |
63
|
|
|
* @param string $text |
64
|
|
|
* @param int $position |
65
|
|
|
*/ |
66
|
|
|
public function __construct(Closure $url, $text, $position = 0) |
67
|
|
|
{ |
68
|
|
|
$this->url = $url; |
69
|
|
|
$this->position = (int) $position; |
70
|
|
|
$this->text = $text; |
71
|
|
|
|
72
|
|
|
$this->setHtmlAttributes([ |
73
|
|
|
'class' => 'btn btn-xs', |
74
|
|
|
'title' => $this->text, |
75
|
|
|
'data-toggle' => 'tooltip', |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool|mixed |
81
|
|
|
*/ |
82
|
|
|
public function isActive() |
83
|
|
|
{ |
84
|
|
|
return $this->condition ? call_user_func($this->condition, $this->model) : true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Model $model |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function getConditionAttributes(Model $model) |
92
|
|
|
{ |
93
|
|
|
$temp = $this->attributeCondition ? call_user_func($this->attributeCondition, $model) : []; |
94
|
|
|
|
95
|
|
|
if ($temp) { |
96
|
|
|
foreach ($temp as $key => $value) { |
97
|
|
|
$this->removeHtmlAttribute($key); |
98
|
|
|
$this->setHtmlAttribute($key, $value); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Closure $condition |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setCondition(Closure $condition) |
111
|
|
|
{ |
112
|
|
|
$this->condition = $condition; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set condition attribute. |
119
|
|
|
* @param Closure $condition |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setAttributeCondition(Closure $condition) |
124
|
|
|
{ |
125
|
|
|
$this->attributeCondition = $condition; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function hideText() |
134
|
|
|
{ |
135
|
|
|
$this->hideText = true; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
|
|
public function getPosition() |
144
|
|
|
{ |
145
|
|
|
return $this->position; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param Closure $closure |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
public function setUrl(Closure $closure) |
154
|
|
|
{ |
155
|
|
|
$this->url = $closure; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Model $model |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
public function getUrl(Model $model) |
166
|
|
|
{ |
167
|
|
|
return call_user_func($this->url, $model); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $text |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setText($text) |
176
|
|
|
{ |
177
|
|
|
$this->text = $text; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getIcon() |
186
|
|
|
{ |
187
|
|
|
return $this->icon; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $icon |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function setIcon($icon) |
196
|
|
|
{ |
197
|
|
|
$this->icon = $icon; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param Model $model |
204
|
|
|
* |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function setModel(Model $model) |
208
|
|
|
{ |
209
|
|
|
$this->model = $model; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return Model |
216
|
|
|
*/ |
217
|
|
|
public function getModel() |
218
|
|
|
{ |
219
|
|
|
return $this->model; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the instance as an array. |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public function toArray() |
228
|
|
|
{ |
229
|
|
|
return [ |
230
|
|
|
'attributes' => $this->getConditionAttributes($this->model)->htmlAttributesToString(), |
231
|
|
|
'url' => $this->getUrl($this->getModel()), |
232
|
|
|
'position' => $this->getPosition(), |
233
|
|
|
'text' => $this->text, |
234
|
|
|
'icon' => $this->getIcon(), |
235
|
|
|
'hideText' => $this->hideText, |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|