Plugin::boot()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 25
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 25
loc 25
rs 8.439
cc 5
eloc 11
nc 1
nop 0
1
<?php namespace CosmicRadioTV\Podcast;
2
3
use Backend\Widgets\Lists;
4
use System\Classes\PluginBase;
5
use Backend;
6
use BackendAuth;
7
use Event;
8
use CosmicRadioTV\Podcast\Models\Show;
9
10
/**
11
 * Podcast Plugin Information File
12
 */
13
class Plugin extends PluginBase
14
{
15
16
    /**
17
     * Returns information about this plugin.
18
     *
19
     * @return array
20
     */
21
    public function pluginDetails()
22
    {
23
        return [
24
            'name'        => 'cosmicradiotv.podcast::lang.plugin.name',
25
            'description' => 'cosmicradiotv.podcast::lang.plugin.description',
26
            'author'      => 'CosmicRadioTV',
27
            'icon'        => 'icon-leaf'
28
        ];
29
    }
30
31
    /**
32
     * Components available from the plugin
33
     *
34
     * @return array
35
     */
36
    public function registerComponents()
37
    {
38
        return [
39
            'CosmicRadioTV\Podcast\Components\EpisodeComponent'       => 'podcastEpisode',
40
            'CosmicRadioTV\Podcast\Components\LatestEpisodeComponent' => 'podcastLatestEpisode',
41
            'CosmicRadioTV\Podcast\Components\EpisodesComponent'      => 'podcastEpisodes',
42
            'CosmicRadioTV\Podcast\Components\FeedComponent'          => 'podcastFeed',
43
        ];
44
    }
45
46
    public function registerMarkupTags()
47
    {
48
        return [
49
            'filters' => [
50
                'sectotime' => ['CosmicRadioTV\Podcast\Classes\TwigUtils', 'sectotime']
51
            ]
52
        ];
53
    }
54
55
    /**
56
     * Called right before the request route.
57
     */
58 View Code Duplication
    public function boot()
59
    {
60
61
        // Extends the list query for shows so that it is restricted to shows the user has permission to access.
62
        Event::listen('backend.list.extendQueryBefore', function (Lists $list, $query) {
63
            /** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query */
64
65
            if ($list->model instanceof \CosmicRadioTV\Podcast\Models\Show) {
66
                $user = BackendAuth::getUser();
67
                if (!$user->hasAccess(['cosmicradiotv.podcast.access_shows_all'])) {
68
                    $shows = Show::all();
69
                    $show_ids_allowed = [];
70
71
                    // Fills the list of allowed show ids.
72
                    foreach ($shows as $show) {
73
                        if ($user->hasAccess(['cosmicradiotv.podcast.access_show_' . $show->slug])) {
74
                            $show_ids_allowed[] = $show->id;
75
                        }
76
                    }
77
78
                    $query->whereIn('id', $show_ids_allowed);
79
                }
80
            }
81
        });
82
    }
83
84
    /**
85
     * Sets up the permissions for the plugin. It sets up permissions for every show that's in the database.
86
     *
87
     * @return array The permissions array.
88
     */
89
    public function registerPermissions()
90
    {
91
        $permissions = [
92
            'cosmicradiotv.podcast.access_release_types' => [
93
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.general',
94
                'label' => 'cosmicradiotv.podcast::lang.permissions.labels.access_release_types'
95
            ],
96
            'cosmicradiotv.podcast.access_tags'          => [
97
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.general',
98
                'label' => 'cosmicradiotv.podcast::lang.permissions.labels.access_tags'
99
            ],
100
            'cosmicradiotv.podcast.access_shows_all'     => [
101
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.general',
102
                'label' => 'cosmicradiotv.podcast::lang.permissions.labels.access_shows_all'
103
            ],
104
            'cosmicradiotv.podcast.access_shows'         => [
105
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.general',
106
                'label' => 'cosmicradiotv.podcast::lang.permissions.labels.access_shows'
107
            ],
108
            'cosmicradiotv.podcast.access_episodes_all'  => [
109
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.general',
110
                'label' => 'cosmicradiotv.podcast::lang.permissions.labels.access_episodes_all'
111
            ],
112
            'cosmicradiotv.podcast.access_episodes'      => [
113
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.general',
114
                'label' => 'cosmicradiotv.podcast::lang.permissions.labels.access_episodes'
115
            ],
116
        ];
117
118
        // Add the permissions for individual shows
119
        $shows = Show::all();
120
        foreach ($shows as $show) {
121
            $permissions['cosmicradiotv.podcast.access_show_' . $show->slug] = [
122
                'tab'   => 'cosmicradiotv.podcast::lang.permissions.tab_labels.shows',
123
                'label' => e(trans('cosmicradiotv.podcast::lang.permissions.labels.access_show')) . ' ' . $show->name
124
            ];
125
        }
126
127
        return $permissions;
128
    }
129
130
    /**
131
     * Sets up the back end navigation for the plugin
132
     *
133
     * @return array The navigation array.
134
     */
135
    public function registerNavigation()
136
    {
137
        return [
138
            'podcast' => [
139
                'label'       => 'cosmicradiotv.podcast::lang.podcast.menu_label',
140
                'url'         => Backend::url('cosmicradiotv/podcast/episodes'),
141
                'icon'        => 'icon-play',
142
                'permissions' => ['cosmicradiotv.podcast.access_episode*'],
143
                'order'       => 500,
144
                'sideMenu'    => [
145
                    'episodes'     => [
146
                        'label'       => 'cosmicradiotv.podcast::lang.episode.menu_label',
147
                        'url'         => Backend::url('cosmicradiotv/podcast/episodes'),
148
                        'icon'        => 'icon-play',
149
                        'permissions' => ['cosmicradiotv.podcast.access_episode*']
150
                    ],
151
                    'shows'        => [
152
                        'label'       => 'cosmicradiotv.podcast::lang.show.menu_label',
153
                        'url'         => Backend::url('cosmicradiotv/podcast/shows'),
154
                        'icon'        => 'icon-list',
155
                        'permissions' => ['cosmicradiotv.podcast.access_show*']
156
                    ],
157
                    'tags'         => [
158
                        'label'       => 'cosmicradiotv.podcast::lang.tag.menu_label',
159
                        'url'         => Backend::url('cosmicradiotv/podcast/tags'),
160
                        'icon'        => 'icon-tags',
161
                        'permissions' => ['cosmicradiotv.podcast.access_tags']
162
                    ],
163
                    'releasetypes' => [
164
                        'label'       => 'cosmicradiotv.podcast::lang.release_type.menu_label',
165
                        'url'         => Backend::url('cosmicradiotv/podcast/releasetypes'),
166
                        'icon'        => 'icon-video-camera',
167
                        'permissions' => ['cosmicradiotv.podcast.access_release_types']
168
                    ]
169
                ]
170
            ]
171
        ];
172
    }
173
}