Completed
Push — master ( 0d0ae6...7c7732 )
by Stone
19s
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
        if (!$this->request->isPost()) {
62
            $this->alertBox->setAlert('Only post messages allowed', 'error');
63
            $this->response->redirect('admin');
64
        }
65
66
        $tag = $this->request->getDataFull();
67
68
        $tagId = $tag["idtags"];
69
        $tagName = $tag["tag_name"];
70
71
72
        //Sanity check on ID
73
        if ($tagId == null) {
74
            throw new \ErrorException("invalid tag ID");
75
        }
76
77
        //Error checking
78
        $error = false;
79
        if ($tagName == "") {
80
            $error = true;
81
            $this->alertBox->setAlert("empty name not allowed", "error");
82
        }
83
84
        if ($error) {
85
            $this->response->redirect("/admin/tag/list");
86
        }
87
88
        $tagUpdate = $this->tagModel->update($tagId, $tagName);
89
90
        //checking result and redirecting
91
        if ($tagUpdate) {
92
            $this->alertBox->setAlert("Tag " . $tagName . " updated");
93
            $this->response->redirect("/admin/tag/list/");
94
        }
95
        $this->alertBox->setAlert("Error updating " . $tagName, "error");
96
        $this->response->redirect("/admin/tag/list/");
97
    }
98
99
    /**
100
     * Delete a specific tag
101
     * @param int $tagId
102
     * @throws \Exception
103
     */
104
    public function delete(int $tagId)
105
    {
106
        $this->onlyAdmin();
107
        $tagName = $this->tagModel->getNameFromId($tagId);
108
109
        $removedTag = $this->tagModel->delete($tagId);
110
111
        if ($removedTag) {
112
            $this->alertBox->setAlert("Tag " . $tagName . " deleted");
113
        }
114
115
        $this->response->redirect("/admin/tag/list/");
116
117
    }
118
119
    /**
120
     * create a new tag
121
     */
122
    public function new()
123
    {
124
        $this->onlyAdmin();
125
        if (!$this->request->isPost()) {
126
            $this->alertBox->setAlert('Only post messages allowed', 'error');
127
            $this->response->redirect('admin');
128
        }
129
130
        $tag = $this->request->getDataFull();
131
        $tagName = $tag["tag_name"];
132
133
        //Error checking
134
        $error = false;
135
        if ($tagName == "") {
136
            $error = true;
137
            $this->alertBox->setAlert("empty name not allowed", "error");
138
        }
139
140
        if ($error) {
141
            $this->container->getResponse()->redirect("/admin/tag/list");
142
        }
143
144
        $tagNew = $this->tagModel->new($tagName);
145
146
        //checking result and redirecting
147
        if ($tagNew) {
148
            $this->alertBox->setAlert("Tag " . $tagName . " created");
149
            $this->response->redirect("/admin/tag/list/");
150
        }
151
        $this->alertBox->setAlert("Error creating " . $tagName, "error");
152
        $this->response->redirect("/admin/tag/list/");
153
    }
154
}