1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package toolkit |
4
|
|
|
*/ |
5
|
|
|
/** |
6
|
|
|
* The `AuthorManager` class is responsible for managing all Author objects |
7
|
|
|
* in Symphony. Unlike other Manager objects, Authors are stored in the |
8
|
|
|
* database in `tbl_authors` and not on the file system. CRUD methods are |
9
|
|
|
* implemented to allow Authors to be created (add), read (fetch), updated |
10
|
|
|
* (edit) and deleted (delete). |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class AuthorManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* An array of all the objects that the Manager is responsible for. |
17
|
|
|
* Defaults to an empty array. |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected static $_pool = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Given an associative array of fields, insert them into the database |
24
|
|
|
* returning the resulting Author ID if successful, or false if there |
25
|
|
|
* was an error |
26
|
|
|
* |
27
|
|
|
* @param array $fields |
28
|
|
|
* Associative array of field names => values for the Author object |
29
|
|
|
* @throws DatabaseException |
30
|
|
|
* @return integer |
31
|
|
|
* Returns an Author ID of the created Author on success, 0 otherwise. |
32
|
|
|
*/ |
33
|
|
|
public static function add(array $fields) |
34
|
|
|
{ |
35
|
|
|
$inserted = Symphony::Database() |
|
|
|
|
36
|
|
|
->insert('tbl_authors') |
37
|
|
|
->values($fields) |
38
|
|
|
->execute() |
39
|
|
|
->success(); |
40
|
|
|
|
41
|
|
|
return $inserted ? Symphony::Database()->getInsertID() : 0; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Given an Author ID and associative array of fields, update an existing Author |
46
|
|
|
* row in the `tbl_authors` database table. Returns boolean for success/failure |
47
|
|
|
* |
48
|
|
|
* @param integer $id |
49
|
|
|
* The ID of the Author that should be updated |
50
|
|
|
* @param array $fields |
51
|
|
|
* Associative array of field names => values for the Author object |
52
|
|
|
* This array does need to contain every value for the author object, it |
53
|
|
|
* can just be the changed values. |
54
|
|
|
* @throws DatabaseException |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
public static function edit($id, array $fields) |
58
|
|
|
{ |
59
|
|
|
return Symphony::Database() |
|
|
|
|
60
|
|
|
->update('tbl_authors') |
61
|
|
|
->set($fields) |
62
|
|
|
->where(['id' => (int)$id]) |
63
|
|
|
->execute() |
64
|
|
|
->success(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Given an Author ID, delete an Author from Symphony. |
69
|
|
|
* |
70
|
|
|
* @param integer $id |
71
|
|
|
* The ID of the Author that should be deleted |
72
|
|
|
* @throws DatabaseException |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
public static function delete($id) |
76
|
|
|
{ |
77
|
|
|
return Symphony::Database() |
|
|
|
|
78
|
|
|
->delete('tbl_authors') |
79
|
|
|
->where(['id' => (int)$id]) |
80
|
|
|
->execute() |
81
|
|
|
->success(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Fetch a single author by its reset password token |
86
|
|
|
* |
87
|
|
|
* @param string $token |
88
|
|
|
* @return Author |
89
|
|
|
* @throws Exception |
90
|
|
|
* If the token is not a string |
91
|
|
|
*/ |
92
|
|
|
public function fetchByPasswordResetToken($token) |
93
|
|
|
{ |
94
|
|
|
if (!$token) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
General::ensureType([ |
98
|
|
|
'token' => ['var' => $token, 'type' => 'string'], |
99
|
|
|
]); |
100
|
|
|
return $this->select() |
|
|
|
|
101
|
|
|
->innerJoin('tbl_forgotpass')->alias('f') |
102
|
|
|
->on(['a.id' => '$f.author_id']) |
103
|
|
|
->where(['f.expiry' => ['>' => DateTimeObj::getGMT('c')]]) |
104
|
|
|
->where(['f.token' => $token]) |
105
|
|
|
->limit(1) |
106
|
|
|
->execute() |
107
|
|
|
->next(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Fetch a single author by its auth token |
112
|
|
|
* |
113
|
|
|
* @param string $token |
114
|
|
|
* @return Author |
115
|
|
|
* @throws Exception |
116
|
|
|
* If the token is not a string |
117
|
|
|
*/ |
118
|
|
|
public function fetchByAuthToken($token) |
119
|
|
|
{ |
120
|
|
|
if (!$token) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
General::ensureType([ |
124
|
|
|
'token' => ['var' => $token, 'type' => 'string'], |
125
|
|
|
]); |
126
|
|
|
return $this->select() |
127
|
|
|
->where(['a.auth_token' => $token]) |
128
|
|
|
->limit(1) |
129
|
|
|
->execute() |
130
|
|
|
->next(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* The fetch method returns all Authors from Symphony with the option to sort |
135
|
|
|
* or limit the output. This method returns an array of Author objects. |
136
|
|
|
* |
137
|
|
|
* @deprecated Symphony 3.0.0 |
138
|
|
|
* Use select() instead |
139
|
|
|
* @param string $sortby |
140
|
|
|
* The field to sort the authors by, defaults to 'id' |
141
|
|
|
* @param string $sortdirection |
142
|
|
|
* Available values of ASC (Ascending) or DESC (Descending), which refer to the |
143
|
|
|
* sort order for the query. Defaults to ASC (Ascending) |
144
|
|
|
* @param integer $limit |
145
|
|
|
* The number of rows to return |
146
|
|
|
* @param integer $start |
147
|
|
|
* The offset start point for limiting, maps to the LIMIT {x}, {y} MySQL functionality |
148
|
|
|
* @param string $where |
149
|
|
|
* Any custom WHERE clauses. The `tbl_authors` alias is `a` |
150
|
|
|
* @param string $joins |
151
|
|
|
* Any custom JOIN's |
152
|
|
|
* @throws DatabaseException |
153
|
|
|
* @return array |
154
|
|
|
* An array of Author objects. If no Authors are found, an empty array is returned. |
155
|
|
|
*/ |
156
|
|
|
public static function fetch($sortby = 'id', $sortdirection = 'ASC', $limit = null, $start = null, $where = null, $joins = null) |
157
|
|
|
{ |
158
|
|
|
if (Symphony::Log()) { |
159
|
|
|
Symphony::Log()->pushDeprecateWarningToLog('AuthorManager::fetch()', 'AuthorManager::select()'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$sortby = $sortby ?: 'id'; |
163
|
|
|
$sortdirection = strtoupper($sortdirection) === 'ASC' ? 'ASC' : 'DESC'; |
164
|
|
|
|
165
|
|
|
$query = (new AuthorManager)->select(); |
166
|
|
|
|
167
|
|
|
$orderBy = []; |
168
|
|
|
foreach (explode(',', $sortby) as $sortby) { |
169
|
|
|
$sortby = trim($sortby); |
170
|
|
|
$orderBy["a.$sortby"] = $sortdirection; |
171
|
|
|
} |
172
|
|
|
$query->orderBy($orderBy); |
173
|
|
|
|
174
|
|
|
if ($joins) { |
175
|
|
|
$joins = $query->replaceTablePrefix($joins); |
176
|
|
|
$query->unsafeAppendSQLPart('join', $joins); |
177
|
|
|
} |
178
|
|
|
if ($where) { |
179
|
|
|
$where = $query->replaceTablePrefix($where); |
180
|
|
|
$query->unsafe()->unsafeAppendSQLPart('where', "WHERE $where"); |
181
|
|
|
} |
182
|
|
|
if ($limit) { |
|
|
|
|
183
|
|
|
$query->limit($limit); |
184
|
|
|
} |
185
|
|
|
if ($start) { |
|
|
|
|
186
|
|
|
$query->offset($start); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$authors = $query->execute()->rows(); |
190
|
|
|
|
191
|
|
|
foreach ($authors as $author) { |
192
|
|
|
self::$_pool[$author->get('id')] = $author; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $authors; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns Author's that match the provided ID's with the option to |
200
|
|
|
* sort or limit the output. This function will search the |
201
|
|
|
* `AuthorManager::$_pool` for Authors first before querying `tbl_authors` |
202
|
|
|
* |
203
|
|
|
* @param integer|array $id |
204
|
|
|
* A single ID or an array of ID's |
205
|
|
|
* @throws DatabaseException |
206
|
|
|
* @return mixed |
207
|
|
|
* If `$id` is an integer, the result will be an Author object, |
208
|
|
|
* otherwise an array of Author objects will be returned. If no |
209
|
|
|
* Authors are found, or no `$id` is given, `null` is returned. |
210
|
|
|
*/ |
211
|
|
|
public static function fetchByID($id) |
212
|
|
|
{ |
213
|
|
|
$return_single = false; |
214
|
|
|
|
215
|
|
|
if (is_null($id)) { |
|
|
|
|
216
|
|
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (!is_array($id)) { |
220
|
|
|
$return_single = true; |
221
|
|
|
$id = array((int)$id); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (empty($id)) { |
225
|
|
|
return null; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Get all the Author ID's that are already in `self::$_pool` |
229
|
|
|
$authors = array(); |
230
|
|
|
$pooled_authors = array_intersect($id, array_keys(self::$_pool)); |
231
|
|
|
|
232
|
|
|
foreach ($pooled_authors as $pool_author) { |
233
|
|
|
$authors[] = self::$_pool[$pool_author]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Get all the Author ID's that are not already stored in `self::$_pool` |
237
|
|
|
$id = array_diff($id, array_keys(self::$_pool)); |
238
|
|
|
$id = array_filter($id); |
239
|
|
|
|
240
|
|
|
if (empty($id)) { |
241
|
|
|
return ($return_single ? $authors[0] : $authors); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$authors = (new AuthorManager) |
245
|
|
|
->select() |
246
|
|
|
->authors($id) |
|
|
|
|
247
|
|
|
->execute() |
248
|
|
|
->rows(); |
249
|
|
|
|
250
|
|
|
foreach ($authors as $author) { |
251
|
|
|
self::$_pool[$author->get('id')] = $author; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return ($return_single ? $authors[0] : $authors); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Returns an Author by Username. This function will search the |
259
|
|
|
* `AuthorManager::$_pool` for Authors first before querying `tbl_authors` |
260
|
|
|
* |
261
|
|
|
* @param string $username |
262
|
|
|
* The Author's username |
263
|
|
|
* @return Author|null |
264
|
|
|
* If an Author is found, an Author object is returned, otherwise null. |
265
|
|
|
*/ |
266
|
|
|
public static function fetchByUsername($username) |
267
|
|
|
{ |
268
|
|
|
if (!isset(self::$_pool[$username])) { |
269
|
|
|
$author = (new AuthorManager) |
270
|
|
|
->select() |
271
|
|
|
->username($username) |
272
|
|
|
->limit(1) |
273
|
|
|
->execute() |
274
|
|
|
->next(); |
275
|
|
|
|
276
|
|
|
if (!$author) { |
|
|
|
|
277
|
|
|
return null; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
self::$_pool[$username] = $author; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return self::$_pool[$username]; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Creates a new Author object. |
288
|
|
|
* |
289
|
|
|
* @return Author |
290
|
|
|
*/ |
291
|
|
|
public static function create() |
292
|
|
|
{ |
293
|
|
|
return new Author; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Factory method that creates a new AuthorQuery. |
298
|
|
|
* |
299
|
|
|
* @since Symphony 3.0.0 |
300
|
|
|
* @param array $projection |
301
|
|
|
* The projection to select. |
302
|
|
|
* If no projection gets added, it defaults to `AuthorQuery::getDefaultProjection()`. |
303
|
|
|
* @return AuthorQuery |
304
|
|
|
*/ |
305
|
|
|
public function select(array $projection = []) |
306
|
|
|
{ |
307
|
|
|
return new AuthorQuery(Symphony::Database(), $projection); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.