Category::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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