Completed
Pull Request — master (#571)
by
unknown
04:06 queued 01:32
created

AdminLte::navbarRightFilter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Support\Facades\View;
8
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
9
use JeroenNoten\LaravelAdminLte\Menu\Builder;
10
11
class AdminLte
12
{
13
    protected $menu;
14
15
    protected $filters;
16
17
    protected $events;
18
19
    protected $container;
20
21 1
    public function __construct(
22
        array $filters,
23
        Dispatcher $events,
24
        Container $container
25
    ) {
26 1
        $this->filters = $filters;
27 1
        $this->events = $events;
28 1
        $this->container = $container;
29 1
    }
30
31 1
    public function menu($filterOpt = null)
32
    {
33 1
        if (! $this->menu) {
34 1
            $this->menu = $this->buildMenu();
35
        }
36
37
        // Check for filter option.
38
39 1
        if ($filterOpt == 'sidebar') {
40
            return array_filter($this->menu, [$this, 'sidebarFilter']);
41 1
        } elseif ($filterOpt == 'navbar-left') {
42
            return array_filter($this->menu, [$this, 'navbarLeftFilter']);
43 1
        } elseif ($filterOpt == 'navbar-right') {
44
            return array_filter($this->menu, [$this, 'navbarRightFilter']);
45 1
        } elseif ($filterOpt == 'navbar-user') {
46
            return array_filter($this->menu, [$this, 'navbarUserMenuFilter']);
47
        } else {
48 1
            return $this->menu;
49
        }
50
    }
51
52
    public function getBodyClasses()
53
    {
54
        $body_classes = [];
55
        $screen_sizes = ['xs', 'sm', 'md', 'lg', 'xl'];
56
57
        // Add classes related to the "sidebar_mini" configuration.
58
59
        if (config('adminlte.sidebar_mini', true) === true) {
60
            $body_classes[] = 'sidebar-mini';
61
        } elseif (config('adminlte.sidebar_mini', true) == 'md') {
62
            $body_classes[] = 'sidebar-mini sidebar-mini-md';
63
        }
64
65
        // Add classes related to the "layout_topnav" configuration.
66
67
        if (config('adminlte.layout_topnav') || View::getSection('layout_topnav')) {
68
            $body_classes[] = 'layout-top-nav';
69
        }
70
71
        // Add classes related to the "layout_boxed" configuration.
72
73
        if (config('adminlte.layout_boxed')) {
74
            $body_classes[] = 'layout-boxed';
75
        }
76
77
        // Add classes related to the "sidebar_collapse" configuration.
78
79
        if (config('adminlte.sidebar_collapse') || View::getSection('sidebar_collapse')) {
80
            $body_classes[] = 'sidebar-collapse';
81
        }
82
83
        // Add classes related to the "right_sidebar" configuration.
84
85
        if (config('adminlte.right_sidebar') && config('adminlte.right_sidebar_push')) {
86
            $body_classes[] = 'control-sidebar-push';
87
        }
88
89
        // Add classes related to the fixed layout configuration, these are not
90
        // compatible with "layout_topnav".
91
92
        if (! config('adminlte.layout_topnav') && ! View::getSection('layout_topnav')) {
93
            // Check for fixed sidebar configuration.
94
95
            if (config('adminlte.layout_fixed_sidebar')) {
96
                $body_classes[] = 'layout-fixed';
97
            }
98
99
            // Check for fixed navbar configuration.
100
101
            $fixed_navbar_cfg = config('adminlte.layout_fixed_navbar');
102
103 View Code Duplication
            if ($fixed_navbar_cfg === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                $body_classes[] = 'layout-navbar-fixed';
105
            } elseif (is_array($fixed_navbar_cfg)) {
106
                foreach ($fixed_navbar_cfg as $size => $enabled) {
107
                    if (in_array($size, $screen_sizes)) {
108
                        $size = $size == 'xs' ? '' : '-'.$size;
109
                        $body_classes[] = $enabled == true ?
110
                            'layout'.$size.'-navbar-fixed' :
111
                            'layout'.$size.'-navbar-not-fixed';
112
                    }
113
                }
114
            }
115
116
            // Check for fixed footer configuration.
117
118
            $fixed_footer_cfg = config('adminlte.layout_fixed_footer');
119
120 View Code Duplication
            if ($fixed_footer_cfg === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
                $body_classes[] = 'layout-footer-fixed';
122
            } elseif (is_array($fixed_footer_cfg)) {
123
                foreach ($fixed_footer_cfg as $size => $enabled) {
124
                    if (in_array($size, $screen_sizes)) {
125
                        $size = $size == 'xs' ? '' : '-'.$size;
126
                        $body_classes[] = $enabled == true ?
127
                            'layout'.$size.'-footer-fixed' :
128
                            'layout'.$size.'-footer-not-fixed';
129
                    }
130
                }
131
            }
132
        }
133
134
        $body_classes[] = config('adminlte.classes_body', '');
135
136
        // Add classes related to the "classes_body" configuration and return the
137
        // set of configured classes for the body tag.
138
139
        return trim(implode(' ', $body_classes));
140
    }
141
142
    public function getBodyData()
143
    {
144
        $body_data = [];
145
146
        // Add data related to the "sidebar_scrollbar_theme" configuration.
147
148
        $sb_theme_cfg = config('adminlte.sidebar_scrollbar_theme', 'os-theme-light');
149
150
        if ($sb_theme_cfg != 'os-theme-light') {
151
            $body_data[] = 'data-scrollbar-theme='.$sb_theme_cfg;
152
        }
153
154
        // Add data related to the "sidebar_scrollbar_auto_hide" configuration.
155
156
        $sb_auto_hide = config('adminlte.sidebar_scrollbar_auto_hide', 'l');
157
158
        if ($sb_auto_hide != 'l') {
159
            $body_data[] = 'data-scrollbar-auto-hide='.$sb_auto_hide;
160
        }
161
162
        return trim(implode(' ', $body_data));
163
    }
164
165 1
    protected function buildMenu()
166
    {
167 1
        $builder = new Builder($this->buildFilters());
168
169 1
        $this->events->dispatch(new BuildingMenu($builder));
170
171 1
        return $builder->menu;
172
    }
173
174 1
    protected function buildFilters()
175
    {
176 1
        return array_map([$this->container, 'make'], $this->filters);
177
    }
178
179
    /**
180
     * Filter method for sidebar menu items.
181
     */
182
    private function sidebarFilter($item)
183
    {
184
        if (isset($item['topnav']) && $item['topnav']) {
185
            return false;
186
        }
187
188
        if (isset($item['topnav_right']) && $item['topnav_right']) {
189
            return false;
190
        }
191
192
        if (isset($item['topnav_user']) && $item['topnav_user']) {
193
            return false;
194
        }
195
196
        return true;
197
    }
198
199
    /**
200
     * Filter method for navbar top left menu items.
201
     */
202
    private function navbarLeftFilter($item)
203
    {
204
        if (isset($item['topnav_right']) && $item['topnav_right']) {
205
            return false;
206
        }
207
208
        if (isset($item['topnav_user']) && $item['topnav_user']) {
209
            return false;
210
        }
211
212
        if (config('adminlte.layout_topnav') || (isset($item['topnav']) && $item['topnav'])) {
213
            return is_array($item) && ! isset($item['header']);
214
        }
215
216
        return false;
217
    }
218
219
    /**
220
     * Filter method for navbar top right menu items.
221
     */
222
    private function navbarRightFilter($item)
223
    {
224
        if (isset($item['topnav_right']) && $item['topnav_right']) {
225
            return true;
226
        }
227
228
        return false;
229
    }
230
231
    /**
232
     * Filter method for navbar dropdown user menu items.
233
     */
234
    private function navbarUserMenuFilter($item)
235
    {
236
        if (isset($item['topnav_user']) && $item['topnav_user']) {
237
            return true;
238
        }
239
240
        return false;
241
    }
242
}
243