Completed
Pull Request — master (#186)
by Vladimir
05:50 queued 02:55
created

Page::setStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
     * Get whether or not this Page is a draft.
119
     *
120
     * @since 0.11.0
121
     *
122
     * @return bool
123
     */
124
    public function isDraft()
125
    {
126
        return (bool)$this->is_draft;
127
    }
128
129 1
    /**
130
     * Get whether or not this Page was unlisted.
131 1
     *
132
     * An unlisted page will not appear in the secondary navigation.
133
     *
134
     * @since 0.11.0
135
     *
136
     * @return bool
137
     */
138 1
    public function isUnlisted()
139
    {
140 1
        return (bool)$this->is_unlisted;
141
    }
142
143
    /**
144
     * Set the content of the page
145
     *
146
     * @param  string $content
147
     *
148
     * @return static
149
     */
150
    public function setContent($content)
151
    {
152
        return $this->updateProperty($this->content, "content", $content);
153
    }
154
155
    /**
156
     * Set the draft status for this page.
157
     *
158
     * @param bool $draft
159
     *
160
     * @return static
161
     */
162
    public function setDraft($draft)
163
    {
164
        return $this->updateProperty($this->is_draft, 'is_draft', $draft);
165
    }
166
167
    /**
168
     * Set the unlisted status for this page.
169
     *
170
     * @param bool $unlisted
171
     *
172
     * @return static
173
     */
174
    public function setUnlisted($unlisted)
175
    {
176
        return $this->updateProperty($this->is_unlisted, 'is_unlisted', $unlisted);
177
    }
178
179
    /**
180
     * Update the last edit timestamp
181
     * @return self
182
     */
183
    public function updateEditTimestamp()
184 1
    {
185
        return $this->updateProperty($this->updated, "updated", TimeDate::now());
186 1
    }
187 1
188 1
    /**
189 1
     * Create a new Page
190 1
     *
191 1
     * @param string $title       The title of the page
192 1
     * @param string $content     The content of page
193 1
     * @param int    $authorID    The ID of the author
194
     * @param bool   $is_draft    Whether or not the page should be saved as a draft
195
     * @param bool   $is_unlisted Whether or not the page should be unlisted
196
     *
197
     * @since 0.11.0 The former enum $status parameter has been changed to the boolean $is_draft. The $is_unlisted
198
     *               argument has been added.
199 1
     *
200
     * @return Page An object representing the page that was just created
201 1
     */
202
    public static function addPage($title, $content, $authorID, $is_draft = false, $is_unlisted = false)
203
    {
204
        return self::create([
205
            'name'    => $title,
206
            'alias'   => self::generateAlias($title),
207 1
            'content' => $content,
208
            'author'  => $authorID,
209
            'is_draft' => (bool)$is_draft,
210 1
            'is_unlisted' => (bool)$is_unlisted,
211
        ], ['created', 'updated']);
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public static function getRouteName($action = 'show')
218
    {
219 1
        return "custom_page_$action";
220
    }
221 1
222
    /**
223
     * {@inheritdoc}
224
     */
225
    protected static function getDisallowedAliases()
226
    {
227 1
        return array(
228
            "admin", "bans", "index", "login", "logout", "maps", "matches",
229
            "messages", "news", "notifications", "pages", "players", "servers",
230 1
            "teams", "visits"
231
        );
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public static function getActiveStatuses()
238
    {
239 1
        return array('live', 'revision');
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public static function getEagerColumns($prefix = null)
246
    {
247
        $columns = [
248
            'id',
249
            'parent_id',
250
            'name',
251
            'alias',
252
            'author',
253
            'home',
254 1
            'status',
255
        ];
256 1
257 1
        return self::formatColumns($prefix, $columns);
0 ignored issues
show
Deprecated Code introduced by
The method BaseModel::formatColumns() has been deprecated with message: 0.10.2 This function has been removed and is no longer required with the new query builder

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public static function getLazyColumns()
264
    {
265
        return 'content,created,updated';
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public static function getQueryBuilder()
272
    {
273
        return QueryBuilderFlex::createForModel(Page::class)
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
274
            ->setNameColumn('name')
275
        ;
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public static function getActiveModels(QueryBuilderFlex &$qb)
282
    {
283
        $qb
284
            ->whereNot(self::DELETED_COLUMN, '=', self::DELETED_VALUE)
285
            ->whereNot('is_draft', '=', true)
286
        ;
287
288
        return true;
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294
    public static function getEagerColumnsList()
295
    {
296
        return [
297
            'id',
298
            'name',
299
            'alias',
300
            'author',
301
            'is_draft',
302
            'is_deleted',
303
            'is_unlisted',
304
        ];
305
    }
306
307
    /**
308
     * Get the home page
309
     * @deprecated
310
     * @return Page
311
     */
312
    public static function getHomePage()
313
    {
314
        return self::get(self::fetchIdFrom(1, "home"));
315
    }
316
}
317