XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
xoopstree.php
1<?php
13
14class XoopsTree
15{
16 public $table; //table with parent-child structure
17 public $id; //name of unique id for records in table $table
18 public $pid; // name of parent id used in table $table
19 public $order; //specifies the order of query results
20 public $title; // name of a field in table $table which will be used when selection box and paths are generated
21 public $db;
22
23 //constructor of class XoopsTree
24 //sets the names of table, unique id, and parend id
25 public function __construct($table_name, $id_name, $pid_name)
26 {
27 $this->db =& Database::getInstance();
28 $this->table = $table_name;
29 $this->id = $id_name;
30 $this->pid = $pid_name;
31 }
32 public function XoopsTree($table_name, $id_name, $pid_name)
33 {
34 return $this->__construct($table_name, $id_name, $pid_name);
35 }
36
37
38 // returns an array of first child objects for a given id($sel_id)
39 public function getFirstChild($sel_id, $order= '')
40 {
41 $arr = [];
42 $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
43 if ('' !== $order) {
44 $sql .= " ORDER BY $order";
45 }
46 $result = $this->db->query($sql);
47 $count = $this->db->getRowsNum($result);
48 if (0 == $count) {
49 return $arr;
50 }
51 while ($myrow=$this->db->fetchArray($result)) {
52 $arr[] = $myrow;
53 }
54 return $arr;
55 }
56
57 // returns an array of all FIRST child ids of a given id($sel_id)
58 public function getFirstChildId($sel_id)
59 {
60 $idarray = [];
61 $result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
62 $count = $this->db->getRowsNum($result);
63 if (0 == $count) {
64 return $idarray;
65 }
66 while (list($id) = $this->db->fetchRow($result)) {
67 $idarray[] = $id;
68 }
69 return $idarray;
70 }
71
72 //returns an array of ALL child ids for a given id($sel_id)
73 public function getAllChildId($sel_id, $order= '', $idarray = [])
74 {
75 $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
76 if ('' !== $order) {
77 $sql .= " ORDER BY $order";
78 }
79 $result=$this->db->query($sql);
80 $count = $this->db->getRowsNum($result);
81 if (0 == $count) {
82 return $idarray;
83 }
84 while (list($r_id) = $this->db->fetchRow($result)) {
85 $idarray[] = $r_id;
86 $idarray = $this->getAllChildId($r_id, $order, $idarray);
87 }
88 return $idarray;
89 }
90
91 //returns an array of ALL parent ids for a given id($sel_id)
92 public function getAllParentId($sel_id, $order= '', $idarray = [])
93 {
94 $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
95 if ('' !== $order) {
96 $sql .= " ORDER BY $order";
97 }
98 $result=$this->db->query($sql);
99 [$r_id] = $this->db->fetchRow($result);
100 if (0 == $r_id) {
101 return $idarray;
102 }
103 $idarray[] = $r_id;
104 $idarray = $this->getAllParentId($r_id, $order, $idarray);
105 return $idarray;
106 }
107
108 //generates path from the root id to a given id($sel_id)
109 // the path is delimetered with "/"
110 public function getPathFromId($sel_id, $title, $path= '')
111 {
112 $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
113 if (0 == $this->db->getRowsNum($result)) {
114 return $path;
115 }
116 [$parentid, $name] = $this->db->fetchRow($result);
118 $name = $myts->makeTboxData4Show($name);
119 $path = '/' . $name . $path . '';
120 if (0 == $parentid) {
121 return $path;
122 }
123 $path = $this->getPathFromId($parentid, $title, $path);
124 return $path;
125 }
126
127 //makes a nicely ordered selection box
128 //$preset_id is used to specify a preselected item
129 //set $none to 1 to add a option with value 0
130 public function makeMySelBox($title, $order= '', $preset_id=0, $none=0, $sel_name= '', $onchange= '')
131 {
132 if ('' == $sel_name) {
133 $sel_name = $this->id;
134 }
136 echo "<select name='".$sel_name."'";
137 if ('' !== $onchange) {
138 echo " onchange='".$onchange."'";
139 }
140 echo ">\n";
141 $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
142 if ('' !== $order) {
143 $sql .= " ORDER BY $order";
144 }
145 $result = $this->db->query($sql);
146 if ($none) {
147 echo "<option value='0'>----</option>\n";
148 }
149 while (list($catid, $name) = $this->db->fetchRow($result)) {
150 $sel = '';
151 if ($catid == $preset_id) {
152 $sel = " selected='selected'";
153 }
154 echo "<option value='$catid'$sel>$name</option>\n";
155 $sel = '';
156 $arr = $this->getChildTreeArray($catid, $order);
157 foreach ($arr as $option) {
158 $option['prefix'] = str_replace('.', '--', $option['prefix']);
159 $catpath = $option['prefix'] . '&nbsp;' . $myts->makeTboxData4Show($option[$title]);
160 if ($option[$this->id] == $preset_id) {
161 $sel = " selected='selected'";
162 }
163 echo "<option value='".$option[$this->id]."'$sel>$catpath</option>\n";
164 $sel = '';
165 }
166 }
167 echo "</select>\n";
168 }
169
170 //generates nicely formatted linked path from the root id to a given id
171 public function getNicePathFromId($sel_id, $title, $funcURL, $path= '')
172 {
173 $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
174 $result = $this->db->query($sql);
175 if (0 == $this->db->getRowsNum($result)) {
176 return $path;
177 }
178 list($parentid, $name) = $this->db->fetchRow($result);
180 $name = $myts->makeTboxData4Show($name);
181 $path = "<a href='".$funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>&nbsp;:&nbsp;' . $path . '';
182 if (0 == $parentid) {
183 return $path;
184 }
185 $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
186 return $path;
187 }
188
189 //generates id path from the root id to a given id
190 // the path is delimetered with "/"
191 public function getIdPathFromId($sel_id, $path= '')
192 {
193 $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
194 if (0 == $this->db->getRowsNum($result)) {
195 return $path;
196 }
197 [$parentid] = $this->db->fetchRow($result);
198 $path = '/' . $sel_id . $path . '';
199 if (0 == $parentid) {
200 return $path;
201 }
202 $path = $this->getIdPathFromId($parentid, $path);
203 return $path;
204 }
205
206 public function getAllChild($sel_id=0, $order= '', $parray = [])
207 {
208 $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
209 if ('' !== $order) {
210 $sql .= " ORDER BY $order";
211 }
212 $result = $this->db->query($sql);
213 $count = $this->db->getRowsNum($result);
214 if (0 == $count) {
215 return $parray;
216 }
217 while ($row = $this->db->fetchArray($result)) {
218 $parray[] = $row;
219 $parray=$this->getAllChild($row[$this->id], $order, $parray);
220 }
221 return $parray;
222 }
223
224 public function getChildTreeArray($sel_id=0, $order= '', $parray = [], $r_prefix= '')
225 {
226 $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
227 if ('' !== $order) {
228 $sql .= " ORDER BY $order";
229 }
230 $result = $this->db->query($sql);
231 $count = $this->db->getRowsNum($result);
232 if (0 == $count) {
233 return $parray;
234 }
235 while ($row = $this->db->fetchArray($result)) {
236 $row['prefix'] = $r_prefix . '.';
237 $parray[] = $row;
238 $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
239 }
240 return $parray;
241 }
242}