Completed
Push — master ( 096c8e...65b9c6 )
by Anthony
02:07
created

Article::getDateFr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
	namespace modules\blog\app\controller;
3
	
4
	
5
	use core\App;
6
	
7
	class Article {
8
		
9
		
10
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
11
		public function __construct() {
12
			
13
		}
14
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
15
		
16
		
17
		//-------------------------- GETTER ----------------------------------------------------------------------------//
18
		protected function getImageArticle($url_article) {
19
			$url_image = ROOT."modules/blog/images/".$url_article.".png";
20
			
21
			if (file_exists($url_image)) {
22
				return WEBROOT."modules/blog/images/".$url_article.".png";;
23
			}
24
			else {
25
				return WEBROOT."modules/blog/images/fond-bloc.jpg";
26
			}
27
		}
28
		
29
		/**
30
		 * @param $date
31
		 * @return string
32
		 * function that return datein french format
33
		 */
34
		protected function getDateFr($date) {
35
			$mois = array("Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre");
36
			
37
			$explode = explode("-", $date);
38
			$jour_d = $explode[2];
39
			$mois_d = $explode[1];
40
			$annee_d = $explode[0];
41
			
42
			return $jour_d." ".$mois[$mois_d - 1]." ".$annee_d;
43
		}
44
		
45
		/**
46
		 * this function get last articles
47
		 */
48
		public function getLastArticle() {
49
			$dbc = App::getDb();
50
			$nb_article = Blog::getArticleIndex();
51
			
52
			$query = $dbc->select()
53
				->from("_blog_article")
54
				->from("identite")
55
				->where("_blog_article.ID_state", "=", 1, "AND")
56
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
57
				->limit(0, $nb_article)->get();
58
			
59 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
60
				$articles = [];
61
				
62
				foreach ($query as $obj) {
63
					$articles[] = [
64
						"id_article" => $obj->ID_article,
65
						"title" => $obj->title,
66
						"url" => $obj->url,
67
						"image" => $this->getImageArticle($obj->url),
68
						"article" => $obj->article,
69
						"pseudo" => $obj->pseudo,
70
						"publication_date" => $this->getDateFr($obj->publication_date),
71
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
72
					];
73
				}
74
				
75
				Blog::setValues(["articles" => $articles]);
76
			}
77
		}
78
		
79
		/**
80
		 * function that get one article
81
		 */
82
		public function getArticle() {
83
			$dbc = App::getDb();
84
			$param = Blog::$router_parameter;
85
			
86
			$query = $dbc->select()->from("_blog_article")->from("identite")->from("_blog_state")
87
				->where("ID_article", "=", $param, "OR")
88
				->where("url", "=", $param, "AND")
89
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "AND", true)
90
				->where("_blog_article.ID_state", "=", "_blog_state.ID_state", "", true)
91
				->get();
92
			
93
			if ((is_array($query)) && (count($query) == 1)) {
94
				foreach ($query as $obj) {
95
					Blog::setValues(["article" => [
96
						"id_article" => $obj->ID_article,
97
						"title" => $obj->title,
98
						"url" => $obj->url,
99
						"article" => $obj->article,
100
						"pseudo" => $obj->pseudo,
101
						"id_state" => $obj->ID_state,
102
						"state" => $obj->state,
103
						"publication_date" => $this->getDateFr($obj->publication_date),
104
						"categories" => Blog::getCategory()->getCategoryArticle()
105
					]]);
106
				}
107
			}
108
		}
109
		
110
		/**
111
		 * function that get all categories of an article
112
		 */
113
		public function getCategoryArticle() {
114
			$dbc = App::getDb();
115
			$category = Blog::$router_parameter;
116
			
117
			$query = $dbc->select()
118
				->from("_blog_article")
119
				->from("_blog_category")
120
				->from("_blog_article_category")
121
				->from("identite")
122
				->where("_blog_category.ID_category", "=", $category, "OR")
123
				->where("_blog_category.category", "=", $category, "AND")
124
				->where("_blog_article.ID_state", "=", 1, "AND")
125
				->where("_blog_article_category.ID_article", "=", "_blog_article.ID_article", "AND", true)
126
				->where("_blog_article_category.ID_category", "=", "_blog_category.ID_category", "AND", true)
127
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
128
				->get();
129
			
130 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
131
				$articles = [];
132
				
133
				foreach ($query as $obj) {
134
					$articles[] = [
135
						"id_article" => $obj->ID_article,
136
						"title" => $obj->title,
137
						"url" => $obj->url,
138
						"image" => $this->getImageArticle($obj->url),
139
						"article" => $obj->article,
140
						"pseudo" => $obj->pseudo,
141
						"publication_date" => $this->getDateFr($obj->publication_date),
142
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
143
					];
144
				}
145
				
146
				Blog::setValues(["articles" => $articles]);
147
			}
148
		}
149
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
150
		
151
		
152
		//-------------------------- SETTER ----------------------------------------------------------------------------//
153
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
154
	}