Passed
Push — master ( 29358d...2ff380 )
by Darko
06:09
created

BasePageController::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 6
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Events\UserLoggedIn;
6
use App\Models\Category;
7
use App\Models\Forumpost;
8
use App\Models\Settings;
9
use App\Models\User;
10
use Blacklight\Contents;
0 ignored issues
show
Bug introduced by
The type Blacklight\Contents was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Illuminate\Http\Request;
12
use Illuminate\Pagination\LengthAwarePaginator;
13
use Illuminate\Support\Arr;
14
use Illuminate\Support\Facades\Auth;
15
use Illuminate\View\View;
16
17
class BasePageController extends Controller
18
{
19
    public Settings $settings;
20
21
    public string $title = '';
22
23
    public string $content = '';
24
25
    public string $meta_keywords = '';
26
27
    public string $meta_title = '';
28
29
    public string $meta_description = '';
30
31
    /**
32
     * Current page the user is browsing. ie browse.
33
     */
34
    public string $page = '';
35
36
    public string $page_template = '';
37
38
    public User $userdata;
39
40
    /**
41
     * User's theme.
42
     */
43
    protected string $theme = 'Gentele';
44
45
    /**
46
     * @var \Illuminate\Foundation\Application|mixed
47
     */
48
    public mixed $smarty;
49
50
    /**
51
     * BasePageController constructor.
52
     *
53
     * @throws \Exception
54
     */
55
    public function __construct()
56
    {
57
        $this->middleware(['auth', 'web', '2fa'])->except('api', 'contact', 'showContactForm', 'callback', 'getNzb', 'terms', 'capabilities', 'movie', 'apiSearch', 'tv', 'details', 'failed', 'showRssDesc', 'fullFeedRss', 'categoryFeedRss', 'cartRss', 'myMoviesRss', 'myShowsRss', 'release');
58
        // Buffer settings/DB connection.
59
        $this->settings = new Settings();
60
        $this->smarty = app('smarty.view');
61
62
        foreach (Arr::get(config('ytake-laravel-smarty'), 'plugins_paths', []) as $plugins) {
63
            $this->smarty->addPluginsDir($plugins);
64
        }
65
66
        $this->smarty->assign('serverroot', url('/'));
67
    }
68
69
    public function paginate($query, $totalCount, $items, $page, $path, $reqQuery): LengthAwarePaginator
70
    {
71
        return new LengthAwarePaginator($query, $totalCount, $items, $page, ['path' => $path, 'query' => $reqQuery]);
72
    }
73
74
    /**
75
     * @throws \Exception
76
     */
77
    protected function setPreferences(): void
78
    {
79
        if (Auth::check()) {
80
            $this->userdata = User::find(Auth::id());
81
            $this->setUserPreferences();
82
            if ($this->theme === 'None') {
83
                $this->theme = Settings::settingValue('site.main.style');
84
            }
85
        } else {
86
            $this->theme = Settings::settingValue('site.main.style');
87
            // Tell Smarty which directories to use for templates
88
            $this->smarty->setTemplateDir([
89
                'user' => config('ytake-laravel-smarty.template_path').DIRECTORY_SEPARATOR.$this->theme,
90
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
91
                'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
92
            ]);
93
94
            $this->smarty->assign(
95
                [
96
                    'isadmin' => false,
97
                    'ismod' => false,
98
                    'loggedin' => false,
99
                ]
100
            );
101
        }
102
103
        $this->smarty->assign(
104
            [
105
                'theme' => $this->theme,
106
                'site' => $this->settings,
107
            ]
108
        );
109
    }
110
111
    public function isPostBack(Request $request): bool
112
    {
113
        return $request->isMethod('POST');
114
    }
115
116
    /**
117
     * Show 404 page.
118
     *
119
     * @param  null  $message
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $message is correct as it would always require null to be passed?
Loading history...
120
     */
121
    public function show404($message = null): View
122
    {
123
        if ($message !== null) {
0 ignored issues
show
introduced by
The condition $message !== null is always false.
Loading history...
124
            return view('errors.404')->with('Message', $message);
125
        }
126
127
        return view('errors.404');
128
    }
129
130
    public function render(): void
131
    {
132
        $this->smarty->display($this->page_template);
133
    }
134
135
    /**
136
     * @throws \Exception
137
     */
138
    protected function setUserPreferences(): void
139
    {
140
        $this->userdata->categoryexclusions = User::getCategoryExclusionById(Auth::id());
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::id() can also be of type null and string; however, parameter $userID of App\Models\User::getCategoryExclusionById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

140
        $this->userdata->categoryexclusions = User::getCategoryExclusionById(/** @scrutinizer ignore-type */ Auth::id());
Loading history...
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User.
Loading history...
141
142
        // Change the theme to user's selected theme if they selected one, else use the admin one.
143
        if ((int) Settings::settingValue('site.main.userselstyle') === 1) {
144
            $this->theme = $this->userdata->style ?? 'None';
145
            if ($this->theme === 'None') {
146
                $this->theme = Settings::settingValue('site.main.style');
147
            }
148
        } else {
149
            $this->theme = Settings::settingValue('site.main.style');
150
        }
151
152
        // Update last login every 15 mins.
153
        if (now()->subHours(3) > $this->userdata->lastlogin) {
154
            event(new UserLoggedIn($this->userdata));
155
        }
156
157
        $this->smarty->assign('userdata', $this->userdata);
158
        $this->smarty->assign('loggedin', 'true');
159
160
        if ($this->userdata->hasRole('Admin')) {
161
            $this->smarty->assign('isadmin', 'true');
162
        }
163
164
        if ($this->userdata->hasRole('Moderator')) {
165
            $this->smarty->assign('ismod', 'true');
166
        }
167
168
        // Tell Smarty which directories to use for templates
169
        $this->smarty->setTemplateDir([
170
            'user' => config('ytake-laravel-smarty.template_path').DIRECTORY_SEPARATOR.$this->theme,
171
            'shared' => config('ytake-laravel-smarty.template_path').'/shared',
172
            'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
173
        ]);
174
175
        $role = User::ROLE_USER;
176
        if (! empty($this->userdata)) {
177
            $role = $this->userdata->roles_id;
178
        }
179
180
        $content = new Contents();
181
        $this->smarty->assign('usefulcontentlist', $content->getForMenuByTypeAndRole(Contents::TYPEUSEFUL, $role));
182
        $this->smarty->assign('articlecontentlist', $content->getForMenuByTypeAndRole(Contents::TYPEARTICLE, $role));
183
        if ($this->userdata !== null) {
184
            $this->smarty->assign('recentforumpostslist', Forumpost::getPosts(Settings::settingValue('..showrecentforumposts')));
185
        }
186
187
        $parentcatlist = Category::getForMenu($this->userdata->categoryexclusions);
188
189
        $this->smarty->assign('parentcatlist', $parentcatlist);
190
        $this->smarty->assign('catClass', Category::class);
191
192
        if (\request()->has('t')) {
193
            $this->smarty->assign('header_menu_cat', \request()->input('t'));
194
        } else {
195
            $this->smarty->assign('header_menu_cat', '');
196
        }
197
        $header_menu = $this->smarty->fetch('headermenu.tpl');
198
        $this->smarty->assign('header_menu', $header_menu);
199
        $notification = $this->smarty->fetch('notification.tpl');
200
        $this->smarty->assign('notification', $notification);
201
        $sidebar = $this->smarty->fetch('sidebar.tpl');
202
        $this->smarty->assign('sidebar', $sidebar);
203
        $footer = $this->smarty->fetch('footer.tpl');
204
        $this->smarty->assign('footer', $footer);
205
    }
206
207
    /**
208
     *  Set admin preferences.
209
     */
210
    public function setAdminPrefs(): void
211
    {
212
        // Tell Smarty which directories to use for templates
213
        $this->smarty->setTemplateDir(
214
            [
215
                'admin' => config('ytake-laravel-smarty.template_path').'/admin',
216
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
217
                'default' => config('ytake-laravel-smarty.template_path').'/admin',
218
            ]
219
        );
220
221
        $this->smarty->assign('catClass', Category::class);
222
    }
223
224
    /**
225
     * Output the page.
226
     */
227
    public function pagerender(): void
228
    {
229
        $this->page_template = 'basepage.tpl';
230
231
        $this->render();
232
    }
233
234
    /**
235
     * Output a page using the admin template.
236
     *
237
     * @throws \Exception
238
     */
239
    public function adminrender(): void
240
    {
241
        $admin_menu = $this->smarty->fetch('adminmenu.tpl');
242
        $this->smarty->assign('admin_menu', $admin_menu);
243
244
        $this->page_template = 'baseadminpage.tpl';
245
246
        $this->render();
247
    }
248
249
    /**
250
     * @throws \Exception
251
     */
252
    public function adminBasePage(): void
253
    {
254
        $this->setAdminPrefs();
255
        $this->smarty->assign([
256
            'meta_title' => 'Admin Home',
257
            'meta_description' => 'Admin home page',
258
        ]);
259
        $this->adminrender();
260
    }
261
}
262