-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBController.phps
99 lines (74 loc) · 2.91 KB
/
DBController.phps
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
<?php
// all DB connections funnel through here
class DBController
{
private $db;
private $servername = "localhost";
private $username = "root";
private $password = "root";
private $database = "documentaries";
function __construct(){
$this->db = new mysqli($this->servername, $this->username, $this->password);
$this->db->select_db($this->database);
}
function db(){
return $this->db;
}
private function query($sql){
$sql = $this->db->prepare($sql);
$sql->execute();
return $sql->get_result();
}
function close(){
$this->db->close();
}
/** data retrieval functions **/
function getFeaturedMovies(){
return $this->query("SELECT * FROM docs WHERE featured=1 AND visible=1 AND cover<>'' ORDER BY createdOn DESC");
}
// get all categories
function getCategories(){
return $this->query("SELECT * FROM categories ORDER BY category ASC");
}
// counts number of movies per category
function getCategoriesCount(){
return $this->query("SELECT GROUP_CONCAT(category) FROM docs WHERE visible=1");
}
// get total doc count
function getMovieCount(){
return $this->query("SELECT COUNT(id) as number FROM docs WHERE visible=1");
}
function getContentByCategory($category, $sortBy, $sortDir){
// ensure no empty docs are selected
$categoryFilter = "WHERE (title<>'' AND description<>'' AND cover<>'' AND visible=1)";
if($category != "all"){
$categoryFilter .= " AND (find_in_set('". $category ."',category) <> 0)";
}
$this->db->query("SET NAMES utf8");
// get all documentaries in table and sorted
return $this->query("SELECT * FROM docs ". $categoryFilter ." ORDER BY ". $sortBy ." ". $sortDir);
}
// returns embed code for given video id and type of video
// video type can be trailer or watch
function getEmbedCodeByID($videoID, $videoType){
$result = $this->query("SELECT ". $videoType ." FROM docs WHERE id = '". $videoID ."'");
$embedCode = $result->fetch_row();
return $embedCode[0];
}
function getRandomQuote(){
return $this->query("SELECT * FROM quotes ORDER BY RAND() LIMIT 1");
}
/** Edit area functions **/
function getAllMovies(){
return $this->query("SELECT * FROM docs ORDER BY id DESC");
}
function getMovieByID($id){
$result = $this->query("SELECT * FROM docs WHERE id = '". $id ."'");
return $result->fetch_array();
}
function dbErrorMessage(){
print json_encode(array('error' => '<div class="docRow">Oops... Lucid Tree doesn't understand your request.<br/><br/><a href="/">Return to the main page</a> or click the menu on the left to try again. <p>'.mysqli_error().'</p></div>'));
exit;
}
}
?>