XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
tree.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
36  var $_myId;
37  var $_rootId = null;
38  var $_tree = array();
39  var $_objects;
40 
49  function XoopsObjectTree( &$objectArr, $myId, $parentId, $rootId = null ) {
50  $this->_objects = &$objectArr;
51  $this->_myId = $myId;
52  $this->_parentId = $parentId;
53  if ( isset( $rootId ) ) {
54  $this->_rootId = $rootId;
55  }
56  $this->_initialize();
57  }
58 
64  function _initialize() {
65  foreach ( array_keys( $this->_objects ) as $i ) {
66  $key1 = $this->_objects[$i]->getVar( $this->_myId );
67  $this->_tree[$key1]['obj'] = &$this->_objects[$i];
68  $key2 = $this->_objects[$i]->getVar( $this->_parentId );
69  $this->_tree[$key1]['parent'] = $key2;
70  $this->_tree[$key2]['child'][] = $key1;
71  if ( isset( $this->_rootId ) ) {
72  $this->_tree[$key1]['root'] = $this->_objects[$i]->getVar( $this->_rootId );
73  }
74  }
75  }
76 
82  function &getTree() {
83  return $this->_tree;
84  }
85 
92  function &getByKey( $key ) {
93  return $this->_tree[$key]['obj'];
94  }
95 
102  function getFirstChild( $key ) {
103  $ret = array();
104  if ( isset( $this->_tree[$key]['child'] ) ) {
105  foreach ( $this->_tree[$key]['child'] as $childkey ) {
106  $ret[$childkey] = &$this->_tree[$childkey]['obj'];
107  }
108  }
109  return $ret;
110  }
111 
119  function getAllChild( $key, $ret = array() ) {
120  if ( isset( $this->_tree[$key]['child'] ) ) {
121  foreach ( $this->_tree[$key]['child'] as $childkey ) {
122  $ret[$childkey] = &$this->_tree[$childkey]['obj'];
123  $children = &$this->getAllChild( $childkey, $ret );
124  foreach ( array_keys( $children ) as $newkey ) {
125  $ret[$newkey] = &$children[$newkey];
126  }
127  }
128  }
129  return $ret;
130  }
131 
141  function getAllParent( $key, $ret = array(), $uplevel = 1 ) {
142  if ( isset( $this->_tree[$key]['parent'] ) && isset( $this->_tree[$this->_tree[$key]['parent']]['obj'] ) ) {
143  $ret[$uplevel] = &$this->_tree[$this->_tree[$key]['parent']]['obj'];
144  $parents = &$this->getAllParent( $this->_tree[$key]['parent'], $ret, $uplevel + 1 );
145  foreach ( array_keys( $parents ) as $newkey ) {
146  $ret[$newkey] = &$parents[$newkey];
147  }
148  }
149  return $ret;
150  }
151 
165  function _makeSelBoxOptions( $fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '' ) {
166  if ( $key > 0 ) {
167  $value = $this->_tree[$key]['obj']->getVar( $this->_myId );
168  $ret .= '<option value="' . $value . '"';
169  if ( $value == $selected ) {
170  $ret .= ' selected="selected"';
171  }
172  $ret .= '>' . $prefix_curr . $this->_tree[$key]['obj']->getVar( $fieldName ) . '</option>';
173  $prefix_curr .= $prefix_orig;
174  }
175  if ( isset( $this->_tree[$key]['child'] ) && !empty( $this->_tree[$key]['child'] ) ) {
176  foreach ( $this->_tree[$key]['child'] as $childkey ) {
177  $this->_makeSelBoxOptions( $fieldName, $selected, $childkey, $ret, $prefix_orig, $prefix_curr );
178  }
179  }
180  }
181 
194  function makeSelBox( $name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = false, $key = 0, $extra = '' ) {
195  $ret = '<select name="' . $name . '" id="' . $name . '" ' . $extra . '>';
196  if ( false != $addEmptyOption ) {
197  $ret .= '<option value="0"></option>';
198  }
199  $this->_makeSelBoxOptions( $fieldName, $selected, $key, $ret, $prefix );
200  return $ret . '</select>';
201  }
202 }
203 
204 ?>