1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package toolkit |
4
|
|
|
*/ |
5
|
|
|
/** |
6
|
|
|
* The Author class represents a Symphony Author object. Authors are |
7
|
|
|
* the backend users in Symphony. |
8
|
|
|
* |
9
|
|
|
* @since Symphony 3.0.0 it implements the ArrayAccess interface. |
10
|
|
|
*/ |
11
|
|
|
class Author implements ArrayAccess |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* An associative array of information relating to this author where |
15
|
|
|
* the keys map directly to the `tbl_authors` columns. |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $fields = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Stores a key => value pair into the Author object's `$this->fields` array. |
22
|
|
|
* |
23
|
|
|
* @param string $field |
24
|
|
|
* Maps directly to a column in the `tbl_authors` table. |
25
|
|
|
* @param string $value |
26
|
|
|
* The value for the given $field |
27
|
|
|
*/ |
28
|
|
|
public function set($field, $value) |
29
|
|
|
{ |
30
|
|
|
$field = trim($field); |
31
|
|
|
if ($value === null) { |
|
|
|
|
32
|
|
|
$this->fields[$field] = null; |
33
|
|
|
} else { |
34
|
|
|
$this->fields[$field] = trim($value); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retrieves the value from the Author object by field from `$this->fields` |
40
|
|
|
* array. If field is omitted, all fields are returned. |
41
|
|
|
* |
42
|
|
|
* @param string $field |
43
|
|
|
* Maps directly to a column in the `tbl_authors` table. Defaults to null |
44
|
|
|
* @return mixed |
45
|
|
|
* If the field is not set or is empty, returns null. |
46
|
|
|
* If the field is not provided, returns the `$this->fields` array |
47
|
|
|
* Otherwise returns a string. |
48
|
|
|
*/ |
49
|
|
|
public function get($field = null) |
50
|
|
|
{ |
51
|
|
|
if (is_null($field)) { |
52
|
|
|
return $this->fields; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!isset($this->fields[$field]) || $this->fields[$field] == '') { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->fields[$field]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Given a field, remove it from `$this->fields` |
64
|
|
|
* |
65
|
|
|
* @since Symphony 2.2.1 |
66
|
|
|
* @param string $field |
67
|
|
|
* Maps directly to a column in the `tbl_authors` table. Defaults to null |
68
|
|
|
*/ |
69
|
|
|
public function remove($field = null) |
70
|
|
|
{ |
71
|
|
|
if (!is_null($field)) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
unset($this->fields[$field]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Implementation of ArrayAccess::offsetExists() |
80
|
|
|
* |
81
|
|
|
* @param mixed $offset |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function offsetExists($offset) |
85
|
|
|
{ |
86
|
|
|
return isset($this->fields[$offset]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Implementation of ArrayAccess::offsetGet() |
91
|
|
|
* |
92
|
|
|
* @param mixed $offset |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function offsetGet($offset) |
96
|
|
|
{ |
97
|
|
|
return $this->fields[$offset]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Implementation of ArrayAccess::offsetSet() |
102
|
|
|
* |
103
|
|
|
* @param mixed $offset |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function offsetSet($offset, $value) |
108
|
|
|
{ |
109
|
|
|
$this->fields[$offset] = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Implementation of ArrayAccess::offsetUnset() |
114
|
|
|
* |
115
|
|
|
* @param mixed $offset |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function offsetUnset($offset) |
119
|
|
|
{ |
120
|
|
|
unset($this->fields[$offset]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets all the fields values from the database for this extension. |
125
|
|
|
* |
126
|
|
|
* @param array $fields |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function setFields(array $fields) |
130
|
|
|
{ |
131
|
|
|
$this->fields = $fields; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns boolean if the current Author is the original creator |
136
|
|
|
* of this Symphony installation. |
137
|
|
|
* |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
|
|
public function isPrimaryAccount() |
141
|
|
|
{ |
142
|
|
|
return ($this->get('primary') === 'yes'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns boolean if the current Author is of the developer |
147
|
|
|
* user type. |
148
|
|
|
* |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
|
|
public function isDeveloper() |
152
|
|
|
{ |
153
|
|
|
return ($this->get('user_type') == 'developer'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns boolean if the current Author is of the manager |
158
|
|
|
* user type. |
159
|
|
|
* |
160
|
|
|
* @since 2.3.3 |
161
|
|
|
* @return boolean |
162
|
|
|
*/ |
163
|
|
|
public function isManager() |
164
|
|
|
{ |
165
|
|
|
return ($this->get('user_type') == 'manager'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns boolean if the current Author is of the author |
170
|
|
|
* user type. |
171
|
|
|
* |
172
|
|
|
* @since 2.4 |
173
|
|
|
* @return boolean |
174
|
|
|
*/ |
175
|
|
|
public function isAuthor() |
176
|
|
|
{ |
177
|
|
|
return ($this->get('user_type') == 'author'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns boolean if the current Author's authentication token is valid. |
182
|
|
|
* |
183
|
|
|
* @return boolean |
184
|
|
|
*/ |
185
|
|
|
public function isTokenActive() |
186
|
|
|
{ |
187
|
|
|
return !empty($this->get('auth_token')); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns the current Author's auth token. |
192
|
|
|
* |
193
|
|
|
* @return mixed |
194
|
|
|
* The auth token or null |
195
|
|
|
*/ |
196
|
|
|
public function getAuthToken() |
197
|
|
|
{ |
198
|
|
|
return $this->get('auth_token'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* A convenience method that returns an Authors full name |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getFullName() |
207
|
|
|
{ |
208
|
|
|
return $this->get('first_name') . ' ' . $this->get('last_name'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Prior to saving an Author object, the validate function ensures that |
213
|
|
|
* the values in `$this->fields` array are correct. As of Symphony 2.3 |
214
|
|
|
* Authors must have unique username AND email address. This function returns |
215
|
|
|
* boolean, with an `$errors` array provided by reference to the callee |
216
|
|
|
* function. |
217
|
|
|
* |
218
|
|
|
* @param array $errors |
219
|
|
|
* @return boolean |
220
|
|
|
*/ |
221
|
|
|
public function validate(&$errors) |
222
|
|
|
{ |
223
|
|
|
$errors = array(); |
224
|
|
|
$current_author = null; |
225
|
|
|
|
226
|
|
|
if (is_null($this->get('first_name'))) { |
227
|
|
|
$errors['first_name'] = __('First name is required'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (is_null($this->get('last_name'))) { |
231
|
|
|
$errors['last_name'] = __('Last name is required'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($this->get('id')) { |
235
|
|
|
$current_author = Symphony::Database() |
236
|
|
|
->select(['email', 'username']) |
237
|
|
|
->from('tbl_authors') |
238
|
|
|
->where(['id' => $this->get('id')]) |
239
|
|
|
->execute() |
240
|
|
|
->next(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// Include validators |
244
|
|
|
include TOOLKIT . '/util.validators.php'; |
|
|
|
|
245
|
|
|
|
246
|
|
|
// Check that Email is provided |
247
|
|
|
if (is_null($this->get('email'))) { |
248
|
|
|
$errors['email'] = __('E-mail address is required'); |
249
|
|
|
|
250
|
|
|
// Check Email is valid |
251
|
|
|
} elseif (isset($validators['email']) && !General::validateString($this->get('email'), $validators['email'])) { |
|
|
|
|
252
|
|
|
$errors['email'] = __('E-mail address entered is invalid'); |
253
|
|
|
|
254
|
|
|
// Check Email is valid, fallback when no validator found |
255
|
|
|
} elseif (!isset($validators['email']) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { |
|
|
|
|
256
|
|
|
$errors['email'] = __('E-mail address entered is invalid'); |
257
|
|
|
|
258
|
|
|
// Check that if an existing Author changes their email address that |
259
|
|
|
// it is not already used by another Author |
260
|
|
|
} elseif ($this->get('id')) { |
261
|
|
|
if ( |
262
|
|
|
$current_author['email'] !== $this->get('email') && |
263
|
|
|
(int)Symphony::Database() |
264
|
|
|
->select() |
265
|
|
|
->count() |
266
|
|
|
->from('tbl_authors') |
267
|
|
|
->where(['email' => $this->get('email')]) |
268
|
|
|
->limit(1) |
269
|
|
|
->execute() |
270
|
|
|
->variable(0) !== 0 |
271
|
|
|
) { |
272
|
|
|
$errors['email'] = __('E-mail address is already taken'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// Check that Email is not in use by another Author |
276
|
|
|
} elseif (Symphony::Database() |
277
|
|
|
->select(['id']) |
278
|
|
|
->from('tbl_authors') |
279
|
|
|
->where(['email' => $this->get('email')]) |
280
|
|
|
->limit(1) |
281
|
|
|
->execute() |
282
|
|
|
->variable('id')) { |
283
|
|
|
$errors['email'] = __('E-mail address is already taken'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// Check the username exists |
287
|
|
|
if (is_null($this->get('username'))) { |
288
|
|
|
$errors['username'] = __('Username is required'); |
289
|
|
|
|
290
|
|
|
// Check that if it's an existing Author that the username is not already |
291
|
|
|
// in use by another Author if they are trying to change it. |
292
|
|
|
} elseif ($this->get('id')) { |
293
|
|
|
if ( |
294
|
|
|
$current_author['username'] !== $this->get('username') && |
295
|
|
|
(int)Symphony::Database() |
296
|
|
|
->select() |
297
|
|
|
->count() |
298
|
|
|
->from('tbl_authors') |
299
|
|
|
->where(['username' => $this->get('username')]) |
300
|
|
|
->limit(1) |
301
|
|
|
->execute() |
302
|
|
|
->variable(0) !== 0 |
303
|
|
|
) { |
304
|
|
|
$errors['username'] = __('Username is already taken'); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
// Check that the username is unique |
308
|
|
|
} elseif (Symphony::Database() |
309
|
|
|
->select(['id']) |
310
|
|
|
->from('tbl_authors') |
311
|
|
|
->where(['username' => $this->get('username')]) |
312
|
|
|
->limit(1) |
313
|
|
|
->execute() |
314
|
|
|
->variable('id')) { |
315
|
|
|
$errors['username'] = __('Username is already taken'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if (is_null($this->get('password'))) { |
319
|
|
|
$errors['password'] = __('Password is required'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return (empty($errors) ? true : false); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* This is the insert method for the Author. This takes the current |
327
|
|
|
* `$this->fields` values and adds them to the database using either the |
328
|
|
|
* `AuthorManager::edit` or `AuthorManager::add` functions. An |
329
|
|
|
* existing user is determined by if an ID is already set. |
330
|
|
|
* When the database is updated successfully, the id of the author is set. |
331
|
|
|
* |
332
|
|
|
* @uses AuthorManager::add() |
333
|
|
|
* @uses AuthorManager::edit() |
334
|
|
|
* @return integer|boolean |
335
|
|
|
* When a new Author is added or updated, an integer of the Author ID |
336
|
|
|
* will be returned, otherwise false will be returned for a failed update. |
337
|
|
|
*/ |
338
|
|
|
public function commit() |
339
|
|
|
{ |
340
|
|
|
if (!is_null($this->get('id'))) { |
341
|
|
|
$id = $this->get('id'); |
342
|
|
|
$this->remove('id'); |
343
|
|
|
|
344
|
|
|
if (AuthorManager::edit($id, $this->get())) { |
345
|
|
|
$this->set('id', $id); |
346
|
|
|
return $id; |
347
|
|
|
} else { |
348
|
|
|
return false; |
349
|
|
|
} |
350
|
|
|
} else { |
351
|
|
|
$id = AuthorManager::add($this->get()); |
352
|
|
|
if ($id) { |
353
|
|
|
$this->set('id', $id); |
354
|
|
|
} |
355
|
|
|
return $id; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|