Completed
Pull Request — master (#186)
by Vladimir
11:52 queued 07:48
created

Page::getStatus()   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
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * 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 string $status   Page status: 'live','disabled',or 'deleted'
144
     *
145
     * @return Page An object representing the page that was just created
146
     */
147
    public static function addPage($title, $content, $authorID, $status = "live")
148
    {
149
        return self::create(array(
150
            'name'    => $title,
151
            'alias'   => self::generateAlias($title),
152
            'content' => $content,
153
            'author'  => $authorID,
154
            'home'    => 0,
155
            'status'  => $status,
156
        ), array('created', 'updated'));
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public static function getRouteName($action = 'show')
163
    {
164
        return "custom_page_$action";
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    protected static function getDisallowedAliases()
171
    {
172
        return array(
173
            "admin", "bans", "index", "login", "logout", "maps", "matches",
174
            "messages", "news", "notifications", "pages", "players", "servers",
175
            "teams", "visits"
176
        );
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public static function getActiveStatuses()
183
    {
184 1
        return array('live', 'revision');
185
    }
186 1
187 1
    /**
188 1
     * {@inheritdoc}
189 1
     */
190 1
    public static function getEagerColumns($prefix = null)
191 1
    {
192 1
        $columns = [
193 1
            'id',
194
            'parent_id',
195
            'name',
196
            'alias',
197
            'author',
198
            'home',
199 1
            'status',
200
        ];
201 1
202
        return self::formatColumns($prefix, $columns);
203
    }
204
205
    /**
206
     * {@inheritdoc}
207 1
     */
208
    public static function getLazyColumns()
209
    {
210 1
        return 'content,created,updated';
211
    }
212
213
    /**
214
     * Get a query builder for pages
215
     * @return QueryBuilder
216
     */
217
    public static function getQueryBuilder()
218
    {
219 1
        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...
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