4
|
4
|
|
|
5
|
5
|
|
class Database
|
6
|
6
|
|
{
|
7
|
|
- |
protected $database;
|
|
7
|
+ |
|
|
8
|
+ |
/***** STATIC *****/
|
|
9
|
+ |
|
|
10
|
+ |
|
|
11
|
+ |
// How many items to display in homepage
|
|
12
|
+ |
const HOMEPAGE_RESULTS = 50;
|
8
|
13
|
|
|
9
|
14
|
|
protected static function get_random_string ($length = 10) {
|
10
|
15
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
18
|
23
|
|
return $hash_id;
|
19
|
24
|
|
}
|
20
|
25
|
|
|
|
26
|
+ |
|
|
27
|
+ |
/***** INSTANCE *****/
|
|
28
|
+ |
|
|
29
|
+ |
|
|
30
|
+ |
// Reference to a database connection
|
|
31
|
+ |
protected $database;
|
|
32
|
+ |
|
21
|
33
|
|
function __construct ()
|
22
|
34
|
|
{
|
23
|
35
|
|
$this->database = NULL;
|
27
|
39
|
|
{
|
28
|
40
|
|
try {
|
29
|
41
|
|
|
30
|
|
- |
$this->database = new PDO (Config::$DATABASE['dsn'], Config::$DATABASE['username'], Config::$DATABASE['password']);
|
|
42
|
+ |
$this->database = new PDO (
|
|
43
|
+ |
Config::$DATABASE['dsn'],
|
|
44
|
+ |
Config::$DATABASE['username'],
|
|
45
|
+ |
Config::$DATABASE['password']);
|
|
46
|
+ |
|
|
47
|
+ |
$this->database->setAttribute (
|
|
48
|
+ |
PDO::ATTR_ERRMODE,
|
|
49
|
+ |
PDO::ERRMODE_EXCEPTION);
|
31
|
50
|
|
|
32
|
51
|
|
return true;
|
33
|
52
|
|
|
317
|
336
|
|
/**
|
318
|
337
|
|
* Get posts by rating (for homepage)
|
319
|
338
|
|
*/
|
320
|
|
- |
function get_hot_posts ()
|
|
339
|
+ |
function get_hot_posts ($page = 0)
|
321
|
340
|
|
{
|
322
|
341
|
|
$submissions = array();
|
|
342
|
+ |
$page = intval ($page);
|
323
|
343
|
|
|
324
|
344
|
|
if (is_null ($this->database))
|
325
|
345
|
|
return $submissions;
|
326
|
346
|
|
|
327
|
|
- |
$query = $this->database->query (
|
|
347
|
+ |
if ($page < 0)
|
|
348
|
+ |
$page = 0;
|
|
349
|
+ |
|
|
350
|
+ |
$query = $this->database->prepare (
|
328
|
351
|
|
'SELECT P.*, U.`username`' .
|
329
|
352
|
|
'FROM `post` AS P ' .
|
330
|
353
|
|
'JOIN `user` AS U ON P.`userId` = U.`id`' .
|
331
|
354
|
|
'ORDER BY P.`dateCreated` DESC, P.`vote` DESC, P.`commentsCount` DESC ' .
|
332
|
|
- |
'LIMIT 50');
|
|
355
|
+ |
'LIMIT :limit OFFSET :offset');
|
|
356
|
+ |
|
|
357
|
+ |
$query->bindValue (':limit', Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
|
|
358
|
+ |
$query->bindValue (':offset', $page * Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
|
|
359
|
+ |
|
|
360
|
+ |
$query->execute ();
|
333
|
361
|
|
|
334
|
362
|
|
$submissions = $query->fetchAll (PDO::FETCH_ASSOC);
|
335
|
363
|
|
|
339
|
367
|
|
/**
|
340
|
368
|
|
* Get posts by date (for homepage)
|
341
|
369
|
|
*/
|
342
|
|
- |
function get_new_posts ()
|
|
370
|
+ |
function get_new_posts ($page = 0)
|
343
|
371
|
|
{
|
344
|
372
|
|
$submissions = array();
|
|
373
|
+ |
$page = intval ($page);
|
345
|
374
|
|
|
346
|
375
|
|
if (is_null ($this->database))
|
347
|
376
|
|
return $submissions;
|
348
|
377
|
|
|
349
|
|
- |
$query = $this->database->query(
|
|
378
|
+ |
if ($page < 0)
|
|
379
|
+ |
$page = 0;
|
|
380
|
+ |
|
|
381
|
+ |
$query = $this->database->prepare (
|
350
|
382
|
|
'SELECT P.*, U.`username`' .
|
351
|
383
|
|
'FROM `post` AS P ' .
|
352
|
384
|
|
'JOIN `user` AS U ON P.`userId` = U.`id`' .
|
353
|
385
|
|
'ORDER BY P.`created` DESC ' .
|
354
|
|
- |
'LIMIT 50');
|
|
386
|
+ |
'LIMIT :limit OFFSET :offset');
|
|
387
|
+ |
|
|
388
|
+ |
$query->bindValue (':limit', Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
|
|
389
|
+ |
$query->bindValue (':offset', $page * Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
|
|
390
|
+ |
|
|
391
|
+ |
$query->execute ();
|
355
|
392
|
|
|
356
|
393
|
|
$submissions = $query->fetchAll (PDO::FETCH_ASSOC);
|
357
|
394
|
|
|