1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (! function_exists('backpack_url')) { |
4
|
|
|
/** |
5
|
|
|
* Appends the configured backpack prefix and returns |
6
|
|
|
* the URL using the standard Laravel helpers. |
7
|
|
|
* |
8
|
|
|
* @param $path |
9
|
|
|
* @return string |
10
|
|
|
*/ |
11
|
|
|
function backpack_url($path = null, $parameters = [], $secure = null) |
12
|
|
|
{ |
13
|
|
|
$path = ! $path || (substr($path, 0, 1) == '/') ? $path : '/'.$path; |
14
|
|
|
|
15
|
|
|
return url(config('backpack.base.route_prefix', 'admin').$path, $parameters, $secure); |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if (! function_exists('backpack_authentication_column')) { |
20
|
|
|
/** |
21
|
|
|
* Return the username column name. |
22
|
|
|
* The Laravel default (and Backpack default) is 'email'. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
function backpack_authentication_column() |
27
|
|
|
{ |
28
|
|
|
return config('backpack.base.authentication_column', 'email'); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (! function_exists('backpack_form_input')) { |
33
|
|
|
/** |
34
|
|
|
* Parse the submitted input in request('form') to an usable array. |
35
|
|
|
* Joins the multiple[] fields in a single key and transform the dot notation fields into arrayed ones. |
36
|
|
|
* |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
function backpack_form_input() |
41
|
|
|
{ |
42
|
|
|
$input = request('form') ?? []; |
43
|
|
|
|
44
|
|
|
$result = []; |
45
|
|
|
foreach ($input as $row) { |
46
|
|
|
// parse the input name to extract the "arg" when using HasOne/MorphOne (address[street]) returns street as arg, address as key |
47
|
|
|
$start = strpos($row['name'], '['); |
48
|
|
|
$input_arg = null; |
49
|
|
|
if ($start !== false) { |
50
|
|
|
$end = strpos($row['name'], ']', $start + 1); |
51
|
|
|
$length = $end - $start; |
52
|
|
|
|
53
|
|
|
$input_arg = substr($row['name'], $start + 1, $length - 1); |
54
|
|
|
$input_arg = strlen($input_arg) >= 1 ? $input_arg : null; |
55
|
|
|
$input_key = substr($row['name'], 0, $start); |
56
|
|
|
} else { |
57
|
|
|
$input_key = $row['name']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (is_null($input_arg)) { |
61
|
|
|
if (! isset($result[$input_key])) { |
62
|
|
|
$result[$input_key] = $start ? [$row['value']] : $row['value']; |
63
|
|
|
} else { |
64
|
|
|
array_push($result[$input_key], $row['value']); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
$result[$input_key][$input_arg] = $row['value']; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (! function_exists('backpack_users_have_email')) { |
76
|
|
|
/** |
77
|
|
|
* Check if the email column is present on the user table. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
function backpack_users_have_email() |
82
|
|
|
{ |
83
|
|
|
$user_model_fqn = config('backpack.base.user_model_fqn'); |
84
|
|
|
$user = new $user_model_fqn(); |
85
|
|
|
|
86
|
|
|
return \Schema::hasColumn($user->getTable(), 'email'); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (! function_exists('backpack_avatar_url')) { |
91
|
|
|
/** |
92
|
|
|
* Returns the avatar URL of a user. |
93
|
|
|
* |
94
|
|
|
* @param $user |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
function backpack_avatar_url($user) |
98
|
|
|
{ |
99
|
|
|
$firstLetter = $user->getAttribute('name') ? mb_substr($user->name, 0, 1, 'UTF-8') : 'A'; |
100
|
|
|
$placeholder = 'https://via.placeholder.com/160x160/00a65a/ffffff/&text='.$firstLetter; |
101
|
|
|
|
102
|
|
|
switch (config('backpack.base.avatar_type')) { |
103
|
|
|
case 'gravatar': |
104
|
|
|
if (backpack_users_have_email()) { |
105
|
|
|
return Gravatar::fallback('https://via.placeholder.com/160x160/00a65a/ffffff/&text='.$firstLetter)->get($user->email); |
|
|
|
|
106
|
|
|
} else { |
107
|
|
|
return $placeholder; |
108
|
|
|
} |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
case 'placehold': |
112
|
|
|
return $placeholder; |
113
|
|
|
break; |
|
|
|
|
114
|
|
|
|
115
|
|
|
default: |
116
|
|
|
return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')}; |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (! function_exists('backpack_middleware')) { |
123
|
|
|
/** |
124
|
|
|
* Return the key of the middleware used across Backpack. |
125
|
|
|
* That middleware checks if the visitor is an admin. |
126
|
|
|
* |
127
|
|
|
* @param $path |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
function backpack_middleware() |
131
|
|
|
{ |
132
|
|
|
return config('backpack.base.middleware_key', 'admin'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (! function_exists('backpack_guard_name')) { |
137
|
|
|
/* |
138
|
|
|
* Returns the name of the guard defined |
139
|
|
|
* by the application config |
140
|
|
|
*/ |
141
|
|
|
function backpack_guard_name() |
142
|
|
|
{ |
143
|
|
|
return config('backpack.base.guard', config('auth.defaults.guard')); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (! function_exists('backpack_auth')) { |
148
|
|
|
/* |
149
|
|
|
* Returns the user instance if it exists |
150
|
|
|
* of the currently authenticated admin |
151
|
|
|
* based off the defined guard. |
152
|
|
|
*/ |
153
|
|
|
function backpack_auth() |
154
|
|
|
{ |
155
|
|
|
return \Auth::guard(backpack_guard_name()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (! function_exists('backpack_user')) { |
160
|
|
|
/* |
161
|
|
|
* Returns back a user instance without |
162
|
|
|
* the admin guard, however allows you |
163
|
|
|
* to pass in a custom guard if you like. |
164
|
|
|
*/ |
165
|
|
|
function backpack_user() |
166
|
|
|
{ |
167
|
|
|
return backpack_auth()->user(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (! function_exists('mb_ucfirst')) { |
172
|
|
|
/** |
173
|
|
|
* Capitalize the first letter of a string, |
174
|
|
|
* even if that string is multi-byte (non-latin alphabet). |
175
|
|
|
* |
176
|
|
|
* @param string $string String to have its first letter capitalized. |
177
|
|
|
* @param encoding $encoding Character encoding |
|
|
|
|
178
|
|
|
* @return string String with first letter capitalized. |
179
|
|
|
*/ |
180
|
|
|
function mb_ucfirst($string, $encoding = false) |
181
|
|
|
{ |
182
|
|
|
$encoding = $encoding ? $encoding : mb_internal_encoding(); |
183
|
|
|
|
184
|
|
|
$strlen = mb_strlen($string, $encoding); |
|
|
|
|
185
|
|
|
$firstChar = mb_substr($string, 0, 1, $encoding); |
|
|
|
|
186
|
|
|
$then = mb_substr($string, 1, $strlen - 1, $encoding); |
187
|
|
|
|
188
|
|
|
return mb_strtoupper($firstChar, $encoding).$then; |
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (! function_exists('backpack_view')) { |
193
|
|
|
/** |
194
|
|
|
* Returns a new displayable view based on the configured backpack view namespace. |
195
|
|
|
* If that view doesn't exist, it will load the one from the original theme. |
196
|
|
|
* |
197
|
|
|
* @param string (see config/backpack/base.php) |
|
|
|
|
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
function backpack_view($view) |
201
|
|
|
{ |
202
|
|
|
$originalTheme = 'backpack::'; |
203
|
|
|
$theme = config('backpack.base.view_namespace'); |
204
|
|
|
|
205
|
|
|
if (is_null($theme)) { |
206
|
|
|
$theme = $originalTheme; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$returnView = $theme.$view; |
210
|
|
|
|
211
|
|
|
if (! view()->exists($returnView)) { |
212
|
|
|
$returnView = $originalTheme.$view; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $returnView; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (! function_exists('square_brackets_to_dots')) { |
220
|
|
|
/** |
221
|
|
|
* Turns a string from bracket-type array to dot-notation array. |
222
|
|
|
* Ex: array[0][property] turns into array.0.property. |
223
|
|
|
* |
224
|
|
|
* @param $path |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
function square_brackets_to_dots($string) |
228
|
|
|
{ |
229
|
|
|
$string = str_replace(['[', ']'], ['.', ''], $string); |
230
|
|
|
|
231
|
|
|
return $string; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (! function_exists('is_countable')) { |
236
|
|
|
/** |
237
|
|
|
* We need this because is_countable was only introduced in PHP 7.3, |
238
|
|
|
* and in PHP 7.2 you should check if count() argument is really countable. |
239
|
|
|
* This function may be removed in future if PHP >= 7.3 becomes a requirement. |
240
|
|
|
* |
241
|
|
|
* @param $obj |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
|
|
function is_countable($obj) |
245
|
|
|
{ |
246
|
|
|
return is_array($obj) || $obj instanceof Countable; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (! function_exists('oldValueDefaultOrFallback')) { |
251
|
|
|
/** |
252
|
|
|
* This function allows us to setup a default value in case there is an old value, but is forcelly null |
253
|
|
|
* by Laravel middleware ConvertEmptyStringsToNull. |
254
|
|
|
* |
255
|
|
|
* @param string $field_name |
256
|
|
|
* @param mixed $fallback |
257
|
|
|
* @return mixed |
258
|
|
|
*/ |
259
|
|
|
function oldValueDefaultOrFallback($field, $fallback, $field_name = null, $default = null, $value = null) |
260
|
|
|
{ |
261
|
|
|
$field_name = $field_name ?? $field['name'] ?? null; |
262
|
|
|
$default = $default ?? $field['default'] ?? null; |
263
|
|
|
$value = $value ?? $field['value'] ?? null; |
264
|
|
|
|
265
|
|
|
// When you submit your field empty, internaly laravel will convert it to «null». |
266
|
|
|
// Using old('field') with coalescing operator (??) we can't match it even if we have it in the old() session (as null). |
267
|
|
|
// This checks that the value is null, but exists in the session old array, so it was submited empty|null, but exists! |
268
|
|
|
if (! empty(session()->getOldInput())) { |
269
|
|
|
if (is_null(old(square_brackets_to_dots($field_name)))) { |
270
|
|
|
return $fallback; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return old(square_brackets_to_dots($field_name)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if (array_key_exists('value', $field)) { |
277
|
|
|
return $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $default ?? $fallback; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|