source: temp/test-xoops.ec-cube.net/html/class/xoopstree.php @ 405

Revision 405, 9.1 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: xoopstree.php,v 1.3 2005/08/03 12:39:11 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32class 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        $this->db =& Database::getInstance();
46        $this->table = $table_name;
47        $this->id = $id_name;
48        $this->pid = $pid_name;
49    }
50
51
52    // returns an array of first child objects for a given id($sel_id)
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    // returns an array of all FIRST child ids of a given id($sel_id)
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    //returns an array of ALL child ids for a given id($sel_id)
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    //returns an array of ALL parent ids for a given id($sel_id)
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    //generates path from the root id to a given id($sel_id)
123    // the path is delimetered with "/"
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    //makes a nicely ordered selection box
142    //$preset_id is used to specify a preselected item
143    //set $none to 1 to add a option with value 0
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']."&nbsp;".$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    //generates nicely formatted linked path from the root id to a given id
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."&amp;".$this->id."=".$sel_id."'>".$name."</a>&nbsp;:&nbsp;".$path."";
196        if ( $parentid == 0 ) {
197            return $path;
198        }
199        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
200        return $path;
201    }
202
203    //generates id path from the root id to a given id
204    // the path is delimetered with "/"
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?>
Note: See TracBrowser for help on using the repository browser.