Passed
Push — master ( 7ee1fc...2d537f )
by Anthony
03:35
created

GestionContenus::getTestTitrePage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
	namespace core\admin\contenus;
3
4
	use core\App;
5
	use core\contenus\Contenus;
6
	use core\functions\ChaineCaractere;
7
	use core\HTML\flashmessage\FlashMessage;
8
9
	class GestionContenus extends Contenus {
10
		use ParentTexte;
11
12
		private $erreur;
13
14
15
		//-------------------------- GETTER ----------------------------------------------------------------------------//
16
		public function getErreur() {
17
			return $this->erreur;
18
		}
19
20
		private function getOrdrePage($parent) {
21
			if (($parent != "") || ($parent != 0)) {
22
				$dbc = \core\App::getDb();
23
				$ordre = 1;
24
25
				$query = $dbc->select("ordre")->from("page")->orderBy("ordre", "DESC")->limit(0, 1)->get();
26 View Code Duplication
				if ((is_array($query)) && (count($query) > 0)) {
0 ignored issues
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...
27
					foreach ($query as $obj) {
28
						$ordre = $obj->ordre;
29
					}
30
				}
31
32
				return $ordre;
33
			}
34
		}
35
36
		private function getParentId($parent) {
37
			$dbc = \core\App::getDb();
38
39
			if ($parent == "") return 0;
40
41
			$query = $dbc->select("ID_page")->from("page")->where("titre", " LIKE ", '"%'.$parent.'%"', "", true)->get();
42
43 View Code Duplication
			if ((is_array($query)) && (count($query) == 1)) {
0 ignored issues
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...
44
				foreach ($query as $obj) {
45
					return $obj->ID_page;
46
				}
47
			}
48
49
			return 0;
50
		}
51
52
		/**
53
		 * @param $nom_table
54
		 * @param $nom_id_table
55
		 * @param $champ
56
		 * @param $value
57
		 * @param $limit_char
58
		 * @param $err_char
59
		 * @param $err_egalite
60
		 * @param null $value_id_table
61
		 * @return string
62
		 * fonction qui permet de vérifier qu'il n'y ait pas d'erreur dans le champ spécifié ni de doublons
63
		 */
64
		private function getVerifChamp($nom_table, $nom_id_table, $champ, $value, $limit_char, $err_char, $err_egalite, $value_id_table = null) {
65
			$dbc = App::getDb();
66
67
			if (strlen(utf8_decode($value)) > $limit_char) {
68
				$this->erreur = true;
69
				return "<li>$err_char</li>";
70
			}
71
			else if ($dbc->rechercherEgalite($nom_table, $champ, $value, $nom_id_table, $value_id_table) == true) {
72
				$this->erreur = true;
73
				return "<li>$err_egalite</li>";
74
			}
75
		}
76
		
77
		private function getTestBaliseTitle($balise_title) {
78
			$err_balise_title_char = "Le titre pour le navigateur ne doit pas dépasser 70 caractères";
79
			$err_balise_title_egalite = "Ce titre est déjà présent en base de données, merci d'en choisir un autre pour optimiser le référencement de votre site";
80
			return $this->getVerifChamp("page", "ID_page", "balise_title", $balise_title, 70, $err_balise_title_char, $err_balise_title_egalite);
81
		}
82
		
83
		private function getTestUrl($url) {
84
			$err_url_char = "L'url ne doit pas dépasser 92 caractères";
85
			$err_url_egalite = "Cette url est déjà présent en base de données, merci d'en choisir une autre pour ne pas avoir de conflit entre vos pages";
86
			return $this->getVerifChamp("page", "ID_page", "url", $url, 92, $err_url_char, $err_url_egalite);
87
		}
88
		
89
		private function getTestMetaDescription($meta_description) {
90
			$err_meta_description_char = "La description de cette page ne doit pas dépasser 158 caractères";
91
			$err_meta_description_egalite = "Cette description est déjà présent en base de données, merci d'en choisir une autre pour optimiser le référencement de votre site";
92
			return $this->getVerifChamp("page", "ID_page", "meta_description", $meta_description, 158, $err_meta_description_char, $err_meta_description_egalite);
93
		}
94
		
95
		private function getTestTitrePage($titre_page) {
96
			$err_titre_page_char = "Le titre de cette page ne doit pas dépasser 50 caractères";
97
			$err_titre_page_egalite = "Cette titre de page est déjà présent en base de données, merci d'en choisir un autre pour ne pas avoir de conflit dans votre navigation";
98
			return $this->getVerifChamp("page", "ID_page", "titre", $titre_page, 50, $err_titre_page_char, $err_titre_page_egalite);
99
		}
100
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
101
102
103
104
		//-------------------------- SETTER ----------------------------------------------------------------------------//
105
		/**
106
		 * @param string $url
107
		 * @param string $err_balise_title
108
		 * @param string $err_url
109
		 * @param string $err_meta_description
110
		 * @param string $err_titre_page
111
		 */
112
		private function setErreurContenus($balise_title, $url, $meta_description, $titre_page, $parent, $err_balise_title, $err_url, $err_meta_description, $err_titre_page) {
113
			$_SESSION['balise_title'] = $balise_title;
114
			$_SESSION['url'] = $url;
115
			$_SESSION['meta_description'] = $meta_description;
116
			$_SESSION['titre_page'] = $titre_page;
117
			$_SESSION['parent'] = $parent;
118
			$_SESSION['err_modification_contenu'] = true;
119
120
			$message = "<ul>".$err_balise_title.$err_url.$err_meta_description.$err_titre_page."</ul>";
121
			FlashMessage::setFlash($message);
122
		}
123
124
		/**
125
		 * fonction qui permet de créer un page
126
		 * @param $balise_title
127
		 * @param $url
128
		 * @param $meta_description
129
		 * @param $titre_page
130
		 * @param $parent
131
		 */
132
		public function setCreerPage($balise_title, $url, $meta_description, $titre_page, $parent, $affiche = 1) {
133
			$dbc = \core\App::getDb();
134
135
			$url = ChaineCaractere::setUrl($url);
136
137
			$nom_page = explode("/", $url);
138
			$nom_page = end($nom_page);
139
140
			$page_type = ROOT."config/page_type/page_type.html";
141
			$new_page = ROOT."app/views/".$nom_page.".html";
142
			
143
			$err_balise_title = $this->getTestBaliseTitle($balise_title);
144
			$err_url = $this->getTestUrl($url);
145
			$err_meta_description = $this->getTestMetaDescription($meta_description);
146
			$err_titre_page = $this->getTestTitrePage($titre_page);
147
			
148
			if ($this->erreur !== true) {
149
				//si le fichier n'existe pas et que la copy est ok on insert en bdd
150
				if ((!file_exists($new_page)) && (copy($page_type, $new_page))) {
151
					$parent = intval($this->getParentId($parent));
152
					$ordre = intval($this->getOrdrePage($parent));
153
					$dbc->insert("titre", $titre_page)
154
						->insert("url", $url)
155
						->insert("meta_description", $meta_description)
156
						->insert("balise_title", $balise_title)
157
						->insert("ordre", $ordre)
158
						->insert("parent", $parent)
159
						->insert("affiche", $affiche)
160
						->into("page")
161
						->set();
162
163
					$this->id_page = $dbc->lastInsertId();
164
					$this->url = $url;
165
					if ($parent == "") {
166
						$this->setAjoutLienNavigation("ID_page", $this->id_page, 1);
167
					}
168
				}
169
				else {
170
					FlashMessage::setFlash("Impossible de créer cette page, veuillez réeseyer dans un moment. Si le problème persiste contactez votre administrateur.");
171
					$this->erreur = true;
172
				}
173
			}
174 View Code Duplication
			else {
0 ignored issues
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...
175
				$this->setErreurContenus($balise_title, $url, $meta_description, $titre_page, $parent, $err_balise_title, $err_url, $err_meta_description, $err_titre_page);
176
				$this->erreur = true;
177
			}
178
		}
179
180
		/**
181
		 * function that will create a redirection on an other site
182
		 * @param $balise_title
183
		 * @param $url
184
		 * @param $titre_page
185
		 * @param $parent
186
		 */
187
		public function setCreerPageRedirect($balise_title, $url, $titre_page, $parent, $affiche = 1) {
188
			$dbc = \core\App::getDb();
189
190
			$err_balise_title = $this->getTestBaliseTitle($balise_title);
191
			$err_url = $this->getTestUrl($url);
192
			$err_titre_page = $this->getTestTitrePage($titre_page);
193
			
194
			if ($this->erreur !== true) {
195
				//si le fichier n'existe pas et que la copy est ok on insert en bdd
196
				$parent = intval($this->getParentId($parent));
197
				$ordre = intval($this->getOrdrePage($parent));
198
				$dbc->insert("titre", $titre_page)
199
					->insert("url", $url)
200
					->insert("balise_title", $balise_title)
201
					->insert("ordre", $ordre)
202
					->insert("parent", $parent)
203
					->insert("affiche", $affiche)
204
					->insert("target", "_blanck")
205
					->into("page")
206
					->set();
207
208
				$this->id_page = $dbc->lastInsertId();
209
				if ($parent == "") {
210
					$this->setAjoutLienNavigation("ID_page", $this->id_page, 1);
211
				}
212
			}
213 View Code Duplication
			else {
0 ignored issues
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...
214
				$this->setErreurContenus($balise_title, $url, "", $titre_page, $parent, $err_balise_title, $err_url, "", $err_titre_page);
215
				$this->erreur = true;
216
			}
217
		}
218
219
		/**
220
		 * fonction qui permet de modifier une page en fonction de son id
221
		 * @param $id_page
222
		 * @param $balise_title
223
		 * @param $url
224
		 * @param $meta_description
225
		 * @param $titre_page
226
		 * @param $parent
227
		 * @param $contenu
228
		 */
229
		public function setModifierPage($id_page, $balise_title, $url, $meta_description, $titre_page, $parent, $affiche = 1) {
230
			$dbc = \core\App::getDb();
231
232
			//on trouve l'ancien fichier à parir de la fin de l'url
233
			$old_url = explode("/", $this->url);
234
			$filename = ROOT."app/views/".end($old_url).".html";
235
236
			//si le fichier existe on modifie le tout
237
			if (file_exists($filename) || ($id_page == 1)) {
238
				$this->id_page = $id_page;
239
				$url = ChaineCaractere::setUrl($url);
240
				
241
				$err_balise_title = $this->getTestBaliseTitle($balise_title);
242
				$err_url = $this->getTestUrl($url);
243
				$err_meta_description = $this->getTestMetaDescription($meta_description);
244
				$err_titre_page = $this->getTestTitrePage($titre_page);
245
				
246
				if ($this->erreur !== true) {
247
					$new_url = explode("/", $url);
248
					$new_filename = ROOT."app/views/".end($new_url).".html";
249
250
					rename($filename, $new_filename);
251
252
					$parent = intval($this->getParentId($parent));
253
					$dbc->update("titre", $titre_page)
254
						->update("url", $url)
255
						->update("meta_description", $meta_description)
256
						->update("balise_title", $balise_title)
257
						->update("parent", $parent)
258
						->update("affiche", $affiche)
259
						->from("page")
260
						->where("ID_page", "=", $id_page, "", true)
261
						->set();
262
263
					$this->setModifierLienNavigation("ID_page", $id_page, $this->getParentId($parent), $affiche);
264
					$this->url = $url;
265
				}
266
				else {
267
					$this->setErreurContenus($balise_title, $url, $meta_description, $titre_page, $parent, $err_balise_title, $err_url, $err_meta_description, $err_titre_page);
268
				}
269
			}
270
			//sinon on renvoi une erreur en disant que le fichier n'existe pas et qu'il faut contacter un administrateur
271
			else {
272
				FlashMessage::setFlash("Impossible de modifier cette page, veuillez contacter votre administrateur pour corriger ce problème");
273
				$this->erreur = true;
274
			}
275
		}
276
277
		/**
278
		 * @param $id_page
279
		 * @param $contenu
280
		 */
281
		public function setModifierContenu($id_page, $contenu) {
282
			$dbc = \core\App::getDb();
283
284
			$dbc->update("contenu", $contenu)->from("page")->where("ID_page", "=", $id_page)->set();
285
		}
286
287
		/**
288
		 * fonction qui permet de supprimer une page, test si fichier exist, si oui on delete
289
		 * @param $id_page
290
		 */
291
		public function setSupprimerPage() {
292
			$url = explode("/", $this->url);
293
			$filename = ROOT."app/views/".end($url).".html";
294
			
295
			if (file_exists($filename) && $this->id_page != 1) {
296
				unlink($filename);
297
				$this->setSupprimerLienNavigation();
298
				
299
				return true;
300
			}
301
			else if (ChaineCaractere::FindInString($this->url, "http://") === true) {
302
				$this->setSupprimerLienNavigation();
303
			}
304
			else {
305
				FlashMessage::setFlash("Impossible de supprimer cette page, veuillez contacter votre administrateur pour corriger ce problème");
306
				$this->erreur = true;
307
				return false;
308
			}
309
		}
310
311
		/**
312
		 * @param string $id
313
		 * @param $value_id
314
		 * @param integer $affiche
315
		 */
316
		private function setAjoutLienNavigation($id, $value_id, $affiche) {
317
			if ($affiche !== null) {
318
				App::getNav()->setAjoutLien($id, $value_id);
319
			}
320
		}
321
322
		/**
323
		 * @param string $id
324
		 * @param integer $affiche
325
		 */
326
		private function setModifierLienNavigation($id, $id_page, $parent, $affiche) {
327
			if ($parent != "") {
328
				App::getNav()->setSupprimerLien($id, $id_page);
329
			}
330
			else if (($affiche == 0) && ($parent == "")) {
331
				App::getNav()->setSupprimerLien($id, $id_page);
332
			}
333
		}
334
		
335
		/**
336
		 * delete link in nav and delete page in table
337
		 */
338
		private function setSupprimerLienNavigation() {
339
			$dbc = App::getDb();
340
			
341
			$dbc->delete()->from("page")->where("ID_page", "=", $this->id_page)->del();
342
			
343
			App::getNav()->setSupprimerLien("ID_page", $this->id_page);
344
		}
345
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
346
	}