Passed
Push — Showing-Posts ( 80516c...edf5ba )
by Stone
01:53
created

Category::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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