Completed
Push — master ( 0d0ae6...7c7732 )
by Stone
19s
created

Category   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 80
dl 0
loc 166
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 12 2
B new() 0 40 7
B update() 0 49 9
A __construct() 0 10 1
A list() 0 15 2
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
        if (!$this->request->isPost()) {
66
            $this->alertBox->setAlert('Only post messages allowed', 'error');
67
            $this->response->redirect('admin');
68
        }
69
70
        $category = $this->container->getRequest()->getDataFull();
71
72
        $categoryId = $category["idcategories"];
73
        $categoryName = $category["category_name"];
74
        $categorySlug = $category["categories_slug"];
75
76
        //Sanity check on ID
77
        if ($categoryId == null) {
78
            throw new \ErrorException("invalid category ID");
79
        }
80
81
        $originalCategorySlug = $this->categoryModel->getCategorySlugFromId($categoryId);
82
83
        //Error checking
84
        $error = false;
85
        if ($categoryName == "") {
86
            $error = true;
87
            $this->alertBox->setAlert("empty name not allowed", "error");
88
        }
89
        if ($categorySlug == "") {
90
            $error = true;
91
            $this->alertBox->setAlert("empty slug not allowed", "error");
92
        }
93
        if (!$this->categoryModel->isCategorySlugUnique($categorySlug) && $categorySlug !== $originalCategorySlug) {
94
            $error = true;
95
            $this->alertBox->setAlert("Slug not unique", "error");
96
        }
97
98
        if ($error) {
99
            $this->container->getResponse()->redirect("/admin/category/list");
100
        }
101
102
        $categoryUpdate = $this->categoryModel->update($categoryId, $categoryName, $categorySlug);
103
104
        //checking result and redirecting
105
        if ($categoryUpdate) {
106
            $this->alertBox->setAlert("Category " . $categoryName . " updated");
107
            $this->container->getResponse()->redirect("/admin/category/list/");
108
        }
109
        $this->alertBox->setAlert("Error updating " . $categoryName, "error");
110
        $this->container->getResponse()->redirect("/admin/category/list/");
111
    }
112
113
    /**
114
     * Delete a specific category
115
     * @param int $categoryId
116
     * @throws \Exception
117
     */
118
    public function delete(int $categoryId)
119
    {
120
        $this->onlyAdmin();
121
        $categoryName = $this->categoryModel->getNameFromId($categoryId);
122
123
        $removedCategory = $this->categoryModel->delete($categoryId);
124
125
        if ($removedCategory) {
126
            $this->alertBox->setAlert("Category " . $categoryName . " deleted");
127
        }
128
129
        $this->response->redirect("/admin/category/list/");
130
131
    }
132
133
    /**
134
     * create a new category
135
     */
136
    public function new()
137
    {
138
        $this->onlyAdmin();
139
        if (!$this->request->isPost()) {
140
            $this->alertBox->setAlert('Only post messages allowed', 'error');
141
            $this->response->redirect('admin');
142
        }
143
144
        $category = $this->container->getRequest()->getDataFull();
145
        $categoryName = $category["category_name"];
146
        $categorySlug = $category["categories_slug"];
147
148
        //Error checking
149
        $error = false;
150
        if ($categoryName == "") {
151
            $error = true;
152
            $this->alertBox->setAlert("empty name not allowed", "error");
153
        }
154
        if ($categorySlug == "") {
155
            $error = true;
156
            $this->alertBox->setAlert("empty slug not allowed", "error");
157
        }
158
        if (!$this->categoryModel->isCategorySlugUnique($categorySlug)) {
159
            $error = true;
160
            $this->alertBox->setAlert("Slug not unique", "error");
161
        }
162
163
        if ($error) {
164
            $this->container->getResponse()->redirect("/admin/category/list");
165
        }
166
167
        $categoryNew = $this->categoryModel->new($categoryName, $categorySlug);
168
169
        //checking result and redirecting
170
        if ($categoryNew) {
171
            $this->alertBox->setAlert("Category " . $categoryName . " created");
172
            $this->container->getResponse()->redirect("/admin/category/list/");
173
        }
174
        $this->alertBox->setAlert("Error creating " . $categoryName, "error");
175
        $this->container->getResponse()->redirect("/admin/category/list/");
176
    }
177
}