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