Passed
Pull Request — master (#57)
by Stone
03:27
created

Category::update()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 33
nop 0
dl 0
loc 46
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\CategoryModel;
6
use Core\AdminController;
7
use Core\Constant;
8
use Core\Container;
9
10
class Category extends AdminController
11
{
12
13
    protected $siteConfig;
14
    protected $pagination;
15
16
    private $categoryModel;
17
18
19
    public function __construct(Container $container)
20
    {
21
        $this->loadModules[] = 'SiteConfig';
22
        $this->loadModules[] = 'pagination';
23
        parent::__construct($container);
24
25
        $this->categoryModel = new CategoryModel($this->container);
26
27
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
28
        $this->data['categories'] = $this->categoryModel->getCategories();
29
    }
30
31
    /**
32
     * Listing all the categories
33
     * @param string $page
34
     * @param int $linesPerPage
35
     * @throws \ReflectionException
36
     * @throws \Twig_Error_Loader
37
     * @throws \Twig_Error_Runtime
38
     * @throws \Twig_Error_Syntax
39
     */
40
    public function list(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
41
    {
42
        $this->onlyAdmin();
43
44
45
        $totalCategories = $this->categoryModel->countCategories();
46
        $pagination = $this->pagination->getPagination($page, $totalCategories, $linesPerPage);
47
48
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
49
            $this->data['paginationPostsPerPage'] = $linesPerPage;
50
        }
51
52
        $this->data["posts"] = $this->categoryModel->getCategoryList($pagination["offset"], $linesPerPage);
53
        $this->data['pagination'] = $pagination;
54
        $this->renderView("Admin/ListCategory");
55
    }
56
57
    /**
58
     * Post function to update a category
59
     * @throws \ErrorException
60
     * @throws \ReflectionException
61
     */
62
    public function update()
63
    {
64
        $this->onlyAdmin();
65
        $this->onlyPost();
66
67
        $category = $this->container->getRequest()->getDataFull();
68
69
        $categoryId = $category["idcategories"];
70
        $categoryName = $category["category_name"];
71
        $categorySlug = $category["categories_slug"];
72
73
        //Sanity check on ID
74
        if ($categoryId == null) {
75
            throw new \ErrorException("invalid category ID");
76
        }
77
78
        $originalCategorySlug = $this->categoryModel->getCategorySlugFromId($categoryId);
79
80
        //Error checking
81
        $error = false;
82
        if ($categoryName == "") {
83
            $error = true;
84
            $this->alertBox->setAlert("empty name not allowed", "error");
85
        }
86
        if ($categorySlug == "") {
87
            $error = true;
88
            $this->alertBox->setAlert("empty slug not allowed", "error");
89
        }
90
        if (!$this->categoryModel->isCategorySlugUnique($categorySlug) && $categorySlug !== $originalCategorySlug) {
91
            $error = true;
92
            $this->alertBox->setAlert("Slug not unique", "error");
93
        }
94
95
        if ($error) {
96
            $this->container->getResponse()->redirect("/admin/category/list");
97
        }
98
99
        $categoryUpdate = $this->categoryModel->update($categoryId, $categoryName, $categorySlug);
100
101
        //checking result and redirecting
102
        if ($categoryUpdate) {
103
            $this->alertBox->setAlert("Category " . $categoryName . " updated");
104
            $this->container->getResponse()->redirect("/admin/category/list/");
105
        }
106
        $this->alertBox->setAlert("Error updating " . $categoryName, "error");
107
        $this->container->getResponse()->redirect("/admin/category/list/");
108
    }
109
110
    /**
111
     * Delete a specific category
112
     * @param int $categoryId
113
     * @throws \Exception
114
     */
115
    public function delete(int $categoryId)
116
    {
117
        $this->onlyAdmin();
118
        $categoryName = $this->categoryModel->getNameFromId($categoryId);
119
120
        $removedCategory = $this->categoryModel->delete($categoryId);
121
122
        if ($removedCategory) {
123
            $this->alertBox->setAlert("Category " . $categoryName . " deleted");
124
        }
125
126
        $this->response->redirect("/admin/category/list/");
127
128
    }
129
130
    /**
131
     * create a new category
132
     */
133
    public function new()
134
    {
135
        $this->onlyAdmin();
136
        $this->onlyPost();
137
138
        $category = $this->container->getRequest()->getDataFull();
139
        $categoryName = $category["category_name"];
140
        $categorySlug = $category["categories_slug"];
141
142
        //Error checking
143
        $error = false;
144
        if ($categoryName == "") {
145
            $error = true;
146
            $this->alertBox->setAlert("empty name not allowed", "error");
147
        }
148
        if ($categorySlug == "") {
149
            $error = true;
150
            $this->alertBox->setAlert("empty slug not allowed", "error");
151
        }
152
        if (!$this->categoryModel->isCategorySlugUnique($categorySlug)) {
153
            $error = true;
154
            $this->alertBox->setAlert("Slug not unique", "error");
155
        }
156
157
        if ($error) {
158
            $this->container->getResponse()->redirect("/admin/category/list");
159
        }
160
161
        $categoryNew = $this->categoryModel->new($categoryName, $categorySlug);
162
163
        //checking result and redirecting
164
        if ($categoryNew) {
165
            $this->alertBox->setAlert("Category " . $categoryName . " created");
166
            $this->container->getResponse()->redirect("/admin/category/list/");
167
        }
168
        $this->alertBox->setAlert("Error creating " . $categoryName, "error");
169
        $this->container->getResponse()->redirect("/admin/category/list/");
170
    }
171
}