Test Failed
Push — master ( f2a606...7148c0 )
by Terzi
03:41
created

has_admin_presenter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
         *
12
         * @return array
13
         */
14
        function array_build($array, callable $callback)
15
        {
16
            $results = [];
17
18
            foreach ($array as $key => $value) {
19
                [$innerKey, $innerValue] = call_user_func($callback, $key, $value);
20
21
                $results[$innerKey] = $innerValue;
22
            }
23
24
            return $results;
25
        }
26
    }
27
28
    if (!function_exists('guarded_auth')) {
29
        /**
30
         * Since version 5.2 Laravel did change the auth model.
31
         * Check if we on the new version.
32
         *
33
         * @return bool
34
         */
35
        function guarded_auth()
36
        {
37
            return version_compare(app()->version(), '5.2') >= 0;
0 ignored issues
show
introduced by
The method version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            return version_compare(app()->/** @scrutinizer ignore-call */ version(), '5.2') >= 0;
Loading history...
38
        }
39
    }
40
}
41
42
namespace admin\db {
43
44
    use Illuminate\Database\Eloquent\Model;
45
    use Illuminate\Support\Facades\DB;
46
    use Terranet\Translatable\Translatable;
47
48
    if (!\function_exists('scheme')) {
49
        function scheme()
50
        {
51
            return app('scaffold.schema');
52
        }
53
    }
54
55
    if (!\function_exists('table_columns')) {
56
        function table_columns($model, $withTranslated = true)
57
        {
58
            static $data = [];
59
60
            $table = $model->getTable();
61
62
            if (!array_has($data, $table)) {
63
                $columns = scheme()->columns($table);
64
65
                if ($withTranslated && $model instanceof Translatable && method_exists($model, 'getTranslationModel')) {
66
                    $related = $model->getTranslationModel()->getTable();
67
                    $columns = array_merge(
68
                        $columns,
69
                        array_except(
70
                            scheme()->columns($related),
71
                            array_keys($columns)
72
                        )
73
                    );
74
                }
75
76
                array_set($data, $table, $columns);
77
            }
78
79
            return array_get($data, $table, []);
80
        }
81
    }
82
83
    if (!\function_exists('table_indexes')) {
84
        function table_indexes(Model $model, $withTranslated = true)
85
        {
86
            $indexes = scheme()->indexedColumns($model->getTable());
87
88
            if ($withTranslated && $model instanceof Translatable && method_exists($model, 'getTranslationModel')) {
89
                $indexes = array_unique(array_merge(
90
                    $indexes,
91
                    scheme()->indexedColumns($model->getTranslationModel()->getTable())
92
                ));
93
            }
94
95
            return $indexes;
96
        }
97
    }
98
99
    if (!\function_exists('connection')) {
100
        /**
101
         * Check if we are on desired connection or get the current connection name.
102
         *
103
         * @param string $name
104
         *
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
            $columns = DB::select("SHOW COLUMNS FROM `{$table}` LIKE '{$column}'");
121
            $values = [];
122
            if (preg_match('/^enum\((.*)\)$/', $columns[0]->Type, $matches)) {
123
                foreach (explode(',', $matches[1]) as $value) {
124
                    $value = trim($value, "'");
125
                    $values[$value] = title_case($value);
126
                }
127
            }
128
129
            return $values;
130
        }
131
132
        /**
133
         * @param $values
134
         * @param $column
135
         * @param mixed $namespace
136
         *
137
         * @return array
138
         */
139
        function translated_values($values, $namespace, $column)
140
        {
141
            $translator = app('translator');
142
143
            foreach ($values as $value => &$label) {
144
                $trKey = "administrator::enums.{$namespace}.{$column}.{$value}";
145
                if ($translator->has($trKey)) {
146
                    $label = $translator->trans($trKey);
147
                } elseif ($translator->has($trKey = "administrator::enums.global.{$column}.{$value}")) {
148
                    $label = $translator->trans($trKey);
149
                }
150
            }
151
152
            return $values;
153
        }
154
    }
155
}
156
157
namespace admin\helpers {
158
159
    use Illuminate\Support\Facades\Request;
160
    use Illuminate\Support\Facades\Route;
161
    use Terranet\Administrator\Contracts\Module\Exportable;
162
163
    if (!\function_exists('html_list')) {
164
        /**
165
         * Fetch key => value pairs from an Eloquent model.
166
         *
167
         * @param mixed $model
168
         * @param string $labelAttribute
169
         * @param string $keyAttribute
170
         *
171
         * @return array
172
         */
173
        function html_list($model, $labelAttribute = 'name', $keyAttribute = 'id')
174
        {
175
            if (\is_string($model)) {
176
                $model = new $model();
177
            }
178
179
            return $model->pluck($labelAttribute, $keyAttribute)->toArray();
180
        }
181
    }
182
183
    if (!\function_exists('qsRoute')) {
184
        /**
185
         * Generate route with query string.
186
         *
187
         * @param       $route
188
         * @param array $params
189
         *
190
         * @return string
191
         */
192
        function qsRoute($route = null, array $params = [])
193
        {
194
            $requestParams = Request::all();
195
196
            if (!$route) {
197
                $current = Route::current();
198
                $requestParams += $current->parameters();
199
                $route = $current->getName();
200
            }
201
202
            $params = array_merge($requestParams, $params);
203
204
            return route($route, $params);
205
        }
206
    }
207
208
    if (!\function_exists('html_attributes')) {
209
        function html_attributes(array $attributes = [])
210
        {
211
            $out = [];
212
            foreach ($attributes as $key => $value) {
213
                // transform
214
                if (\is_bool($value)) {
215
                    $out[] = "{$key}=\"{$key}\"";
216
                } else {
217
                    if (is_numeric($key)) {
218
                        $out[] = "{$value}=\"{$value}\"";
219
                    } else {
220
                        $value = htmlspecialchars($value);
221
                        $out[] = "{$key}=\"{$value}\"";
222
                    }
223
                }
224
            }
225
226
            return implode(' ', $out);
227
        }
228
    }
229
230
    if (!\function_exists('auto_p')) {
231
        function auto_p($value, $lineBreaks = true)
232
        {
233
            if ('' === trim($value)) {
234
                return '';
235
            }
236
237
            $value = $value."\n"; // just to make things a little easier, pad the end
238
            $value = preg_replace('|<br />\s*<br />|', "\n\n", $value);
239
240
            // Space things out a little
241
            $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)';
242
            $value = preg_replace('!(<'.$allblocks.'[^>]*>)!', "\n$1", $value);
243
            $value = preg_replace('!(</'.$allblocks.'>)!', "$1\n\n", $value);
244
            $value = str_replace(["\r\n", "\r"], "\n", $value); // cross-platform newlines
245
246
            if (false !== strpos($value, '<object')) {
247
                $value = preg_replace('|\s*<param([^>]*)>\s*|', '<param$1>', $value); // no pee inside object/embed
248
                $value = preg_replace('|\s*</embed>\s*|', '</embed>', $value);
249
            }
250
251
            $value = preg_replace("/\n\n+/", "\n\n", $value); // take care of duplicates
252
253
            // make paragraphs, including one at the end
254
            $values = preg_split('/\n\s*\n/', $value, -1, PREG_SPLIT_NO_EMPTY);
255
            $value = '';
256
257
            foreach ($values as $tinkle) {
258
                $value .= '<p>'.trim($tinkle, "\n")."</p>\n";
259
            }
260
261
            // under certain strange conditions it could create a P of entirely whitespace
262
            $value = preg_replace('|<p>\s*</p>|', '', $value);
263
            $value = preg_replace('!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $value);
264
            $value = preg_replace('!<p>\s*(</?'.$allblocks.'[^>]*>)\s*</p>!', '$1', $value); // don't pee all over a tag
265
            $value = preg_replace('|<p>(<li.+?)</p>|', '$1', $value); // problem with nested lists
266
            $value = preg_replace('|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $value);
267
            $value = str_replace('</blockquote></p>', '</p></blockquote>', $value);
268
            $value = preg_replace('!<p>\s*(</?'.$allblocks.'[^>]*>)!', '$1', $value);
269
            $value = preg_replace('!(</?'.$allblocks.'[^>]*>)\s*</p>!', '$1', $value);
270
271
            if ($lineBreaks) {
272
                $value = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '\admin\helpers\autop_newline_preservation_helper', $value);
273
                $value = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $value); // optionally make line breaks
274
                $value = str_replace('<WPPreserveNewline />', "\n", $value);
275
            }
276
277
            $value = preg_replace('!(</?'.$allblocks.'[^>]*>)\s*<br />!', '$1', $value);
278
            $value = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $value);
279
280
            if (false !== strpos($value, '<pre')) {
281
                $value = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', '\admin\helpers\clean_pre', $value);
282
            }
283
            $value = preg_replace("|\n</p>$|", '</p>', $value);
284
285
            return $value;
286
        }
287
288
        /**
289
         * Accepts matches array from preg_replace_callback in wpautop() or a string.
290
         * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
291
         * converted into paragraphs or line-breaks.
292
         *
293
         * @param array|string $matches The array or string
294
         *
295
         * @return string the pre block without paragraph/line-break conversion
296
         */
297
        function clean_pre($matches)
298
        {
299
            if (\is_array($matches)) {
300
                $text = $matches[1].$matches[2].'</pre>';
301
            } else {
302
                $text = $matches;
303
            }
304
305
            $text = str_replace(['<br />', '<br/>', '<br>'], ['', '', ''], $text);
306
            $text = str_replace('<p>', "\n", $text);
307
            $text = str_replace('</p>', '', $text);
308
309
            return $text;
310
        }
311
312
        /**
313
         * Newline preservation help function for wpautop.
314
         *
315
         * @since  3.1.0
316
         *
317
         * @param array $matches preg_replace_callback matches array
318
         * @returns string
319
         *
320
         * @return mixed
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
340
    function boolean($value)
341
    {
342
        return $value ? '<i class="fa fa-fw fa-check"></i>' : '';
343
    }
344
345
    function rank($name, $value, $key)
346
    {
347
        return '<input type="number" style="width: 50px;" value="'.$value.'" name="'.$name.'['.$key.']" />';
348
    }
349
350
    /**
351
     * @param       $image
352
     * @param array $attributes
353
     *
354
     * @return string
355
     */
356
    function image($image, array $attributes = [])
357
    {
358
        $attributes = \admin\helpers\html_attributes($attributes);
359
360
        return $image ? '<img src="'.$image.'" '.$attributes.' />' : '';
361
    }
362
363
    /**
364
     * Output image from Paperclip attachment object.
365
     *
366
     * @param null $attachment
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $attachment is correct as it would always require null to be passed?
Loading history...
367
     * @param null $style
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $style is correct as it would always require null to be passed?
Loading history...
368
     * @param array $attributes
369
     *
370
     * @return null|string
371
     */
372
    function staplerImage($attachment = null, $style = null, $attributes = [])
373
    {
374
        if ($attachment && $attachment->originalFilename()) {
0 ignored issues
show
introduced by
$attachment is of type null, thus it always evaluated to false.
Loading history...
375
            $styles = $attachment->variants();
376
377
            if (\count($styles)) {
378
                $firstStyle = $style ?: head($styles);
379
                $origStyle = 'original';
380
381
                // in case then style dimensions are less than predefined, adjust width & height to style's
382
                $aWidth = (int) array_get($attributes, 'width');
383
                $aHeight = (int) array_get($attributes, 'height');
384
385
                if (($aWidth || $aHeight) && $firstStyle) {
386
                    $size = array_filter($styles, function ($style) use ($firstStyle) {
387
                        return $style === $firstStyle;
388
                    });
389
390
                    if (($size = array_shift($size))) {
391
                        $dimensions = array_get($attachment->getConfig(), "variants.{$size}");
392
393
                        if (\is_array($dimensions)) {
394
                            $dimensions = array_get($dimensions, 'resize.dimensions');
395
                        }
396
397
                        if ($dimensions && str_contains($dimensions, 'x')) {
398
                            [$width, $height] = explode('x', $dimensions);
399
400
                            if ($aWidth > $width) {
401
                                $attributes['width'] = $width;
402
                            }
403
404
                            if ($aHeight > $height) {
405
                                $attributes['height'] = $height;
406
                            }
407
                        }
408
                    }
409
                }
410
411
                return
412
                    '<a class="fancybox" href="'.url($attachment->url($origStyle)).'">'.
413
                    \admin\output\image($attachment->url($firstStyle), $attributes).
414
                    '</a>';
415
            }
416
417
            return link_to($attachment->url(), '<i class="fa fa-cloud-download"></i>', [], false, false);
418
        }
419
420
        return null;
421
    }
422
423
    function _prepare_collection($items, Closure $callback = null)
424
    {
425
        if (\is_object($items) && method_exists($items, 'toArray')) {
426
            $items = $items->toArray();
427
        }
428
429
        if (empty($items)) {
430
            return '';
431
        }
432
433
        if ($callback) {
434
            array_walk($items, $callback);
435
        }
436
437
        return $items;
438
    }
439
440
    function label($label = '', $class = 'label-success')
441
    {
442
        return '<span class="label '.$class.'">'.$label.'</span>';
443
    }
444
445
    function badge($label = '', $class = 'bg-green')
446
    {
447
        return '<span class="badge '.$class.'">'.$label.'</span>';
448
    }
449
}
450