Location: PHPKode > scripts > Tree Node > tree-node/Tree.php
<?php

/**
 * @author M Zaenal M
 * @email hide@address.com
 * @copyright 2008
 * @date 3-18-2008 16:32
 */
@set_time_limit(0);
require_once 'Core.php';
require_once 'TreeItem.php';

class Tree extends Tree_Core{
	/**
	 * @param const
	 */
	const DEFAULT_BEHAVIOR = 'classic';
	/**
	 * @param  WebFXTreeItem $Root Object on this class, default value is 0
	 */
	private $Root = null;
	
	/**
	 * @param WebFXTreeItem $Item Object on WebFXTreeItem Sub, default value is array
	 */
	private $Item = array();

	/**
	 * @param string $sCript to handle string script;
	 */
	 
	private $sCript = '';
	/**
	 * @param string $countTree ;
	 */
	 
	private $countTree = 0;
	
	/**
	 * @param array $_config to handle string script;
	 */
	public $_config = array(
		'rootIcon'        => 'empty.png',
		'openRootIcon'    => 'empty.png',
		'folderIcon'      => 'empty.png',
		'openFolderIcon'  => 'empty.png',
		'fileIcon'        => 'empty.png',
		'iIcon'           => 'I.png',
		'lIcon'           => 'L.png',
		'lMinusIcon'      => 'Lminus.png',
		'lPlusIcon'       => 'Lplus.png',
		'tIcon'           => 'T.png',
		'tMinusIcon'      => 'Tminus.png',
		'tPlusIcon'       => 'Tplus.png',
		'blankIcon'       => 'blank.png',
		'defaultText'     => 'Tree Item',
		'defaultAction'   => 'javascript:void(0);',
		'defaultTarget'   => '_self',
		'defaultBehavior' => 'classic'
	);	
  		
	/** 
	 * @see __constructor
	 * @param string $root name of root
	 * @param string $url link direction or action onclicked to url, default value is null
	 * @param string $behavior behavior classic or etc, default value is string 'classic'
	 * @param string $icon image of icon, default value is null
	 * @param string $openicon image of icon when clicked, default value is null
	 * @param array $_option option to set a default script, see TreeCore class, default value is null
	 * @void
	 * @access public
	 */
	public function __construct($root , $url = null, $icon = null, $openicon = null, 
		$_option = null, $behavior = self::DEFAULT_BEHAVIOR){
		parent::__construct($_option);
		
		if( null !== $root 		){$this->setRoot($root);$this->setName($root);				}
		if( null !== $url  		){$this->setUrl($url);										}
		if( null !== $icon 		){$this->setIcon($icon);									}
		if( null !== $openicon 	){$this->setOpenIcon($openicon);							}
		if( null !== $behavior 	){$this->setBehavior($behavior);							}
		
	}
	/**
	 * @method createBodyScript create script into body
	 * @access public
	 */				
	public function createBodyScript(){
		if( "" == $this->getName() ){
			echo "<pre>" 
			. print_r($this,1) . "</code>";
			throw new Exception("Name have not been set, please set first! ");
		}
	
		$name 	= Tree_Core::trim($this->getName());
		$name 	= 'tree_' . $name ;
		if($this->isLoadedScript() == false && $this->_option['headerScript'] == ""){
			throw new Exception("Cannot load Head Script!");
		}
		
		$s = '';
		if($this->getUrl() != ""){
			$s	= ",'" . $this->getUrl() . "'";
		}
		
		//$this->sCript 	.= "<h3 class=\"package-title\">" . $this->getName() . "</h3>\n";
		$this->sCript	.= '<div class="tree">';
		$this->sCript  	.= "<script language=\"Javascript\">\n";
		$this->sCript	.= "\tif(document.getElementById) {\n";
		// config here
		$this->sCript	.= "\t webFXTreeConfig.rootIcon      = '" . $this->getPath() 
						. $this->_config['rootIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.openRootIcon  = '" . $this->getPath() 
						. $this->_config['openRootIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.folderIcon    = '" . $this->getPath() 
						. $this->_config['folderIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.openFolderIcon= '" . $this->getPath() 
						. $this->_config['openFolderIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.fileIcon      = '" . $this->getPath() 
						. $this->_config['fileIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.iIcon         = '" . $this->getPath() 
						. $this->_config['iIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.lIcon         = '" . $this->getPath() 
						. $this->_config['lIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.lMinusIcon    = '" . $this->getPath() 
						. $this->_config['lMinusIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.lPlusIcon     = '" . $this->getPath() 
						. $this->_config['lPlusIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.tIcon         = '" . $this->getPath() 
						. $this->_config['tIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.tMinusIcon    = '" . $this->getPath() 
						. $this->_config['tMinusIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.tPlusIcon     = '" . $this->getPath() 
						. $this->_config['tPlusIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.blankIcon     = '" . $this->getPath() 
						. $this->_config['blankIcon'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.defaultText   = '" 
						. $this->_config['defaultText'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.defaultAction = '" 
						. $this->_config['defaultAction'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.defaultTarget = '" 
						. $this->_config['defaultTarget'] . "';\n";
		$this->sCript	.= "\t webFXTreeConfig.defaultBehavior= '" 
						. $this->_config['defaultBehavior'] . "';\n";
		//end config
		$this->sCript	.= "\t\tvar $name = new WebFXTree('<span class=\"package\">" 
				. $this->getName() . "</span>' $s);\n";
		$this->sCript	.= "\t\t$name.setBehavior('" . $this->getBehavior() . "');\n";
		$this->sCript	.= "\t\t$name.openIcon = '" . $this->getOpenIcon() . "';\n";
		$this->sCript	.= "\t\t$name.icon = '" . $this->getIcon() . "';\n";
		$this->countTree++;
		if($this->getCountItem() > 0){
			for($i = 0; $i< $this->getCountItem(); $i++){
				$nameItem = $name . "_" . Tree_Core::trim($this->Item[$i]['name']);
				$this->sCript	.= "\t\tvar $nameItem = new WebFXTreeItem('<span class=\"sub-package\">" 
				. $this->Item[$i]['name'] . "</span>', '" . $this->Item[$i]['url'] . "');\n";
				$this->sCript	.= "\t\t$nameItem.openIcon = '" . $this->Item[$i]['openicon'] . "';\n";
				$this->sCript	.= "\t\t$nameItem.icon = '" . $this->Item[$i]['icon'] . "';\n";			
				$this->roleItem($this->Item[$i], $nameItem);
				$this->sCript .= "\t\t$name.add($nameItem);\n";
			}
			$this->countTree++;		
		}
		$this->sCript 	.= "\tdocument.write($name);\n";
		$this->sCript	.="}\n</script>\n</div>";
		$this->_option['bodyScript'] = $this->sCript;
		return $this;
	}

	/**
	 * @method roleItem get child of TreeItem $ob and set into $this->sCript
	 * @param TreeItem $ob
	 * @param string $nameItem
	 * @access public
	 */				
	public function &roleItem($item, $nameItem){
		//$this->sCript .= '<pre>'.print_r($item,1)."</pre>\n";
		if(is_array($item['item'])){		
			for($i = 0; $i< count($item['item']); $i++){
				$nameItems = $nameItem . '_' . Tree_Core::trim($item['item'][$i]['name']);
				$this->sCript	.= "\t\tvar $nameItems = new WebFXTreeItem('" 
				. $item['item'][$i]['name'] . "', '" . $item['item'][$i]['url'] . "');\n";
				$this->sCript	.= "\t\t$nameItems.openIcon 	= '" 
				. $item['item'][$i]['openicon'] . "';\n";
				$this->sCript	.= "\t\t$nameItems.icon 		= '" 
				. $item['item'][$i]['icon'] . "';\n";	
				$this->roleItem($item['item'][$i], $nameItems);
				$this->sCript .= "$nameItem.add($nameItems);\n";
			}//end for			  
		}else{
			$nameItems = $nameItem . '_' . Tree_Core::trim($item['name']);
			$this->sCript	.= "\t\tvar $nameItems = new WebFXTreeItem('" 
			. $item['name'] . "', '" . $item['url'] . "');\n";
			$this->sCript	.= "\t\t$nameItems.openIcon 	= '" 
			. $item['openicon'] . "';\n";
			$this->sCript	.= "\t\t$nameItems.icon 		= '" 
			. $item['icon'] . "';\n";
			$this->sCript .= "$nameItem.add($nameItems);\n";			
		}
		$this->countTree++;
	} 
	/**
	 * @method setName set an name
	 * @return string $name
	 * @access public
	 */					
	public function setName($name){
		$this->Root['name'] = $name;
		return $this;
	}
	/**
	 * @method getName get an name
	 * @return string
	 * @access public
	 */				
	public function getName(){
		return $this->Root['name'];
	}
	/**
	 * @method setUrl set an url name
	 * @param string $url
	 * @access public
	 */				
	public function setUrl($url){
		$this->Root['url'] = $url;
		return $this;
	}
	/**
	 * @method getUrl get an url name
	 * @return string
	 * @access public
	 */				
	public function getUrl(){
		return $this->Root['url'];
	}
	/**
	 * @method setBehavior set an behavior name
	 * @param string $bev , default value is string 'classic'
	 * @access public
	 */					
	public function setBehavior($bev = 'classic'){
		$this->Root['behavior'] = $bev;
		return $this;
	}
	/**
	 * @method getBehavior get an behavior name
	 * @return string
	 * @access public
	 */					
	public function getBehavior(){
		return $this->Root['behavior'];
	}
	/**
	 * @method void setOpenIcon set an icon name, when clicked
	 * @param string $openicon
	 * @access public
	 */				
	public function setOpenIcon($openicon){
		$this->Root['openicon'] =  $openicon;
		return $this;
	}
	/**
	 * @method getOpenIcon get an icon name, when clicked
	 * @return string
	 * @access public
	 */					
	public function getOpenIcon(){
		return $this->getPath() . $this->Root['openicon'];
	}
	/**
	 * @method void setIcon set an icon name
	 * @param string $icon
	 * @access public
	 */				
	public function setIcon($icon){
		$this->Root['icon'] =  $icon;
		return $this;
	}
	/**
	 * @method getIcon get an icon name
	 * @return string
	 * @access public
	 */				
	public function getIcon(){
		return $this->getPath() . $this->Root['icon'];
	}
	/**
	 * @method add Add any object to child of root
	 * @param TreeItem $item
	 * @access public
	 */			
	public function &addItem(TreeItem $item){
		$itemnow = $this->getCountItem();
		$this->Item[$itemnow]['name'] 		= $item->getName();
		$this->Item[$itemnow]['icon'] 		= $item->getIcon();
		$this->Item[$itemnow]['openicon'] 	= $item->getOpenIcon();
		$this->Item[$itemnow]['url'] 		= $item->getUrl();
		$this->Item[$itemnow]['item'] 		= $item->getItem();
		$this->Item[$itemnow]['parentItem'] = $item->getRoot();
	}
	/**
	 * @method getCountItem get acount or number of $Item
	 * @return integer
	 * @access public
	 */		
	public function getCountItem(){
		return count($this->Item);
	}
	/**
	 * @method getPath get path of directory media
	 * @return string $this->_option['media']
	 * @access public
	 */		
	public function getPath(){
		return $this->_option['media'];
	}
	/**
	 * @method void remove remove element of $element in array $Item[$element] if exists
	 * @param integer|string $element
	 * @access public
	 */	
	public function removeElement($element){
		
		if(array_key_exists($element)){
			$Item = array();
			foreach($this->Item as $k => $v){
				if($k != $element){
					$Item[$k] = $v;
				}
			}	
			$this->Item = array();
			$this->Item = $Item;
			reset($this->Item);
		}
	}
	/**
	 * @method void removeAll remove any object or array of $Item and Root
	 * @access public
	 */
	public function removeAll(){
		$this->Item = array();
		$this->Root = array();
		$this->countTree = 0;
	}
	/**
	 * @method toHTML get string Code and print to browser HTML
	 * @return string
	 * @access public
	 */				
	public function toHTML(){
		echo "$this->sCript";
	}
	/**
	 * @method toString get string Code
	 * @return string
	 * @access public
	 */					
	public function toString(){
		return $this->sCript;
	} 
	/**
	 * @method countTree get countTree of tree
	 * @return string
	 * @access public
	 */					
	public function countTree(){
		return $this->countTree;
	} 	
	/**
	 * @method getRoot get a $Root
	 * @return array 
	 * @access public
	 */		
	public function getRoot(){
		return $this->Root;
	}					
}


?>
Return current item: Tree Node