home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopstree.php < prev    next >
Encoding:
PHP Script  |  2008-01-14  |  9.6 KB  |  267 lines

  1. <?php
  2. // $Id: xoopstree.php 1252 2008-01-13 17:48:35Z dugris $
  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.  
  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.         $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.         $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);
  136.         $myts =& MyTextSanitizer::getInstance();
  137.         $name = $myts->makeTboxData4Show($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.         }
  154.         $myts =& MyTextSanitizer::getInstance();
  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']." ".$myts->makeTboxData4Show($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) ? " : " . $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);
  200.         $myts =& MyTextSanitizer::getInstance();
  201.         $name = $myts->makeTboxData4Show($name);
  202.         $path = "<a href='".$funcURL."&".$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.  
  228.     function getAllChild($sel_id=0,$order="",$parray = array())
  229.     {
  230.         $sel_id = intval($sel_id);
  231.         $sql = "SELECT * FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."";
  232.         if ( $order != "" ) {
  233.             $sql .= " ORDER BY $order";
  234.         }
  235.         $result = $this->db->query($sql);
  236.         $count = $this->db->getRowsNum($result);
  237.         if ( $count == 0 ) {
  238.             return $parray;
  239.         }
  240.         while ( $row = $this->db->fetchArray($result) ) {
  241.             array_push($parray, $row);
  242.             $parray=$this->getAllChild($row[$this->id],$order,$parray);
  243.         }
  244.         return $parray;
  245.     }
  246.  
  247.     function getChildTreeArray($sel_id=0,$order="",$parray = array(),$r_prefix="")
  248.     {
  249.         $sel_id = intval($sel_id);
  250.         $sql = "SELECT * FROM ".$this->table." WHERE ".$this->pid."=".$sel_id."";
  251.         if ( $order != "" ) {
  252.             $sql .= " ORDER BY $order";
  253.         }
  254.         $result = $this->db->query($sql);
  255.         $count = $this->db->getRowsNum($result);
  256.         if ( $count == 0 ) {
  257.             return $parray;
  258.         }
  259.         while ( $row = $this->db->fetchArray($result) ) {
  260.             $row['prefix'] = $r_prefix.".";
  261.             array_push($parray, $row);
  262.             $parray = $this->getChildTreeArray($row[$this->id],$order,$parray,$row['prefix']);
  263.         }
  264.         return $parray;
  265.     }
  266. }
  267. ?>