1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\MessageBag; |
4
|
|
|
|
5
|
|
|
if (!function_exists('admin_path')) { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Get admin path. |
9
|
|
|
* |
10
|
|
|
* @param string $path |
11
|
|
|
* |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
|
|
function admin_path($path = '') |
15
|
|
|
{ |
16
|
|
|
return ucfirst(config('admin.directory')).($path ? DIRECTORY_SEPARATOR.$path : $path); |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (!function_exists('admin_url')) { |
21
|
|
|
/** |
22
|
|
|
* Get admin url. |
23
|
|
|
* |
24
|
|
|
* @param string $path |
25
|
|
|
* @param mixed $parameters |
26
|
|
|
* @param bool $secure |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
function admin_url($path = '', $parameters = [], $secure = null) |
31
|
|
|
{ |
32
|
|
|
if (\Illuminate\Support\Facades\URL::isValidUrl($path)) { |
33
|
|
|
return $path; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$secure = $secure ?: (config('admin.https') || config('admin.secure')); |
37
|
|
|
|
38
|
|
|
return url(admin_base_path($path), $parameters, $secure); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!function_exists('admin_base_path')) { |
43
|
|
|
/** |
44
|
|
|
* Get admin url. |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
function admin_base_path($path = '') |
51
|
|
|
{ |
52
|
|
|
$prefix = '/'.trim(config('admin.route.prefix'), '/'); |
53
|
|
|
|
54
|
|
|
$prefix = ($prefix == '/') ? '' : $prefix; |
55
|
|
|
|
56
|
|
|
$path = trim($path, '/'); |
57
|
|
|
|
58
|
|
|
if (is_null($path) || strlen($path) == 0) { |
59
|
|
|
return $prefix ?: '/'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $prefix.'/'.$path; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
if (!function_exists('admin_toastr')) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Flash a toastr message bag to session. |
70
|
|
|
* |
71
|
|
|
* @param string $message |
72
|
|
|
* @param string $type |
73
|
|
|
* @param array $options |
74
|
|
|
*/ |
75
|
|
|
function admin_toastr($message = '', $type = 'success', $options = []) |
76
|
|
|
{ |
77
|
|
|
$toastr = new MessageBag(get_defined_vars()); |
78
|
|
|
|
79
|
|
|
session()->flash('toastr', $toastr); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!function_exists('admin_success')) { |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Flash a success message bag to session. |
87
|
|
|
* |
88
|
|
|
* @param string $title |
89
|
|
|
* @param string $message |
90
|
|
|
*/ |
91
|
|
|
function admin_success($title, $message = '') |
92
|
|
|
{ |
93
|
|
|
admin_info($title, $message, 'success'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!function_exists('admin_error')) { |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Flash a error message bag to session. |
101
|
|
|
* |
102
|
|
|
* @param string $title |
103
|
|
|
* @param string $message |
104
|
|
|
*/ |
105
|
|
|
function admin_error($title, $message = '') |
106
|
|
|
{ |
107
|
|
|
admin_info($title, $message, 'error'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!function_exists('admin_warning')) { |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Flash a warning message bag to session. |
115
|
|
|
* |
116
|
|
|
* @param string $title |
117
|
|
|
* @param string $message |
118
|
|
|
*/ |
119
|
|
|
function admin_warning($title, $message = '') |
120
|
|
|
{ |
121
|
|
|
admin_info($title, $message, 'warning'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
if (!function_exists('admin_info')) { |
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Flash a message bag to session. |
129
|
|
|
* |
130
|
|
|
* @param string $title |
131
|
|
|
* @param string $message |
132
|
|
|
* @param string $type |
133
|
|
|
*/ |
134
|
|
|
function admin_info($title, $message = '', $type = 'info') |
135
|
|
|
{ |
136
|
|
|
$message = new MessageBag(get_defined_vars()); |
137
|
|
|
|
138
|
|
|
session()->flash($type, $message); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (!function_exists('admin_asset')) { |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $path |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
function admin_asset($path) |
150
|
|
|
{ |
151
|
|
|
return (config('admin.https') || config('admin.secure')) ? secure_asset($path) : asset($path); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!function_exists('admin_trans')) { |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Translate the given message. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* @param array $replace |
162
|
|
|
* @param string $locale |
163
|
|
|
* |
164
|
|
|
* @return \Illuminate\Contracts\Translation\Translator|string|array|null |
165
|
|
|
*/ |
166
|
|
|
function admin_trans($key = null, $replace = [], $locale = null) |
167
|
|
|
{ |
168
|
|
|
$line = __($key, $replace, $locale); |
169
|
|
|
|
170
|
|
|
if (!is_string($line)) { |
171
|
|
|
return $key; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $line; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (!function_exists('array_delete')) { |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Delete from array by value. |
182
|
|
|
* |
183
|
|
|
* @param array $array |
184
|
|
|
* @param mixed $value |
185
|
|
|
*/ |
186
|
|
|
function array_delete(&$array, $value) |
187
|
|
|
{ |
188
|
|
|
foreach ($array as $index => $item) { |
189
|
|
|
if ($value == $item) { |
190
|
|
|
unset($array[$index]); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (!function_exists('class_uses_deep')) { |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* To get ALL traits including those used by parent classes and other traits. |
200
|
|
|
* |
201
|
|
|
* @param $class |
202
|
|
|
* @param bool $autoload |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
function class_uses_deep($class, $autoload = true) |
207
|
|
|
{ |
208
|
|
|
$traits = []; |
209
|
|
|
|
210
|
|
|
do { |
211
|
|
|
$traits = array_merge(class_uses($class, $autoload), $traits); |
212
|
|
|
} while ($class = get_parent_class($class)); |
213
|
|
|
|
214
|
|
|
foreach ($traits as $trait => $same) { |
215
|
|
|
$traits = array_merge(class_uses($trait, $autoload), $traits); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return array_unique($traits); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (!function_exists('admin_dump')) { |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param $var |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
function admin_dump($var) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
ob_start(); |
232
|
|
|
|
233
|
|
|
dump(...func_get_args()); |
234
|
|
|
|
235
|
|
|
$contents = ob_get_contents(); |
236
|
|
|
|
237
|
|
|
ob_end_clean(); |
238
|
|
|
|
239
|
|
|
return $contents; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (!function_exists('file_size')) { |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Convert file size to a human readable format like `100mb`. |
247
|
|
|
* |
248
|
|
|
* @param int $bytes |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
* |
252
|
|
|
* @see https://stackoverflow.com/a/5501447/9443583 |
253
|
|
|
*/ |
254
|
|
|
function file_size($bytes) |
255
|
|
|
{ |
256
|
|
|
if ($bytes >= 1073741824) { |
257
|
|
|
$bytes = number_format($bytes / 1073741824, 2).' GB'; |
258
|
|
|
} elseif ($bytes >= 1048576) { |
259
|
|
|
$bytes = number_format($bytes / 1048576, 2).' MB'; |
260
|
|
|
} elseif ($bytes >= 1024) { |
261
|
|
|
$bytes = number_format($bytes / 1024, 2).' KB'; |
262
|
|
|
} elseif ($bytes > 1) { |
263
|
|
|
$bytes = $bytes.' bytes'; |
264
|
|
|
} elseif ($bytes == 1) { |
265
|
|
|
$bytes = $bytes.' byte'; |
266
|
|
|
} else { |
267
|
|
|
$bytes = '0 bytes'; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $bytes; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if (!function_exists('prepare_options')) { |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param array $options |
278
|
|
|
* |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
function prepare_options(array $options) |
282
|
|
|
{ |
283
|
|
|
$original = []; |
284
|
|
|
$toReplace = []; |
285
|
|
|
|
286
|
|
|
foreach ($options as $key => &$value) { |
287
|
|
|
if (is_array($value)) { |
288
|
|
|
$subArray = prepare_options($value); |
289
|
|
|
$value = $subArray['options']; |
290
|
|
|
$original = array_merge($original, $subArray['original']); |
291
|
|
|
$toReplace = array_merge($toReplace, $subArray['toReplace']); |
292
|
|
|
} elseif(strpos($value, 'function(') === 0){ |
293
|
|
|
$original[] = $value; |
294
|
|
|
$value = "%{$key}%"; |
295
|
|
|
$toReplace[] = "\"{$value}\""; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return compact('original', 'toReplace', 'options'); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (!function_exists('json_encode_options')) { |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param array $options |
307
|
|
|
* |
308
|
|
|
* @return string |
309
|
|
|
* |
310
|
|
|
* @see http://web.archive.org/web/20080828165256/http://solutoire.com/2008/06/12/sending-javascript-functions-over-json/ |
311
|
|
|
*/ |
312
|
|
|
function json_encode_options(array $options) |
313
|
|
|
{ |
314
|
|
|
$data = prepare_options($options); |
315
|
|
|
|
316
|
|
|
$json = json_encode($data['options']); |
317
|
|
|
|
318
|
|
|
return str_replace($data['toReplace'], $data['original'], $json); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.