/
class.authormanager.php
294 lines (256 loc) · 8.74 KB
/
class.authormanager.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* @package toolkit
*/
/**
* The `AuthorManager` class is responsible for managing all Author objects
* in Symphony. Unlike other Manager objects, Authors are stored in the
* database in `tbl_authors` and not on the file system. CRUD methods are
* implemented to allow Authors to be created (add), read (fetch), updated
* (edit) and deleted (delete).
*/
class AuthorManager
{
/**
* An array of all the objects that the Manager is responsible for.
* Defaults to an empty array.
* @var array
*/
protected static $_pool = array();
/**
* Given an associative array of fields, insert them into the database
* returning the resulting Author ID if successful, or false if there
* was an error
*
* @param array $fields
* Associative array of field names => values for the Author object
* @throws DatabaseException
* @return integer|boolean
* Returns an Author ID of the created Author on success, false otherwise.
*/
public static function add(array $fields)
{
if (!Symphony::Database()->insert($fields, 'tbl_authors')) {
return false;
}
$author_id = Symphony::Database()->getInsertID();
return $author_id;
}
/**
* Given an Author ID and associative array of fields, update an existing Author
* row in the `tbl_authors` database table. Returns boolean for success/failure
*
* @param integer $id
* The ID of the Author that should be updated
* @param array $fields
* Associative array of field names => values for the Author object
* This array does need to contain every value for the author object, it
* can just be the changed values.
* @throws DatabaseException
* @return boolean
*/
public static function edit($id, array $fields)
{
return Symphony::Database()->update($fields, 'tbl_authors', sprintf(
" `id` = %d",
$id
));
}
/**
* Given an Author ID, delete an Author from Symphony.
*
* @param integer $id
* The ID of the Author that should be deleted
* @throws DatabaseException
* @return boolean
*/
public static function delete($id)
{
return Symphony::Database()->delete('tbl_authors', sprintf(
" `id` = %d",
$id
));
}
/**
* The fetch method returns all Authors from Symphony with the option to sort
* or limit the output. This method returns an array of Author objects.
*
* @param string $sortby
* The field to sort the authors by, defaults to 'id'
* @param string $sortdirection
* Available values of ASC (Ascending) or DESC (Descending), which refer to the
* sort order for the query. Defaults to ASC (Ascending)
* @param integer $limit
* The number of rows to return
* @param integer $start
* The offset start point for limiting, maps to the LIMIT {x}, {y} MySQL functionality
* @param string $where
* Any custom WHERE clauses. The `tbl_authors` alias is `a`
* @param string $joins
* Any custom JOIN's
* @throws DatabaseException
* @return array
* An array of Author objects. If no Authors are found, an empty array is returned.
*/
public static function fetch($sortby = 'id', $sortdirection = 'ASC', $limit = null, $start = null, $where = null, $joins = null)
{
$sortby = is_null($sortby) ? 'id' : Symphony::Database()->cleanValue($sortby);
$sortdirection = $sortdirection === 'ASC' ? 'ASC' : 'DESC';
$records = Symphony::Database()->fetch(sprintf(
"SELECT a.*
FROM `tbl_authors` AS `a`
%s
WHERE %s
ORDER BY %s %s
%s %s",
$joins,
($where) ? $where : 1,
'a.'. $sortby,
$sortdirection,
($limit) ? sprintf("LIMIT %d", $limit) : '',
($start && $limit) ? ', ' . $start : ''
));
if (!is_array($records) || empty($records)) {
return array();
}
$authors = array();
foreach ($records as $row) {
$author = new Author;
foreach ($row as $field => $val) {
$author->set($field, $val);
}
self::$_pool[$author->get('id')] = $author;
$authors[] = $author;
}
return $authors;
}
/**
* Returns Author's that match the provided ID's with the option to
* sort or limit the output. This function will search the
* `AuthorManager::$_pool` for Authors first before querying `tbl_authors`
*
* @param integer|array $id
* A single ID or an array of ID's
* @throws DatabaseException
* @return mixed
* If `$id` is an integer, the result will be an Author object,
* otherwise an array of Author objects will be returned. If no
* Authors are found, or no `$id` is given, `null` is returned.
*/
public static function fetchByID($id)
{
$return_single = false;
if (is_null($id)) {
return null;
}
if (!is_array($id)) {
$return_single = true;
$id = array((int)$id);
}
if (empty($id)) {
return null;
}
// Get all the Author ID's that are already in `self::$_pool`
$authors = array();
$pooled_authors = array_intersect($id, array_keys(self::$_pool));
foreach ($pooled_authors as $pool_author) {
$authors[] = self::$_pool[$pool_author];
}
// Get all the Author ID's that are not already stored in `self::$_pool`
$id = array_diff($id, array_keys(self::$_pool));
$id = array_filter($id);
if (empty($id)) {
return ($return_single ? $authors[0] : $authors);
}
$records = Symphony::Database()->fetch(sprintf(
"SELECT *
FROM `tbl_authors`
WHERE `id` IN (%s)",
implode(",", $id)
));
if (!is_array($records) || empty($records)) {
return ($return_single ? $authors[0] : $authors);
}
foreach ($records as $row) {
$author = new Author;
foreach ($row as $field => $val) {
$author->set($field, $val);
}
self::$_pool[$author->get('id')] = $author;
$authors[] = $author;
}
return ($return_single ? $authors[0] : $authors);
}
/**
* Returns an Author by Username. This function will search the
* `AuthorManager::$_pool` for Authors first before querying `tbl_authors`
*
* @param string $username
* The Author's username
* @return Author|null
* If an Author is found, an Author object is returned, otherwise null.
*/
public static function fetchByUsername($username)
{
if (!isset(self::$_pool[$username])) {
$records = Symphony::Database()->fetchRow(0, sprintf(
"SELECT *
FROM `tbl_authors`
WHERE `username` = '%s'
LIMIT 1",
Symphony::Database()->cleanValue($username)
));
if (!is_array($records) || empty($records)) {
return null;
}
$author = new Author;
foreach ($records as $field => $val) {
$author->set($field, $val);
}
self::$_pool[$username] = $author;
}
return self::$_pool[$username];
}
/**
* This function will allow an Author to sign into Symphony by using their
* authentication token as well as username/password.
*
* @param integer $author_id
* The Author ID to allow to use their authentication token.
* @throws DatabaseException
* @return boolean
*/
public static function activateAuthToken($author_id)
{
if (!is_int($author_id)) {
return false;
}
return Symphony::Database()->query(sprintf(
"UPDATE `tbl_authors`
SET `auth_token_active` = 'yes'
WHERE `id` = %d",
$author_id
));
}
/**
* This function will remove the ability for an Author to sign into Symphony
* by using their authentication token
*
* @param integer $author_id
* The Author ID to allow to use their authentication token.
* @throws DatabaseException
* @return boolean
*/
public static function deactivateAuthToken($author_id)
{
if (!is_int($author_id)) {
return false;
}
return Symphony::Database()->query(sprintf(
"UPDATE `tbl_authors`
SET `auth_token_active` = 'no'
WHERE `id` = %d",
$author_id
));
}
}