Skip to content

Commit 2b49dbb

Browse files
Add files via upload
1 parent 4e26b02 commit 2b49dbb

File tree

2 files changed

+1592
-0
lines changed

2 files changed

+1592
-0
lines changed

controllers/PageLoader.php

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
<?php
2+
3+
/**
4+
* Tester.su pages rendering without forms.
5+
*/
6+
7+
8+
abstract class Loader
9+
{
10+
/**
11+
* The Factory Method.
12+
* Get input out URL.
13+
* @url https://ru.wikipedia.org/wiki/%D0%A4%D0%B0%D0%B1%D1%80%D0%B8%D1%87%D0%BD%D1%8B%D0%B9_%D0%BC%D0%B5%D1%82%D0%BE%D0%B4_(%D1%88%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD_%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)
14+
*/
15+
16+
/**
17+
* Returns an object based on type.
18+
*
19+
* @param string $page
20+
*
21+
* @param array $param
22+
*
23+
* @return object
24+
*/
25+
public static function initial( $page, $param )
26+
{
27+
return new $page( $param );
28+
}
29+
30+
/**
31+
* Validation and actions with data.
32+
*
33+
* @param array $get
34+
*
35+
* @return void
36+
*/
37+
public function __construct( $get )
38+
{
39+
/**
40+
* @var array
41+
*/
42+
$this->data = $get;
43+
}
44+
45+
/**
46+
* Displays the template using params taken from the DB.
47+
*
48+
* @param template
49+
*
50+
* @return void
51+
*/
52+
abstract public function showPage( $template );
53+
}
54+
55+
56+
final class Index extends Loader
57+
{
58+
59+
/**
60+
* Index.php.
61+
* Displays the questions based on page param.
62+
*/
63+
64+
/**
65+
* @see Loader::__construct
66+
*
67+
* Page GET param validation and gets a questions information from the DB.
68+
*/
69+
public function __construct( $get )
70+
{
71+
parent::__construct( $get );
72+
73+
/**
74+
* @var integer
75+
*/
76+
$this->page = ( int ) @$this->data['page'];
77+
78+
/**
79+
* @var integer
80+
*/
81+
$this->all_pages = ceil( R::count( 'questions' ) / 10 );
82+
83+
/**
84+
* @var integer
85+
*/
86+
$this->all_questions = R::count( 'questions' );
87+
88+
if ( !is_int( $this->page ) || !$this->page || !R::count( 'questions', 'id > ?', array( ( $this->page-1 ) * 10 ) ) )
89+
{
90+
$this->page = 1;
91+
}
92+
93+
$beans[] = R::findAll( 'questions', 'WHERE date BETWEEN ? AND ? ORDER BY views DESC LIMIT ?',
94+
array( date( "Y-m-d", time() - 60 * 60 * 24 * 7 ), date( "Y-m-d" ), 10 ) );
95+
96+
$beans[] = R::findAll( 'questions', 'WHERE id BETWEEN ? AND ? ORDER BY views DESC LIMIT ?',
97+
array( ( $this->page - 1 ) * 10, $this->page * 10 , 10 ) );
98+
99+
foreach ($beans as $item)
100+
{
101+
102+
$tmp_array = array();
103+
104+
foreach ($item as $i) {
105+
$tmp_array[] = $i;
106+
}
107+
108+
$this->questions[] = $tmp_array;
109+
}
110+
111+
/**
112+
* @var array
113+
*/
114+
$this->page_numbers = self::getPagination();
115+
}
116+
117+
/**
118+
* Gets an information to the pagination to the template.
119+
*
120+
* @return array
121+
*/
122+
private function getPagination()
123+
{
124+
if ( $this->page == $this->all_pages )
125+
{
126+
for ($i=1; $i < 3; $i++)
127+
{
128+
if ( $this->page-$i > 0 )
129+
{
130+
$p[] = $this->page-$i;
131+
} else
132+
{
133+
break;
134+
}
135+
}
136+
} else
137+
{
138+
if ( $this->page-1 >= 1 )
139+
{
140+
$p[] = $this->page-1;
141+
}
142+
143+
$p[] = $this->page;
144+
145+
for ($i=1; $i < 3; $i++)
146+
{
147+
if ( $this->page+$i < $this->all_pages )
148+
{
149+
$p[] = $this->page+$i;
150+
} else
151+
{
152+
break;
153+
}
154+
}
155+
}
156+
157+
$p[] = $this->all_pages;
158+
sort( $p );
159+
return $p;
160+
}
161+
162+
/**
163+
* @see Loader::showPage
164+
*/
165+
public function showPage( $template )
166+
{
167+
load_template( $template, array(
168+
'message_color' => 'green',
169+
'message' => 'Welcome to the forum, '.htmlspecialchars_decode( $_SESSION['user']->login, ENT_QUOTES).'!',
170+
'user' => $_SESSION['user'],
171+
'questions' => $this->questions,
172+
'all' => $this->all_questions,
173+
'p' => $this->page_numbers ) );
174+
}
175+
}
176+
177+
178+
final class Forum extends Loader
179+
{
180+
181+
/**
182+
* Forum.php.
183+
* Displays the question based on id param.
184+
*/
185+
186+
/**
187+
* @see Loader::__construct
188+
*
189+
* Id GET param validation and views increased, if need were.
190+
*/
191+
public function __construct( $get )
192+
{
193+
parent::__construct( $get );
194+
195+
/**
196+
* @var integer
197+
*/
198+
$this->id = ( int ) @$this->data['id'];
199+
200+
if ( !is_int( $this->id ) || !$this->id )
201+
{
202+
header( 'Location: ask.php' );
203+
}
204+
205+
if ( !R::count( 'questions', 'id = ?', array( $this->id ) ) )
206+
{
207+
header( 'Location: ask.php' );
208+
}
209+
210+
self::viewsCounter();
211+
}
212+
213+
/**
214+
* Question views counter method.
215+
* If the user not visit the question today,
216+
* the views row will be increased for 1.
217+
*
218+
* @return void
219+
*/
220+
private function viewsCounter()
221+
{
222+
if ( !R::count( 'visitors', 'visistor_id = ? AND page_id = ? AND visit_date = ?',
223+
array( $_SESSION['user']->id, $this->id, date( "Y-m-d" ) ) ) )
224+
{
225+
/* DB cleaning */
226+
$trash_visit = R::findOne( 'visitors', 'visistor_id = ? AND page_id = ?', array(
227+
$_SESSION['user']->id, $this->id ) );
228+
229+
if ( $trash_visit )
230+
{
231+
R::trash( $trash_visit );
232+
}
233+
234+
/* Commits new visit */
235+
$visit = R::dispense( 'visitors' );
236+
237+
$visit->visistor_id = $_SESSION['user']->id;
238+
$visit->page_id = $this->id;
239+
$visit->visit_date = date( "Y-m-d" );
240+
241+
R::store( $visit );
242+
243+
$question = R::findOne( 'questions', 'id = ?', array( $this->id ) );
244+
245+
$question->views ++;
246+
247+
R::store( $question );
248+
}
249+
}
250+
251+
/**
252+
* @see Loader::showPage
253+
*/
254+
public function showPage( $template )
255+
{
256+
$data = R::findOne( 'questions', 'id = ?', array( $this->id ) );
257+
258+
$data = array(
259+
'title' => $data['title'],
260+
'name' => $data['author'],
261+
'views' => $data['views'],
262+
'image' => $data['preview'],
263+
'text' => $data['body'],
264+
'date' => $data['date'] );
265+
266+
load_template( $template, array(
267+
'data' => $data ) );
268+
}
269+
}

0 commit comments

Comments
 (0)