1
|
|
|
<?php |
2
|
|
|
namespace modules\blog\admin\controller; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use core\App; |
6
|
|
|
use core\functions\ChaineCaractere; |
7
|
|
|
use core\HTML\flashmessage\FlashMessage; |
8
|
|
|
use modules\blog\app\controller\Blog; |
9
|
|
|
|
10
|
|
|
class AdminCategory { |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
14
|
|
|
public function __construct() { |
15
|
|
|
|
16
|
|
|
} |
17
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
21
|
|
|
/** |
22
|
|
|
* @param $name |
23
|
|
|
* @param null $id |
24
|
|
|
* @return bool |
25
|
|
|
* test if a category exist |
26
|
|
|
*/ |
27
|
|
|
private function getTestCategoryExist($name, $id=null) { |
28
|
|
|
$dbc = App::getDb(); |
29
|
|
|
|
30
|
|
|
if ($id === null) { |
31
|
|
|
$query = $dbc->select()->from("_blog_category")->where("category", "=", $name)->get(); |
32
|
|
|
} |
33
|
|
|
else { |
34
|
|
|
$query = $dbc->select()->from("_blog_category") |
35
|
|
|
->where("category", "=", $name, "AND")->where("ID_category", "!=", $id)->get(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (count($query) > 0) { |
39
|
|
|
foreach ($query as $obj) { |
40
|
|
|
|
41
|
|
|
return $obj->ID_category; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* function t get name of a category with it url |
50
|
|
|
*/ |
51
|
|
|
public function getNameCategoryUrl() { |
52
|
|
|
$dbc = App::getDb(); |
53
|
|
|
|
54
|
|
|
$query = $dbc->select()->from("_blog_category")->where("url_category", "=", Blog::$router_parameter)->get(); |
55
|
|
|
|
56
|
|
|
if (count($query) == 1) { |
57
|
|
|
foreach ($query as $obj) { |
58
|
|
|
Blog::setValues([ |
59
|
|
|
"category_name" => $obj->category, |
60
|
|
|
"id_category" => $obj->ID_category |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
return $obj->category; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
71
|
|
|
/** |
72
|
|
|
* @param $name |
73
|
|
|
* @return bool |
74
|
|
|
* function to add a category |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function setAddCategory($name) { |
77
|
|
|
$dbc = App::getDb(); |
78
|
|
|
|
79
|
|
|
if ($this->getTestCategoryExist($name) !== false) { |
80
|
|
|
FlashMessage::setFlash("Cette catégorie existe déjà, merci de changer de nom"); |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$dbc->insert("category", $name)->insert("url_category", ChaineCaractere::setUrl($name))->into("_blog_category")->set(); |
85
|
|
|
FlashMessage::setFlash("Votre catégorie a été correctement ajoutée", "success"); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $name |
91
|
|
|
* @param $id |
92
|
|
|
* @return bool |
93
|
|
|
* function to edit the name of a category |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function setEditCategory($name, $id) { |
96
|
|
|
$dbc = App::getDb(); |
97
|
|
|
|
98
|
|
|
if ($this->getTestCategoryExist($name, $id) !== false) { |
99
|
|
|
FlashMessage::setFlash("Cette catégorie existe déjà, merci de changer de nom"); |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$dbc->update("category", $name)->update("url_category", ChaineCaractere::setUrl($name)) |
104
|
|
|
->from("_blog_category")->where("ID_category", "=", $id)->set(); |
105
|
|
|
|
106
|
|
|
FlashMessage::setFlash("Votre catégorie a été correctement ajoutée", "success"); |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $categories |
112
|
|
|
* @param $id_article |
113
|
|
|
* add list of categories to an article |
114
|
|
|
*/ |
115
|
|
|
public function setCategoriesArticle($categories, $id_article) { |
116
|
|
|
$dbc = App::getDb(); |
117
|
|
|
|
118
|
|
|
$count = count($categories); |
119
|
|
|
if ((is_array($categories)) && ($count > 0)) { |
120
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
121
|
|
|
if ($this->getTestCategoryExist($categories[$i]) !== false) { |
122
|
|
|
$dbc->insert("ID_category", $this->getTestCategoryExist($categories[$i]))->insert("ID_article", $id_article)->into("_blog_article_category")->set(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $categories |
130
|
|
|
* @param $id_article |
131
|
|
|
* function to update catgories, in first step delete all categories and reinsert it after |
132
|
|
|
*/ |
133
|
|
|
public function setUpdateCategoriesArticle($categories, $id_article) { |
134
|
|
|
$dbc = App::getDb(); |
135
|
|
|
|
136
|
|
|
$dbc->delete()->from("_blog_article_category")->where("ID_article", "=", $id_article)->del(); |
137
|
|
|
|
138
|
|
|
$this->setCategoriesArticle($categories, $id_article); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $id_category |
143
|
|
|
* function that can delete an category and all article related to it |
144
|
|
|
*/ |
145
|
|
|
public function setDeleteCategory($id_category) { |
146
|
|
|
$dbc = App::getDb(); |
147
|
|
|
|
148
|
|
|
$dbc->delete()->from("_blog_category")->where("ID_category", "=", $id_category)->del(); |
149
|
|
|
$dbc->delete()->from("_blog_article_category")->where("ID_category", "=", $id_category)->del(); |
150
|
|
|
} |
151
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
152
|
|
|
} |