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_email_column')) { |
33
|
|
|
/** |
34
|
|
|
* Return the email column name. |
35
|
|
|
* The Laravel default (and Backpack default) is 'email'. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
function backpack_email_column() |
40
|
|
|
{ |
41
|
|
|
return config('backpack.base.email_column', 'email'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (! function_exists('backpack_form_input')) { |
46
|
|
|
/** |
47
|
|
|
* Parse the submitted input in request('form') to an usable array. |
48
|
|
|
* Joins the multiple[] fields in a single key and transform the dot notation fields into arrayed ones. |
49
|
|
|
* |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
function backpack_form_input() |
54
|
|
|
{ |
55
|
|
|
$input = request('form') ?? []; |
56
|
|
|
$result = []; |
57
|
|
|
|
58
|
|
|
foreach ($input as $row) { |
59
|
|
|
$repeatableRowKey = null; |
60
|
|
|
|
61
|
|
|
// regular fields don't need any aditional parsing |
62
|
|
|
if (strpos($row['name'], '[') === false) { |
63
|
|
|
$result[$row['name']] = $row['value']; |
64
|
|
|
|
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$isMultiple = substr($row['name'], -2, 2) === '[]'; |
69
|
|
|
|
70
|
|
|
if ($isMultiple && substr_count($row['name'], '[') === 1) { |
71
|
|
|
$result[substr($row['name'], 0, -2)][] = $row['value']; |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// dot notation fields |
76
|
|
|
if (substr_count($row['name'], '[') === 1) { |
77
|
|
|
// start in the first occurence since it's HasOne/MorphOne with dot notation (address[street] in request) to get the input name (address) |
78
|
|
|
$inputNameStart = strpos($row['name'], '[') + 1; |
79
|
|
|
} else { |
80
|
|
|
// repeatable fields, we need to get the input name and the row number |
81
|
|
|
// start on the second occurence since it's a repeatable and we want to bypass the row number (repeatableName[rowNumber][inputName]) |
82
|
|
|
$inputNameStart = strpos($row['name'], '[', strpos($row['name'], '[') + 1) + 1; |
83
|
|
|
|
84
|
|
|
// get the array key (aka repeatable row) from field name |
85
|
|
|
$startKey = strpos($row['name'], '[') + 1; |
86
|
|
|
$endKey = strpos($row['name'], ']', $startKey); |
87
|
|
|
$lengthKey = $endKey - $startKey; |
88
|
|
|
$repeatableRowKey = substr($row['name'], $startKey, $lengthKey); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$inputNameEnd = strpos($row['name'], ']', $inputNameStart); |
92
|
|
|
$inputNameLength = $inputNameEnd - $inputNameStart; |
93
|
|
|
$inputName = substr($row['name'], $inputNameStart, $inputNameLength); |
94
|
|
|
$parentInputName = substr($row['name'], 0, strpos($row['name'], '[')); |
95
|
|
|
|
96
|
|
|
if (isset($repeatableRowKey)) { |
97
|
|
|
if ($isMultiple) { |
98
|
|
|
$result[$parentInputName][$repeatableRowKey][$inputName][] = $row['value']; |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$result[$parentInputName][$repeatableRowKey][$inputName] = $row['value']; |
103
|
|
|
|
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($isMultiple) { |
108
|
|
|
$result[$parentInputName][$inputName][] = $row['value']; |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
$result[$parentInputName][$inputName] = $row['value']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (! function_exists('backpack_users_have_email')) { |
119
|
|
|
/** |
120
|
|
|
* Check if the email column is present on the user table. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
function backpack_users_have_email() |
125
|
|
|
{ |
126
|
|
|
$user_model_fqn = config('backpack.base.user_model_fqn'); |
127
|
|
|
$user = new $user_model_fqn(); |
128
|
|
|
|
129
|
|
|
return \Schema::hasColumn($user->getTable(), config('backpack.base.email_column') ?? 'email'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (! function_exists('backpack_avatar_url')) { |
134
|
|
|
/** |
135
|
|
|
* Returns the avatar URL of a user. |
136
|
|
|
* |
137
|
|
|
* @param $user |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
function backpack_avatar_url($user) |
141
|
|
|
{ |
142
|
|
|
switch (config('backpack.base.avatar_type')) { |
143
|
|
|
case 'gravatar': |
144
|
|
|
if (backpack_users_have_email() && ! empty($user->email)) { |
145
|
|
|
return Gravatar::fallback(config('backpack.base.gravatar_fallback'))->get($user->email); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
break; |
148
|
|
|
default: |
149
|
|
|
return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')}; |
150
|
|
|
break; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (! function_exists('backpack_middleware')) { |
156
|
|
|
/** |
157
|
|
|
* Return the key of the middleware used across Backpack. |
158
|
|
|
* That middleware checks if the visitor is an admin. |
159
|
|
|
* |
160
|
|
|
* @param $path |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
function backpack_middleware() |
164
|
|
|
{ |
165
|
|
|
return config('backpack.base.middleware_key', 'admin'); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (! function_exists('backpack_guard_name')) { |
170
|
|
|
/* |
171
|
|
|
* Returns the name of the guard defined |
172
|
|
|
* by the application config |
173
|
|
|
*/ |
174
|
|
|
function backpack_guard_name() |
175
|
|
|
{ |
176
|
|
|
return config('backpack.base.guard', config('auth.defaults.guard')); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (! function_exists('backpack_auth')) { |
181
|
|
|
/* |
182
|
|
|
* Returns the user instance if it exists |
183
|
|
|
* of the currently authenticated admin |
184
|
|
|
* based off the defined guard. |
185
|
|
|
*/ |
186
|
|
|
function backpack_auth() |
187
|
|
|
{ |
188
|
|
|
return \Auth::guard(backpack_guard_name()); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (! function_exists('backpack_user')) { |
193
|
|
|
/* |
194
|
|
|
* Returns back a user instance without |
195
|
|
|
* the admin guard, however allows you |
196
|
|
|
* to pass in a custom guard if you like. |
197
|
|
|
*/ |
198
|
|
|
function backpack_user() |
199
|
|
|
{ |
200
|
|
|
return backpack_auth()->user(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (! function_exists('mb_ucfirst')) { |
205
|
|
|
/** |
206
|
|
|
* Capitalize the first letter of a string, |
207
|
|
|
* even if that string is multi-byte (non-latin alphabet). |
208
|
|
|
* |
209
|
|
|
* @param string $string String to have its first letter capitalized. |
210
|
|
|
* @param encoding $encoding Character encoding |
|
|
|
|
211
|
|
|
* @return string String with first letter capitalized. |
212
|
|
|
*/ |
213
|
|
|
function mb_ucfirst($string, $encoding = false) |
214
|
|
|
{ |
215
|
|
|
$encoding = $encoding ? $encoding : mb_internal_encoding(); |
216
|
|
|
|
217
|
|
|
$strlen = mb_strlen($string, $encoding); |
|
|
|
|
218
|
|
|
$firstChar = mb_substr($string, 0, 1, $encoding); |
|
|
|
|
219
|
|
|
$then = mb_substr($string, 1, $strlen - 1, $encoding); |
220
|
|
|
|
221
|
|
|
return mb_strtoupper($firstChar, $encoding).$then; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if (! function_exists('backpack_view')) { |
226
|
|
|
/** |
227
|
|
|
* Returns a new displayable view based on the configured backpack view namespace. |
228
|
|
|
* If that view doesn't exist, it will load the one from the original theme. |
229
|
|
|
* |
230
|
|
|
* @param string (see config/backpack/base.php) |
|
|
|
|
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
function backpack_view($view) |
234
|
|
|
{ |
235
|
|
|
$originalTheme = 'backpack::'; |
236
|
|
|
$theme = config('backpack.base.view_namespace'); |
237
|
|
|
|
238
|
|
|
if (is_null($theme)) { |
239
|
|
|
$theme = $originalTheme; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$returnView = $theme.$view; |
243
|
|
|
|
244
|
|
|
if (! view()->exists($returnView)) { |
245
|
|
|
$returnView = $originalTheme.$view; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $returnView; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if (! function_exists('square_brackets_to_dots')) { |
253
|
|
|
/** |
254
|
|
|
* Turns a string from bracket-type array to dot-notation array. |
255
|
|
|
* Ex: array[0][property] turns into array.0.property. |
256
|
|
|
* |
257
|
|
|
* @param $path |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
function square_brackets_to_dots($string) |
261
|
|
|
{ |
262
|
|
|
$string = str_replace(['[', ']'], ['.', ''], $string); |
263
|
|
|
|
264
|
|
|
return $string; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if (! function_exists('old_empty_or_null')) { |
269
|
|
|
/** |
270
|
|
|
* This method is an alternative to Laravel's old() helper, which mistakenly |
271
|
|
|
* returns NULL it two cases: |
272
|
|
|
* - if there is an old value, and it was empty or null |
273
|
|
|
* - if there is no old value |
274
|
|
|
* (this is because of the ConvertsEmptyStringsToNull middleware). |
275
|
|
|
* |
276
|
|
|
* In contrast, this method will return: |
277
|
|
|
* - the old value, if there actually is an old value for that key; |
278
|
|
|
* - the second parameter, if there is no old value for that key, but it was empty string or null; |
279
|
|
|
* - null, if there is no old value at all for that key; |
280
|
|
|
* |
281
|
|
|
* @param string $key |
282
|
|
|
* @param array|string $empty_value |
283
|
|
|
* @return mixed |
284
|
|
|
*/ |
285
|
|
|
function old_empty_or_null($key, $empty_value = '') |
286
|
|
|
{ |
287
|
|
|
$key = square_brackets_to_dots($key); |
288
|
|
|
$old_inputs = session()->getOldInput(); |
289
|
|
|
|
290
|
|
|
// if the input name is present in the old inputs we need to return earlier and not in a coalescing chain |
291
|
|
|
// otherwise `null` aka empty will not pass the condition and the field value would be returned. |
292
|
|
|
if (\Arr::has($old_inputs, $key)) { |
293
|
|
|
return \Arr::get($old_inputs, $key) ?? $empty_value; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return null; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if (! function_exists('is_multidimensional_array')) { |
301
|
|
|
/** |
302
|
|
|
* If any of the items inside a given array is an array, the array is considered multidimensional. |
303
|
|
|
* |
304
|
|
|
* @param array $array |
305
|
|
|
* @return bool |
306
|
|
|
*/ |
307
|
|
|
function is_multidimensional_array(array $array) |
308
|
|
|
{ |
309
|
|
|
foreach ($array as $item) { |
310
|
|
|
if (is_array($item)) { |
311
|
|
|
return true; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
if (! function_exists('backpack_pro')) { |
320
|
|
|
/** |
321
|
|
|
* Check if the backpack/pro package is installed. |
322
|
|
|
* |
323
|
|
|
* @return bool |
324
|
|
|
*/ |
325
|
|
|
function backpack_pro() |
326
|
|
|
{ |
327
|
|
|
if (app()->runningUnitTests()) { |
|
|
|
|
328
|
|
|
return true; |
329
|
|
|
} |
330
|
|
|
if (! \Composer\InstalledVersions::isInstalled('backpack/pro')) { |
331
|
|
|
return false; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return \PackageVersions\Versions::getVersion('backpack/pro'); |
|
|
|
|
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|