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

Article::getImageArticle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
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
		 * this function get last articles
31
		 */
32
		public function getLastArticle() {
33
			$dbc = App::getDb();
34
			$nb_article = Blog::getArticleIndex();
35
			
36
			$query = $dbc->select()
37
				->from("_blog_article")
38
				->from("identite")
39
				->where("_blog_article.ID_state", "=", 1, "AND")
40
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
41
				->limit(0, $nb_article)->get();
42
			
43 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
44
				$articles = [];
45
				
46
				foreach ($query as $obj) {
47
					$articles[] = [
48
						"id_article" => $obj->ID_article,
49
						"title" => $obj->title,
50
						"url" => $obj->url,
51
						"image" => $this->getImageArticle($obj->url),
52
						"article" => $obj->article,
53
						"pseudo" => $obj->pseudo,
54
						"publication_date" => $obj->publication_date,
55
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
56
					];
57
				}
58
				
59
				Blog::setValues(["articles" => $articles]);
60
			}
61
		}
62
		
63
		/**
64
		 * function that get one article
65
		 */
66
		public function getArticle() {
67
			$dbc = App::getDb();
68
			$param = Blog::$router_parameter;
69
			
70
			$query = $dbc->select()->from("_blog_article")->from("identite")->from("_blog_state")
71
				->where("ID_article", "=", $param, "OR")
72
				->where("url", "=", $param, "AND")
73
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "AND", true)
74
				->where("_blog_article.ID_state", "=", "_blog_state.ID_state", "", true)
75
				->get();
76
			
77
			if ((is_array($query)) && (count($query) == 1)) {
78
				foreach ($query as $obj) {
79
					Blog::setValues(["article" => [
80
						"id_article" => $obj->ID_article,
81
						"title" => $obj->title,
82
						"url" => $obj->url,
83
						"article" => $obj->article,
84
						"pseudo" => $obj->pseudo,
85
						"id_state" => $obj->ID_state,
86
						"state" => $obj->state,
87
						"publication_date" => $obj->publication_date,
88
						"categories" => Blog::getCategory()->getCategoryArticle()
89
					]]);
90
				}
91
			}
92
		}
93
		
94
		/**
95
		 * function that get all categories of an article
96
		 */
97
		public function getCategoryArticle() {
98
			$dbc = App::getDb();
99
			$category = Blog::$router_parameter;
100
			
101
			$query = $dbc->select()
102
				->from("_blog_article")
103
				->from("_blog_category")
104
				->from("_blog_article_category")
105
				->from("identite")
106
				->where("_blog_category.ID_category", "=", $category, "OR")
107
				->where("_blog_category.category", "=", $category, "AND")
108
				->where("_blog_article.ID_state", "=", 1, "AND")
109
				->where("_blog_article_category.ID_article", "=", "_blog_article.ID_article", "AND", true)
110
				->where("_blog_article_category.ID_category", "=", "_blog_category.ID_category", "AND", true)
111
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
112
				->get();
113
			
114 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
115
				$articles = [];
116
				
117
				foreach ($query as $obj) {
118
					$articles[] = [
119
						"id_article" => $obj->ID_article,
120
						"title" => $obj->title,
121
						"url" => $obj->url,
122
						"image" => $this->getImageArticle($obj->url),
123
						"article" => $obj->article,
124
						"pseudo" => $obj->pseudo,
125
						"publication_date" => $obj->publication_date,
126
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
127
					];
128
				}
129
				
130
				Blog::setValues(["articles" => $articles]);
131
			}
132
		}
133
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
134
		
135
		
136
		//-------------------------- SETTER ----------------------------------------------------------------------------//
137
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
138
	}