<?php

/**
 * This example illustrates the use of the 
 * DataList object classes.  This object
 * can show a list of data from any data source
 * and have any GUI layout and provide the 
 * features of:
 * searching, sorting, paging of the data.
 *
 * This page shows the Data coming from 2 different
 * types of DB objects.  One from a PEAR::DB object,
 * and another from a ADODB object.
 *
 * $Id: widget6.php,v 1.10.2.1 2005/05/12 01:24:02 hemna Exp $
 *
 * @author Walter A. Boring IV <waboring@newsblob.com>
 * @package phpHtmlLib
 * @subpackage widget-examples
 * @version 2.0
 *
 */ 

/**
 * Include the phphtmllib libraries
 *
 */
include_once("includes.inc");
include_once("db_defines.inc");

include_once($phphtmllib."/widgets/data_list/includes.inc");
include_once($phphtmllib."/widgets/data_list/PEARSQLDataListSource.inc");

//add the include path for adodb
//assuming you have the adodb dir in your
//site's document root.
ini_set("include_path", 
		ini_get("include_path").":".$_SERVER["DOCUMENT_ROOT"]."/adodb");
require_once( "adodb.inc.php" );

include_once($phphtmllib."/widgets/data_list/ADODBSQLDataListSource.inc");

/**
 * This is an example that shows how to use a PEAR db object
 * as the source for the data to show.
 *
 * @author Walter A. Boring IV <waboring@newsblob.com>
 * @package phpHtmlLib
 * @subpackage widget-examples
 * @version 2.0
 */
class pearmysqllist extends DefaultGUIDataList {
	//change the # of rows to display to 20 from 10
	var $_default_rows_per_page = 20;

	/**
	 * This function is called automatically by
	 * the DataList constructor.  It must be
	 * extended by the child class to actually
	 * set the DataListSource object.
	 *
	 * 
	 */
	function get_data_source() {
		//build the PEAR DB object and connect
		//to the database.
		$dsn = "mysql://".DB_USERNAME.":".DB_PASSWORD."@".DB_HOSTNAME."/".DB_NAME;
		$db = DB::connect($dsn, TRUE);
		if (DB::isError($db)) {
			die( $db->getMessage() );
		}

		//create the DataListSource object
		//and pass in the PEAR DB object
		$source = new PEARSQLDataListSource($db);

		//set the DataListSource for this DataList
		//Every DataList needs a Source for it's data.
		$this->set_data_source( $source );

		//set the prefix for all the internal query string 
		//variables.  You really only need to change this
		//if you have more then 1 DataList object per page.
		$this->set_global_prefix("pear_");
	}

	/**
     * This method is used to setup the options
	 * for the DataList object's display. 
	 * Which columns to show, their respective 
	 * source column name, width, etc. etc.
	 *
     * The constructor automatically calls 
	 * this function.
	 *
     */
	function user_setup() {
		//add the columns in the display that you want to view.
		//The API is :
		//Title, width, DB column name, field SORTABLE?, field SEARCHABLE?, align
		$this->add_header_item("IP", "100", "ip_address", SORTABLE, 
							   SEARCHABLE, "left");
        $this->add_header_item("Version", "100", "version", SORTABLE,
							    SEARCHABLE,"center");
        $this->add_header_item("Time", "200", "time", SORTABLE, 
							   NOT_SEARCHABLE,"center");

		

		$columns = "*";
		$tables = "user_downloads u, versions v";
		$where_clause = "u.version_id=v.version_id";
		$this->_datasource->setup_db_options($columns, $tables, $where_clause);
	}

    /**
     * This is the basic function for letting us
     * do a mapping between the column name in
     * the header, to the value found in the DataListSource.
     *
     * NOTE: this function is can be overridden
     *       so that you can return whatever you want for
	 *       any given column.  
     *
     * @param array - $row_data - the entire data for the row
     * @param string - $col_name - the name of the column header
     *                             for this row to render.
     * @return  mixed - either a HTMLTag object, or raw text.
     */
	function build_column_item($row_data, $col_name) {
		switch ($col_name) {
		case "Time":
			$dt = $row_data["time"];
			$yr=strval(substr($dt,0,4)); 
			$mo=strval(substr($dt,4,2)); 
			$da=strval(substr($dt,6,2)); 
			$hr=strval(substr($dt,8,2)); 
			$mi=strval(substr($dt,10,2)); 
            $obj = date("m/d/Y h:i A", mktime ($hr,$mi,0,$mo,$da,$yr));
			break;
		default:
			$obj = DefaultGUIDataList::build_column_item($row_data, $col_name);
			break;
		}
		return $obj;
	}
}

/**
 * This is a subclass of the pear mysql list object.
 * The only difference being is the DataListSource object
 * for an ADODB object, instead of a PEAR DB object.
 *
 */
class adodbmysqllist extends pearmysqllist {

	/**
	 * This function is called automatically by
	 * the DataList constructor.  It must be
	 * extended by the child class to actually
	 * set the DataListSource object.
	 *
	 * 
	 */
	function get_data_source() {
		//build the PEAR DB object and connect
		//to the database.
		$db = &ADONewConnection('mysql');  # create a connection
		$db->PConnect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_NAME);

		//create the DataListSource object
		//and pass in the PEAR DB object
		$source = new ADODBSQLDataListSource($db);

		//set the DataListSource for this DataList
		//Every DataList needs a Source for it's data.
		$this->set_data_source( $source );

		//set the prefix for all the internal query string 
		//variables.  You really only need to change this
		//if you have more then 1 DataList object per page.
		$this->set_global_prefix("adodb_");
	}

}


//create the page object
$page = new HTMLPageClass("phpHtmlLib Widgets - DataList Example",
						  XHTML_TRANSITIONAL);

//enable output debugging.
if (isset($_GET['debug'])) {
    $page->set_text_debug( TRUE );
}

//add the css
$page->add_head_css( new DefaultGUIDataListCSS );

//build the PEAR list and sort by ip_address by default
$pearlist = new pearmysqllist("PEAR::MySQL List", 600, "ip_address", TRUE);
$pearlist->set_align("right");

//build the ADODB list using the same exact table in the DB
//and sort by version by default
$adodblist = new adodbmysqllist("ADODB::MySQL List", 600, "version", TRUE);
$adodblist->set_align("left");

$page->add( $pearlist, html_br(2), $adodblist );

print $page->render();
?>
