Passed
Push — dev ( c55b95...34fb50 )
by Darko
06:54
created

AdminCategoryRegexesController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Models\Category;
7
use Blacklight\Regexes;
8
use Illuminate\Http\Request;
9
10
class AdminCategoryRegexesController extends BasePageController
11
{
12
    /**
13
     * @param \Illuminate\Http\Request $request
14
     *
15
     * @throws \Exception
16
     */
17
    public function index(Request $request)
18
    {
19
        $this->setAdminPrefs();
20
        $regexes = new Regexes(['Settings' =>null, 'Table_Name' => 'category_regexes']);
21
22
        $meta_title = $title = 'Category Regex List';
23
24
        $group = $request->has('group') && ! empty($request->input('group')) ? $request->input('group') : '';
25
        $regex = $regexes->getRegex($group);
26
27
        $this->smarty->assign(
0 ignored issues
show
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

27
        $this->smarty->/** @scrutinizer ignore-call */ 
28
                       assign(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
            [
29
                'group' => $group,
30
                'regex' => $regex,
31
            ]
32
        );
33
34
        $content = $this->smarty->fetch('category_regexes-list.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

34
        /** @scrutinizer ignore-call */ 
35
        $content = $this->smarty->fetch('category_regexes-list.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
37
38
        $this->adminrender();
39
    }
40
41
    /**
42
     * @param \Illuminate\Http\Request $request
43
     *
44
     * @throws \Exception
45
     */
46
    public function edit(Request $request)
47
    {
48
        $this->setAdminPrefs();
49
        $regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']);
50
51
        // Set the current action.
52
        $action = $request->input('action') ?? 'view';
53
54
        $regex = [
55
            'id' => '',
56
            'group_regex' => '',
57
            'regex' => '',
58
            'description' => '',
59
            'ordinal' => '',
60
            'categories_id' => '',
61
            'status' => 1, ];
62
63
        $this->smarty->assign('regex', $regex);
64
65
        switch ($action) {
66
            case 'submit':
67
                if (empty($request->input('group_regex'))) {
68
                    $this->smarty->assign('error', 'Group regex must not be empty!');
69
                    break;
70
                }
71
72
                if (empty($request->input('regex'))) {
73
                    $this->smarty->assign('error', 'Regex cannot be empty');
74
                    break;
75
                }
76
77
                if (! is_numeric($request->input('ordinal')) || $request->input('ordinal') < 0) {
78
                    $this->smarty->assign('error', 'Ordinal must be a number, 0 or higher.');
79
                    break;
80
                }
81
82
                if (empty($request->input('id'))) {
83
                    $regexes->addRegex($request->all());
84
                } else {
85
                    $regexes->updateRegex($request->all());
86
                }
87
88
                return redirect('admin/category_regexes-list');
89
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
90
91
            case 'view':
92
            default:
93
                if ($request->has('id')) {
94
                    $meta_title = $title = 'Category Regex Edit';
95
                    $id = $request->input('id');
96
                    $regex = $regexes->getRegexByID($id);
97
                } else {
98
                    $meta_title = $title = 'Category Regex Add';
99
                }
100
                $this->smarty->assign('regex', $regex);
101
                break;
102
        }
103
104
        $this->smarty->assign('status_ids', [Category::STATUS_ACTIVE, Category::STATUS_INACTIVE]);
105
        $this->smarty->assign('status_names', ['Yes', 'No']);
106
107
        $categories_db = Category::query()
108
            ->select(['c.id', 'c.title', 'cp.title as parent_title'])
109
            ->from('categories as c')
110
            ->join('root_categories as cp', 'c.root_categories_id', '=', 'cp.id')
111
            ->whereNotNull('c.root_categories_id')
112
            ->orderBy('c.id')
113
            ->get();
114
        $categories = ['category_names', 'category_ids'];
115
        if ($categories_db) {
116
            foreach ($categories_db as $category_db) {
117
                $categories['category_names'][] = $category_db->parent_title.' '.$category_db->title.': '.$category_db->id;
118
                $categories['category_ids'][] = $category_db->id;
119
            }
120
        }
121
        $this->smarty->assign('category_names', $categories['category_names']);
122
        $this->smarty->assign('category_ids', $categories['category_ids']);
123
124
        $content = $this->smarty->fetch('category_regexes-edit.tpl');
125
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
126
        $this->adminrender();
127
    }
128
}
129