Location: PHPKode > projects > OpenNitro > trunk/Nitro/Libraries/Listing.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Libraries :: Listing                                             |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2006 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Jesper Avôt <hide@address.com>                        |
// +---------------------------------------------------------------------------+
//
// $Id: Listing.inc.php 229 2008-04-17 09:20:31Z oli $
//
// This file contains the List functions to create Listings
//

/**
 * Defines
 */
define( "NITRO_DEF_LIST_TEMPLATE", 		"List" );
define( "NITRO_DEF_LISTROW_TEMPLATE", "ListRow" );

/**
 * Include Nitro template support
 */
require_once( "Nitro/Template.inc.php" );

/**
 * The listing class
 *
 * This class can be used to create lists.
 *
 * @package			Nitro
 * @subpackage	Libraries
 * @author 			Jesper Avôt
 * @version 		$Revision:$
 * @copyright		2006 June Systems BV
 *
 * <CODE>
 * Coming Soon...
 * </CODE>
 */
class Listing2 {
	/**
	 * Predefined variablen
	 */
	var $Title = "";
	var $ListID = FALSE;
	var $NoRedirect = FALSE;
	
	var $Template;
	var $inDiv = FALSE;
	
	// Columns and Rows
	var $Columns = Array();
	var $ColumnWidths = Array();
	var $ColumnStyles = Array();
	var $Rows = Array();
	var $hiddenRows = Array();
	var $ColumnID = 0;
	var $RowID = 0;
	
	// Page navigation
	var $pageTemplate = "";
	var $usePages = FALSE;
	var $maxPerPage = 20;
	var $allPages = 0;
	var $Start = 0;
	var $onChange = "";
	var $sesPrefix = "";
	
	// Filter
	var $useFilter = FALSE;
	var $Filter = Array();
	var $hideFilter = Array();
	
	// Actions and ListActions
	var $Actions;
	var $ListActions = Array();
	
	// Other stuff
	var $TableWidth = "100%";
	var $AlignData = "right";
	var $RepeatHeader = FALSE;
	var $EnableRowHighlighting = FALSE;
	var $EnableColumnHighlighting = FALSE;
	
	/**
	 * Listing constructor
	 *
	 * @param	string	$Title			Title of the Form
	 * @param	string	$ListID			ID of the list used to set and restore session information
	 * @param	boolean	$NoRedirect	Variable used to define if document should be redirected after form submit
	 * @access	public
	 */
	function Listing2( $Title = "", $ListID = FALSE, $inDiv = FALSE, $Start = Array( FALSE, FALSE ), $Filter = Array( FALSE, FALSE ), $sesPrefix = FALSE )
	{
		global $__NSess, $__NitroConf;
		
		$this->Template 	= "file:" . NITRO_PATH . "Defaults/Templates/Listing.tpl"; // Set the default list template
		$this->Title 			= $Title; // Set the list title
		$this->Conf 			= $__NitroConf->CONF; // Set the default configuration of Nitro, don't know why ?
		$this->inDiv			= ( $inDiv !== FALSE ) ? $inDiv : "";
		$this->NoRedirect = $NoRedirect; // TODO: Remove, is not needed i think.
		$this->pageTemplate = "file:".NITRO_PATH."Defaults/Templates/Navigation.tpl";
		
		if( $ListID !== FALSE ) {
			$this->ListID = $ListID;
			
			// Create session support for the list.
			if( !is_array( $__NSess->Lists[$ListID] ) ) {
				$__NSess->Lists[$ListID] = Array();
			}
			
			// Create an reference to the List items.
			$this->ListSettings =& $__NSess->Lists[$ListID];
		}
		
		// Page navigation
		if( $sesPrefix !== FALSE ) {
			$this->sesPrefix = $sesPrefix;
		}
		
		if( $Filter[0] !== FALSE ) {
			$this->useFilter = TRUE;
			
			if( isset( $Filter[1] ) && is_array( $Filter[1] ) ) {			
				$_SESSION[$this->sesPrefix . "_ModFilter"] = $Filter[1];
			}
			
			if( $_SESSION[$this->sesPrefix . "_ModFilter"] ) {
				$this->Filter = $_SESSION[$this->sesPrefix . "_ModFilter"];
			}
		}
		
		$tmp = FALSE;
		
		foreach( $this->Filter AS $Key => $Value ) {
			if( $Value !== "" ) {
				$tmp = TRUE;
			}
		}
		
		if( $Start[0] !== FALSE ) {
			$this->usePages = TRUE;
			
			if(	$tmp !== FALSE ) {
				if( !$_SESSION[$this->sesPrefix . "_StartFilter"] || strlen( $Start[1] ) && $Start[1] !== $_SESSION[$this->sesPrefix . "_StartFilter"] && $Start[1] !== 0) {
					$_SESSION[$this->sesPrefix . "_StartFilter"] = $Start[1];
				}
			}	else {
				if( $_SESSION[$this->sesPrefix . "_StartFilter"] ) {
					unset( $_SESSION[$this->sesPrefix . "_StartFilter"] );
				}
						 
				if( !$_SESSION[$this->sesPrefix . "_Start"] || strlen( $Start[1] ) && $Start[1] !== $_SESSION[$this->sesPrefix . "_Start"] && $Start[1] !== 0 ) {
					$_SESSION[$this->sesPrefix . "_Start"] = $Start[1];
				}
			}
			
			$this->Start = ( $tmp !== FALSE ) ? $_SESSION[$this->sesPrefix . "_StartFilter"] : $_SESSION[$this->sesPrefix . "_Start"];
		}
	}
	
	/**
	 * Set the Template
	 *
	 * @param	string	$Template	Template string, e.g. 'Nitro:'.NitroGetTemplateID('DefaultList')
	 * @access	public
	 */
	function SetTemplate( $Template )
	{
		$this->Template = $Template;
	}
	
	/**
	 * Adds a ListRow
	 *
	 * @param		object	$ListRow Complete class ListRow object containing the row information, but undrawn
	 * @access	public
	 */
	function AddListRow($ListRow)
	{
		if( is_object( $ListRow ) && count( $ListRow ) ) {
			$this->Rows[$this->RowID] = $ListRow;
			$this->RowID++;
			
			$RV = TRUE;
		} else {
			$RV = FALSE;
		}
		return $RV;
	}
	
	/**
	 * Adds a Column
	 *
	 * @param		string	$ColumnName	Name of column to be used in Header
	 * @param 	string	$ColumnID ID of Column to be used when linking it to links
	 * @access	public
	 */
	function AddColumn( $ColumnName, $ColumnID = FALSE, $Center = FALSE )
	{
		if( !is_array( $ColumnName ) || !count( $ColumnName ) ) {
			$ColumnName = Array( Array( $ColumnName, 
													 				$ColumnID, 
													 				$Center ) );
		}
		
		foreach( $ColumnName AS $Columns ) {
			if( $Columns[1] !== FALSE ) {
				$this->Columns[$Columns[1]] = Array( ( strlen( $Columns[0] ) ? $Columns[0] : Language( "Undefined" ) ), 
																						 ( $Columns[2] !== FALSE ? $Columns[2] : "" ) );
			}
		}
		
		return TRUE;
	}
	
	/**
	 * Sets the width for 1 or more columns at once
	 *
	 * @param	mixed 	$ColumnWidths array of Columnwidths, eg. array('ID1' => 'Width1', 'ID2' => 'Width2') OR str Width of 1 column
	 * @param string	$ColumnKey ColumnID, only used if given together with a width
	 * @access	public
	 */
	function SetColumnWidth( $ColumnWidths, $ColumnKey = FALSE )
	{
		if( !is_array( $ColumnWidths ) || !count( $ColumnWidths ) ) {
			$ColumnWidths = Array( Array( $ColumnWidths, 
														 				$ColumnKey ) );
		}
		
		foreach( $ColumnWidths AS $Columns ) {
			$this->ColumnWidths[$Columns[1]] = ( strlen( $Columns[0] ) ? $Columns[0] : "" );
		}
		
		return TRUE;
	}
	
	function SetColumnStyles( $ColumnStyles, $ColumnKey = FALSE )
	{
		if( !is_array( $ColumnStyles ) || !count( $ColumnStyles ) ) {
			$ColumnStyles = Array( Array( $ColumnStyles, 
														 				$ColumnKey ) );
		}
		
		foreach( $ColumnStyles AS $Columns ) {
			$this->ColumnStyles[$Columns[1]] = ( strlen( $Columns[0] ) ? $Columns[0] : "" );
		}
		
		return TRUE;
	}
	
	/**
	 * Add an action
	 *
	 * @param	string $ID ActionID by which links will be added to
	 * @param string	$Lable Action lable, if not given, ID will be used
	 * @access	public
	 */
	function AddAction( $ID, $Lable = FALSE )
	{
		if( !is_array( $ID ) || !count( $ID ) ) {
			$ID = Array( Array( $ID, 
									 				$Lable ) );
		}
		
		foreach( $ID As $Actions ) {
			$this->Actions[strtoupper( $Actions[0] )] = $Actions[1];
		}
		
		return TRUE;
	}
	
	/**
	 * Adds list action (usually a link above the listing to for example Add an item)
	 *
	 * @param	string $Type Action description usually displayed inside the link
	 * @param string $URL The URL where the link refers to
	 * @param string $onClickAction onClickAction used, if you use 'return false' at the end of the onClickAction, the browser will not use the URL link
	 * @access	public
	 */
	function AddListAction( $Type, $URL = FALSE, $onClickAction = FALSE, $Style = FALSE, $Title = FALSE, $useLink = TRUE )
	{
		if( !is_array( $Type ) || !count( $Type ) ) {
			$Type = Array( Array( $Type, 
										 	 			$URL, 
										 	 			$onClickAction, 
										 	 			$Style, 
										 	 			$Title, 
										 	 			$useLink ) );
		}
		
		foreach( $Type AS $Actions ) {
			$this->ListActions[] = Array( "Type" => $Actions[0], 
																		"URL" => $Actions[1], 
																		"onClickAction" => $Actions[2], 
																		"Style" => $Actions[3], 
																		"Title" => $Actions[4], 
																		"useA" => $Actions[5] );
		}
		
		return TRUE;
	}
	
	/**
	 * Draws the List
	 */
	function Draw()
	{
		global $__NSess;
		
		// Filter
		if( $this->useFilter !== FALSE ) {			
			$ListRow = new ListingRow2();
			
			$i = 0;
			$j = 0;
			
			foreach( $this->Columns AS $Key => $Value ) {
				if( !in_array( $Key, $this->hideFilter ) ) {
					$j++;
				}
			}
			
			foreach( $this->Columns AS $Key => $Value ) {
				if( !in_array( $Key, $this->hideFilter ) ) {
					$tmp = "<!--Filter--><input type='text' name='ModFilter/[" . $Key . "]' value=\"" . (strlen( $this->Filter[$Key] ) ? $this->Filter[$Key] : "") . "\" onfocus=\"changeBG(this, true);\" onblur=\"changeBG(this, false);\" />";
					
					if( $i == ( $j - 1 ) ) {
						$tmp.= "<input type='submit' class='bluebutton' onclick=\"ModuleXMLRequest('FilterMod', GetXMLURL('" . $this->ListID . "')); return false;\" value='" . Language( "Filter" ) . "' />";
					}
					
					$ListRow->AddData( $Key, $tmp );
				} else {
					$ListRow->AddData( $Key, "<!--Filter-->&nbsp;" );
				}
				
				$i++;
			}
			
			if( is_array( $this->Actions ) && count( $this->Actions ) ) {
				foreach( $this->Actions AS $Key => $Value ) {
					$ListRow->SetAction( $Key, "" );
				}
			}
			
			array_unshift( $this->Rows, $ListRow );
			unset( $ListRow );
		}
		
		// Page navigation
		if ($this->allPages > $this->maxPerPage) {
			$Pages = Array();
			
			for( $i = 0; $i < $this->allPages; $i = $i + $this->maxPerPage ) {
				$Pages[] = $i;
			}
			
			if (!strstr($this->pageTemplate, "file:")) {
				$this->pageTemplate = NitroGetTemplateID($this->pageTemplate);
			}
			
			$NavTPL = new NitroTemplate($this->pageTemplate);
			$NavTPL->ClearAll();
			$NavTPL->Assign("onChange", $this->onChange);
			$NavTPL->Assign("Start", $this->Start);
			$NavTPL->Assign("Pages", $Pages);
			$NavTPL->Assign("allPages", $this->allPages);
			$NavTPL->Assign("maxPerPage", $this->maxPerPage);
			$NavTPL->Assign("Page", Language('Page'));
			$NavTPL->Assign("PreviousPage", Language('Previous Page'));
			$NavTPL->Assign("NextPage", Language('Next Page'));
			$Nav = $NavTPL->Fetch();
		} else {
			$Nav = "";
		}
		
		// Create new Template Instance
		$NitroTemplate = new NitroTemplate();
		
		$NitroTemplate->assign( "Navigation", $Nav );
		$NitroTemplate->assign( "ListID", $this->ListID );
		$NitroTemplate->assign( "URL", $_SERVER['REQUEST_URI'] );
		$NitroTemplate->assign( "Title", $this->Title );
		$NitroTemplate->assign( "inDiv", $this->inDiv );
		$NitroTemplate->assign( "Columns", $this->Columns );
		$NitroTemplate->assign( "ColumnWidths", $this->ColumnWidths );
		$NitroTemplate->assign( "ColumnStyles", $this->ColumnStyles );
		$NitroTemplate->assign( "Rows", $this->Rows );
		$NitroTemplate->assign( "ListActions", $this->ListActions );	
		$NitroTemplate->assign( "Actions", $this->Actions );
		
		// Other stuff
		$NitroTemplate->assign( "TableWidth", $this->TableWidth );	
		$NitroTemplate->assign( "AlignData", $this->AlignData );	
		$NitroTemplate->assign( "RepeatHeader", $this->RepeatHeader );
		$NitroTemplate->assign( "EnableRowHighlighting", $this->EnableRowHighlighting );
		$NitroTemplate->assign( "EnableColumnHighlighting", $this->EnableColumnHighlighting );
		
		// TODO: Fix or Remove
		$NitroTemplate->assign( "ScrollTop", (int)$__NSess->Scroll[$this->ListID]["Top"] );	
		$NitroTemplate->assign( "ScrollLeft", (int)$__NSess->Scroll[$this->ListID]["Left"] );	
	//echo '<pre>'; print_r($NitroTemplate); 
		// Create the List
		$RV = $NitroTemplate->fetch( $this->Template );

		return $RV;
	}
}

/**
 * The Listing row class
 *
 * This class should only by used by the listing class
 *
 * @package			Nitro
 * @subpackage	Libraries
 * @access			public
 * @see 				Listing class
 */
class ListingRow2 {
	var $Data = Array();
	var $DataValue = Array();
	
	var $ActionName = Array();
	var $ActionURL = Array();
	var $ActionOnClick = Array();
	var $ActionStyle = Array();
	
	/**
	 * ListingRow constructor
	 *
	 * @access	public
	 */
	function AddData( $Key, $Value )
	{
		if( is_array( $Value ) ) {
			$this->Data[$Key] = reset( $Value );
			$this->DataValue[$Key] = key( $Value );
		} else {
			$this->Data[$Key] = $Value;
		}
		
		return TRUE;
	}
	
	/**
	 * Defines an action for the current RowObject
	 *
	 * @param	string	$ActionID ID of the Action Column being set
	 * @param	string	$Name Name that's gonna be displayed, often an Image is used here
	 * @param string	$URL URL that Action points to
	 * @param string	$onClickAction onClick action to be taken when clicked on, if 'return false' is added the URL will not be executed
	 * @access	public
	 */
	function SetAction( $ActionID, $Name, $URL = FALSE, $onClickAction = FALSE, $Style = FALSE, $Center = FALSE, $Title = FALSE, $NoLink = FALSE )
	{
		$ActionID = strtoupper( $ActionID );
		
		$this->ActionName[$ActionID] = $Name;
		$this->ActionURL[$ActionID] = ( $URL !== FALSE ? $URL : "" );
		$this->ActionOnClick[$ActionID] = ( $onClickAction !== FALSE ? $onClickAction : "" );
		$this->ActionStyle[$ActionID] = ( $Style !== FALSE ? $Style : "" );
		$this->ActionCenter[$ActionID] = ( $Center !== FALSE ? $Center : "" );
		$this->ActionTitle[$ActionID] = ( $Title !== FALSE ? $Title : "" );
		$this->ActionNoLink[$ActionID] = ( $NoLink !== FALSE ? $NoLink : "" );
	}
}
?>
Return current item: OpenNitro