1 <?php
  2 
  3 /**
  4  * @package redaxo\structure
  5  */
  6 class rex_category_select extends rex_select
  7 {
  8     private $ignore_offlines;
  9     private $clang;
 10     private $check_perms;
 11     private $add_homepage;
 12 
 13     /**
 14      * @var int
 15      */
 16     private $rootId;
 17 
 18     private $loaded;
 19 
 20     public function __construct($ignore_offlines = false, $clang = false, $check_perms = true, $add_homepage = true)
 21     {
 22         $this->ignore_offlines = $ignore_offlines;
 23         $this->clang = $clang;
 24         $this->check_perms = $check_perms;
 25         $this->add_homepage = $add_homepage;
 26         $this->rootId = null;
 27         $this->loaded = false;
 28 
 29         parent::__construct();
 30     }
 31 
 32     /**
 33      * Kategorie-Id oder ein Array von Kategorie-Ids als Wurzelelemente der Select-Box.
 34      *
 35      * @param mixed $rootId Kategorie-Id oder Array von Kategorie-Ids zur Identifikation der Wurzelelemente
 36      */
 37     public function setRootId($rootId)
 38     {
 39         $this->rootId = $rootId;
 40     }
 41 
 42     protected function addCatOptions()
 43     {
 44         if ($this->add_homepage) {
 45             $this->addOption('Homepage', 0);
 46         }
 47 
 48         if ($this->rootId !== null) {
 49             if (is_array($this->rootId)) {
 50                 foreach ($this->rootId as $rootId) {
 51                     if ($rootCat = rex_category::get($rootId, $this->clang)) {
 52                         $this->addCatOption($rootCat, 0);
 53                     }
 54                 }
 55             } else {
 56                 if ($rootCat = rex_category::get($this->rootId, $this->clang)) {
 57                     $this->addCatOption($rootCat, 0);
 58                 }
 59             }
 60         } else {
 61             if (!$this->check_perms || rex::getUser()->getComplexPerm('structure')->hasCategoryPerm(0)) {
 62                 if ($rootCats = rex_category::getRootCategories($this->ignore_offlines, $this->clang)) {
 63                     foreach ($rootCats as $rootCat) {
 64                         $this->addCatOption($rootCat);
 65                     }
 66                 }
 67             } elseif (rex::getUser()->getComplexPerm('structure')->hasMountpoints()) {
 68                 $mountpoints = rex::getUser()->getComplexPerm('structure')->getMountpoints();
 69                 foreach ($mountpoints as $id) {
 70                     $cat = rex_category::get($id, $this->clang);
 71                     if ($cat && !rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($cat->getParentId())) {
 72                         $this->addCatOption($cat, 0);
 73                     }
 74                 }
 75             }
 76         }
 77     }
 78 
 79     protected function addCatOption(rex_category $cat, $group = null)
 80     {
 81         if (!$this->check_perms ||
 82                 $this->check_perms && rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($cat->getId(), false)
 83         ) {
 84             $cid = $cat->getId();
 85             $cname = $cat->getName() . ' [' . $cid . ']';
 86 
 87             if ($group === null) {
 88                 $group = $cat->getParentId();
 89             }
 90 
 91             $this->addOption($cname, $cid, $cid, $group);
 92             $childs = $cat->getChildren($this->ignore_offlines, $this->clang);
 93             if (is_array($childs)) {
 94                 foreach ($childs as $child) {
 95                     $this->addCatOption($child);
 96                 }
 97             }
 98         }
 99     }
100 
101     public function get()
102     {
103         if (!$this->loaded) {
104             $this->addCatOptions();
105             $this->loaded = true;
106         }
107 
108         return parent::get();
109     }
110 
111     protected function outGroup($parent_id, $level = 0)
112     {
113         if ($level > 100) {
114             // nur mal so zu sicherheit .. man weiss nie ;)
115             throw new rex_exception('select->_outGroup overflow');
116         }
117 
118         $ausgabe = '';
119         $group = $this->getGroup($parent_id);
120         if (!is_array($group)) {
121             return '';
122         }
123         foreach ($group as $option) {
124             $name = $option[0];
125             $value = $option[1];
126             $id = $option[2];
127             if ($id == 0 || !$this->check_perms || ($this->check_perms && rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($option[2]))) {
128                 $ausgabe .= $this->outOption($name, $value, $level);
129             } elseif (($this->check_perms && rex::getUser()->getComplexPerm('structure')->hasCategoryPerm($option[2]))) {
130                 --$level;
131             }
132 
133             $subgroup = $this->getGroup($id, true);
134             if ($subgroup !== false) {
135                 $ausgabe .= $this->outGroup($id, $level + 1);
136             }
137         }
138         return $ausgabe;
139     }
140 }
141