1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: class XoopsTree
33: {
34: var $table;
35: var $id;
36: var $pid;
37: var $order;
38: var $title;
39: var $db;
40:
41:
42:
43: function XoopsTree($table_name, $id_name, $pid_name)
44: {
45: $this->db =& Database::getInstance();
46: $this->table = $table_name;
47: $this->id = $id_name;
48: $this->pid = $pid_name;
49: }
50:
51:
52:
53: function getFirstChild($sel_id, $order="")
54: {
55: $arr =array();
56: $sql = "SELECT * FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."";
57: if ( $order != "" ) {
58: $sql .= " ORDER BY $order";
59: }
60: $result = $this->db->query($sql);
61: $count = $this->db->getRowsNum($result);
62: if ( $count==0 ) {
63: return $arr;
64: }
65: while ( $myrow=$this->db->fetchArray($result) ) {
66: array_push($arr, $myrow);
67: }
68: return $arr;
69: }
70:
71:
72: function getFirstChildId($sel_id)
73: {
74: $idarray =array();
75: $result = $this->db->query("SELECT ".$this->id." FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."");
76: $count = $this->db->getRowsNum($result);
77: if ( $count == 0 ) {
78: return $idarray;
79: }
80: while ( list($id) = $this->db->fetchRow($result) ) {
81: array_push($idarray, $id);
82: }
83: return $idarray;
84: }
85:
86:
87: function getAllChildId($sel_id, $order="", $idarray = array())
88: {
89: $sql = "SELECT ".$this->id." FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."";
90: if ( $order != "" ) {
91: $sql .= " ORDER BY $order";
92: }
93: $result=$this->db->query($sql);
94: $count = $this->db->getRowsNum($result);
95: if ( $count==0 ) {
96: return $idarray;
97: }
98: while ( list($r_id) = $this->db->fetchRow($result) ) {
99: array_push($idarray, $r_id);
100: $idarray = $this->getAllChildId($r_id,$order,$idarray);
101: }
102: return $idarray;
103: }
104:
105:
106: function getAllParentId($sel_id, $order="", $idarray = array())
107: {
108: $sql = "SELECT ".$this->pid." FROM ".$this->table." WHERE ".$this->id."=".$sel_id."";
109: if ( $order != "" ) {
110: $sql .= " ORDER BY $order";
111: }
112: $result=$this->db->query($sql);
113: list($r_id) = $this->db->fetchRow($result);
114: if ( $r_id == 0 ) {
115: return $idarray;
116: }
117: array_push($idarray, $r_id);
118: $idarray = $this->getAllParentId($r_id,$order,$idarray);
119: return $idarray;
120: }
121:
122:
123:
124: function getPathFromId($sel_id, $title, $path="")
125: {
126: $result = $this->db->query("SELECT ".$this->pid.", ".$title." FROM ".$this->table." WHERE ".$this->id."=$sel_id");
127: if ( $this->db->getRowsNum($result) == 0 ) {
128: return $path;
129: }
130: list($parentid,$name) = $this->db->fetchRow($result);
131: $myts =& MyTextSanitizer::getInstance();
132: $name = $myts->makeTboxData4Show($name);
133: $path = "/".$name.$path."";
134: if ( $parentid == 0 ) {
135: return $path;
136: }
137: $path = $this->getPathFromId($parentid, $title, $path);
138: return $path;
139: }
140:
141:
142:
143:
144: function makeMySelBox($title,$order="",$preset_id=0, $none=0, $sel_name="", $onchange="")
145: {
146: if ( $sel_name == "" ) {
147: $sel_name = $this->id;
148: }
149: $myts =& MyTextSanitizer::getInstance();
150: echo "<select name='".$sel_name."'";
151: if ( $onchange != "" ) {
152: echo " onchange='".$onchange."'";
153: }
154: echo ">\n";
155: $sql = "SELECT ".$this->id.", ".$title." FROM ".$this->table." WHERE ".$this->pid."=0";
156: if ( $order != "" ) {
157: $sql .= " ORDER BY $order";
158: }
159: $result = $this->db->query($sql);
160: if ( $none ) {
161: echo "<option value='0'>----</option>\n";
162: }
163: while ( list($catid, $name) = $this->db->fetchRow($result) ) {
164: $sel = "";
165: if ( $catid == $preset_id ) {
166: $sel = " selected='selected'";
167: }
168: echo "<option value='$catid'$sel>$name</option>\n";
169: $sel = "";
170: $arr = $this->getChildTreeArray($catid, $order);
171: foreach ( $arr as $option ) {
172: $option['prefix'] = str_replace(".","--",$option['prefix']);
173: $catpath = $option['prefix']." ".$myts->makeTboxData4Show($option[$title]);
174: if ( $option[$this->id] == $preset_id ) {
175: $sel = " selected='selected'";
176: }
177: echo "<option value='".$option[$this->id]."'$sel>$catpath</option>\n";
178: $sel = "";
179: }
180: }
181: echo "</select>\n";
182: }
183:
184:
185: function getNicePathFromId($sel_id, $title, $funcURL, $path="")
186: {
187: $sql = "SELECT ".$this->pid.", ".$title." FROM ".$this->table." WHERE ".$this->id."=$sel_id";
188: $result = $this->db->query($sql);
189: if ( $this->db->getRowsNum($result) == 0 ) {
190: return $path;
191: }
192: list($parentid,$name) = $this->db->fetchRow($result);
193: $myts =& MyTextSanitizer::getInstance();
194: $name = $myts->makeTboxData4Show($name);
195: $path = "<a href='".$funcURL."&".$this->id."=".$sel_id."'>".$name."</a> : ".$path."";
196: if ( $parentid == 0 ) {
197: return $path;
198: }
199: $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
200: return $path;
201: }
202:
203:
204:
205: function getIdPathFromId($sel_id, $path="")
206: {
207: $result = $this->db->query("SELECT ".$this->pid." FROM ".$this->table." WHERE ".$this->id."=$sel_id");
208: if ( $this->db->getRowsNum($result) == 0 ) {
209: return $path;
210: }
211: list($parentid) = $this->db->fetchRow($result);
212: $path = "/".$sel_id.$path."";
213: if ( $parentid == 0 ) {
214: return $path;
215: }
216: $path = $this->getIdPathFromId($parentid, $path);
217: return $path;
218: }
219:
220: function getAllChild($sel_id=0,$order="",$parray = array())
221: {
222: $sql = "SELECT * FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."";
223: if ( $order != "" ) {
224: $sql .= " ORDER BY $order";
225: }
226: $result = $this->db->query($sql);
227: $count = $this->db->getRowsNum($result);
228: if ( $count == 0 ) {
229: return $parray;
230: }
231: while ( $row = $this->db->fetchArray($result) ) {
232: array_push($parray, $row);
233: $parray=$this->getAllChild($row[$this->id],$order,$parray);
234: }
235: return $parray;
236: }
237:
238: function getChildTreeArray($sel_id=0,$order="",$parray = array(),$r_prefix="")
239: {
240: $sql = "SELECT * FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."";
241: if ( $order != "" ) {
242: $sql .= " ORDER BY $order";
243: }
244: $result = $this->db->query($sql);
245: $count = $this->db->getRowsNum($result);
246: if ( $count == 0 ) {
247: return $parray;
248: }
249: while ( $row = $this->db->fetchArray($result) ) {
250: $row['prefix'] = $r_prefix.".";
251: array_push($parray, $row);
252: $parray = $this->getChildTreeArray($row[$this->id],$order,$parray,$row['prefix']);
253: }
254: return $parray;
255: }
256: }
257: ?>