1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Backpack\Basset\Facades\Basset; |
4
|
|
|
use Creativeorange\Gravatar\Facades\Gravatar; |
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
if (! function_exists('backpack_url')) { |
9
|
|
|
/** |
10
|
|
|
* Appends the configured backpack prefix and returns |
11
|
|
|
* the URL using the standard Laravel helpers. |
12
|
|
|
* |
13
|
|
|
* @param $path |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
function backpack_url($path = null, $parameters = [], $secure = null) |
17
|
|
|
{ |
18
|
|
|
$path = ! $path || (substr($path, 0, 1) == '/') ? $path : '/'.$path; |
19
|
|
|
|
20
|
|
|
return url(config('backpack.base.route_prefix', 'admin').$path, $parameters, $secure); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (! function_exists('backpack_authentication_column')) { |
25
|
|
|
/** |
26
|
|
|
* Return the username column name. |
27
|
|
|
* The Laravel default (and Backpack default) is 'email'. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
function backpack_authentication_column() |
32
|
|
|
{ |
33
|
|
|
return config('backpack.base.authentication_column', 'email'); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (! function_exists('backpack_email_column')) { |
38
|
|
|
/** |
39
|
|
|
* Return the email column name. |
40
|
|
|
* The Laravel default (and Backpack default) is 'email'. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
function backpack_email_column() |
45
|
|
|
{ |
46
|
|
|
return config('backpack.base.email_column', 'email'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (! function_exists('backpack_form_input')) { |
51
|
|
|
/** |
52
|
|
|
* Parse the submitted input in request('form') to an usable array. |
53
|
|
|
* Joins the multiple[] fields in a single key and transform the dot notation fields into arrayed ones. |
54
|
|
|
* |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
function backpack_form_input() |
59
|
|
|
{ |
60
|
|
|
$input = request('form') ?? []; |
61
|
|
|
$result = []; |
62
|
|
|
|
63
|
|
|
foreach ($input as $row) { |
64
|
|
|
$repeatableRowKey = null; |
65
|
|
|
|
66
|
|
|
// regular fields don't need any additional parsing |
67
|
|
|
if (strpos($row['name'], '[') === false) { |
68
|
|
|
$result[$row['name']] = $row['value']; |
69
|
|
|
|
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$isMultiple = substr($row['name'], -2, 2) === '[]'; |
74
|
|
|
|
75
|
|
|
if ($isMultiple && substr_count($row['name'], '[') === 1) { |
76
|
|
|
$result[substr($row['name'], 0, -2)][] = $row['value']; |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// dot notation fields |
81
|
|
|
if (substr_count($row['name'], '[') === 1) { |
82
|
|
|
// start in the first occurrence since it's HasOne/MorphOne with dot notation (address[street] in request) to get the input name (address) |
83
|
|
|
$inputNameStart = strpos($row['name'], '[') + 1; |
84
|
|
|
} else { |
85
|
|
|
// repeatable fields, we need to get the input name and the row number |
86
|
|
|
// start on the second occurrence since it's a repeatable and we want to bypass the row number (repeatableName[rowNumber][inputName]) |
87
|
|
|
$inputNameStart = strpos($row['name'], '[', strpos($row['name'], '[') + 1) + 1; |
88
|
|
|
|
89
|
|
|
// get the array key (aka repeatable row) from field name |
90
|
|
|
$startKey = strpos($row['name'], '[') + 1; |
91
|
|
|
$endKey = strpos($row['name'], ']', $startKey); |
92
|
|
|
$lengthKey = $endKey - $startKey; |
93
|
|
|
$repeatableRowKey = substr($row['name'], $startKey, $lengthKey); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$inputNameEnd = strpos($row['name'], ']', $inputNameStart); |
97
|
|
|
$inputNameLength = $inputNameEnd - $inputNameStart; |
98
|
|
|
$inputName = substr($row['name'], $inputNameStart, $inputNameLength); |
99
|
|
|
$parentInputName = substr($row['name'], 0, strpos($row['name'], '[')); |
100
|
|
|
|
101
|
|
|
if (isset($repeatableRowKey)) { |
102
|
|
|
if ($isMultiple) { |
103
|
|
|
$result[$parentInputName][$repeatableRowKey][$inputName][] = $row['value']; |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$result[$parentInputName][$repeatableRowKey][$inputName] = $row['value']; |
108
|
|
|
|
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($isMultiple) { |
113
|
|
|
$result[$parentInputName][$inputName][] = $row['value']; |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
$result[$parentInputName][$inputName] = $row['value']; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (! function_exists('backpack_users_have_email')) { |
124
|
|
|
/** |
125
|
|
|
* Check if the email column is present on the user table. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
function backpack_users_have_email() |
130
|
|
|
{ |
131
|
|
|
$user_model_fqn = config('backpack.base.user_model_fqn'); |
132
|
|
|
$user = new $user_model_fqn(); |
133
|
|
|
|
134
|
|
|
return \Schema::hasColumn($user->getTable(), config('backpack.base.email_column') ?? 'email'); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (! function_exists('backpack_avatar_url')) { |
139
|
|
|
/** |
140
|
|
|
* Returns the avatar URL of a user. |
141
|
|
|
* |
142
|
|
|
* @param $user |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
function backpack_avatar_url($user) |
146
|
|
|
{ |
147
|
|
|
switch (config('backpack.base.avatar_type')) { |
148
|
|
|
case 'gravatar': |
149
|
|
|
if (backpack_users_have_email() && ! empty($user->email)) { |
150
|
|
|
$avatarLink = Gravatar::fallback(config('backpack.base.gravatar_fallback'))->get($user->email, ['size' => 80]); |
151
|
|
|
|
152
|
|
|
// if we can save it locally, for safer loading, let's do it |
153
|
|
|
if (in_array(Basset::basset($avatarLink, false)->name, ['INTERNALIZED', 'IN_CACHE', 'LOADED'])) { |
|
|
|
|
154
|
|
|
return Basset::getUrl($avatarLink); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $avatarLink; |
158
|
|
|
} |
159
|
|
|
break; |
160
|
|
|
default: |
161
|
|
|
return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')}; |
162
|
|
|
break; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (! function_exists('backpack_middleware')) { |
168
|
|
|
/** |
169
|
|
|
* Return the key of the middleware used across Backpack. |
170
|
|
|
* That middleware checks if the visitor is an admin. |
171
|
|
|
* |
172
|
|
|
* @param $path |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
function backpack_middleware() |
176
|
|
|
{ |
177
|
|
|
return config('backpack.base.middleware_key', 'admin'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (! function_exists('backpack_guard_name')) { |
182
|
|
|
/* |
183
|
|
|
* Returns the name of the guard defined |
184
|
|
|
* by the application config |
185
|
|
|
*/ |
186
|
|
|
function backpack_guard_name() |
187
|
|
|
{ |
188
|
|
|
return config('backpack.base.guard', config('auth.defaults.guard')); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (! function_exists('backpack_auth')) { |
193
|
|
|
/* |
194
|
|
|
* Returns the user instance if it exists |
195
|
|
|
* of the currently authenticated admin |
196
|
|
|
* based off the defined guard. |
197
|
|
|
*/ |
198
|
|
|
function backpack_auth() |
199
|
|
|
{ |
200
|
|
|
return \Auth::guard(backpack_guard_name()); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (! function_exists('backpack_user')) { |
205
|
|
|
/* |
206
|
|
|
* Returns back a user instance without |
207
|
|
|
* the admin guard, however allows you |
208
|
|
|
* to pass in a custom guard if you like. |
209
|
|
|
*/ |
210
|
|
|
function backpack_user() |
211
|
|
|
{ |
212
|
|
|
return backpack_auth()->user(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (! function_exists('mb_ucfirst')) { |
217
|
|
|
/** |
218
|
|
|
* Capitalize the first letter of a string, |
219
|
|
|
* even if that string is multi-byte (non-latin alphabet). |
220
|
|
|
* |
221
|
|
|
* @param string $string String to have its first letter capitalized. |
222
|
|
|
* @param encoding $encoding Character encoding |
|
|
|
|
223
|
|
|
* @return string String with first letter capitalized. |
224
|
|
|
*/ |
225
|
|
|
function mb_ucfirst($string, $encoding = false) |
226
|
|
|
{ |
227
|
|
|
$encoding = $encoding ? $encoding : mb_internal_encoding(); |
228
|
|
|
|
229
|
|
|
$strlen = mb_strlen($string, $encoding); |
|
|
|
|
230
|
|
|
$firstChar = mb_substr($string, 0, 1, $encoding); |
|
|
|
|
231
|
|
|
$then = mb_substr($string, 1, $strlen - 1, $encoding); |
232
|
|
|
|
233
|
|
|
return mb_strtoupper($firstChar, $encoding).$then; |
|
|
|
|
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (! function_exists('backpack_view')) { |
238
|
|
|
/** |
239
|
|
|
* Returns a new displayable view path, based on the configured backpack view namespace. |
240
|
|
|
* If that view doesn't exist, it falls back to the fallback namespace. |
241
|
|
|
* If that view doesn't exist, it falls back to the one from the Backpack UI directory. |
242
|
|
|
* |
243
|
|
|
* @param string (see config/backpack/base.php) |
|
|
|
|
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
function backpack_view($view) |
247
|
|
|
{ |
248
|
|
|
$viewPaths = [ |
249
|
|
|
config('backpack.ui.view_namespace').$view, |
250
|
|
|
backpack_theme_config('view_namespace_fallback').$view, |
251
|
|
|
'backpack.ui::'.$view, |
252
|
|
|
]; |
253
|
|
|
|
254
|
|
|
foreach ($viewPaths as $view) { |
|
|
|
|
255
|
|
|
if (view()->exists($view)) { |
256
|
|
|
return $view; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$errorMessage = 'The view: ['.$view.'] was not found in any of the following view paths: ['.implode(' ], [ ', $viewPaths).']'; |
261
|
|
|
|
262
|
|
|
$errorDetails = (function () { |
263
|
|
|
if (env('APP_ENV') === 'production' || ! env('APP_DEBUG')) { |
264
|
|
|
return ''; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2) ?? []; |
268
|
|
|
$functionCaller = $backtrace[1] ?? []; |
269
|
|
|
$functionLine = $functionCaller['line'] ?? 'N/A'; |
270
|
|
|
$functionFile = $functionCaller['file'] ?? 'N/A'; |
271
|
|
|
|
272
|
|
|
return '- Called in: '.Str::after($functionFile, base_path()).' on line: '.$functionLine; |
273
|
|
|
})(); |
274
|
|
|
|
275
|
|
|
abort(500, $errorMessage.$errorDetails, ['developer-error-exception']); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (! function_exists('backpack_theme_config')) { |
280
|
|
|
/** |
281
|
|
|
* Returns a config value from the current theme's config file. |
282
|
|
|
* It assumes the theme's config namespace is the same as the view namespace. |
283
|
|
|
* |
284
|
|
|
* @param string |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
function backpack_theme_config($key) |
288
|
|
|
{ |
289
|
|
|
$namespacedKey = config('backpack.ui.view_namespace').$key; |
290
|
|
|
$namespacedKey = str_replace('::', '.', $namespacedKey); |
291
|
|
|
|
292
|
|
|
// if the config exists in the theme config file, use it |
293
|
|
|
if (config()->has($namespacedKey)) { |
294
|
|
|
return config($namespacedKey); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// if not, fall back to a general the config in the fallback theme |
298
|
|
|
$namespacedKey = config('backpack.ui.view_namespace_fallback').$key; |
299
|
|
|
$namespacedKey = str_replace('::', '.', $namespacedKey); |
300
|
|
|
|
301
|
|
|
if (config()->has($namespacedKey)) { |
302
|
|
|
return config($namespacedKey); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// if not, fall back to the config in ui |
306
|
|
|
$namespacedKey = 'backpack.ui.'.$key; |
307
|
|
|
|
308
|
|
|
if (config()->has($namespacedKey)) { |
309
|
|
|
return config($namespacedKey); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
Log::error('Could not find config key: '.$key.'. Neither in the Backpack theme, nor in the fallback theme, nor in ui.'); |
313
|
|
|
|
314
|
|
|
return null; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if (! function_exists('square_brackets_to_dots')) { |
319
|
|
|
/** |
320
|
|
|
* Turns a string from bracket-type array to dot-notation array. |
321
|
|
|
* Ex: array[0][property] turns into array.0.property. |
322
|
|
|
* |
323
|
|
|
* @param $path |
324
|
|
|
* @return string |
325
|
|
|
*/ |
326
|
|
|
function square_brackets_to_dots($string) |
327
|
|
|
{ |
328
|
|
|
$string = str_replace(['[', ']'], ['.', ''], $string); |
329
|
|
|
|
330
|
|
|
return $string; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
if (! function_exists('old_empty_or_null')) { |
335
|
|
|
/** |
336
|
|
|
* This method is an alternative to Laravel's old() helper, which mistakenly |
337
|
|
|
* returns NULL it two cases: |
338
|
|
|
* - if there is an old value, and it was empty or null |
339
|
|
|
* - if there is no old value |
340
|
|
|
* (this is because of the ConvertsEmptyStringsToNull middleware). |
341
|
|
|
* |
342
|
|
|
* In contrast, this method will return: |
343
|
|
|
* - the old value, if there actually is an old value for that key; |
344
|
|
|
* - the second parameter, if there is no old value for that key, but it was empty string or null; |
345
|
|
|
* - null, if there is no old value at all for that key; |
346
|
|
|
* |
347
|
|
|
* This version is form-aware to prevent old values from bleeding across multiple forms. |
348
|
|
|
* |
349
|
|
|
* @param string $key |
350
|
|
|
* @param array|string $empty_value |
351
|
|
|
* @return mixed |
352
|
|
|
*/ function old_empty_or_null($key, $empty_value = '') |
353
|
|
|
{ |
354
|
|
|
$key = square_brackets_to_dots($key); |
355
|
|
|
$old_inputs = session()->getOldInput(); |
356
|
|
|
|
357
|
|
|
// Check if we have a form ID in the old inputs to determine if this is form-specific |
358
|
|
|
$submittedFormId = data_get($old_inputs, '_form_id'); |
359
|
|
|
|
360
|
|
|
if ($submittedFormId) { |
361
|
|
|
// Check if we're currently rendering a DataForm with a specific ID |
362
|
|
|
// Use Laravel's service container to get the current form context |
363
|
|
|
$currentFormId = app()->bound('backpack.current_form_id') ? app('backpack.current_form_id') : null; |
364
|
|
|
|
365
|
|
|
// If we can determine the current form ID and it doesn't match the submitted form ID, |
366
|
|
|
// don't return old values to prevent bleeding across forms |
367
|
|
|
if ($currentFormId && $currentFormId !== $submittedFormId) { |
368
|
|
|
return null; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
// if the input name is present in the old inputs we need to return earlier and not in a coalescing chain |
373
|
|
|
// otherwise `null` aka empty will not pass the condition and the field value would be returned. |
374
|
|
|
if (\Illuminate\Support\Arr::has($old_inputs, $key)) { |
375
|
|
|
return \Illuminate\Support\Arr::get($old_inputs, $key) ?? $empty_value; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return null; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
if (! function_exists('is_multidimensional_array')) { |
383
|
|
|
/** |
384
|
|
|
* Check if the array is multidimensional. |
385
|
|
|
* |
386
|
|
|
* If $strict is enabled, the array is considered multidimensional only if all elements of the array are arrays. |
387
|
|
|
*/ |
388
|
|
|
function is_multidimensional_array(array $array, bool $strict = false): bool |
389
|
|
|
{ |
390
|
|
|
foreach ($array as $item) { |
391
|
|
|
if ($strict && ! is_array($item)) { |
392
|
|
|
return false; |
393
|
|
|
} |
394
|
|
|
if (! $strict && is_array($item)) { |
395
|
|
|
return true; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
return $strict; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if (! function_exists('backpack_pro')) { |
404
|
|
|
/** |
405
|
|
|
* Check if the backpack/pro package is installed. |
406
|
|
|
* |
407
|
|
|
* @return bool |
408
|
|
|
*/ |
409
|
|
|
function backpack_pro() |
410
|
|
|
{ |
411
|
|
|
if (app()->runningUnitTests()) { |
|
|
|
|
412
|
|
|
return true; |
413
|
|
|
} |
414
|
|
|
if (! \Composer\InstalledVersions::isInstalled('backpack/pro')) { |
415
|
|
|
return false; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
return \Composer\InstalledVersions::getVersion('backpack/pro'); |
|
|
|
|
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|