Passed
Push — master ( 9f15d2...3a106b )
by Darko
10:30
created

AdminCategoryController::create()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 31
c 1
b 1
f 0
dl 0
loc 50
rs 8.8017
cc 6
nc 5
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 App\Models\RootCategory;
8
use Illuminate\Http\Request;
9
10
class AdminCategoryController extends BasePageController
11
{
12
    /**
13
     * @throws \Exception
14
     */
15
    public function index()
16
    {
17
        $this->setAdminPrefs();
18
        $meta_title = $title = 'Category List';
19
20
        $categorylist = Category::getFlat();
21
22
        $this->viewData = array_merge($this->viewData, [
23
            'categorylist' => $categorylist,
24
            'title' => $title,
25
            'meta_title' => $meta_title,
26
        ]);
27
28
        return view('admin.categories.index', $this->viewData);
29
    }
30
31
    /**
32
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
33
     *
34
     * @throws \Exception
35
     */
36
    public function create(Request $request)
37
    {
38
        $this->setAdminPrefs();
39
40
        // set the current action
41
        $action = $request->input('action') ?? 'view';
42
43
        switch ($action) {
44
            case 'submit':
45
                // Create new category
46
                $category = new Category;
47
48
                // Allow custom ID only when creating
49
                if ($request->filled('id')) {
50
                    $customId = $request->input('id');
51
                    // Check if ID already exists
52
                    if (Category::where('id', $customId)->exists()) {
53
                        return redirect()->back()->withInput()->with('error', 'Category ID '.$customId.' already exists. Please choose a different ID.');
54
                    }
55
                    $category->id = $customId;
56
                }
57
58
                $category->title = $request->input('title');
59
                $category->root_categories_id = $request->input('root_categories_id') ?: null;
60
                $category->status = Category::STATUS_ACTIVE;  // Always active
61
                $category->description = $request->input('description');
62
                $category->disablepreview = 0;  // Always enabled
63
                $category->minsizetoformrelease = 0;
64
                $category->maxsizetoformrelease = 0;
65
                $category->save();
66
67
                return redirect()->to('admin/category-list')->with('success', 'Category created successfully');
68
            case 'view':
69
            default:
70
                $title = 'Add New Category';
71
                break;
72
        }
73
74
        // Get root categories for parent selection
75
        $rootCategories = RootCategory::orderBy('title')->get();
76
77
        $this->viewData = array_merge($this->viewData, [
78
            'category' => null,
79
            'rootCategories' => $rootCategories,
80
            'title' => $title,
81
            'meta_title' => 'Add New Category',
82
            'isCreate' => true,
83
        ]);
84
85
        return view('admin.categories.edit', $this->viewData);
86
    }
87
88
    /**
89
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
90
     *
91
     * @throws \Exception
92
     */
93
    public function edit(Request $request)
94
    {
95
        $this->setAdminPrefs();
96
97
        // set the current action
98
        $action = $request->input('action') ?? 'view';
99
100
        switch ($action) {
101
            case 'submit':
102
                $category = Category::find($request->input('id'));
103
                if ($category) {
104
                    $category->root_categories_id = $request->input('root_categories_id') ?: null;
105
                    $category->status = Category::STATUS_ACTIVE;  // Always active
106
                    $category->description = $request->input('description');
107
                    $category->disablepreview = 0;  // Always enabled
108
                    $category->minsizetoformrelease = 0;
109
                    $category->maxsizetoformrelease = 0;
110
                    $category->save();
111
                }
112
113
                return redirect()->to('admin/category-list')->with('success', 'Category updated successfully');
114
            case 'view':
115
            default:
116
                $category = null;
117
                $title = 'Category Edit';
118
                if ($request->has('id')) {
119
                    $id = $request->input('id');
120
                    $category = Category::find($id);
121
                }
122
                break;
123
        }
124
125
        // Get root categories for parent selection
126
        $rootCategories = RootCategory::orderBy('title')->get();
127
128
        $this->viewData = array_merge($this->viewData, [
129
            'category' => $category,
130
            'rootCategories' => $rootCategories,
131
            'title' => $title,
132
            'meta_title' => 'View/Edit categories',
133
            'isCreate' => false,
134
        ]);
135
136
        return view('admin.categories.edit', $this->viewData);
137
    }
138
}
139