XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
tree.php
1<?php
12
13
15{
16
20 public $_parentId;
21 public $_myId;
22 public $_rootId = null;
23 public $_tree = [];
24 public $_objects;
26
35 public function __construct(&$objectArr, $myId, $parentId, $rootId = null)
36 {
37 $this->_objects =& $objectArr;
38 $this->_myId = $myId;
39 $this->_parentId = $parentId;
40 if (isset($rootId)) {
41 $this->_rootId = $rootId;
42 }
43 $this->_initialize();
44 }
45
51 public function _initialize()
52 {
53 foreach (array_keys($this->_objects) as $i) {
54 $key1 = $this->_objects[$i]->getVar($this->_myId);
55 $this->_tree[$key1]['obj'] =& $this->_objects[$i];
56 $key2 = $this->_objects[$i]->getVar($this->_parentId);
57 $this->_tree[$key1]['parent'] = $key2;
58 $this->_tree[$key2]['child'][] = $key1;
59 if (isset($this->_rootId)) {
60 $this->_tree[$key1]['root'] = $this->_objects[$i]->getVar($this->_rootId);
61 }
62 }
63 }
64
70 public function &getTree()
71 {
72 return $this->_tree;
73 }
74
81 public function &getByKey($key)
82 {
83 return $this->_tree[$key]['obj'];
84 }
85
92 public function &getFirstChild($key)
93 {
94 $ret = [];
95 if (isset($this->_tree[$key]['child'])) {
96 foreach ($this->_tree[$key]['child'] as $childkey) {
97 $ret[$childkey] =& $this->_tree[$childkey]['obj'];
98 }
99 }
100 return $ret;
101 }
102
110 public function &getAllChild($key, $ret = [])
111 {
112 if (isset($this->_tree[$key]['child'])) {
113 foreach ($this->_tree[$key]['child'] as $childkey) {
114 $ret[$childkey] =& $this->_tree[$childkey]['obj'];
115 $children =& $this->getAllChild($childkey, $ret);
116 foreach (array_keys($children) as $newkey) {
117 $ret[$newkey] =& $children[$newkey];
118 }
119 }
120 }
121 return $ret;
122 }
123
133 public function &getAllParent($key, $ret = [], $uplevel = 1)
134 {
135 if (isset($this->_tree[$key]['parent'], $this->_tree[$this->_tree[$key]['parent']]['obj'])) {
136 $ret[$uplevel] =& $this->_tree[$this->_tree[$key]['parent']]['obj'];
137 $parents =& $this->getAllParent($this->_tree[$key]['parent'], $ret, $uplevel+1);
138 foreach (array_keys($parents) as $newkey) {
139 $ret[$newkey] =& $parents[$newkey];
140 }
141 }
142 return $ret;
143 }
144
159 public function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '')
160 {
161 if ($key > 0) {
162 $value = $this->_tree[$key]['obj']->getVar($this->_myId);
163 $ret .= '<option value="'.$value.'"';
164 if ($value == $selected) {
165 $ret .= ' selected="selected"';
166 }
167 $ret .= '>'.$prefix_curr.$this->_tree[$key]['obj']->getVar($fieldName).'</option>';
168 $prefix_curr .= $prefix_orig;
169 }
170 if (isset($this->_tree[$key]['child']) && !empty($this->_tree[$key]['child'])) {
171 foreach ($this->_tree[$key]['child'] as $childkey) {
172 $this->_makeSelBoxOptions($fieldName, $selected, $childkey, $ret, $prefix_orig, $prefix_curr);
173 }
174 }
175 }
176
189 public function &makeSelBox($name, $fieldName, $prefix='-', $selected='', $addEmptyOption = false, $key=0)
190 {
191 $ret = '<select name="'.$name.'" id="'.$name.'">';
192 if (false !== $addEmptyOption) {
193 $ret .= '<option value="0"></option>';
194 }
195 $this->_makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix);
196 $ret .= '</select>';
197 return $ret;
198 }
199}
& makeSelBox($name, $fieldName, $prefix='-', $selected='', $addEmptyOption=false, $key=0)
Definition tree.php:189
& getAllParent($key, $ret=[], $uplevel=1)
Definition tree.php:133
_makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr='')
Definition tree.php:159
& getAllChild($key, $ret=[])
Definition tree.php:110
__construct(&$objectArr, $myId, $parentId, $rootId=null)
Definition tree.php:35
& getFirstChild($key)
Definition tree.php:92
& getByKey($key)
Definition tree.php:81