Completed
Push — master ( c2f5ca...c699f6 )
by Anthony
02:11
created

AdminArticle::setImageArticle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
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
		 * this function get last articles
86
		 */
87
		public function getAllArticle($id_state = null) {
88
			$dbc = App::getDb();
89
			
90
			if ($id_state === null) {
91
				$query = $dbc->select()
92
					->from("_blog_article")
93
					->from("identite")
94
					->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
95
					->get();
96
			}
97
			else {
98
				$query = $dbc->select()
99
					->from("_blog_article")
100
					->from("identite")
101
					->where("_blog_article.ID_state", "=", $id_state, "AND")
102
					->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
103
					->get();
104
			}
105
			
106 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
107
				$articles = [];
108
				
109
				foreach ($query as $obj) {
110
					$articles[] = [
111
						"id_article" => $obj->ID_article,
112
						"title" => $obj->title,
113
						"url" => $obj->url,
114
						"image" => $this->getImageArticle($obj->url),
115
						"article" => $obj->article,
116
						"pseudo" => $obj->pseudo,
117
						"publication_date" => $obj->publication_date,
118
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
119
					];
120
				}
121
				
122
				Blog::setValues(["articles" => $articles]);
123
			}
124
		}
125
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
126
		
127
		
128
		//-------------------------- SETTER ----------------------------------------------------------------------------//
129
		/**
130
		 * @param $title
131
		 * function that add image to an article
132
		 */
133
		private function setImageArticle($title) {
134
			if (!empty($_FILES['image']['tmp_name'])) {
135
				$title_image = ChaineCaractere::setUrl($title);
136
				
137
				$name_image = ROOT."modules/blog/images/".$title_image.".png";
138
				
139
				$manager = new ImageManager();
140
				$image = $manager->make($_FILES['image']['tmp_name']);
141
				$image->crop(400, 400);
142
				$image->save($name_image);
143
			}
144
		}
145
		
146
		/**
147
		 * @param $title
148
		 * @param $categories
149
		 * @param $article
150
		 * @param $state
151
		 * @return bool
152
		 * function to add an article and his categories
153
		 */
154
		public function setAddArticle($title, $categories, $article, $state) {
155
			$dbc = App::getDb();
156
			
157 View Code Duplication
			if ($this->getTestTitle($title) == false || $this->getTestArticle($article) == false) {
1 ignored issue
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...
Duplication introduced by
This code seems to be duplicated across 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...
158
				FlashMessage::setFlash($this->error_title.$this->error_article);
159
				return false;
160
			}
161
			
162
			$this->setImageArticle($title);
163
			
164
			$dbc->insert("title", $title)
165
				->insert("url", ChaineCaractere::setUrl($title))
166
				->insert("publication_date", date("Y-m-d H:i:s"))
167
				->insert("article", $article)
168
				->insert("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
169
				->insert("ID_state", $state)
170
				->into("_blog_article")->set();
171
			
172
			$id_article = $dbc->lastInsertId();
173
			
174
			AdminBlog::getAdminCategory()->setCategoriesArticle($categories, $id_article);
175
			return true;
176
		}
177
		
178
		/**
179
		 * @param $title
180
		 * @param $categories
181
		 * @param $article
182
		 * @param $state
183
		 * @return bool
184
		 * function to edit an article and his categories
185
		 */
186
		public function setEditArticle($title, $categories, $article, $state, $id_article) {
187
			$dbc = App::getDb();
188
			
189 View Code Duplication
			if ($this->getTestTitle($title, $id_article) == false || $this->getTestArticle($article) == false) {
1 ignored issue
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...
Duplication introduced by
This code seems to be duplicated across 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...
190
				FlashMessage::setFlash($this->error_title.$this->error_article);
191
				return false;
192
			}
193
			
194
			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...
195
				FlashMessage::setFlash("votre article n'existe pas");
196
				return false;
197
			}
198
			
199
			$dbc->update("title", $title)
200
				->update("url", ChaineCaractere::setUrl($title))
201
				->update("publication_date", date("Y-m-d H:i:s"))
202
				->update("article", $article)
203
				->update("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
204
				->update("ID_state", $state)
205
				->from("_blog_article")
206
				->where("ID_article", "=", $id_article)
207
				->set();
208
			
209
			AdminBlog::getAdminCategory()->setUpdateCategoriesArticle($categories, $id_article);
210
			return true;
211
		}
212
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
213
	}