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
|
|
|
* @TODO Set up methods to modify the News Categories |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A news category |
16
|
|
|
* @package BZiON\Models |
17
|
|
|
*/ |
18
|
|
|
class NewsCategory extends AliasModel |
19
|
|
|
{ |
20
|
|
|
/** @var bool Whether or not the category is protected from being deleted from the UI */ |
21
|
|
|
protected $is_protected; |
22
|
|
|
|
23
|
|
|
/** @var bool When set to true, no new articles can be assigned this category */ |
24
|
|
|
protected $is_read_only; |
25
|
|
|
|
26
|
|
|
const DEFAULT_STATUS = 'enabled'; |
27
|
|
|
|
28
|
|
|
const DELETED_COLUMN = 'is_deleted'; |
29
|
|
|
const TABLE = "news_categories"; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function assignResult($category) |
35
|
|
|
{ |
36
|
|
|
$this->alias = $category['alias']; |
37
|
|
|
$this->name = $category['name']; |
38
|
|
|
$this->is_protected = $category['is_protected']; |
39
|
11 |
|
$this->is_deleted = $category['is_deleted']; |
40
|
|
|
} |
41
|
11 |
|
|
42
|
11 |
|
/** |
43
|
11 |
|
* Delete a category. Only delete a category if it is not protected |
44
|
11 |
|
* |
45
|
11 |
|
* @throws DeletionDeniedException |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
public function delete() |
49
|
|
|
{ |
50
|
3 |
|
$hasArticles = (bool) News::getQueryBuilder() |
51
|
|
|
->where('category', '=', $this->getId()) |
52
|
|
|
->active() |
53
|
3 |
|
->count() |
54
|
|
|
; |
55
|
|
|
|
56
|
3 |
|
if ($hasArticles) { |
57
|
1 |
|
throw new DeletionDeniedException('This category has news articles and cannot be deleted.'); |
58
|
|
|
} |
59
|
3 |
|
|
60
|
|
|
if ($this->isProtected()) { |
61
|
|
|
throw new DeletionDeniedException('This category is protected and cannot be deleted.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
parent::delete(); |
65
|
|
|
} |
66
|
3 |
|
|
67
|
|
|
/** |
68
|
3 |
|
* Get all the news entries in the category that aren't disabled or deleted |
69
|
3 |
|
* |
70
|
3 |
|
* @param int $start The offset used when fetching matches, i.e. the starting point |
71
|
|
|
* @param int $limit The amount of matches to be retrieved |
72
|
3 |
|
* @param bool $getDrafts Whether or not to fetch drafts |
73
|
|
|
* |
74
|
|
|
* @throws \Pixie\Exception |
75
|
|
|
* @throws Exception |
76
|
|
|
* |
77
|
|
|
* @return News[] An array of news objects |
78
|
|
|
*/ |
79
|
1 |
View Code Duplication |
public function getNews($start = 0, $limit = 5, $getDrafts = false) |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
$qb = News::getQueryBuilder() |
82
|
1 |
|
->limit($limit) |
83
|
1 |
|
->offset($start) |
84
|
|
|
->active() |
85
|
1 |
|
->where('category', '=', $this->getId()) |
86
|
|
|
; |
87
|
|
|
|
88
|
|
|
if ($getDrafts) { |
89
|
|
|
$qb->whereNot('is_draft', '=', true); |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
return $qb->getModels(true); |
93
|
|
|
} |
94
|
7 |
|
|
95
|
|
|
/** |
96
|
|
|
* Check if the category is protected from being deleted. |
97
|
|
|
* |
98
|
|
|
* @return bool Whether or not the category is protected |
99
|
|
|
*/ |
100
|
|
|
public function isProtected() |
101
|
|
|
{ |
102
|
|
|
return (bool) $this->is_protected; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
1 |
|
* Check if new News article can be assigned this category. |
107
|
|
|
* |
108
|
1 |
|
* @return bool |
109
|
|
|
*/ |
110
|
1 |
|
public function isReadOnly() |
111
|
1 |
|
{ |
112
|
|
|
return (bool) $this->is_read_only; |
113
|
|
|
} |
114
|
1 |
|
|
115
|
|
|
/** |
116
|
1 |
|
* Create a new category |
117
|
1 |
|
* |
118
|
|
|
* @param string $name The name of the category |
119
|
1 |
|
* |
120
|
|
|
* @return NewsCategory An object representing the category that was just created |
121
|
|
|
*/ |
122
|
|
|
public static function addCategory($name) |
123
|
|
|
{ |
124
|
|
|
return self::create(array( |
125
|
|
|
'alias' => self::generateAlias($name), |
126
|
|
|
'name' => $name, |
127
|
4 |
|
)); |
128
|
|
|
} |
129
|
4 |
|
|
130
|
|
|
/** |
131
|
|
|
* Get all of the categories for the news |
132
|
|
|
* |
133
|
|
|
* @throws Exception |
134
|
|
|
* |
135
|
|
|
* @return NewsCategory[] An array of categories |
136
|
|
|
*/ |
137
|
|
|
public static function getCategories() |
138
|
|
|
{ |
139
|
11 |
|
return self::getQueryBuilder() |
140
|
|
|
->orderBy('name', 'ASC') |
141
|
11 |
|
->active() |
142
|
11 |
|
->getModels(true) |
143
|
11 |
|
; |
144
|
11 |
|
} |
145
|
11 |
|
|
146
|
|
|
/** |
147
|
|
|
* Get a query builder for news categories. |
148
|
|
|
* |
149
|
|
|
* @throws Exception |
150
|
|
|
* |
151
|
|
|
* @return QueryBuilderFlex |
152
|
|
|
*/ |
153
|
|
|
public static function getQueryBuilder() |
154
|
4 |
|
{ |
155
|
|
|
return QueryBuilderFlex::createForModel(NewsCategory::class) |
|
|
|
|
156
|
4 |
|
->setNameColumn('name') |
157
|
4 |
|
; |
158
|
4 |
|
} |
159
|
4 |
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public static function getParamName() |
164
|
|
|
{ |
165
|
|
|
return "category"; |
166
|
|
|
} |
167
|
1 |
|
|
168
|
|
|
/** |
169
|
1 |
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public static function getTypeForHumans() |
172
|
|
|
{ |
173
|
|
|
return "news category"; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.