Passed
Push — master ( a79f35...85b490 )
by Darko
11:19
created

BrowseController::index()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 27
rs 9.0111
cc 6
nc 32
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Category;
6
use App\Models\RootCategory;
7
use App\Services\Releases\ReleaseBrowseService;
8
use Illuminate\Http\Request;
9
10
class BrowseController extends BasePageController
11
{
12
    private ReleaseBrowseService $releaseBrowseService;
13
14
    public function __construct(ReleaseBrowseService $releaseBrowseService)
15
    {
16
        parent::__construct();
17
        $this->releaseBrowseService = $releaseBrowseService;
18
    }
19
20
    /**
21
     * @throws \Exception
22
     */
23
    public function index(Request $request)
24
    {
25
        $ordering = $this->releaseBrowseService->getBrowseOrdering();
26
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : '';
27
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
28
        $offset = ($page - 1) * config('nntmux.items_per_page');
29
30
        $rslt = $this->releaseBrowseService->getBrowseRange($page, [-1], $offset, config('nntmux.items_per_page'), $orderBy, -1, $this->userdata->categoryexclusions, -1);
31
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
32
33
        // Build order by URLs
34
        $orderByUrls = [];
35
        foreach ($ordering as $orderType) {
36
            $orderByUrls['orderby'.$orderType] = url('browse/All?ob='.$orderType);
37
        }
38
39
        $this->viewData = array_merge($this->viewData, [
40
            'category' => -1,
41
            'catname' => 'All',
42
            'results' => $results,
43
            'lastvisit' => $this->userdata->lastlogin,
44
            'meta_title' => 'Browse All Releases',
45
            'meta_keywords' => 'browse,nzb,description,details',
46
            'meta_description' => 'Browse for Nzbs',
47
        ], $orderByUrls);
48
49
        return view('browse.index', $this->viewData);
50
    }
51
52
    /**
53
     * @throws \Exception
54
     */
55
    public function show(Request $request, string $parentCategory, string $id = 'All')
56
    {
57
58
        $parentId = RootCategory::query()->where('title', $parentCategory)->value('id');
59
60
        $query = Category::query()->where('title', $id)->where('root_categories_id', $parentId);
61
        if ($id !== 'All') {
62
            $cat = $query->first();
63
            $category = $cat !== null ? $cat->id : -1;
64
        } else {
65
            $category = $parentId ?? -1;
66
        }
67
68
        $grp = -1;
69
70
        $catarray = [];
71
        $catarray[] = $category;
72
73
        $ordering = $this->releaseBrowseService->getBrowseOrdering();
74
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : '';
75
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
76
        $offset = ($page - 1) * config('nntmux.items_per_page');
77
78
        $rslt = $this->releaseBrowseService->getBrowseRange($page, $catarray, $offset, config('nntmux.items_per_page'), $orderBy, -1, $this->userdata->categoryexclusions, $grp);
79
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
80
81
        $covgroup = '';
82
        $shows = false;
83
        if ($category === -1 && $grp === -1) {
84
            $catname = 'All';
85
        } elseif ($category !== -1 && $grp === -1) {
86
            $catname = $id;
87
            $cdata = Category::find($category);
88
            if ($cdata !== null) {
89
                if ($cdata->root_categories_id === Category::GAME_ROOT) {
90
                    $covgroup = 'console';
91
                } elseif ($cdata->root_categories_id === Category::MOVIE_ROOT) {
92
                    $covgroup = 'movies';
93
                } elseif ($cdata->root_categories_id === Category::PC_ROOT) {
94
                    $covgroup = 'games';
95
                } elseif ($cdata->root_categories_id === Category::MUSIC_ROOT) {
96
                    $covgroup = 'music';
97
                } elseif ($cdata->root_categories_id === Category::BOOKS_ROOT) {
98
                    $covgroup = 'books';
99
                } elseif ($cdata->root_categories_id === Category::TV_ROOT) {
100
                    $shows = true;
101
                }
102
            }
103
        } else {
104
            $catname = $grp;
105
        }
106
107
        // Build order by URLs
108
        $orderByUrls = [];
109
        if ($id === 'All' && $parentCategory === 'All') {
110
            $meta_title = 'Browse '.$parentCategory.' releases';
111
            foreach ($ordering as $orderType) {
112
                $orderByUrls['orderby'.$orderType] = url('browse/'.$parentCategory.'?ob='.$orderType);
113
            }
114
        } else {
115
            $meta_title = 'Browse '.$parentCategory.' / '.$id.' releases';
116
            foreach ($ordering as $orderType) {
117
                $orderByUrls['orderby'.$orderType] = url('browse/'.$parentCategory.'/'.$id.'?ob='.$orderType);
118
            }
119
        }
120
121
        $viewData = [
122
            'parentcat' => ucfirst($parentCategory),
123
            'category' => $category,
124
            'catname' => $catname,
125
            'results' => $results,
126
            'lastvisit' => $this->userdata->lastlogin,
127
            'covgroup' => $covgroup,
128
            'meta_title' => $meta_title,
129
            'meta_keywords' => 'browse,nzb,description,details',
130
            'meta_description' => 'Browse for Nzbs',
131
        ];
132
133
        if ($shows) {
134
            $viewData['shows'] = true;
135
        }
136
137
        $this->viewData = array_merge($this->viewData, $viewData, $orderByUrls);
138
139
        return view('browse.index', $this->viewData);
140
    }
141
142
    /**
143
     * @throws \Exception
144
     */
145
    public function group(Request $request)
146
    {
147
        if ($request->has('g')) {
148
            $group = $request->input('g');
149
            $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
150
            $offset = ($page - 1) * config('nntmux.items_per_page');
151
            $rslt = $this->releaseBrowseService->getBrowseRange($page, [-1], $offset, config('nntmux.items_per_page'), '', -1, $this->userdata->categoryexclusions, $group);
152
            $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
153
154
            $this->viewData = array_merge($this->viewData, [
155
                'results' => $results,
156
                'parentcat' => $group,
157
                'catname' => 'all',
158
                'lastvisit' => $this->userdata->lastlogin,
159
                'meta_title' => 'Browse Groups',
160
                'meta_keywords' => 'browse,nzb,description,details',
161
                'meta_description' => 'Browse Groups',
162
            ]);
163
164
            return view('browse.index', $this->viewData);
165
        }
166
167
        return redirect()->back()->with('error', 'Group parameter is required');
168
    }
169
}
170