1 <?php
  2 
  3 /**
  4  * Object Oriented Framework: Bildet eine Kategorie im Medienpool ab.
  5  *
  6  * @package redaxo\mediapool
  7  */
  8 class rex_media_category
  9 {
 10     use rex_instance_pool_trait;
 11     use rex_instance_list_pool_trait;
 12 
 13     // id
 14     private $id = '';
 15     // parent_id
 16     private $parent_id = '';
 17 
 18     // name
 19     private $name = '';
 20     // path
 21     private $path = '';
 22 
 23     // createdate
 24     private $createdate = '';
 25     // updatedate
 26     private $updatedate = '';
 27 
 28     // createuser
 29     private $createuser = '';
 30     // updateuser
 31     private $updateuser = '';
 32 
 33     /**
 34      * @param int $id
 35      *
 36      * @return self
 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      * @return self[]
 79      */
 80     public static function getRootCategories()
 81     {
 82         return self::getChildCategories(0);
 83     }
 84 
 85     /**
 86      * @param int $parentId
 87      *
 88      * @return self[]
 89      */
 90     protected static function getChildCategories($parentId)
 91     {
 92         $parentId = (int) $parentId;
 93         // for $parentId=0 root categories will be returned, so abort here for $parentId<0 only
 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      * @return int
113      */
114     public function getId()
115     {
116         return $this->id;
117     }
118 
119     /**
120      * @return string
121      */
122     public function getName()
123     {
124         return $this->name;
125     }
126 
127     /**
128      * @return string
129      */
130     public function getPath()
131     {
132         return $this->path;
133     }
134 
135     /**
136      * Returns the path ids of the category as an array.
137      *
138      * @return int[]
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      * @return string
156      */
157     public function getUpdateUser()
158     {
159         return $this->updateuser;
160     }
161 
162     /**
163      * @return int
164      */
165     public function getUpdateDate()
166     {
167         return $this->updatedate;
168     }
169 
170     /**
171      * @return string
172      */
173     public function getCreateUser()
174     {
175         return $this->createuser;
176     }
177 
178     /**
179      * @return int
180      */
181     public function getCreateDate()
182     {
183         return $this->createdate;
184     }
185 
186     /**
187      * @return int
188      */
189     public function getParentId()
190     {
191         return $this->parent_id;
192     }
193 
194     /**
195      * @return self
196      */
197     public function getParent()
198     {
199         return self::get($this->getParentId());
200     }
201 
202     /**
203      * Get an array of all parentCategories.
204      * Returns an array of rex_media_category objects sorted by $priority.
205      *
206      * @return self[]
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      * Checks if $anObj is in the parent tree of the object.
226      *
227      * @param self $anObj
228      *
229      * @return bool
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      * @return self[]
244      */
245     public function getChildren()
246     {
247         return self::getChildCategories($this->getId());
248     }
249 
250     /**
251      * @return rex_media[]
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      * @param self $mediaCat
270      *
271      * @return bool
272      */
273     public function isParent(self $mediaCat)
274     {
275         return $this->getParentId() == $mediaCat->getId();
276     }
277 }
278