BasePageController::render()   A
last analyzed

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 0
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\Settings;
8
use App\Models\User;
9
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...
10
use Illuminate\Http\Request;
11
use Illuminate\Pagination\LengthAwarePaginator;
12
use Illuminate\Support\Arr;
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\View\View;
15
16
class BasePageController extends Controller
17
{
18
    public Settings $settings;
19
20
    public string $title = '';
21
22
    public string $content = '';
23
24
    public string $meta_keywords = '';
25
26
    public string $meta_title = '';
27
28
    public string $meta_description = '';
29
30
    /**
31
     * Current page the user is browsing. ie browse.
32
     */
33
    public string $page = '';
34
35
    public string $page_template = '';
36
37
    public User $userdata;
38
39
    /**
40
     * User's theme.
41
     */
42
    protected string $theme = 'Gentele';
43
44
    /**
45
     * @var \Illuminate\Foundation\Application|mixed
46
     */
47
    public mixed $smarty;
48
49
    /**
50
     * BasePageController constructor.
51
     *
52
     * @throws \Exception
53
     */
54
    public function __construct()
55
    {
56
        $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', 'reset', 'showLinkRequestForm');
57
        // Buffer settings/DB connection.
58
        $this->settings = new Settings;
59
        $this->smarty = app('smarty.view');
60
61
        foreach (Arr::get(config('ytake-laravel-smarty'), 'plugins_paths', []) as $plugins) {
62
            $this->smarty->addPluginsDir($plugins);
63
        }
64
65
        $this->smarty->assign('serverroot', url('/'));
66
    }
67
68
    public function paginate($query, $totalCount, $items, $page, $path, $reqQuery): LengthAwarePaginator
69
    {
70
        return new LengthAwarePaginator($query, $totalCount, $items, $page, ['path' => $path, 'query' => $reqQuery]);
71
    }
72
73
    /**
74
     * @throws \Exception
75
     */
76
    protected function setPreferences(): void
77
    {
78
        if (Auth::check()) {
79
            $this->userdata = User::find(Auth::id());
80
            $this->setUserPreferences();
81
        } else {
82
            // Tell Smarty which directories to use for templates
83
            $this->smarty->setTemplateDir([
84
                'user' => config('ytake-laravel-smarty.template_path').'/Gentele',
85
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
86
                'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
87
            ]);
88
89
            $this->smarty->assign(
90
                [
91
                    'isadmin' => false,
92
                    'ismod' => false,
93
                    'loggedin' => false,
94
                ]
95
            );
96
        }
97
98
        $this->smarty->assign(
99
            [
100
                'theme' => 'Gentele',
101
                'site' => $this->settings,
102
            ]
103
        );
104
    }
105
106
    public function isPostBack(Request $request): bool
107
    {
108
        return $request->isMethod('POST');
109
    }
110
111
    /**
112
     * Show 404 page.
113
     *
114
     * @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...
115
     */
116
    public function show404($message = null): View
117
    {
118
        if ($message !== null) {
0 ignored issues
show
introduced by
The condition $message !== null is always false.
Loading history...
119
            return view('errors.404')->with('Message', $message);
120
        }
121
122
        return view('errors.404');
123
    }
124
125
    public function render(): void
126
    {
127
        $this->smarty->display($this->page_template);
128
    }
129
130
    /**
131
     * @throws \Exception
132
     */
133
    protected function setUserPreferences(): void
134
    {
135
        $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

135
        $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...
136
137
        // Update last login every 15 mins.
138
        if (now()->subHours(3) > $this->userdata->lastlogin) {
139
            event(new UserLoggedIn($this->userdata));
140
        }
141
142
        $this->smarty->assign('userdata', $this->userdata);
143
        $this->smarty->assign('loggedin', 'true');
144
145
        if ($this->userdata->hasRole('Admin')) {
146
            $this->smarty->assign('isadmin', 'true');
147
        }
148
149
        if ($this->userdata->hasRole('Moderator')) {
150
            $this->smarty->assign('ismod', 'true');
151
        }
152
153
        // Tell Smarty which directories to use for templates
154
        $this->smarty->setTemplateDir([
155
            'user' => config('ytake-laravel-smarty.template_path').'/Gentele',
156
            'shared' => config('ytake-laravel-smarty.template_path').'/shared',
157
            'default' => config('ytake-laravel-smarty.template_path').'/Gentele',
158
        ]);
159
160
        $role = $this->userdata->roles_id ?? User::ROLE_USER;
0 ignored issues
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
161
162
        $content = new Contents;
0 ignored issues
show
Unused Code introduced by
The assignment to $content is dead and can be removed.
Loading history...
163
        $parentcatlist = Category::getForMenu($this->userdata->categoryexclusions);
164
165
        $this->smarty->assign('parentcatlist', $parentcatlist);
166
        $this->smarty->assign('catClass', Category::class);
167
168
        if (\request()->has('t')) {
169
            $this->smarty->assign('header_menu_cat', \request()->input('t'));
170
        } else {
171
            $this->smarty->assign('header_menu_cat', '');
172
        }
173
        $header_menu = $this->smarty->fetch('headermenu.tpl');
174
        $this->smarty->assign('header_menu', $header_menu);
175
        $notification = $this->smarty->fetch('notification.tpl');
176
        $this->smarty->assign('notification', $notification);
177
        $sidebar = $this->smarty->fetch('sidebar.tpl');
178
        $this->smarty->assign('sidebar', $sidebar);
179
        $footer = $this->smarty->fetch('footer.tpl');
180
        $this->smarty->assign('footer', $footer);
181
    }
182
183
    /**
184
     *  Set admin preferences.
185
     */
186
    public function setAdminPrefs(): void
187
    {
188
        // Tell Smarty which directories to use for templates
189
        $this->smarty->setTemplateDir(
190
            [
191
                'admin' => config('ytake-laravel-smarty.template_path').'/admin',
192
                'shared' => config('ytake-laravel-smarty.template_path').'/shared',
193
                'default' => config('ytake-laravel-smarty.template_path').'/admin',
194
            ]
195
        );
196
197
        $this->smarty->assign('catClass', Category::class);
198
    }
199
200
    /**
201
     * Output the page.
202
     */
203
    public function pagerender(): void
204
    {
205
        $this->page_template = 'basepage.tpl';
206
207
        $this->render();
208
    }
209
210
    /**
211
     * Output a page using the admin template.
212
     *
213
     * @throws \Exception
214
     */
215
    public function adminrender(): void
216
    {
217
        $admin_menu = $this->smarty->fetch('adminmenu.tpl');
218
        $this->smarty->assign('admin_menu', $admin_menu);
219
220
        $this->page_template = 'baseadminpage.tpl';
221
222
        $this->render();
223
    }
224
225
    /**
226
     * @throws \Exception
227
     */
228
    public function adminBasePage(): void
229
    {
230
        $this->setAdminPrefs();
231
        $this->smarty->assign([
232
            'meta_title' => 'Admin Home',
233
            'meta_description' => 'Admin home page',
234
        ]);
235
        $this->adminrender();
236
    }
237
}
238