Passed
Pull Request — master (#58)
by Stone
04:29 queued 02:04
created

Tag::delete()   A

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\TagModel;
6
use Core\AdminController;
7
use Core\Constant;
8
use Core\Container;
9
10
class Tag extends AdminController
11
{
12
13
    protected $siteConfig;
14
    protected $pagination;
15
16
    private $tagModel;
17
18
    public function __construct(Container $container)
19
    {
20
        $this->loadModules[] = 'SiteConfig';
21
        $this->loadModules[] = 'pagination';
22
        parent::__construct($container);
23
24
        $this->tagModel = new TagModel($this->container);
25
26
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
27
    }
28
29
    /**
30
     * List all the posts with pagination
31
     * @param string $page
32
     * @param int $linesPerPage
33
     * @throws \ReflectionException
34
     * @throws \Twig_Error_Loader
35
     * @throws \Twig_Error_Runtime
36
     * @throws \Twig_Error_Syntax
37
     */
38
    public function list(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
39
    {
40
        $this->onlyAdmin();
41
42
        $totalCategories = $this->tagModel->countTags();
43
        $pagination = $this->pagination->getPagination($page, $totalCategories, $linesPerPage);
44
45
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
46
            $this->data['paginationPostsPerPage'] = $linesPerPage;
47
        }
48
49
        $this->data["posts"] = $this->tagModel->getTagList($pagination["offset"], $linesPerPage);
50
        $this->data['pagination'] = $pagination;
51
        $this->renderView("Admin/ListTag");
52
    }
53
54
    /**
55
     * Post function to update a tag
56
     * @throws \ErrorException
57
     */
58
    public function update()
59
    {
60
        $this->onlyAdmin();
61
        $this->onlyPost();
62
63
        $tag = $this->request->getDataFull();
64
65
        $tagId = $tag["idtags"];
66
        $tagName = $tag["tag_name"];
67
68
69
        //Sanity check on ID
70
        if ($tagId == null) {
71
            throw new \ErrorException("invalid tag ID");
72
        }
73
74
        //Error checking
75
        $error = false;
76
        if ($tagName == "") {
77
            $error = true;
78
            $this->alertBox->setAlert("empty name not allowed", "error");
79
        }
80
81
        if ($error) {
82
            $this->response->redirect("/admin/tag/list");
83
        }
84
85
        $tagUpdate = $this->tagModel->update($tagId, $tagName);
86
87
        //checking result and redirecting
88
        if ($tagUpdate) {
89
            $this->alertBox->setAlert("Tag " . $tagName . " updated");
90
            $this->response->redirect("/admin/tag/list/");
91
        }
92
        $this->alertBox->setAlert("Error updating " . $tagName, "error");
93
        $this->response->redirect("/admin/tag/list/");
94
    }
95
96
    /**
97
     * Delete a specific tag
98
     * @param int $tagId
99
     * @throws \Exception
100
     */
101
    public function delete(int $tagId)
102
    {
103
        $this->onlyAdmin();
104
        $tagName = $this->tagModel->getNameFromId($tagId);
105
106
        $removedTag = $this->tagModel->delete($tagId);
107
108
        if ($removedTag) {
109
            $this->alertBox->setAlert("Tag " . $tagName . " deleted");
110
        }
111
112
        $this->response->redirect("/admin/tag/list/");
113
114
    }
115
116
    /**
117
     * create a new tag
118
     */
119
    public function new()
120
    {
121
        $this->onlyAdmin();
122
        $this->onlyPost();
123
124
        $tag = $this->request->getDataFull();
125
        $tagName = $tag["tag_name"];
126
127
        //Error checking
128
        $error = false;
129
        if ($tagName == "") {
130
            $error = true;
131
            $this->alertBox->setAlert("empty name not allowed", "error");
132
        }
133
134
        if ($error) {
135
            $this->container->getResponse()->redirect("/admin/tag/list");
136
        }
137
138
        $tagNew = $this->tagModel->new($tagName);
139
140
        //checking result and redirecting
141
        if ($tagNew) {
142
            $this->alertBox->setAlert("Tag " . $tagName . " created");
143
            $this->response->redirect("/admin/tag/list/");
144
        }
145
        $this->alertBox->setAlert("Error creating " . $tagName, "error");
146
        $this->response->redirect("/admin/tag/list/");
147
    }
148
}