/
media_category_select.php
90 lines (79 loc) · 2.28 KB
/
media_category_select.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
<?php
/**
* Class MediaKategorie Select.
*
* @package redaxo\mediapool
*/
class rex_media_category_select extends rex_select
{
/**
* @var bool
*/
private $check_perms;
/**
* @var int|int[]|null
*/
private $rootId;
private $loaded = false;
public function __construct($check_perms = true)
{
$this->check_perms = $check_perms;
$this->rootId = null;
parent::__construct();
}
/**
* Kategorie-Id oder ein Array von Kategorie-Ids als Wurzelelemente der Select-Box.
*
* @param int|int[]|null $rootId Kategorie-Id oder Array von Kategorie-Ids zur Identifikation der Wurzelelemente
*/
public function setRootId($rootId)
{
$this->rootId = $rootId;
}
protected function addCatOptions()
{
if (null !== $this->rootId) {
if (is_array($this->rootId)) {
foreach ($this->rootId as $rootId) {
if ($rootCat = rex_media_category::get($rootId)) {
$this->addCatOption($rootCat);
}
}
} else {
if ($rootCat = rex_media_category::get($this->rootId)) {
$this->addCatOption($rootCat);
}
}
} else {
if ($rootCats = rex_media_category::getRootCategories()) {
foreach ($rootCats as $rootCat) {
$this->addCatOption($rootCat);
}
}
}
}
protected function addCatOption(rex_media_category $mediacat)
{
if (!$this->check_perms ||
$this->check_perms && rex::getUser()->getComplexPerm('media')->hasCategoryPerm($mediacat->getId())
) {
$mid = $mediacat->getId();
$mname = $mediacat->getName();
$this->addOption($mname, $mid, $mid, $mediacat->getParentId());
$childs = $mediacat->getChildren();
if (is_array($childs)) {
foreach ($childs as $child) {
$this->addCatOption($child);
}
}
}
}
public function get()
{
if (!$this->loaded) {
$this->addCatOptions();
$this->loaded = true;
}
return parent::get();
}
}