/
select_category.php
148 lines (132 loc) · 4.46 KB
/
select_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
<?php
/**
* @package redaxo\structure
*/
class rex_category_select extends rex_select
{
/** @var bool */
private $ignoreOfflines;
/** @var null|int */
private $clang;
/** @var bool */
private $checkPerms;
/** @var bool */
private $addHomepage;
/** @var int|int[]|null */
private $rootId;
/** @var bool */
private $loaded;
public function __construct($ignoreOfflines = false, $clang = false, $checkPerms = true, $addHomepage = true)
{
$this->ignoreOfflines = $ignoreOfflines;
$this->clang = false === $clang ? null : $clang;
$this->checkPerms = $checkPerms;
$this->addHomepage = $addHomepage;
$this->rootId = null;
$this->loaded = false;
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
* @return void
*/
public function setRootId($rootId)
{
$this->rootId = $rootId;
}
/**
* @return void
*/
protected function addCatOptions()
{
if ($this->addHomepage) {
$this->addOption('Homepage', 0);
}
if (null !== $this->rootId) {
if (is_array($this->rootId)) {
foreach ($this->rootId as $rootId) {
if ($rootCat = rex_category::get($rootId, $this->clang)) {
$this->addCatOption($rootCat, 0);
}
}
} else {
if ($rootCat = rex_category::get($this->rootId, $this->clang)) {
$this->addCatOption($rootCat, 0);
}
}
} else {
if (!$this->checkPerms || rex::getUser()->getComplexPerm('structure')->hasCategoryPerm(0)) {
if ($rootCats = rex_category::getRootCategories($this->ignoreOfflines, $this->clang)) {
foreach ($rootCats as $rootCat) {
$this->addCatOption($rootCat);
}
}
} elseif (rex::getUser()->getComplexPerm('structure')->hasMountpoints()) {
$mountpoints = rex::getUser()->getComplexPerm('structure')->getMountpointCategories();
foreach ($mountpoints as $cat) {
if (!$this->ignoreOfflines || $cat->isOnline()) {
$this->addCatOption($cat, 0);
}
}
}
}
}
/**
* @return void
*/
protected function addCatOption(rex_category $cat, $group = null)
{
if (!$this->checkPerms || rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($cat->getId())
) {
$cid = $cat->getId();
$cname = $cat->getName() . ' [' . $cid . ']';
if (null === $group) {
$group = $cat->getParentId();
}
$this->addOption($cname, $cid, $cid, $group);
$childs = $cat->getChildren($this->ignoreOfflines);
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();
}
protected function outGroup($parentId, $level = 0)
{
if ($level > 100) {
// nur mal so zu sicherheit .. man weiss nie ;)
throw new rex_exception('select->_outGroup overflow');
}
$ausgabe = '';
$group = $this->getGroup($parentId);
if (!is_array($group)) {
return '';
}
foreach ($group as $option) {
$name = $option[0];
$value = $option[1];
$id = $option[2];
if (0 == $id || !$this->checkPerms || rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($option[2])) {
$ausgabe .= $this->outOption($name, $value, $level);
} elseif ($this->checkPerms && rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($option[2])) {
--$level;
}
$subgroup = $this->getGroup($id, true);
if (false !== $subgroup) {
$ausgabe .= $this->outGroup($id, $level + 1);
}
}
return $ausgabe;
}
}