XOOPS  2.6.0
tree.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
31 {
35  private $_parentId;
36 
40  private $_myId;
41 
45  private $_rootId = null;
46 
50  private $_tree = array();
51 
55  private $_objects;
56 
65  public function __construct(&$objectArr, $myId, $parentId, $rootId = null)
66  {
67  $this->_objects = $objectArr;
68  $this->_myId = $myId;
69  $this->_parentId = $parentId;
70  if (isset($rootId)) {
71  $this->_rootId = $rootId;
72  }
73  $this->_initialize();
74  }
75 
81  private function _initialize()
82  {
83  /* @var $object XoopsObject */
84  foreach ($this->_objects as $object) {
85  $key1 = $object->getVar($this->_myId);
86  $this->_tree[$key1]['obj'] = $object;
87  $key2 = $object->getVar($this->_parentId);
88  $this->_tree[$key1]['parent'] = $key2;
89  $this->_tree[$key2]['child'][] = $key1;
90  if (isset($this->_rootId)) {
91  $this->_tree[$key1]['root'] = $object->getVar($this->_rootId);
92  }
93  }
94  }
95 
101  public function getTree()
102  {
103  return $this->_tree;
104  }
105 
112  public function getByKey($key)
113  {
114  return $this->_tree[$key]['obj'];
115  }
116 
123  public function getFirstChild($key)
124  {
125  $ret = array();
126  if (isset($this->_tree[$key]['child'])) {
127  foreach ($this->_tree[$key]['child'] as $childkey) {
128  $ret[$childkey] = $this->_tree[$childkey]['obj'];
129  }
130  }
131  return $ret;
132  }
133 
141  public function getAllChild($key, $ret = array())
142  {
143  if (isset($this->_tree[$key]['child'])) {
144  foreach ($this->_tree[$key]['child'] as $childkey) {
145  $ret[$childkey] = $this->_tree[$childkey]['obj'];
146  $children = $this->getAllChild($childkey, $ret);
147  foreach (array_keys($children) as $newkey) {
148  $ret[$newkey] = $children[$newkey];
149  }
150  }
151  }
152  return $ret;
153  }
154 
164  public function getAllParent($key, $ret = array(), $uplevel = 1)
165  {
166  if (isset($this->_tree[$key]['parent']) && isset($this->_tree[$this->_tree[$key]['parent']]['obj'])) {
167  $ret[$uplevel] = $this->_tree[$this->_tree[$key]['parent']]['obj'];
168  $parents = $this->getAllParent($this->_tree[$key]['parent'], $ret, $uplevel + 1);
169  foreach (array_keys($parents) as $newkey) {
170  $ret[$newkey] = $parents[$newkey];
171  }
172  }
173  return $ret;
174  }
175 
189  private function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '')
190  {
191  if ($key > 0) {
192  /* @var $object XoopsObject */
193  $object = $this->_tree[$key]['obj'];
194  $value = $object->getVar($this->_myId);
195  $ret .= '<option value="' . $value . '"';
196  if ($value == $selected) {
197  $ret .= ' selected="selected"';
198  }
199  $ret .= '>' . $prefix_curr . $object->getVar($fieldName) . '</option>';
200  $prefix_curr .= $prefix_orig;
201  }
202  if (isset($this->_tree[$key]['child']) && !empty($this->_tree[$key]['child'])) {
203  foreach ($this->_tree[$key]['child'] as $childkey) {
204  $this->_makeSelBoxOptions($fieldName, $selected, $childkey, $ret, $prefix_orig, $prefix_curr);
205  }
206  }
207  }
208 
222  public function makeSelBox($name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = false, $key = 0, $extra = '')
223  {
224  $ret = '<select name="' . $name . '" id="' . $name . '" ' . $extra . '>';
225  if (false != $addEmptyOption) {
226  $ret .= '<option value="0"></option>';
227  }
228  $this->_makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix);
229  return $ret . '</select>';
230  }
231 
241  public function makeArrayTree( $fieldName, $prefix = '-', $key = 0) {
242  $ret = array();
243  $this->_makeArrayTreeOptions( $fieldName, $key, $ret, $prefix );
244  return $ret;
245  }
246 
258  public function _makeArrayTreeOptions( $fieldName, $key, &$ret, $prefix_orig, $prefix_curr = '' ) {
259  if ( $key > 0 ) {
260  $value = $this->_tree[$key]['obj']->getVar( $this->_myId );
261  $ret[$value] = $prefix_curr . $this->_tree[$key]['obj']->getVar( $fieldName );
262  $prefix_curr .= $prefix_orig;
263 
264  }
265  if ( isset( $this->_tree[$key]['child'] ) && !empty( $this->_tree[$key]['child'] ) ) {
266  foreach ( $this->_tree[$key]['child'] as $childkey ) {
267  $this->_makeArrayTreeOptions( $fieldName, $childkey, $ret, $prefix_orig, $prefix_curr );
268  }
269  }
270  }
271 }
_makeArrayTreeOptions($fieldName, $key, &$ret, $prefix_orig, $prefix_curr= '')
Definition: tree.php:258
getAllChild($key, $ret=array())
Definition: tree.php:141
makeArrayTree($fieldName, $prefix= '-', $key=0)
Definition: tree.php:241
getAllParent($key, $ret=array(), $uplevel=1)
Definition: tree.php:164
getByKey($key)
Definition: tree.php:112
__construct(&$objectArr, $myId, $parentId, $rootId=null)
Definition: tree.php:65
_makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr= '')
Definition: tree.php:189
makeSelBox($name, $fieldName, $prefix= '-', $selected= '', $addEmptyOption=false, $key=0, $extra= '')
Definition: tree.php:222
getFirstChild($key)
Definition: tree.php:123