1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality relating to the custom pages that admins can great |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A custom page |
11
|
|
|
* @package BZiON\Models |
12
|
|
|
*/ |
13
|
|
|
class Page extends AliasModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The content of the page |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $content; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The creation date of the page |
23
|
|
|
* @var TimeDate |
24
|
|
|
*/ |
25
|
|
|
protected $created; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The date the page was last updated |
29
|
|
|
* @var TimeDate |
30
|
|
|
*/ |
31
|
|
|
protected $updated; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The ID of the author of the page |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $author; |
38
|
|
|
|
39
|
|
|
protected $is_draft; |
40
|
|
|
protected $is_unlisted; |
41
|
|
|
|
42
|
|
|
const DEFAULT_STATUS = 'live'; |
43
|
|
|
|
44
|
|
|
const DELETED_COLUMN = 'is_deleted'; |
45
|
|
|
const TABLE = 'pages'; |
46
|
|
|
|
47
|
|
|
const CREATE_PERMISSION = Permission::CREATE_PAGE; |
48
|
|
|
const EDIT_PERMISSION = Permission::EDIT_PAGE; |
49
|
|
|
const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_PAGE; |
50
|
|
|
const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_PAGE; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
protected function assignResult($page) |
56
|
|
|
{ |
57
|
|
|
$this->name = $page['name']; |
58
|
|
|
$this->alias = $page['alias']; |
59
|
|
|
$this->author = $page['author']; |
60
|
|
|
$this->is_unlisted = $page['is_unlisted']; |
61
|
|
|
$this->is_draft = $page['is_draft']; |
62
|
|
|
$this->is_deleted = $page['is_deleted']; |
63
|
|
|
} |
64
|
1 |
|
|
65
|
|
|
/** |
66
|
1 |
|
* {@inheritdoc} |
67
|
1 |
|
*/ |
68
|
1 |
|
protected function assignLazyResult($page) |
69
|
1 |
|
{ |
70
|
1 |
|
$this->content = $page['content']; |
71
|
1 |
|
$this->created = TimeDate::fromMysql($page['created']); |
72
|
|
|
$this->updated = TimeDate::fromMysql($page['updated']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the raw content of the page |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getContent() |
80
|
|
|
{ |
81
|
|
|
$this->lazyLoad(); |
82
|
|
|
|
83
|
|
|
return $this->content; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the page's submission time |
88
|
|
|
* @return TimeDate |
89
|
|
|
*/ |
90
|
|
|
public function getCreated() |
91
|
|
|
{ |
92
|
|
|
$this->lazyLoad(); |
93
|
|
|
|
94
|
|
|
return $this->created->copy(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the time when the page was last updated |
99
|
|
|
* @return TimeDate |
100
|
|
|
*/ |
101
|
|
|
public function getUpdated() |
102
|
|
|
{ |
103
|
|
|
$this->lazyLoad(); |
104
|
|
|
|
105
|
|
|
return $this->updated->copy(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the user who created the page |
110
|
|
|
* @return Player The page's author |
111
|
|
|
*/ |
112
|
|
|
public function getAuthor() |
113
|
|
|
{ |
114
|
|
|
return Player::get($this->author); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set the content of the page |
119
|
|
|
* |
120
|
|
|
* @param string $content |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function setContent($content) |
124
|
|
|
{ |
125
|
|
|
return $this->updateProperty($this->content, "content", $content); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
1 |
|
* Update the last edit timestamp |
130
|
|
|
* @return self |
131
|
1 |
|
*/ |
132
|
|
|
public function updateEditTimestamp() |
133
|
|
|
{ |
134
|
|
|
return $this->updateProperty($this->updated, "updated", TimeDate::now()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
1 |
|
* Create a new Page |
139
|
|
|
* |
140
|
1 |
|
* @param string $title The title of the page |
141
|
|
|
* @param string $content The content of page |
142
|
|
|
* @param int $authorID The ID of the author |
143
|
|
|
* @param bool $is_draft Whether or not |
144
|
|
|
* |
145
|
|
|
* @since 0.11.0 The former enum $status parameter has been changed to the boolean $is_draft |
146
|
|
|
* |
147
|
|
|
* @return Page An object representing the page that was just created |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public static function addPage($title, $content, $authorID, $is_draft = false) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
return self::create([ |
152
|
|
|
'name' => $title, |
153
|
|
|
'alias' => self::generateAlias($title), |
154
|
|
|
'content' => $content, |
155
|
|
|
'author' => $authorID, |
156
|
|
|
'is_draft' => (bool)$is_draft |
157
|
|
|
], ['created', 'updated']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public static function getRouteName($action = 'show') |
164
|
|
|
{ |
165
|
|
|
return "custom_page_$action"; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
protected static function getDisallowedAliases() |
172
|
|
|
{ |
173
|
|
|
return array( |
174
|
|
|
"admin", "bans", "index", "login", "logout", "maps", "matches", |
175
|
|
|
"messages", "news", "notifications", "pages", "players", "servers", |
176
|
|
|
"teams", "visits" |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public static function getActiveStatuses() |
184
|
1 |
|
{ |
185
|
|
|
return array('live', 'revision'); |
186
|
1 |
|
} |
187
|
1 |
|
|
188
|
1 |
|
/** |
189
|
1 |
|
* {@inheritdoc} |
190
|
1 |
|
*/ |
191
|
1 |
|
public static function getEagerColumns($prefix = null) |
192
|
1 |
|
{ |
193
|
1 |
|
$columns = [ |
194
|
|
|
'id', |
195
|
|
|
'parent_id', |
196
|
|
|
'name', |
197
|
|
|
'alias', |
198
|
|
|
'author', |
199
|
1 |
|
'home', |
200
|
|
|
'status', |
201
|
1 |
|
]; |
202
|
|
|
|
203
|
|
|
return self::formatColumns($prefix, $columns); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
1 |
|
* {@inheritdoc} |
208
|
|
|
*/ |
209
|
|
|
public static function getLazyColumns() |
210
|
1 |
|
{ |
211
|
|
|
return 'content,created,updated'; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
*/ |
217
|
|
|
public static function getQueryBuilder() |
218
|
|
|
{ |
219
|
1 |
|
return QueryBuilderFlex::createForModel(Page::class) |
|
|
|
|
220
|
|
|
->setNameColumn('name') |
221
|
1 |
|
; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
1 |
|
public static function getActiveModels(QueryBuilderFlex &$qb) |
228
|
|
|
{ |
229
|
|
|
$qb |
230
|
1 |
|
->whereNot(self::DELETED_COLUMN, '=', self::DELETED_VALUE) |
231
|
|
|
->whereNot('is_draft', '=', true) |
232
|
|
|
; |
233
|
|
|
|
234
|
|
|
return true; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
1 |
|
*/ |
240
|
|
|
public static function getEagerColumnsList() |
241
|
|
|
{ |
242
|
|
|
return [ |
243
|
|
|
'id', |
244
|
|
|
'name', |
245
|
|
|
'alias', |
246
|
|
|
'author', |
247
|
|
|
'is_draft', |
248
|
|
|
'is_deleted', |
249
|
|
|
'is_unlisted', |
250
|
|
|
]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
1 |
|
* Get the home page |
255
|
|
|
* @deprecated |
256
|
1 |
|
* @return Page |
257
|
1 |
|
*/ |
258
|
|
|
public static function getHomePage() |
259
|
|
|
{ |
260
|
|
|
return self::get(self::fetchIdFrom(1, "home")); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.