Tag   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

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