1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace { |
4
|
|
|
|
5
|
|
|
if (!function_exists('array_build')) { |
6
|
|
|
/** |
7
|
|
|
* Build a new array using a callback (Original method was deprecetad since version 5.2). |
8
|
|
|
* |
9
|
|
|
* @param array $array |
10
|
|
|
* @param callable $callback |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
|
|
function array_build($array, callable $callback) |
14
|
|
|
{ |
15
|
|
|
$results = []; |
16
|
|
|
|
17
|
|
|
foreach ($array as $key => $value) { |
18
|
|
|
[$innerKey, $innerValue] = call_user_func($callback, $key, $value); |
19
|
|
|
|
20
|
|
|
$results[$innerKey] = $innerValue; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $results; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (!function_exists('guarded_auth')) { |
28
|
|
|
/** |
29
|
|
|
* Since version 5.2 Laravel did change the auth model. |
30
|
|
|
* Check if we on the new version. |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
function guarded_auth() |
35
|
|
|
{ |
36
|
|
|
return version_compare(app()->version(), '5.2') >= 0; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
namespace admin\db { |
42
|
|
|
|
43
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
44
|
|
|
use Illuminate\Support\Arr; |
45
|
|
|
use Illuminate\Support\Facades\DB; |
46
|
|
|
use Illuminate\Support\Str; |
47
|
|
|
use Terranet\Translatable\Translatable; |
48
|
|
|
|
49
|
|
|
if (!\function_exists('scheme')) { |
50
|
|
|
function scheme() |
51
|
|
|
{ |
52
|
|
|
return app('scaffold.schema'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!\function_exists('table_columns')) { |
57
|
|
|
function table_columns($model, $withTranslated = true) |
58
|
|
|
{ |
59
|
|
|
static $data = []; |
60
|
|
|
|
61
|
|
|
$table = $model->getTable(); |
62
|
|
|
|
63
|
|
|
if (!Arr::has($data, $table)) { |
64
|
|
|
$columns = scheme()->columns($table); |
65
|
|
|
|
66
|
|
|
if ($withTranslated && $model instanceof Translatable && method_exists($model, 'getTranslationModel')) { |
67
|
|
|
$related = $model->getTranslationModel()->getTable(); |
68
|
|
|
$columns = array_merge( |
69
|
|
|
$columns, |
70
|
|
|
Arr::except( |
71
|
|
|
scheme()->columns($related), |
72
|
|
|
array_keys($columns) |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
Arr::set($data, $table, $columns); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return Arr::get($data, $table, []); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!\function_exists('table_indexes')) { |
85
|
|
|
function table_indexes(Model $model, $withTranslated = true) |
86
|
|
|
{ |
87
|
|
|
$indexes = scheme()->indexedColumns($model->getTable()); |
88
|
|
|
|
89
|
|
|
if ($withTranslated && $model instanceof Translatable && method_exists($model, 'getTranslationModel')) { |
90
|
|
|
$indexes = array_unique(array_merge( |
91
|
|
|
$indexes, |
92
|
|
|
scheme()->indexedColumns($model->getTranslationModel()->getTable()) |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $indexes; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!\function_exists('connection')) { |
101
|
|
|
/** |
102
|
|
|
* Check if we are on desired connection or get the current connection name. |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return mixed string|boolean |
106
|
|
|
*/ |
107
|
|
|
function connection($name = null) |
108
|
|
|
{ |
109
|
|
|
if (null === $name) { |
110
|
|
|
return DB::connection()->getName(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return strtolower($name) === strtolower(DB::connection()->getName()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!\function_exists('enum_values')) { |
118
|
|
|
function enum_values($table, $column) |
119
|
|
|
{ |
120
|
|
|
static $hashMap = []; |
121
|
|
|
|
122
|
|
|
if (!array_key_exists($key = Str::slug("{$table}.{$column}", '_'), $hashMap)) { |
123
|
|
|
$columns = DB::select("SHOW COLUMNS FROM `{$table}` LIKE '{$column}'"); |
124
|
|
|
$values = []; |
125
|
|
|
if (preg_match('/^enum\((.*)\)$/', $columns[0]->Type, $matches)) { |
126
|
|
|
foreach (explode(',', $matches[1]) as $value) { |
127
|
|
|
$value = trim($value, "'"); |
128
|
|
|
$values[$value] = Str::title(strtr($value, ['-' => ' ', '_' => ' '])); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$hashMap[$key] = $values; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $hashMap[$key]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $values |
140
|
|
|
* @param $column |
141
|
|
|
* @param mixed $namespace |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
function translated_values($values, $namespace, $column) |
145
|
|
|
{ |
146
|
|
|
$translator = trans(); |
|
|
|
|
147
|
|
|
|
148
|
|
|
foreach ($values as $value => &$label) { |
149
|
|
|
$trKey = "administrator::enums.{$namespace}.{$column}.{$value}"; |
150
|
|
|
if ($translator->has($trKey)) { |
151
|
|
|
$label = $translator->get($trKey); |
152
|
|
|
} elseif ($translator->has($trKey = "administrator::enums.global.{$column}.{$value}")) { |
153
|
|
|
$label = $translator->get($trKey); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $values; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
namespace admin\helpers { |
163
|
|
|
|
164
|
|
|
use Illuminate\Support\Facades\Request; |
165
|
|
|
use Illuminate\Support\Facades\Route; |
166
|
|
|
use Terranet\Administrator\Contracts\Module\Exportable; |
167
|
|
|
|
168
|
|
|
if (!\function_exists('html_list')) { |
169
|
|
|
/** |
170
|
|
|
* Fetch key => value pairs from an Eloquent model. |
171
|
|
|
* |
172
|
|
|
* @param mixed $model |
173
|
|
|
* @param string $labelAttribute |
174
|
|
|
* @param string $keyAttribute |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
function html_list($model, $labelAttribute = 'name', $keyAttribute = 'id') |
178
|
|
|
{ |
179
|
|
|
if (\is_string($model)) { |
180
|
|
|
$model = new $model(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $model->pluck($labelAttribute, $keyAttribute)->toArray(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!\function_exists('qsRoute')) { |
188
|
|
|
/** |
189
|
|
|
* Generate route with query string. |
190
|
|
|
* |
191
|
|
|
* @param $route |
192
|
|
|
* @param array $params |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
function qsRoute($route = null, array $params = []) |
196
|
|
|
{ |
197
|
|
|
$requestParams = Request::all(); |
198
|
|
|
|
199
|
|
|
if (!$route) { |
200
|
|
|
$current = Route::current(); |
201
|
|
|
$requestParams += $current->parameters(); |
202
|
|
|
$route = $current->getName(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$params = array_merge($requestParams, $params); |
206
|
|
|
|
207
|
|
|
return route($route, $params); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (!\function_exists('html_attributes')) { |
212
|
|
|
function html_attributes(array $attributes = []) |
213
|
|
|
{ |
214
|
|
|
$out = []; |
215
|
|
|
foreach ($attributes as $key => $value) { |
216
|
|
|
// transform |
217
|
|
|
if (\is_bool($value)) { |
218
|
|
|
$out[] = "{$key}=\"{$key}\""; |
219
|
|
|
} else { |
220
|
|
|
if (is_numeric($key)) { |
221
|
|
|
$out[] = "{$value}=\"{$value}\""; |
222
|
|
|
} else { |
223
|
|
|
$value = htmlspecialchars($value); |
224
|
|
|
$out[] = "{$key}=\"{$value}\""; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return implode(' ', $out); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if (!\function_exists('auto_p')) { |
234
|
|
|
function auto_p($value, $lineBreaks = true) |
235
|
|
|
{ |
236
|
|
|
if ('' === trim($value)) { |
237
|
|
|
return ''; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$value = $value."\n"; // just to make things a little easier, pad the end |
241
|
|
|
$value = preg_replace('|<br />\s*<br />|', "\n\n", $value); |
242
|
|
|
|
243
|
|
|
// Space things out a little |
244
|
|
|
$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; |
245
|
|
|
$value = preg_replace('!(<'.$allblocks.'[^>]*>)!', "\n$1", $value); |
246
|
|
|
$value = preg_replace('!(</'.$allblocks.'>)!', "$1\n\n", $value); |
247
|
|
|
$value = str_replace(["\r\n", "\r"], "\n", $value); // cross-platform newlines |
248
|
|
|
|
249
|
|
|
if (false !== strpos($value, '<object')) { |
250
|
|
|
$value = preg_replace('|\s*<param([^>]*)>\s*|', '<param$1>', $value); // no pee inside object/embed |
251
|
|
|
$value = preg_replace('|\s*</embed>\s*|', '</embed>', $value); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$value = preg_replace("/\n\n+/", "\n\n", $value); // take care of duplicates |
255
|
|
|
|
256
|
|
|
// make paragraphs, including one at the end |
257
|
|
|
$values = preg_split('/\n\s*\n/', $value, -1, PREG_SPLIT_NO_EMPTY); |
258
|
|
|
$value = ''; |
259
|
|
|
|
260
|
|
|
foreach ($values as $tinkle) { |
261
|
|
|
$value .= '<p>'.trim($tinkle, "\n")."</p>\n"; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// under certain strange conditions it could create a P of entirely whitespace |
265
|
|
|
$value = preg_replace('|<p>\s*</p>|', '', $value); |
266
|
|
|
$value = preg_replace('!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $value); |
267
|
|
|
$value = preg_replace('!<p>\s*(</?'.$allblocks.'[^>]*>)\s*</p>!', '$1', $value); // don't pee all over a tag |
268
|
|
|
$value = preg_replace('|<p>(<li.+?)</p>|', '$1', $value); // problem with nested lists |
269
|
|
|
$value = preg_replace('|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $value); |
270
|
|
|
$value = str_replace('</blockquote></p>', '</p></blockquote>', $value); |
271
|
|
|
$value = preg_replace('!<p>\s*(</?'.$allblocks.'[^>]*>)!', '$1', $value); |
272
|
|
|
$value = preg_replace('!(</?'.$allblocks.'[^>]*>)\s*</p>!', '$1', $value); |
273
|
|
|
|
274
|
|
|
if ($lineBreaks) { |
275
|
|
|
$value = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '\admin\helpers\autop_newline_preservation_helper', $value); |
276
|
|
|
$value = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $value); // optionally make line breaks |
277
|
|
|
$value = str_replace('<WPPreserveNewline />', "\n", $value); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$value = preg_replace('!(</?'.$allblocks.'[^>]*>)\s*<br />!', '$1', $value); |
281
|
|
|
$value = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $value); |
282
|
|
|
|
283
|
|
|
if (false !== strpos($value, '<pre')) { |
284
|
|
|
$value = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', '\admin\helpers\clean_pre', $value); |
285
|
|
|
} |
286
|
|
|
$value = preg_replace("|\n</p>$|", '</p>', $value); |
287
|
|
|
|
288
|
|
|
return $value; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Accepts matches array from preg_replace_callback in wpautop() or a string. |
293
|
|
|
* Ensures that the contents of a <<pre>>...<</pre>> HTML block are not |
294
|
|
|
* converted into paragraphs or line-breaks. |
295
|
|
|
* |
296
|
|
|
* @param array|string $matches The array or string |
297
|
|
|
* @return string the pre block without paragraph/line-break conversion |
298
|
|
|
*/ |
299
|
|
|
function clean_pre($matches) |
300
|
|
|
{ |
301
|
|
|
if (\is_array($matches)) { |
302
|
|
|
$text = $matches[1].$matches[2].'</pre>'; |
303
|
|
|
} else { |
304
|
|
|
$text = $matches; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$text = str_replace(['<br />', '<br/>', '<br>'], ['', '', ''], $text); |
308
|
|
|
$text = str_replace('<p>', "\n", $text); |
309
|
|
|
$text = str_replace('</p>', '', $text); |
310
|
|
|
|
311
|
|
|
return $text; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Newline preservation help function for wpautop. |
316
|
|
|
* |
317
|
|
|
* @param array $matches preg_replace_callback matches array |
318
|
|
|
* @returns string |
319
|
|
|
* @return mixed |
320
|
|
|
* @since 3.1.0 |
321
|
|
|
*/ |
322
|
|
|
function autop_newline_preservation_helper($matches) |
323
|
|
|
{ |
324
|
|
|
return str_replace("\n", '<PreserveNewline />', $matches[0]); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if (!\function_exists('exportable')) { |
329
|
|
|
function exportable($module) |
330
|
|
|
{ |
331
|
|
|
return $module instanceof Exportable; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
namespace admin\output { |
337
|
|
|
|
338
|
|
|
use Closure; |
339
|
|
|
use Czim\Paperclip\Attachment\Attachment; |
|
|
|
|
340
|
|
|
use Illuminate\Support\Arr; |
341
|
|
|
use Illuminate\Support\Str; |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param $value |
345
|
|
|
* @return string |
346
|
|
|
*/ |
347
|
|
|
function boolean($value) |
348
|
|
|
{ |
349
|
|
|
return $value ? '<i class="fa fa-fw fa-check"></i>' : ''; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param string $name |
354
|
|
|
* @param $value |
355
|
|
|
* @param string $key |
356
|
|
|
* @return string |
357
|
|
|
*/ |
358
|
|
|
function rank(string $name, $value, string $key) |
359
|
|
|
{ |
360
|
|
|
return '<input type="number" style="width: 50px;" value="'.$value.'" name="'.$name.'['.$key.']" />'; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string $image |
365
|
|
|
* @param array $attributes |
366
|
|
|
* @return string |
367
|
|
|
*/ |
368
|
|
|
function image(string $image, array $attributes = []) |
369
|
|
|
{ |
370
|
|
|
$attributes = \admin\helpers\html_attributes($attributes); |
371
|
|
|
|
372
|
|
|
return $image ? '<img src="'.$image.'" '.$attributes.' />' : ''; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Output image from Paperclip attachment object. |
377
|
|
|
* |
378
|
|
|
* @param null|Attachment $attachment |
379
|
|
|
* @param null|string $style |
380
|
|
|
* @param array $attributes |
381
|
|
|
* @return null|string |
382
|
|
|
*/ |
383
|
|
|
function staplerImage(Attachment $attachment = null, string $style = null, $attributes = []) |
384
|
|
|
{ |
385
|
|
|
if ($attachment && $attachment->originalFilename()) { |
386
|
|
|
$styles = $attachment->variants(); |
387
|
|
|
|
388
|
|
|
if (\count($styles)) { |
389
|
|
|
$firstStyle = $style ?: head($styles); |
390
|
|
|
$origStyle = 'original'; |
391
|
|
|
|
392
|
|
|
// in case then style dimensions are less than predefined, adjust width & height to style's |
393
|
|
|
$aWidth = (int) Arr::get($attributes, 'width'); |
394
|
|
|
$aHeight = (int) Arr::get($attributes, 'height'); |
395
|
|
|
|
396
|
|
|
if (($aWidth || $aHeight) && $firstStyle) { |
397
|
|
|
$size = array_filter($styles, function ($style) use ($firstStyle) { |
398
|
|
|
return $style === $firstStyle; |
399
|
|
|
}); |
400
|
|
|
|
401
|
|
|
if (($size = array_shift($size))) { |
402
|
|
|
$dimensions = Arr::get($attachment->getConfig(), "variants.{$size}"); |
403
|
|
|
|
404
|
|
|
if (\is_array($dimensions)) { |
405
|
|
|
$dimensions = Arr::get($dimensions, 'resize.dimensions'); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
if ($dimensions && Str::contains($dimensions, 'x')) { |
409
|
|
|
[$width, $height] = explode('x', $dimensions); |
410
|
|
|
|
411
|
|
|
if ($aWidth > $width) { |
412
|
|
|
$attributes['width'] = $width; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if ($aHeight > $height) { |
416
|
|
|
$attributes['height'] = $height; |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return |
423
|
|
|
'<a class="fancybox" href="'.url($attachment->url($origStyle)).'">'. |
|
|
|
|
424
|
|
|
\admin\output\image($attachment->url($firstStyle), $attributes). |
425
|
|
|
'</a>'; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
return link_to($attachment->url(), '<i class="fa fa-cloud-download"></i>', [], false, false); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return null; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @param array $items |
436
|
|
|
* @param null|Closure $callback |
437
|
|
|
* @return array|string |
438
|
|
|
*/ |
439
|
|
|
function _prepare_collection(array $items, Closure $callback = null) |
440
|
|
|
{ |
441
|
|
|
if (\is_object($items) && method_exists($items, 'toArray')) { |
|
|
|
|
442
|
|
|
$items = $items->toArray(); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
if (empty($items)) { |
446
|
|
|
return ''; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
if ($callback) { |
450
|
|
|
array_walk($items, $callback); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
return $items; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* @param string $label |
458
|
|
|
* @param string $class |
459
|
|
|
* @return string |
460
|
|
|
*/ |
461
|
|
|
function label(string $label = '', string $class = 'label-success') |
462
|
|
|
{ |
463
|
|
|
return '<span class="label '.$class.'">'.$label.'</span>'; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @param string $label |
468
|
|
|
* @param string $class |
469
|
|
|
* @return string |
470
|
|
|
*/ |
471
|
|
|
function badge(string $label = '', string $class = 'bg-green') |
472
|
|
|
{ |
473
|
|
|
return '<span class="badge '.$class.'">'.$label.'</span>'; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|