Completed
Push — master ( 278a25...5890e5 )
by Anthony
03:55
created

AdminArticle::setDeleteArticle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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 Intervention\Image\ImageManager;
9
	use modules\blog\app\controller\Article;
10
	use modules\blog\app\controller\Blog;
11
	
12
	class AdminArticle extends Article {
13
		private $error_title;
14
		private $error_article;
15
		
16
		
17
		
18
		//-------------------------- GETTER ----------------------------------------------------------------------------//
19
		/**
20
		 * @param $title
21
		 * @return bool
22
		 * function that verify if title of the article is ok
23
		 */
24
		private function getTestTitle($title, $id_article=null) {
25
			$dbc = App::getDb();
26
			
27
			if (strlen($title) < 5){
28
				$this->error_title = "votre titre doit être supérieur à 4 caractères";
29
				return false;
30
			}
31
			
32
			if (strlen($title) > 20) {
33
				$this->error_title = "votre titre ne doit pas eccéder 20 caractères";
34
				return false;
35
			}
36
			
37
			if ($id_article == null) {
38
				$query = $dbc->select()->from("_blog_article")->where("title", "=", $title)->get();
39
			}
40
			else {
41
				$query = $dbc->select()->from("_blog_article")
42
					->where("title", "=", $title, "AND")->where("ID_article", "!=", $id_article)->get();
43
			}
44
			
45
			if (count($query) > 0) {
46
				$this->error_title = "votre titre existe déjà merci d'en choisir un autre";
47
				return false;
48
			}
49
			
50
			return true;
51
		}
52
		
53
		/**
54
		 * @param $article
55
		 * @return bool
56
		 * function that verify if article is ok
57
		 */
58
		private function getTestArticle($article) {
59
			if (ChaineCaractere::testMinLenght($article, 10) == false) {
60
				$this->error_article = "votre article doit être supérieur à 10 caractères";
61
				return false;
62
			}
63
			
64
			return true;
65
		}
66
		
67
		/**
68
		 * @param $id_article
69
		 * @return bool
70
		 * test if an article exist in bdd
71
		 */
72
		private function getTestArticleExist($id_article) {
73
			$dbc = App::getDb();
74
			
75
			$query = $dbc->select()->from("_blog_article")->where("ID_article", "=", $id_article)->get();
76
			
77
			if (count($query) == 0) {
78
				return false;
79
			}
80
			
81
			return true;
82
		}
83
		
84
		/**
85
		 * @param $id_article
86
		 * @return mixed
87
		 * function that get url of an article
88
		 */
89
		private function getUrl($id_article) {
90
			$dbc = App::getDb();
91
			
92
			$query = $dbc->select("url")->from("_blog_article")->where("ID_article", "=", $id_article)->get();
93
			
94
			foreach ($query as $obj) {
95
				return $obj->url;
96
			}
97
		}
98
		
99
		/**
100
		 * this function get last articles
101
		 */
102
		public function getAllArticle($id_state = null) {
103
			$dbc = App::getDb();
104
			
105
			if ($id_state === null) {
106
				$query = $dbc->select()
107
					->from("_blog_article")
108
					->from("identite")
109
					->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
110
					->get();
111
			}
112
			else {
113
				$query = $dbc->select()
114
					->from("_blog_article")
115
					->from("identite")
116
					->where("_blog_article.ID_state", "=", $id_state, "AND")
117
					->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
118
					->get();
119
			}
120
			
121 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
122
				$articles = [];
123
				
124
				foreach ($query as $obj) {
125
					$articles[] = [
126
						"id_article" => $obj->ID_article,
127
						"title" => $obj->title,
128
						"url" => $obj->url,
129
						"image" => $this->getImageArticle($obj->url),
130
						"article" => $obj->article,
131
						"pseudo" => $obj->pseudo,
132
						"publication_date" => $this->getDateFr($obj->publication_date),
133
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
134
					];
135
				}
136
				
137
				Blog::setValues(["articles" => $articles]);
138
			}
139
		}
140
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
141
		
142
		
143
		//-------------------------- SETTER ----------------------------------------------------------------------------//
144
		/**
145
		 * @param $title
146
		 * function that add image to an article
147
		 */
148
		private function setImageArticle($title) {
149
			if (!empty($_FILES['image']['tmp_name'])) {
150
				$title_image = ChaineCaractere::setUrl($title);
151
				
152
				$name_image = ROOT."modules/blog/images/".$title_image.".png";
153
				
154
				$manager = new ImageManager();
155
				$image = $manager->make($_FILES['image']['tmp_name']);
156
				$image->crop(400, 400);
157
				$image->save($name_image);
158
			}
159
		}
160
		
161
		/**
162
		 * @param $title
163
		 * @param $categories
164
		 * @param $article
165
		 * @param $state
166
		 * @return bool
167
		 * function to add an article and his categories
168
		 */
169
		public function setAddArticle($title, $categories, $article, $state) {
170
			$dbc = App::getDb();
171
			
172 View Code Duplication
			if ($this->getTestTitle($title) == false || $this->getTestArticle($article) == 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...
173
				FlashMessage::setFlash($this->error_title.$this->error_article);
174
				return false;
175
			}
176
			
177
			$this->setImageArticle($title);
178
			
179
			$dbc->insert("title", $title)
180
				->insert("url", ChaineCaractere::setUrl($title))
181
				->insert("publication_date", date("Y-m-d H:i:s"))
182
				->insert("article", $article)
183
				->insert("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
184
				->insert("ID_state", $state)
185
				->into("_blog_article")->set();
186
			
187
			$id_article = $dbc->lastInsertId();
188
			
189
			AdminBlog::getAdminCategory()->setCategoriesArticle($categories, $id_article);
190
			return true;
191
		}
192
		
193
		/**
194
		 * @param $title
195
		 * @param $categories
196
		 * @param $article
197
		 * @param $state
198
		 * @return bool
199
		 * function to edit an article and his categories
200
		 */
201
		public function setEditArticle($title, $categories, $article, $state, $id_article) {
202
			$dbc = App::getDb();
203
			
204 View Code Duplication
			if ($this->getTestTitle($title, $id_article) == false || $this->getTestArticle($article) == 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...
205
				FlashMessage::setFlash($this->error_title.$this->error_article);
206
				return false;
207
			}
208
			
209
			if ($this->getTestArticleExist($id_article) == 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...
210
				FlashMessage::setFlash("votre article n'existe pas");
211
				return false;
212
			}
213
			
214
			$dbc->update("title", $title)
215
				->update("url", ChaineCaractere::setUrl($title))
216
				->update("publication_date", date("Y-m-d H:i:s"))
217
				->update("article", $article)
218
				->update("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
219
				->update("ID_state", $state)
220
				->from("_blog_article")
221
				->where("ID_article", "=", $id_article)
222
				->set();
223
			
224
			AdminBlog::getAdminCategory()->setUpdateCategoriesArticle($categories, $id_article);
225
			return true;
226
		}
227
		
228
		/**
229
		 * @param $id_article
230
		 * function that is used to delete an article
231
		 */
232
		public function setDeleteArticle($id_article) {
233
			$dbc = App::getDb();
234
			
235
			if ($this->getTestArticleExist($id_article) == true) {
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...
236
				unlink(ROOT."modules/blog/images/".$this->getUrl($id_article).".png");
237
				
238
				$dbc->delete()->from("_blog_article")->where("ID_article", "=", $id_article)->del();
239
			}
240
			
241
			FlashMessage::setFlash("Votre message a bien été supprimé", "success");
242
		}
243
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
244
	}