XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
xoopstree.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
32 class XoopsTree
33 {
34  var $table; //table with parent-child structure
35  var $id; //name of unique id for records in table $table
36  var $pid; // name of parent id used in table $table
37  var $order; //specifies the order of query results
38  var $title; // name of a field in table $table which will be used when selection box and paths are generated
39  var $db;
40 
41  //constructor of class XoopsTree
42  //sets the names of table, unique id, and parend id
43  function XoopsTree($table_name, $id_name, $pid_name)
44  {
45  $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php");
47  $this->table = $table_name;
48  $this->id = $id_name;
49  $this->pid = $pid_name;
50  }
51 
52  // returns an array of first child objects for a given id($sel_id)
53  function getFirstChild($sel_id, $order = "")
54  {
55  $sel_id = intval($sel_id);
56  $arr = array();
57  $sql = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
58  if ($order != "") {
59  $sql .= " ORDER BY $order";
60  }
61  $result = $this->db->query($sql);
62  $count = $this->db->getRowsNum($result);
63  if ($count == 0) {
64  return $arr;
65  }
66  while ($myrow = $this->db->fetchArray($result)) {
67  array_push($arr, $myrow);
68  }
69  return $arr;
70  }
71 
72  // returns an array of all FIRST child ids of a given id($sel_id)
73  function getFirstChildId($sel_id)
74  {
75  $sel_id = intval($sel_id);
76  $idarray = array();
77  $result = $this->db->query("SELECT " . $this->id . " FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "");
78  $count = $this->db->getRowsNum($result);
79  if ($count == 0) {
80  return $idarray;
81  }
82  while (list ($id) = $this->db->fetchRow($result)) {
83  array_push($idarray, $id);
84  }
85  return $idarray;
86  }
87 
88  //returns an array of ALL child ids for a given id($sel_id)
89  function getAllChildId($sel_id, $order = "", $idarray = array())
90  {
91  $sel_id = intval($sel_id);
92  $sql = "SELECT " . $this->id . " FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
93  if ($order != "") {
94  $sql .= " ORDER BY $order";
95  }
96  $result = $this->db->query($sql);
97  $count = $this->db->getRowsNum($result);
98  if ($count == 0) {
99  return $idarray;
100  }
101  while (list ($r_id) = $this->db->fetchRow($result)) {
102  array_push($idarray, $r_id);
103  $idarray = $this->getAllChildId($r_id, $order, $idarray);
104  }
105  return $idarray;
106  }
107 
108  //returns an array of ALL parent ids for a given id($sel_id)
109  function getAllParentId($sel_id, $order = "", $idarray = array())
110  {
111  $sel_id = intval($sel_id);
112  $sql = "SELECT " . $this->pid . " FROM " . $this->table . " WHERE " . $this->id . "=" . $sel_id . "";
113  if ($order != "") {
114  $sql .= " ORDER BY $order";
115  }
116  $result = $this->db->query($sql);
117  list ($r_id) = $this->db->fetchRow($result);
118  if ($r_id == 0) {
119  return $idarray;
120  }
121  array_push($idarray, $r_id);
122  $idarray = $this->getAllParentId($r_id, $order, $idarray);
123  return $idarray;
124  }
125 
126  //generates path from the root id to a given id($sel_id)
127  // the path is delimetered with "/"
128  function getPathFromId($sel_id, $title, $path = "")
129  {
130  $sel_id = intval($sel_id);
131  $result = $this->db->query("SELECT " . $this->pid . ", " . $title . " FROM " . $this->table . " WHERE " . $this->id . "=$sel_id");
132  if ($this->db->getRowsNum($result) == 0) {
133  return $path;
134  }
135  list ($parentid, $name) = $this->db->fetchRow($result);
137  $name = $myts->htmlspecialchars($name);
138  $path = "/" . $name . $path . "";
139  if ($parentid == 0) {
140  return $path;
141  }
142  $path = $this->getPathFromId($parentid, $title, $path);
143  return $path;
144  }
145 
146  //makes a nicely ordered selection box
147  //$preset_id is used to specify a preselected item
148  //set $none to 1 to add a option with value 0
149  function makeMySelBox($title, $order = "", $preset_id = 0, $none = 0, $sel_name = "", $onchange = "")
150  {
151  if ($sel_name == "") {
152  $sel_name = $this->id;
153  }
155  echo "<select name='" . $sel_name . "'";
156  if ($onchange != "") {
157  echo " onchange='" . $onchange . "'";
158  }
159  echo ">\n";
160  $sql = "SELECT " . $this->id . ", " . $title . " FROM " . $this->table . " WHERE " . $this->pid . "=0";
161  if ($order != "") {
162  $sql .= " ORDER BY $order";
163  }
164  $result = $this->db->query($sql);
165  if ($none) {
166  echo "<option value='0'>----</option>\n";
167  }
168  while (list ($catid, $name) = $this->db->fetchRow($result)) {
169  $sel = "";
170  if ($catid == $preset_id) {
171  $sel = " selected='selected'";
172  }
173  echo "<option value='$catid'$sel>$name</option>\n";
174  $sel = "";
175  $arr = $this->getChildTreeArray($catid, $order);
176  foreach($arr as $option) {
177  $option['prefix'] = str_replace(".", "--", $option['prefix']);
178  $catpath = $option['prefix'] . "&nbsp;" . $myts->htmlspecialchars($option[$title]);
179  if ($option[$this->id] == $preset_id) {
180  $sel = " selected='selected'";
181  }
182  echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
183  $sel = "";
184  }
185  }
186  echo "</select>\n";
187  }
188 
189  //generates nicely formatted linked path from the root id to a given id
190  function getNicePathFromId($sel_id, $title, $funcURL, $path = "")
191  {
192  $path = ! empty($path) ? "&nbsp;:&nbsp;" . $path : $path;
193  $sel_id = intval($sel_id);
194  $sql = "SELECT " . $this->pid . ", " . $title . " FROM " . $this->table . " WHERE " . $this->id . "=$sel_id";
195  $result = $this->db->query($sql);
196  if ($this->db->getRowsNum($result) == 0) {
197  return $path;
198  }
199  list ($parentid, $name) = $this->db->fetchRow($result);
201  $name = $myts->htmlspecialchars($name);
202  $path = "<a href='" . $funcURL . "&amp;" . $this->id . "=" . $sel_id . "'>" . $name . "</a>" . $path . "";
203  if ($parentid == 0) {
204  return $path;
205  }
206  $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
207  return $path;
208  }
209 
210  //generates id path from the root id to a given id
211  // the path is delimetered with "/"
212  function getIdPathFromId($sel_id, $path = "")
213  {
214  $sel_id = intval($sel_id);
215  $result = $this->db->query("SELECT " . $this->pid . " FROM " . $this->table . " WHERE " . $this->id . "=$sel_id");
216  if ($this->db->getRowsNum($result) == 0) {
217  return $path;
218  }
219  list ($parentid) = $this->db->fetchRow($result);
220  $path = "/" . $sel_id . $path . "";
221  if ($parentid == 0) {
222  return $path;
223  }
224  $path = $this->getIdPathFromId($parentid, $path);
225  return $path;
226  }
227 
236  function getAllChild($sel_id = 0, $order = "", $parray = array())
237  {
238  $sel_id = intval($sel_id);
239  $sql = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
240  if ($order != "") {
241  $sql .= " ORDER BY $order";
242  }
243  $result = $this->db->query($sql);
244  $count = $this->db->getRowsNum($result);
245  if ($count == 0) {
246  return $parray;
247  }
248  while ($row = $this->db->fetchArray($result)) {
249  array_push($parray, $row);
250  $parray = $this->getAllChild($row[$this->id], $order, $parray);
251  }
252  return $parray;
253  }
263  function getChildTreeArray($sel_id = 0, $order = "", $parray = array(), $r_prefix = "")
264  {
265  $sel_id = intval($sel_id);
266  $sql = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
267  if ($order != "") {
268  $sql .= " ORDER BY $order";
269  }
270  $result = $this->db->query($sql);
271  $count = $this->db->getRowsNum($result);
272  if ($count == 0) {
273  return $parray;
274  }
275  while ($row = $this->db->fetchArray($result)) {
276  $row['prefix'] = $r_prefix . ".";
277  array_push($parray, $row);
278  $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
279  }
280  return $parray;
281  }
282 }
283 ?>