Location: PHPKode > projects > OpenNitro > trunk/Nitro/Libraries/HierarchicalListing.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Libraries :: HierarchicalListing                                 |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This source file is copyrighted by June Systems BV, the Netherlands       |
// | If you would like to use this file in your projects, please contact       |
// | hide@address.com                                                     |
// +---------------------------------------------------------------------------+
// | Author: Oli Oskarsson <hide@address.com>                               |
// +---------------------------------------------------------------------------+
//
// $Id: HierarchicalListing.inc.php 229 2008-04-17 09:20:31Z oli $
//
// This file contains the List functions to create Listings
//

/**
 * This file contains the hierarchical listing class.
 *
 * This class can be used to create hierarchical lists that
 * are dynamically drawn in javascript.
 *
 * @package	Nitro
 * @subpackage	Libraries
 * @author 		Oli Oskarsson
 * @version 	$Revision: 1.12 $
 * @copyright	2004 June Systems BV
 */

/**
 * Include Nitro template support
 */
include_once "Nitro/Template.inc.php";

/**
 * The List class
 *
 * This class contains the Listing definition
 *
 * @package	Nitro
 * @subpackage	Libraries
 * @access	public
 */
class hierarchicalListing {
	var $ListID = "HierarchicalListing";
	
	var $Actions = array();
	var $defaultAction;
	
	var $Rows = array();
	var $RowID = 0;
	
	var $enableFilter = FALSE;
	
	var $realRowID = 1;
	
	var $ListActions;
	
	var $CompleteJScript;
	
	function hierarchicalListing($Title = "", $ListID = NULL)
	{
		global $__NSess;
		
		$this->Template = "file:".NITRO_PATH."Defaults/Templates/HierarchicalListing.tpl";
		$this->ListID = $ListID;
		$this->Title = $Title;
	}
	
	function setIndex($indexTitle = "", $indexWidth = 200)
	{
		$this->indexTitle = $indexTitle;
		$this->indexWidth = ((int)$indexWidth ? $indexWidth : 200);
	}
	
	function enableFilter($enableFilter = true)
	{
		$this->enableFilter = $enableFilter;		
	}

	function addRow($rowObject)
	{
		if(is_object($rowObject) && count($rowObject)){
			$this->Rows[$this->RowID] = $rowObject;
			$this->RowID++;
		}else{
			$rv = false;
		}
		return $rv;
	}
	
	function addColumn($ColumnID, $ColumnName, $ColumnWidth = 100)
	{
		if(isset($ColumnID)){
			$this->Columns[$ColumnID]['Name'] = $ColumnName;
			$this->Columns[$ColumnID]['Width'] = $ColumnWidth;
		}else{
			return false;
		}
		
		return true;
	}
	
	function addColumns($Columns = array())
	{
		if(is_array($Columns) && count($Columns)){
			foreach($Columns AS $ColumnID => $ColumnName){
				$this->AddColumn($ColumnID, $ColumnName);
			}
			$rv = true;
		}else{
			$rv = false;
		}
		return $rv;
	}
	
	function setColumnWidth($ColumnWidths = array(), $ColumnKey = NULL)
	{
		if(is_array($ColumnWidths) && count($ColumnWidths)){
			foreach($ColumnWidths AS $ColumnKey => $Width){
				$this->Columns[$ColumnKey]['Width'] = $Width;
			}
			$rv = true;
		}elseif (strlen($ColumnWidths) && $ColumnKey) {
			$this->Columns[$ColumnKey]['Width'] = $ColumnWidths;
		}else{
			$rv = false;
		}
		return $rv;
	}
	
	function addAction($ID, $Lable = NULL, $actionWidth = 50)
	{
		if (!isset($Lable)) $Lable = $ID;
		$ID = strtoupper($ID);
		$this->Actions[$ID]['Lable'] = $Lable;
		$this->Actions[$ID]['Width'] = $actionWidth;
	}
	
	function addActions($Actions = array())
	{
		if(is_array($Actions) && count($Actions)){
			foreach($Actions AS $ActionID => $ActionLable){
				if (is_int($ActionID)) $ActionID = $ActionLable;
				$this->AddAction($ActionID, $ActionLable);
			}
			$rv = true;
		}else{
			$rv = false;
		}
		return $rv;
	}
	
	function setActionWidth($ActionWidths = array(), $ActionKey = NULL)
	{
		$ActionKey = strtoupper($ActionKey);
		if(is_array($ActionWidths) && count($ActionWidths)){
			foreach($ActionWidths AS $ActionKey => $Width){
				$key = strtoupper($ActionKey);
				$this->Actions[$key]['Width'] = $Width;
			}
			$rv = true;
		}elseif (strlen($ActionWidths) && $ActionKey) {
			$this->Actions[$ActionKey]['Width'] = $ActionWidths;
		}else{
			$rv = false;
		}
		return $rv;
	}

	function addListAction($Type, $URL, $onClickAction = "")
	{
		$tmp["Type"] = $Type;
		$tmp["URL"] = $URL;
		$tmp["onClickAction"] = $onClickAction;
		$this->ListActions[] = $tmp;
	}
	
	function setDefaultAction($ActionID)
	{
		$this->defaultAction = strtoupper($ActionID);
	}
	
	function Draw()
	{
		global $__NSess;
		
		//$jsListingArray = "var Listing_".$this->ListID." = new Array();\n";
		$jsListingArray = "Listing_".$this->ListID." = new Array();\n"; //Do not use 'var' because that only changes the variable in a function scope when called from XML and eval'd in drawXML
		$jsListingArray.= "Listing_".$this->ListID."['listID'] = '".$this->ListID."';\n";
		
		$jsListingArray.= "Listing_".$this->ListID."['CompleteJScript'] = '".rawurlencode($this->CompleteJScript)."';\n";
		
		$jsListingArray.= "Listing_".$this->ListID."['sortSettings'] = new Array();\n";
		$jsListingArray.= "Listing_".$this->ListID."['sortSettings']['sortColumn'] = '".$__NSess->hierarchicalListing[$this->ListID]['sortSettings']['sortColumn']."';\n";
		$jsListingArray.= "Listing_".$this->ListID."['sortSettings']['sortDirection'] = '".$__NSess->hierarchicalListing[$this->ListID]['sortSettings']['sortDirection']."';\n";
		
		$jsListingArray.= "Listing_".$this->ListID."['filterSettings'] = new Array();\n";
		$jsListingArray.= "Listing_".$this->ListID."['enableFilter'] = ".(int)$this->enableFilter.";\n";
		$jsListingArray.= "Listing_".$this->ListID."['defaultAction'] = '".$this->defaultAction."';\n";
		$jsListingArray.= "Listing_".$this->ListID."['Title'] = '".addslashes($this->Title)."';\n";
		$jsListingArray.= "Listing_".$this->ListID."['indexWidth'] = '".$this->indexWidth."';\n";
		$jsListingArray.= "Listing_".$this->ListID."['indexTitle'] = '".addslashes($this->indexTitle)."';\n";
		$jsListingArray.= "Listing_".$this->ListID."['filterSettings']['indexTitle'] = '".addslashes($__NSess->hierarchicalListing[$this->ListID]['filterSettings']['indexTitle'])."';\n";
		$jsListingArray.= "Listing_".$this->ListID."['Columns'] = new Array();\n";
		foreach ($this->Columns AS $ColumnID => $ColumnInfo) {
			$jsListingArray.= "Listing_".$this->ListID."['Columns']['".$ColumnID."'] = new Array();\n";
			$jsListingArray.= "Listing_".$this->ListID."['Columns']['".$ColumnID."']['Name'] = '".addslashes($ColumnInfo["Name"])."';\n";
			$jsListingArray.= "Listing_".$this->ListID."['Columns']['".$ColumnID."']['Width'] = ".(int)$ColumnInfo["Width"].";\n";
			$jsListingArray.= "Listing_".$this->ListID."['filterSettings']['".$ColumnID."'] = '".addslashes($__NSess->hierarchicalListing[$this->ListID]['filterSettings'][$ColumnID])."';\n";
		}
		$jsListingArray.= "Listing_".$this->ListID."['Actions'] = new Array();\n";
		foreach ($this->Actions AS $ActionID => $ActionInfo) {
			$jsListingArray.= "Listing_".$this->ListID."['Actions']['".$ActionID."'] = new Array();\n";
			$jsListingArray.= "Listing_".$this->ListID."['Actions']['".$ActionID."']['Lable'] = '".addslashes($ActionInfo["Lable"])."';\n";
			$jsListingArray.= "Listing_".$this->ListID."['Actions']['".$ActionID."']['Width'] = ".(int)$ActionInfo["Width"].";\n";
		}
		
		$jsListingArray.= "R = new Array();\n";
		$jsListingArray.= "rS = new Array();\n";
		$parentsDone = array();
		foreach ($this->Rows AS $RowObject) {
			$jsListingArray.= "rS['".$RowObject->rowID."'] = ".(int)$__NSess->hierarchicalListing[$this->ListID]['rowStates'][$RowObject->rowID].";\n";
			if (!$parentsDone[$RowObject->parentID]) $jsListingArray.= "R['".$RowObject->parentID."'] = new Array();\n";
			$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."'] = new Array();\n";
			$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['Name'] = '".addslashes($RowObject->Name)."';\n";
			foreach ($this->Columns AS $ColumnID => $ColumnInfo) {
				$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['".$ColumnID."'] = '".addslashes(str_replace(array("\n", "\r"), '<BR>', $RowObject->Data[$ColumnID]))."';\n";
			}
			$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['Actions'] = new Array();\n";
			foreach ($this->Actions AS $ActionID => $ActionInfo) {
				$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['Actions']['".$ActionID."'] = new Array();\n";
				$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['Actions']['".$ActionID."']['Lable'] = '".addslashes($RowObject->Actions[$ActionID]['Lable'])."';\n";
				$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['Actions']['".$ActionID."']['URL'] = '".addslashes($RowObject->Actions[$ActionID]['URL'])."';\n";
				$jsListingArray.= "R['".$RowObject->parentID."']['".$RowObject->rowID."']['Actions']['".$ActionID."']['onClickAction'] = '".addslashes($RowObject->Actions[$ActionID]['onClickAction'])."';\n";
			}
			$parentsDone[$RowObject->parentID] = true;
		}
		$jsListingArray.= "Listing_".$this->ListID."['Rows'] = R;\n";
		$jsListingArray.= "Listing_".$this->ListID."['rowStates'] = rS;\n";
		//$jsListingArray.= "R = new Array();\n";
		//$jsListingArray.= "rS = new Array();\n";
		
		$NitroTemplate = new NitroTemplate();
		
		$NitroTemplate->assign("ListActions", $this->ListActions);	
		$NitroTemplate->assign("jsListingArray", $jsListingArray);
		$NitroTemplate->assign("ListID", $this->ListID);
		$NitroTemplate->assign("Title", $this->Title);
		$NitroTemplate->assign("Columns", $this->Columns);
		$NitroTemplate->assign("ColumnWidths", $this->ColumnWidths);
		$NitroTemplate->assign("Rows", $this->Rows);
		
		
		// Create List
		$rv = $NitroTemplate->fetch($this->Template);

		return $rv;
	}
}

/**
 * The List row class
 *
 * This class should only be used by the List class
 *
 * @package	Nitro
 * @subpackage	Libraries
 * @access	public
 * @see	hierarchicalListing
 */
class listRow {
	var $Data = array();
	var $Actions = array();
	var $ColumnID = 0;
	
	function listRow($Name = NULL, $rowID, $parentID = 0)
	{
		$this->Name = $Name;
		$this->rowID = $rowID;
		$this->parentID = $parentID;
	}
	
	function addData($DataRow = array(), $Value = NULL)
	{
		if(is_array($DataRow) && count($DataRow)){
			foreach($DataRow AS $Key => $Data){
				$this->Data[$Key] = $Data;
			}
			$rv = true;
		} elseif ($DataRow && isset($Value)) {
			$this->Data[$DataRow] = $Value;
			$rv = true;
		} else {
			$rv = false;
		}
		return $rv;
	}
	
	function setAction($ActionID, $Lable, $URL = "", $onClickAction = "")
	{
		$ActionID = strtoupper($ActionID);
		$this->Actions[$ActionID] = array("Lable" => $Lable, "URL" => $URL, "onClickAction" => $onClickAction);
	}
}
?>
Return current item: OpenNitro