-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB.php
159 lines (135 loc) · 4.27 KB
/
DB.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
<?php
namespace portalove;
class DB
{
private $host;
private $dbname;
private $username;
private $password;
private $connection;
public function __construct($host, $dbname, $username, $password)
{
$this->host = $host;
$this->dbname = $dbname;
$this->username = $username;
$this->password = $password;
try {
$this->connection = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password);
} catch (\PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
public function insertMenuItem($sysName, $displayName, $icon, $contentId)
{
$dateTime = date('Y-m-d H:i:s', time());
$sql = "INSERT INTO menu(sys_name, display_name, icon, content_id, created_at, updated_at)
VALUE ('".$sysName."',
'".$displayName."',
'".$icon."',
".$contentId.",
'".$dateTime."',
'".$dateTime."')";
try {
$this->connection->exec($sql);
return true;
} catch (\PDOException $e) {
return false;
}
}
public function getMenu()
{
$menuItems = [];
$sql = "SELECT M.*, C.content AS content, C.display_name AS title
FROM menu AS M
INNER JOIN content AS C ON M.content_id = C.id";
$query = $this->connection->query($sql);
while ($row = $query->fetch()) {
$menuItems[] = [
'name' => $row['display_name'],
'icon' => $row['icon'],
'content' => $row['content'],
'title' => $row['title']
];
}
return $menuItems;
}
public function insertEmail($name, $email, $message)
{
$dateTime = date('Y-m-d H:i:s', time());
$sql = "INSERT INTO email(name, email, message, created_at, updated_at)
VALUE ('".$name."', '".$email."', '".$message."', '".$dateTime."', '".$dateTime."')";
try {
$this->connection->exec($sql);
return true;
} catch (\PDOException $e) {
return false;
}
}
public function getAllEmails()
{
$emails = [];
$sql = "SELECT * FROM email";
$query = $this->connection->query($sql);
while ($row = $query->fetch()) {
$emails[] = [
'id' => $row['id'],
'name' => $row['name'],
'email' => $row['email'],
'message' => $row['message'],
'created_at' => $row['created_at']
];
}
return $emails;
}
public function deleteEmail($id)
{
$sql = "DELETE FROM email WHERE id = ".$id;
try {
$this->connection->exec($sql);
return true;
} catch (\PDOException $e) {
return false;
}
}
public function getEmailDetails($id)
{
$sql = "SELECT id, name, email, message FROM email WHERE id = " . $id;
$result = [];
try {
$query = $this->connection->query($sql);
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
return $result;
} catch (\PDOException $e) {
return $result;
}
}
public function updateEmail($id, $from, $email, $message)
{
$sql = "UPDATE email
SET name = '".$from."', email = '".$email."', message = '".$message."'
WHERE id = ".$id;
try {
$this->connection->exec($sql);
return true;
} catch (\PDOException $e) {
return false;
}
}
public function login($username, $password)
{
$hasPassword = sha1($password);
$sql = "SELECT COUNT(id) AS is_admin FROM user WHERE meno = '".$username."' AND heslo = '".$hasPassword."'";
try {
$query = $this->connection->query($sql);
$result = $query->fetch(\PDO::FETCH_ASSOC);
if(intval($result['is_admin']) === 1) {
return true;
} else {
return false;
}
} catch (\PDOException $e) {
return false;
}
}
}