1
|
|
|
<?php |
2
|
|
|
namespace modules\blog\admin\controller; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use core\App; |
6
|
|
|
|
7
|
|
|
class AdminCategory { |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
11
|
|
|
public function __construct() { |
12
|
|
|
|
13
|
|
|
} |
14
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
18
|
|
|
/** |
19
|
|
|
* @param $name |
20
|
|
|
* @return bool |
21
|
|
|
* test if a category exist |
22
|
|
|
*/ |
23
|
|
|
private function getTestCategoryExist($name) { |
24
|
|
|
$dbc = App::getDb(); |
25
|
|
|
|
26
|
|
|
$query = $dbc->select()->from("_blog_category")->where("category", "=", $name)->get(); |
27
|
|
|
|
28
|
|
|
if (count($query) > 0) { |
29
|
|
|
foreach ($query as $obj) { |
30
|
|
|
|
31
|
|
|
return $obj->ID_category; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
41
|
|
|
/** |
42
|
|
|
* @param $categories |
43
|
|
|
* @param $id_article |
44
|
|
|
* add list of categories to an article |
45
|
|
|
*/ |
46
|
|
|
public function setCategoriesArticle($categories, $id_article) { |
47
|
|
|
$dbc = App::getDb(); |
48
|
|
|
|
49
|
|
|
$count = count($categories); |
50
|
|
|
if ((is_array($categories)) && ($count > 0)) { |
51
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
52
|
|
|
if ($this->getTestCategoryExist($categories[$i]) != false) { |
|
|
|
|
53
|
|
|
$dbc->insert("ID_category", $this->getTestCategoryExist($categories[$i]))->insert("ID_article", $id_article)->into("_blog_article_category")->set(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $categories |
61
|
|
|
* @param $id_article |
62
|
|
|
* function to update catgories, in first step delete all categories and reinsert it after |
63
|
|
|
*/ |
64
|
|
|
public function setUpdateCategoriesArticle($categories, $id_article) { |
65
|
|
|
$dbc = App::getDb(); |
66
|
|
|
|
67
|
|
|
$dbc->delete()->from("_blog_article_category")->where("ID_article", "=", $id_article)->del(); |
68
|
|
|
|
69
|
|
|
$this->setCategoriesArticle($categories, $id_article); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $id_category |
74
|
|
|
* function that can delete an category and all article related to it |
75
|
|
|
*/ |
76
|
|
|
public function setDeleteCategory($id_category) { |
77
|
|
|
$dbc = App::getDb(); |
78
|
|
|
|
79
|
|
|
$dbc->delete()->from("_blog_category")->where("ID_category", "=", $id_category)->del(); |
80
|
|
|
$dbc->delete()->from("_blog_article_category")->where("ID_category", "=", $id_category)->del(); |
81
|
|
|
} |
82
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
83
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.