-
Notifications
You must be signed in to change notification settings - Fork 0
/
CChapters.php
238 lines (168 loc) · 6.21 KB
/
CChapters.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
require_once '../db_cfg.php';
require_once 'config.php';
// Object class to wrap access to the Chapters DB
class CChapters
{
// Class members
private $_db = NULL;
// Initialize Class
function init()
{
$this->_db = db_connect();
if (isset($this->_db))
return true;
else
return false;
}
// TODO: Implement Access Control
function getChapterLive()
{
if (!isset($this->_db)) return NULL;
$criteria = "mediaid = 0 AND posted = 1";
$sort = "date DESC";
$limit = 1;
return $this->_db_getChapters($criteria, $sort, $limit);
}
function findChapter($mediaid, $time_offset)
{
if (!isset($this->_db)) return NULL;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_time_offset = $this->_db->quoteSmart($time_offset);
$criteria = "mediaid = $db_mediaid AND offset <= $db_time_offset AND posted=1";
$sort = "offset DESC";
$limit = 1;
return $this->_db_getChapters($criteria, $sort, $limit);
}
function getChapterList($mediaid, $posted = NULL)
{
if (!isset($this->_db)) return NULL;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$criteria = "mediaid=$db_mediaid";
if (isset($posted)) {
$db_posted = $this->_db->quoteSmart($posted);
$criteria .= " AND posted = $db_posted";
}
$sort = "posted DESC, offset ASC, date ASC";
return $this->_db_getChapters($criteria, $sort);
}
function _db_getChapters($criteria = NULL, $sort = NULL, $limit = NULL)
{
if (!isset($this->_db)) return false;
$sql = 'SELECT id, mediaid, name, offset, posted FROM agenda_items WHERE NOT deleted AND broadcasterid=' . $this->_db->quoteSmart(PFS_BROADCASTERID);
if (isset($criteria) && !empty($criteria))
$sql .= ' AND ' . $criteria;
if (isset($sort) && !empty($sort))
$sql .= ' ORDER BY ' . $sort;
if (isset($limit) && !empty($limit) && is_int($limit))
$sql .= ' LIMIT ' . $this->_db->quoteSmart($limit);
$sql .= ';';
$result = $this->_db->getAll($sql);
if (PEAR::isError($result)) {
error_log("__CLASS__::__METHOD__ DB error from query ( $sql ): " . $result->GetMessage());
return false;
}
if (!isset($result) || empty($result))
return 'No Results';
else {
// This loop is to ensure every chapter name is properly escaped for html
// foreach( $result as &$c )
// {
// $c['name'] = htmlspecialchars( $c['name'], ENT_QUOTES );
// }
return $result;
}
}
// All methods past this point modify the DB
function _db_insert($values)
{
if (empty($values)) return false;
$values .= ', broadcasterid=' . $this->_db->quoteSmart(PFS_BROADCASTERID);
$sql = "INSERT INTO agenda_items SET $values;";
$result = $this->_db->query($sql);
if (PEAR::isError($result)) {
error_log("__CLASS__::__METHOD__ DB error from query ( $sql ): " . $result->GetMessage());
return false;
}
return true;
}
function _db_delete($criteria)
{
if (empty($criteria)) return false;
$sql = "UPDATE agenda_items SET deleted=1 WHERE broadcasterid=' . $this->_db->quoteSmart( PFS_BROADCASTERID ) . ' AND ' . $criteria;";
$result = $this->_db->query($sql);
if (PEAR::isError($result)) {
error_log("__CLASS__::__METHOD__ DB error from query ( $sql ): " . $result->GetMessage());
return false;
}
return true;
}
function _db_update($values, $criteria)
{
if (empty($criteria)) return false;
if (empty($values)) return false;
$sql = "UPDATE agenda_items SET date = CURRENT_TIMESTAMP, $values WHERE NOT deleted AND broadcasterid=' . $this->_db->quoteSmart( PFS_BROADCASTERID ) . ' AND $criteria;";
$result = $this->_db->query($sql);
if (PEAR::isError($result)) {
error_log("__CLASS__::__METHOD__ DB error from query ( $sql ): " . $result->GetMessage());
return false;
}
return true;
}
function newChapter($mediaid, $name, $time_offset)
{
if (!isset($this->_db)) return false;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_time_offset = $this->_db->quoteSmart($time_offset);
$db_name = $this->_db->quoteSmart($name);
$values = "mediaid=$db_mediaid, offset=$db_time_offset, name=$db_name";
return $this->_db_insert($values);
}
function delChapter($mediaid, $chapterid)
{
if (!isset($this->_db)) return false;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_chapterid = $this->_db->quoteSmart($chapterid);
$criteria = "mediaid=$db_mediaid AND id=$db_chapterid";
return $this->_db_delete($criteria);
}
function setChapterName($mediaid, $chapterid, $name)
{
if (!isset($this->_db)) return false;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_chapterid = $this->_db->quoteSmart($chapterid);
$db_name = $this->_db->quoteSmart($name);
$values = "name=$db_name";
$criteria = "mediaid=$db_mediaid AND id=$db_chapterid";
return $this->_db_update($values, $criteria);
}
// Setting the Chapter's time offset, automatically posts the chapter
function setChapterOffset($mediaid, $chapterid, $time_offset)
{
if (!isset($this->_db)) return false;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_chapterid = $this->_db->quoteSmart($chapterid);
$db_time_offset = $this->_db->quoteSmart($time_offset);
$values = "offset=$db_time_offset, posted=1";
$criteria = "mediaid=$db_mediaid AND id=$db_chapterid";
return $this->_db_update($values, $criteria);
}
function postChapter($mediaid, $chapterid)
{
if (!isset($this->_db)) return false;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_chapterid = $this->_db->quoteSmart($chapterid);
$criteria = "mediaid=$db_mediaid AND id=$db_chapterid";
$values = "date = CURRENT_TIMESTAMP, posted = 1";
return $this->_db_update($values, $criteria);
}
function unpostChapter($mediaid, $chapterid)
{
if (!isset($this->_db)) return false;
$db_mediaid = $this->_db->quoteSmart($mediaid);
$db_chapterid = $this->_db->quoteSmart($chapterid);
$criteria = "mediaid=$db_mediaid AND id=$db_chapterid";
$values = "date = CURRENT_TIMESTAMP, posted = 0";
return $this->_db_update($values, $criteria);
}
} // class CChapters