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