tree.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 151 行

PHP
151
字号
<?php
/**
 * Article management
 *
 * @copyright	The XOOPS project http://www.xoops.org/
 * @license		http://www.fsf.org/copyleft/gpl.html GNU public license
 * @author		Taiwen Jiang (phppp or D.J.) <php_pp@hotmail.com>
 * @since		1.00
 * @version		$Id$
 * @package		module::article
 */
 
if (!defined("XOOPS_ROOT_PATH")) {
	exit();
}
require_once(XOOPS_ROOT_PATH."/class/tree.php");

if(!class_exists("artTree")){
class artTree extends XoopsObjectTree {

	function artTree(&$objectArr, $rootId = null)
	{
		$this->XoopsObjectTree($objectArr, "cat_id", "cat_pid", $rootId);
	}

	/**
	 * Make options for a select box from
	 *
	 * @param   string  $fieldName   Name of the member variable from the
     *  node objects that should be used as the title for the options.
	 * @param   string  $selected    Value to display as selected
	 * @param   int $key         ID of the object to display as the root of select options
     * @param   string  $ret         (reference to a string when called from outside) Result from previous recursions
	 * @param   string  $prefix_orig  String to indent items at deeper levels
	 * @param   string  $prefix_curr  String to indent the current item
	 * @return
     *
     * @access	private
	 **/
	function _makeTreeItems($key, &$ret, $prefix_orig, $prefix_curr = '', $tags=null)
	{
        if ($key > 0) {
	        if(count($tags)>0) foreach($tags as $tag){
	        	$ret[$key][$tag] = $this->_tree[$key]['obj']->getVar($tag);
	        }else{
	        	$ret[$key]["cat_title"] = $this->_tree[$key]['obj']->getVar("cat_title");
	        }
	        $ret[$key]["prefix"] = $prefix_curr;
            $prefix_curr .= $prefix_orig;
        }
        if (isset($this->_tree[$key]['child']) && !empty($this->_tree[$key]['child'])) {
            foreach ($this->_tree[$key]['child'] as $childkey) {
                $this->_makeTreeItems($childkey, $ret, $prefix_orig, $prefix_curr, $tags);
            }
        }
	}

	/**
	 * Make a select box with options from the tree
	 *
	 * @param   string  $name            Name of the select box
	 * @param   string  $fieldName       Name of the member variable from the
     *  node objects that should be used as the title for the options.
	 * @param   string  $prefix          String to indent deeper levels
	 * @param   string  $selected        Value to display as selected
	 * @param   bool    $addEmptyOption  Set TRUE to add an empty option with value "0" at the top of the hierarchy
	 * @param   integer $key             ID of the object to display as the root of select options
	 * @return  string  HTML select box
	 **/
	function &makeTree($prefix='-', $key=0, $tags=null)
    {
	    //art_message($prefix);
        $ret = array();
        $this->_makeTreeItems($key, $ret, $prefix, '', $tags);
        return $ret;
    }

	/**
	 * Make a select box with options from the tree
	 *
	 * @param   string  $name            Name of the select box
	 * @param   string  $fieldName       Name of the member variable from the
     *  node objects that should be used as the title for the options.
	 * @param   string  $prefix          String to indent deeper levels
	 * @param   string  $selected        Value to display as selected
	 * @param   bool    $addEmptyOption  Set TRUE to add an empty option with value "0" at the top of the hierarchy
	 * @param   integer $key             ID of the object to display as the root of select options
	 * @return  string  HTML select box
	 **/
	function &makeSelBox($name, $prefix='-', $selected='', $EmptyOption = false, $key=0)
    {
        $ret = '<select name='.$name.'>';
        if (!empty($addEmptyOption)) {
            $ret .= '<option value="0">'.(is_string($EmptyOption)?$EmptyOption:'').'</option>';
        }
        $this->_makeSelBoxOptions("cat_title", $selected, $key, $ret, $prefix);
        $ret .= '</select>';
        return $ret;
    }
    
    
	/**
	 * Make a tree for the array of a given category
	 * 
	 * @param   string  $key	top key of the tree
	 * @param   array	$ret	the tree
	 * @param   array	$tags   fields to be used
	 * @param   integer	$depth	level of subcategories
	 * @return  array  	
	 **/
	function getAllChild_array($key, &$ret, $tags = array(), $depth = 0)
	{
		if(-- $depth == 0) {
			return;
		}
		
		if (isset($this->_tree[$key]['child'])) {
			foreach ($this->_tree[$key]['child'] as $childkey) {
				if(isset($this->_tree[$childkey]['obj'])):
		        if(count($tags)>0){
			        foreach($tags as $tag) {
				        $ret['child'][$childkey][$tag] = $this->_tree[$childkey]['obj']->getVar($tag);
			        }
		        }else{
		        	$ret['child'][$childkey]["cat_title"] = $this->_tree[$childkey]['obj']->getVar("cat_title");
		        }
		        endif;
				
				$this->getAllChild_array($childkey, $ret['child'][$childkey], $tags, $depth);
			}
		}
	}

	/**
	 * Make a tree for the array
	 * 
	 * @param   string  $key	top key of the tree
	 * @param   array	$tags   fields to be used
	 * @param   integer	$depth	level of subcategories
	 * @return  array  	
	 **/
	function &makeArrayTree($key=0, $tags=null, $depth = 0)
    {
        $ret = array();
        if($depth > 0) $depth++;
        $this->getAllChild_array($key, $ret, $tags, $depth);
        return $ret;
    }
}
}
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?