-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_controller.php
186 lines (142 loc) · 5.1 KB
/
main_controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
require_once "./model/article.php";
require_once "./model/user.php";
require_once "./model/mysql.php";
require_once "./model/news_text.php";
require_once './modules/login.php';
require_once './modules/admin.php';
class Controller
{
private $_action;
private $_mysql;
private $_login;
public function __construct(MySQL $mysql, Login $login)
{
$this->_action = isset($_GET['action']) ? $_GET['action'] : "index";
$this->_mysql = $mysql;
$this->_login = $login;
}
public function GetAction()
{
return $this->_action == "news" && isset($_GET['news_id']) ? "news_concrete" : $this->_action;
}
public function Decision()
{
if ( $this->_action == 'site' )
return $this->GetSite();
if ( $this->_action == 'news' || $this->_action == 'index' )
return $this->GetNews();
if ( $this->_action == 'login' )
return $this->GetLoginForm();
if ( $this->_action == 'logout' )
{
$this->_login->Logout();
Header("Location: /");
}
if ( $this->_action == 'show_user' )
return $this->ShowUser();
if ( $this->_action == 'admin' )
return $this->AdminMode();
}
private function GetSite()
{
$id = intval($_GET['id']);
$this->_mysql->Query('select *, '.$this->_mysql->GetImprovedLang('menu.title').'title, '.$this->_mysql->GetImprovedLang('menu.content').'content from menu where id='.$id);
if ( $this->_mysql->NumberOfRows() == 0 )
return "ni ma artykulu"; // TODO ??
$siteInfo = $this->_mysql->FetchAssoc();
$user_id = $siteInfo['author'];
$this->_mysql->Query('select first_name, second_name, id from user where id = '.$user_id);
if ( $this->_mysql->NumberOfRows() == 0 )
return "ni ma usera";
$user = new User($this->_mysql->FetchAssoc());
$art = new Article($siteInfo['title'], $siteInfo['content'], $user, $siteInfo['date']);
return $art->GetHTMLArticle();
}
private function GetNews()
{
if ( isset($_GET['news_id'] ) )
return $this->GetSingleNews(intval($_GET['news_id']));
else
return $this->GetShortNewsList();
}
private function GetSingleNews($id)
{
$provider = new NewsProvider($this->_mysql);
$row = $provider->GetNews($id);
if ( $row == null )
{
echo "Ni ma takigo newsa! Wylyz stond!";
return ;
}
$this->_mysql->Query('select first_name, second_name, id from user where id = '.intval($row['author']));
if ( $this->_mysql->NumberOfRows() == 0 )
return "ni ma usera";
$user = new User($this->_mysql->FetchAssoc());
$date = $provider -> formatDate($row['date']);
$news = new NewsText($row['title'], $row['content'], $user, $id, $date);
return $news->GetHTMLNews();
}
private function GetShortNewsList()
{
$template = new Template(CURRENT_TEMPLATE."news_short_index.htm"); //tutaj
$template->Laduj();
$provider = new NewsProvider($this->_mysql);
$current = isset($_GET['news_list']) ? intval($_GET['news_list']) - 1 : 0;
$tmp = $provider->GetShortNewsList(5, $current * 5);
$content = array();
foreach ( $tmp as $val )
array_push($content, $val->GetShortHTMLNews());
$displayedNums = 18;
$dnPerTwo = $displayedNums / 2;
$nums = array();
$cnt = $provider->GetNewsCount() / 5;
$maxCur = ($current > $dnPerTwo ) ? $current : $dnPerTwo;
$right = ($maxCur + $dnPerTwo < $cnt ) ? $maxCur + $dnPerTwo : $cnt;
$left = ($right - $displayedNums < 0 ) ? 0 : $right - $displayedNums;
for ( $i = $left; $i < $right; $i++ )
{
array_push($nums,$i+1);
}
$template->DodajWarunek("not_first", $current != 0);
$template->DodajWarunek("not_last", $current+1 != ceil($cnt));
$template->Dodaj("max_number", ceil($cnt));
$template->Dodaj("prev", max($current, 1));
$template->Dodaj("next", min($current + 2, ceil($cnt)));
$template->DodajPetle("news_short", $content);
$template->DodajPetle("numbers", $nums);
return $template->Parsuj();
}
private function GetLoginForm()
{
$err = "";
if ( isset($_POST['logmenow']) )
{
$username = htmlspecialchars($_POST['nick']);
$password = htmlspecialchars($_POST['pass']);
$this->_login->LoginProcess($username, md5($password));
if ( $this->_login->IsLoggedIn() )
Header("Location: /index.php");
else
$err = "Niepoprawny login lub hasło.";
}
return $this->_login->GetLoginForm($err);
}
private function ShowUser()
{
if ( !isset($_GET['user_id']) )
return "ni ma takigo usera";
$id = intval($_GET['user_id']);
$this->_mysql->Query('select * from user where id = '.$id);
if ( $this->_mysql->NumberOfRows() == 0 )
return "ni ma usera";
$user = new User($this->_mysql->FetchAssoc());
return $user->ShowUserPage($this->_login->IsLoggedIn());
}
private function AdminMode()
{
$admin = new AdminController($this->_login->GetUser(), $this->_mysql);
return $admin->Decision();
}
}
?>