|
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 SleepingOwl\Admin\Traits\Renderable; |
|
9
|
|
|
use KodiComponents\Support\HtmlAttributes; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\ControlButtonInterface; |
|
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 $class = 'btn btn-xs'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Closure|string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $text; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Closure|string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $title; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var bool |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $hideText = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $hideTitle = false; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $icon; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var Model |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $model; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var Closure |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $condition; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Closure $url |
|
78
|
|
|
* @param Closure|string $text |
|
79
|
|
|
* @param int $position |
|
80
|
|
|
* @param string $title |
|
81
|
|
|
* @param string $class |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct(Closure $url, $text, $position = 0, $title = null, $class = null) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->url = $url; |
|
86
|
|
|
$this->position = (int) $position; |
|
87
|
|
|
$this->text = $text; |
|
88
|
|
|
|
|
89
|
|
|
if ($title !== null) { |
|
90
|
|
|
$this->setTitle($title); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($class !== null) { |
|
94
|
|
|
$this->setClass($class); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return bool|mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isActive() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->condition ? call_user_func($this->condition, $this->model) : true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param Model $model |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getConditionAttributes(Model $model) |
|
111
|
|
|
{ |
|
112
|
|
|
$temp = $this->attributeCondition ? call_user_func($this->attributeCondition, $model) : []; |
|
113
|
|
|
|
|
114
|
|
|
if ($temp) { |
|
115
|
|
|
foreach ($temp as $key => $value) { |
|
116
|
|
|
$this->removeHtmlAttribute($key); |
|
117
|
|
|
$this->setHtmlAttribute($key, $value); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param Closure $condition |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setCondition(Closure $condition) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->condition = $condition; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set condition attribute. |
|
138
|
|
|
* @param Closure $condition |
|
139
|
|
|
* |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setAttributeCondition(Closure $condition) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->attributeCondition = $condition; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function hideText() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->hideText = true; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
|
|
public function hideTitle() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->hideTitle = true; |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getPosition() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->position; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param Closure $closure |
|
179
|
|
|
* |
|
180
|
|
|
* @return $this |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setUrl(Closure $closure) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->url = $closure; |
|
185
|
|
|
|
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param Model $model |
|
191
|
|
|
* |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getUrl(Model $model) |
|
195
|
|
|
{ |
|
196
|
|
|
return call_user_func($this->url, $model); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param Model|null $model |
|
201
|
|
|
* |
|
202
|
|
|
* @return mixed |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getText($model = null) |
|
205
|
|
|
{ |
|
206
|
|
|
$text = (is_callable($this->text) && is_object($model)) ? call_user_func($this->text, $model) : $this->text; |
|
207
|
|
|
|
|
208
|
|
|
$title = null; |
|
209
|
|
|
if (is_callable($this->title) && is_object($model)) { |
|
210
|
|
|
$title = call_user_func($this->title, $model); |
|
211
|
|
|
} elseif ($this->title !== null) { |
|
212
|
|
|
$title = $this->title; |
|
213
|
|
|
} else { |
|
214
|
|
|
$title = $text; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if (! $this->hideTitle) { |
|
218
|
|
|
$this->setHtmlAttributes([ |
|
219
|
|
|
'title' => $title, |
|
220
|
|
|
'data-toggle' => 'tooltip', |
|
221
|
|
|
]); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$this->setHtmlAttributes([ |
|
225
|
|
|
'class' => $this->class, |
|
226
|
|
|
]); |
|
227
|
|
|
|
|
228
|
|
|
return $text; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param Closure|string $text |
|
233
|
|
|
* |
|
234
|
|
|
* @return $this |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setText($text) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->text = $text; |
|
239
|
|
|
|
|
240
|
|
|
return $this; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param Closure|string $title |
|
245
|
|
|
* |
|
246
|
|
|
* @return $this |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setTitle($title) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->title = $title; |
|
251
|
|
|
|
|
252
|
|
|
return $this; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @return string |
|
257
|
|
|
*/ |
|
258
|
|
|
public function getIcon() |
|
259
|
|
|
{ |
|
260
|
|
|
return $this->icon; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @param string $icon |
|
265
|
|
|
* |
|
266
|
|
|
* @return $this |
|
267
|
|
|
*/ |
|
268
|
|
|
public function setIcon($icon) |
|
269
|
|
|
{ |
|
270
|
|
|
$this->icon = $icon; |
|
271
|
|
|
|
|
272
|
|
|
return $this; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @return string |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getClass() |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->class; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @param string $class |
|
285
|
|
|
* |
|
286
|
|
|
* @return $this |
|
287
|
|
|
*/ |
|
288
|
|
|
public function setClass($class) |
|
289
|
|
|
{ |
|
290
|
|
|
$this->class = $class; |
|
291
|
|
|
|
|
292
|
|
|
return $this; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param Model $model |
|
297
|
|
|
* |
|
298
|
|
|
* @return $this |
|
299
|
|
|
*/ |
|
300
|
|
|
public function setModel(Model $model) |
|
301
|
|
|
{ |
|
302
|
|
|
$this->model = $model; |
|
303
|
|
|
|
|
304
|
|
|
return $this; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @return Model |
|
309
|
|
|
*/ |
|
310
|
|
|
public function getModel() |
|
311
|
|
|
{ |
|
312
|
|
|
return $this->model; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Get the instance as an array. |
|
317
|
|
|
* |
|
318
|
|
|
* @return array |
|
319
|
|
|
*/ |
|
320
|
|
|
public function toArray() |
|
321
|
|
|
{ |
|
322
|
|
|
return [ |
|
323
|
|
|
'text' => $this->getText($this->getModel()), |
|
324
|
|
|
'attributes' => $this->getConditionAttributes($this->model)->htmlAttributesToString(), |
|
325
|
|
|
'url' => $this->getUrl($this->getModel()), |
|
326
|
|
|
'position' => $this->getPosition(), |
|
327
|
|
|
'icon' => $this->getIcon(), |
|
328
|
|
|
'hideText' => $this->hideText, |
|
329
|
|
|
]; |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|