Completed
Push — master ( 08cfe9...f5e65d )
by Anthony
05:26
created

AdminCategory::setEditCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
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
				
64
				return $obj->category;
0 ignored issues
show
Bug introduced by
The variable $obj seems to be defined by a foreach iteration on line 57. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
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) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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
	}