1
|
|
|
<?php namespace Arcanesoft\Core\Helpers\UI; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
4
|
|
|
use Illuminate\Support\HtmlString; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Label |
8
|
|
|
* |
9
|
|
|
* @package Arcanesoft\Core\Helpers\UI |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @TODO: Refactoring |
13
|
|
|
*/ |
14
|
|
|
class Label |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Main Methods |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* Generate active icon label. |
22
|
|
|
* |
23
|
|
|
* @param bool $active |
24
|
|
|
* @param array $options |
25
|
|
|
* @param bool $withTooltip |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Support\HtmlString |
28
|
|
|
*/ |
29
|
6 |
|
public static function activeIcon($active, array $options = [], $withTooltip = true) |
30
|
|
|
{ |
31
|
|
|
// TODO: Refactor the options to a dedicated config file. |
32
|
6 |
|
$classes = Arr::get($options, 'classes', [ |
33
|
6 |
|
'enabled' => 'label label-success', |
34
|
2 |
|
'disabled' => 'label label-default', |
35
|
2 |
|
]); |
36
|
6 |
|
$translations = Arr::get($options, 'trans', [ |
37
|
6 |
|
'enabled' => 'core::statuses.enabled', |
38
|
2 |
|
'disabled' => 'core::statuses.disabled', |
39
|
2 |
|
]); |
40
|
6 |
|
$icons = Arr::get($options, 'icons', [ |
41
|
6 |
|
'enabled' => 'fa fa-fw fa-check', |
42
|
2 |
|
'disabled' => 'fa fa-fw fa-ban', |
43
|
2 |
|
]); |
44
|
|
|
|
45
|
6 |
|
$key = $active ? 'enabled' : 'disabled'; |
46
|
6 |
|
$attributes = ['class' => $classes[$key]]; |
47
|
6 |
|
$value = static::generateIcon($icons[$key]); |
48
|
|
|
|
49
|
4 |
|
return $withTooltip |
50
|
4 |
|
? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes) |
51
|
6 |
|
: static::generate($value, $attributes); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Generate active status label. |
56
|
|
|
* |
57
|
|
|
* @param bool $active |
58
|
|
|
* @param array $options |
59
|
|
|
* @param bool $withIcon |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Support\HtmlString |
62
|
|
|
*/ |
63
|
|
|
public static function activeStatus($active, array $options = [], $withIcon) |
64
|
|
|
{ |
65
|
|
|
// TODO: Refactor the options to a dedicated config file. |
66
|
|
|
$classes = Arr::get($options, 'classes', [ |
67
|
|
|
'enabled' => 'label label-success', |
68
|
|
|
'disabled' => 'label label-default', |
69
|
|
|
]); |
70
|
|
|
$translations = Arr::get($options, 'trans', [ |
71
|
|
|
'enabled' => 'core::statuses.enabled', |
72
|
|
|
'disabled' => 'core::statuses.disabled', |
73
|
|
|
]); |
74
|
|
|
$icons = Arr::get($options, 'icons', [ |
75
|
|
|
'enabled' => 'fa fa-fw fa-check', |
76
|
|
|
'disabled' => 'fa fa-fw fa-ban', |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$key = $active ? 'enabled' : 'disabled'; |
80
|
|
|
$attributes = ['class' => $classes[$key]]; |
81
|
|
|
|
82
|
|
|
return $withIcon |
83
|
|
|
? static::generateWithIcon(ucfirst(trans($translations[$key])), $icons[$key], $attributes) |
84
|
|
|
: static::generate(ucfirst(trans($translations[$key])), $attributes); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Generate count label. |
89
|
|
|
* |
90
|
|
|
* @param int|float $count |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Support\HtmlString |
94
|
|
|
*/ |
95
|
9 |
|
public static function count($count, array $options = []) { |
96
|
|
|
|
97
|
|
|
// TODO: Refactor the options to a dedicated config file. |
98
|
9 |
|
$classes = Arr::get($options, 'classes', [ |
99
|
9 |
|
'positive' => 'label label-info', |
100
|
3 |
|
'zero' => 'label label-default', |
101
|
3 |
|
'negative' => 'label label-danger', |
102
|
3 |
|
]); |
103
|
|
|
|
104
|
9 |
|
return static::generate($count, [ |
105
|
9 |
|
'class' => $count > 0 ? $classes['positive'] : ($count < 0 ? $classes['negative'] : $classes['zero']) |
106
|
3 |
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Generate locked icon label. |
111
|
|
|
* |
112
|
|
|
* @param bool $locked |
113
|
|
|
* @param array $options |
114
|
|
|
* @param bool $withTooltip |
115
|
|
|
* |
116
|
|
|
* @return \Illuminate\Support\HtmlString |
117
|
|
|
*/ |
118
|
6 |
|
public static function lockedIcon($locked, array $options = [], $withTooltip = true) |
119
|
|
|
{ |
120
|
|
|
// TODO: Refactor the options to a dedicated config file. |
121
|
6 |
|
$classes = Arr::get($options, 'classes', [ |
122
|
6 |
|
'locked' => 'label label-danger', |
123
|
2 |
|
'unlocked' => 'label label-success', |
124
|
2 |
|
]); |
125
|
6 |
|
$translations = Arr::get($options, 'trans', [ |
126
|
6 |
|
'locked' => 'core::statuses.locked', |
127
|
2 |
|
'unlocked' => 'core::statuses.unlocked', |
128
|
2 |
|
]); |
129
|
6 |
|
$icons = Arr::get($options, 'icons', [ |
130
|
6 |
|
'locked' => 'fa fa-fw fa-lock', |
131
|
2 |
|
'unlocked' => 'fa fa-fw fa-unlock', |
132
|
2 |
|
]); |
133
|
|
|
|
134
|
6 |
|
$key = $locked ? 'locked' : 'unlocked'; |
135
|
6 |
|
$value = static::generateIcon($icons[$key]); |
136
|
6 |
|
$attributes = ['class' => $classes[$key]]; |
137
|
|
|
|
138
|
4 |
|
return $withTooltip |
139
|
4 |
|
? static::generateWithTooltip($value, ucfirst(trans($translations[$key])), $attributes) |
140
|
6 |
|
: static::generate($value, $attributes); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Generate locked status label. |
145
|
|
|
* |
146
|
|
|
* @param bool $locked |
147
|
|
|
* @param array $options |
148
|
|
|
* @param bool $withIcon |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Support\HtmlString |
151
|
|
|
*/ |
152
|
6 |
|
public static function lockedStatus($locked, array $options = [], $withIcon = true) |
153
|
|
|
{ |
154
|
|
|
// TODO: Refactor the options to a dedicated config file. |
155
|
6 |
|
$classes = Arr::get($options, 'classes', [ |
156
|
6 |
|
'locked' => 'label label-danger', |
157
|
2 |
|
'unlocked' => 'label label-success', |
158
|
2 |
|
]); |
159
|
6 |
|
$translations = Arr::get($options, 'trans', [ |
160
|
6 |
|
'locked' => 'core::statuses.locked', |
161
|
2 |
|
'unlocked' => 'core::statuses.unlocked', |
162
|
2 |
|
]); |
163
|
6 |
|
$icons = Arr::get($options, 'icons', [ |
164
|
6 |
|
'locked' => 'fa fa-fw fa-lock', |
165
|
2 |
|
'unlocked' => 'fa fa-fw fa-unlock', |
166
|
2 |
|
]); |
167
|
|
|
|
168
|
6 |
|
$key = $locked ? 'locked' : 'unlocked'; |
169
|
6 |
|
$value = ucfirst(trans($translations[$key])); |
170
|
6 |
|
$attributes = ['class' => $classes[$key]]; |
171
|
|
|
|
172
|
4 |
|
return $withIcon |
173
|
4 |
|
? static::generateWithIcon($value, $icons[$key], $attributes) |
174
|
6 |
|
: static::generate($value, $attributes); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Generate trashed icon label. |
179
|
|
|
* |
180
|
|
|
* @param array $options |
181
|
|
|
* @return \Illuminate\Support\HtmlString |
182
|
|
|
*/ |
183
|
6 |
|
public static function trashedStatus(array $options = [], $withIcon = true) |
184
|
|
|
{ |
185
|
6 |
|
$trans = Arr::get($options, 'trans', 'core::statuses.trashed'); |
186
|
6 |
|
$icon = Arr::get($options, 'icon', 'fa fa-fw fa-trash-o'); |
187
|
|
|
$attributes = [ |
188
|
6 |
|
'class' => Arr::get($options, 'class', 'label label-danger') |
189
|
2 |
|
]; |
190
|
|
|
|
191
|
4 |
|
return $withIcon |
192
|
4 |
|
? static::generateWithIcon(ucfirst(trans($trans)), $icon, $attributes) |
193
|
6 |
|
: static::generate(ucfirst(trans($trans)), $attributes); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/* ----------------------------------------------------------------- |
197
|
|
|
| Other Methods |
198
|
|
|
| ----------------------------------------------------------------- |
199
|
|
|
*/ |
200
|
|
|
/** |
201
|
|
|
* Generate the label. |
202
|
|
|
* |
203
|
|
|
* @param mixed $value |
204
|
|
|
* @param array $attributes |
205
|
|
|
* |
206
|
|
|
* @return \Illuminate\Support\HtmlString |
207
|
|
|
*/ |
208
|
33 |
|
protected static function generate($value, array $attributes = []) |
209
|
|
|
{ |
210
|
33 |
|
return new HtmlString( |
211
|
33 |
|
'<span'.html()->attributes($attributes).'>'.$value.'</span>' |
212
|
11 |
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Generate the label with tooltip. |
217
|
|
|
* |
218
|
|
|
* @param string $value |
219
|
|
|
* @param string $title |
220
|
|
|
* @param array $attributes |
221
|
|
|
* |
222
|
|
|
* @return \Illuminate\Support\HtmlString |
223
|
|
|
*/ |
224
|
6 |
|
protected static function generateWithTooltip($value, $title, array $attributes = []) |
225
|
|
|
{ |
226
|
6 |
|
$attributes['data-toggle'] = 'tooltip'; |
227
|
6 |
|
$attributes['data-original-title'] = $title; |
228
|
|
|
|
229
|
6 |
|
return static::generate($value, $attributes); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Generate the label with icon & text as value. |
234
|
|
|
* |
235
|
|
|
* @param string $value |
236
|
|
|
* @param string $icon |
237
|
|
|
* @param array $attributes |
238
|
|
|
* |
239
|
|
|
* @return \Illuminate\Support\HtmlString |
240
|
|
|
*/ |
241
|
6 |
|
protected static function generateWithIcon($value, $icon, array $attributes = []) |
242
|
|
|
{ |
243
|
6 |
|
return static::generate(static::generateIcon($icon). ' '.$value, $attributes); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Generate icon tag. |
248
|
|
|
* |
249
|
|
|
* @param string $class |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
18 |
|
protected static function generateIcon($class) |
254
|
|
|
{ |
255
|
18 |
|
return '<i class="'.$class.'"></i>'; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|