Completed
Push — master ( 5b50de...13a6fd )
by Anthony
02:07
created

AdminArticle::setEditArticle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 4
Ratio 15.38 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 26
rs 8.5806
cc 4
eloc 18
nc 3
nop 5
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 AdminArticle {
11
		private $error_title;
12
		private $error_article;
13
		
14
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
15
		public function __construct() {
16
			
17
		}
18
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
19
		
20
		
21
		//-------------------------- GETTER ----------------------------------------------------------------------------//
22
		/**
23
		 * @param $title
24
		 * @return bool
25
		 * function that verify if title of the article is ok
26
		 */
27
		private function getTestTitle($title) {
28
			$dbc = App::getDb();
29
			
30
			if (ChaineCaractere::testMinLenght($title, 4) == false) {
31
				$this->error_title = "votre titre doit être supérieur à 4 caractères";
32
				return false;
33
			}
34
			
35
			if (strlen($title) > 20) {
36
				$this->error_title = "votre titre ne doit pas eccéder 20 caractères";
37
				return false;
38
			}
39
			
40
			$query = $dbc->select()->from("_blog_article")->where("title", "=", $title)->get();
41
			
42
			if (count($query) > 0) {
43
				$this->error_title = "votre titre existe déjà merci d'en choisir un autre";
44
				return false;
45
			}
46
			
47
			return true;
48
		}
49
		
50
		/**
51
		 * @param $article
52
		 * @return bool
53
		 * function that verify if article is ok
54
		 */
55
		private function getTestArticle($article) {
56
			if (ChaineCaractere::testMinLenght($article, 10) == false) {
57
				$this->error_article = "votre article doit être supérieur à 10 caractères";
58
				return false;
59
			}
60
			
61
			return true;
62
		}
63
		
64
		/**
65
		 * @param $id_article
66
		 * @return bool
67
		 * test if an article exist in bdd
68
		 */
69
		private function getTestArticleExist($id_article) {
70
			$dbc = App::getDb();
71
			
72
			$query = $dbc->select()->from("_blog_article")->where("ID_article", "=", $id_article)->get();
73
			
74
			if (count($query) == 0) {
75
				return false;
76
			}
77
			
78
			return true;
79
		}
80
		
81
		/**
82
		 * this function get last articles
83
		 */
84 View Code Duplication
		public function getAllArticle() {
0 ignored issues
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...
85
			$dbc = App::getDb();
86
			
87
			$query = $dbc->select()
88
				->from("_blog_article")
89
				->from("identite")
90
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
91
				->get();
92
			
93
			if ((is_array($query)) && (count($query) > 0)) {
94
				$articles = [];
95
				
96
				foreach ($query as $obj) {
97
					$articles[] = [
98
						"id_article" => $obj->ID_article,
99
						"title" => $obj->title,
100
						"url" => $obj->url,
101
						"article" => $obj->article,
102
						"pseudo" => $obj->pseudo,
103
						"publication_date" => $obj->publication_date,
104
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
105
					];
106
				}
107
				
108
				Blog::setValues(["articles" => $articles]);
109
			}
110
		}
111
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
112
		
113
		
114
		//-------------------------- SETTER ----------------------------------------------------------------------------//
115
		/**
116
		 * @param $title
117
		 * @param $categories
118
		 * @param $article
119
		 * @param $state
120
		 * @return bool
121
		 * function to add an article and his categories
122
		 */
123
		public function setAddArticle($title, $categories, $article, $state) {
124
			$dbc = App::getDb();
125
			
126 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...
127
				FlashMessage::setFlash($this->error_title.$this->error_article);
128
				return false;
129
			}
130
			
131
			$dbc->insert("title", $title)
132
				->insert("url", ChaineCaractere::setUrl($title))
133
				->insert("publication_date", date("Y-m-d H:i:s"))
134
				->insert("article", $article)
135
				->insert("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
136
				->insert("ID_state", $state)
137
				->into("_blog_article")->set();
138
			
139
			$id_article = $dbc->lastInsertId();
140
			
141
			AdminBlog::getAdminCategory()->setCategoriesArticle($categories, $id_article);
142
			return true;
143
		}
144
		
145
		/**
146
		 * @param $title
147
		 * @param $categories
148
		 * @param $article
149
		 * @param $state
150
		 * @return bool
151
		 * function to edit an article and his categories
152
		 */
153
		public function setEditArticle($title, $categories, $article, $state, $id_article) {
154
			$dbc = App::getDb();
155
			
156 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...
157
				FlashMessage::setFlash($this->error_title.$this->error_article);
158
				return false;
159
			}
160
			
161
			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...
162
				FlashMessage::setFlash("votre article n'existe pas");
163
				return false;
164
			}
165
			
166
			$dbc->insert("title", $title)
167
				->insert("url", ChaineCaractere::setUrl($title))
168
				->insert("publication_date", date("Y-m-d H:i:s"))
169
				->insert("article", $article)
170
				->insert("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
171
				->insert("ID_state", $state)
172
				->into("_blog_article")->set();
173
			
174
			$id_article = $dbc->lastInsertId();
175
			
176
			AdminBlog::getAdminCategory()->setCategoriesArticle($categories, $id_article);
177
			return true;
178
		}
179
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
180
	}