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