/
media_category.php
277 lines (238 loc) · 5.74 KB
/
media_category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Object Oriented Framework: Bildet eine Kategorie im Medienpool ab.
*
* @package redaxo\mediapool
*/
class rex_media_category
{
use rex_instance_pool_trait;
use rex_instance_list_pool_trait;
// id
private $id = '';
// parent_id
private $parent_id = '';
// name
private $name = '';
// path
private $path = '';
// createdate
private $createdate = '';
// updatedate
private $updatedate = '';
// createuser
private $createuser = '';
// updateuser
private $updateuser = '';
/**
* @param int $id
*
* @return self|null
*/
public static function get($id)
{
$id = (int) $id;
if (0 >= $id) {
return null;
}
return self::getInstance($id, static function ($id) {
$cat_path = rex_path::addonCache('mediapool', $id . '.mcat');
$cache = rex_file::getCache($cat_path);
if (!$cache) {
rex_media_cache::generateCategory($id);
$cache = rex_file::getCache($cat_path);
}
if ($cache) {
$cat = new self();
$cat->id = $cache['id'];
$cat->parent_id = $cache['parent_id'];
$cat->name = $cache['name'];
$cat->path = $cache['path'];
$cat->createdate = $cache['createdate'];
$cat->updatedate = $cache['updatedate'];
$cat->createuser = $cache['createuser'];
$cat->updateuser = $cache['updateuser'];
return $cat;
}
return null;
});
}
/**
* @return self[]
*/
public static function getRootCategories()
{
return self::getChildCategories(0);
}
/**
* @param int $parentId
*
* @return self[]
*/
protected static function getChildCategories($parentId)
{
$parentId = (int) $parentId;
// for $parentId=0 root categories will be returned, so abort here for $parentId<0 only
if (0 > $parentId) {
return [];
}
return self::getInstanceList([$parentId, 'children'], ['self', 'get'], static function ($parentId) {
$catlist_path = rex_path::addonCache('mediapool', $parentId . '.mclist');
$list = rex_file::getCache($catlist_path, null);
if (null === $list) {
rex_media_cache::generateCategoryList($parentId);
$list = rex_file::getCache($catlist_path);
}
return $list;
});
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Returns the path ids of the category as an array.
*
* @return int[]
*
* @psalm-return list<int>
*/
public function getPathAsArray()
{
$p = explode('|', $this->path);
foreach ($p as $k => $v) {
if ('' == $v) {
unset($p[$k]);
} else {
$p[$k] = (int) $v;
}
}
return array_values($p);
}
/**
* @return string
*/
public function getUpdateUser()
{
return $this->updateuser;
}
/**
* @return int
*/
public function getUpdateDate()
{
return $this->updatedate;
}
/**
* @return string
*/
public function getCreateUser()
{
return $this->createuser;
}
/**
* @return int
*/
public function getCreateDate()
{
return $this->createdate;
}
/**
* @return int
*/
public function getParentId()
{
return $this->parent_id;
}
/**
* @return self
*/
public function getParent()
{
return self::get($this->getParentId());
}
/**
* Get an array of all parentCategories.
* Returns an array of rex_media_category objects sorted by $priority.
*
* @return self[]
*/
public function getParentTree()
{
$tree = [];
if ($this->path) {
$explode = explode('|', $this->path);
if (is_array($explode)) {
foreach ($explode as $var) {
if ('' != $var) {
$tree[] = self::get($var);
}
}
}
}
return $tree;
}
/**
* Checks if $anObj is in the parent tree of the object.
*
* @param self $anObj
*
* @return bool
*/
public function inParentTree($anObj)
{
$tree = $this->getParentTree();
foreach ($tree as $treeObj) {
if ($treeObj == $anObj) {
return true;
}
}
return false;
}
/**
* @return self[]
*/
public function getChildren()
{
return self::getChildCategories($this->getId());
}
/**
* @return rex_media[]
*/
public function getMedia()
{
return self::getInstanceList([$this->getId(), 'media'], ['rex_media', 'get'], static function ($id) {
$list_path = rex_path::addonCache('mediapool', $id . '.mlist');
$list = rex_file::getCache($list_path, null);
if (null === $list) {
rex_media_cache::generateList($id);
$list = rex_file::getCache($list_path);
}
return $list;
});
}
/**
* @return bool
*/
public function isParent(self $mediaCat)
{
return $this->getParentId() == $mediaCat->getId();
}
}