Completed
Push — master ( 13e33a...7c2796 )
by Vladimir
17s
created

NewsCategory::getActiveStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file contains functionality relating to the news categories admins can use
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
 * @TODO Create permissions for creating, editing, and modifying categories
11
 */
12
13
/**
14
 * A news category
15
 * @package    BZiON\Models
16
 */
17
class NewsCategory extends AliasModel
18
{
19
    /**
20
     * Whether or not the category is protected from being deleted
21
     * @var bool
22
     */
23
    protected $protected;
24
25
    const DEFAULT_STATUS = 'enabled';
26
27
    /**
28
     * The name of the database table used for queries
29
     */
30
    const TABLE = "news_categories";
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function assignResult($category)
36
    {
37
        $this->alias = $category['alias'];
38
        $this->name = $category['name'];
39 11
        $this->protected = $category['protected'];
40
        $this->status = $category['status'];
41 11
    }
42 11
43 11
    /**
44 11
     * Delete a category. Only delete a category if it is not protected
45 11
     */
46
    public function delete()
47
    {
48
        // Get any articles using this category
49
        $articles = News::fetchIdsFrom("category", $this->getId());
0 ignored issues
show
Bug introduced by
The method fetchIdsFrom() cannot be called from this context as it is declared protected in class BaseModel.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
50 3
51
        // Only delete a category if it is not protected and is not being used
52
        if (!$this->isProtected() && count($articles) == 0) {
53 3
            parent::delete();
54
        }
55
    }
56 3
57 1
    /**
58
     * Disable the category
59 3
     *
60
     * @return void
61
     */
62
    public function disableCategory()
63
    {
64
        if ($this->getStatus() != "disabled") {
65
            $this->status = "disabled";
66 3
            $this->update("status", "disabled");
67
        }
68 3
    }
69 3
70 3
    /**
71
     * Enable the category
72 3
     *
73
     * @return void
74
     */
75
    public function enableCategory()
76
    {
77
        if ($this->getStatus() != "enabled") {
78
            $this->status = "enabled";
79 1
            $this->update("status", "enabled");
80
        }
81 1
    }
82 1
83 1
    /**
84
     * Get the status of the category
85 1
     *
86
     * @return string Either 'enabled', 'disabled', or 'deleted'
87
     */
88
    public function getStatus()
89
    {
90
        return $this->status;
91
    }
92 7
93
    /**
94 7
     * Get all the news entries in the category that aren't disabled or deleted
95
     *
96
     * @param int  $start     The offset used when fetching matches, i.e. the starting point
97
     * @param int  $limit     The amount of matches to be retrieved
98
     * @param bool $getDrafts Whether or not to fetch drafts
99
     *
100
     * @return News[] An array of news objects
101
     */
102
    public function getNews($start = 0, $limit = 5, $getDrafts = false)
103
    {
104
        $ignoredStatuses = "";
105
106 1
        if (!$getDrafts) {
107
            $ignoredStatuses = "'draft', ";
108 1
        }
109
110 1
        $ignoredStatuses .= "'deleted'";
111 1
112
        $query  = "WHERE status NOT IN ($ignoredStatuses) AND category = ? ";
113
        $query .= "ORDER BY created DESC LIMIT $limit OFFSET $start";
114 1
115
        return News::arrayIdToModel(News::fetchIds($query, array($this->getId())));
0 ignored issues
show
Bug introduced by
The method fetchIds() cannot be called from this context as it is declared protected in class BaseModel.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
116 1
    }
117 1
118
    /**
119 1
     * Check if the category is protected from being deleted
120
     *
121
     * @return bool Whether or not the category is protected
122
     */
123
    public function isProtected()
124
    {
125
        return (bool) $this->protected;
126
    }
127 4
128
    /**
129 4
     * Create a new category
130
     *
131
     * @param string $name The name of the category
132
     *
133
     * @return NewsCategory An object representing the category that was just created
134
     */
135
    public static function addCategory($name)
136
    {
137
        return self::create(array(
138
            'alias'     => self::generateAlias($name),
139 11
            'name'      => $name,
140
            'protected' => 0,
141 11
            'status'    => 'enabled'
142 11
        ));
143 11
    }
144 11
145 11
    /**
146
     * Get all of the categories for the news
147
     *
148
     * @return NewsCategory[] An array of categories
149
     */
150
    public static function getCategories()
151
    {
152
        return self::arrayIdToModel(
153
            self::fetchIdsFrom(
154 4
                "status", array("deleted"), true,
155
                "ORDER BY name ASC"
156 4
            )
157 4
        );
158 4
    }
159 4
160
    /**
161
     * Get a query builder for news categories
162
     * @return QueryBuilder
163
     */
164 View Code Duplication
    public static function getQueryBuilder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
165
    {
166
        return new QueryBuilder('NewsCategory', array(
167 1
            'columns' => array(
168
                'name'   => 'name',
169 1
                'status' => 'status'
170
            ),
171
            'name' => 'name',
172
        ));
173
    }
174
175
    /**
176 1
     * {@inheritdoc}
177
     */
178 1
    public static function getParamName()
179 1
    {
180
        return "category";
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public static function getTypeForHumans()
187
    {
188
        return "news category";
189
    }
190
}
191