Location: PHPKode > scripts > PHP 4 Applications > php-4-applications/p4a_listview.php
<?php
	require_once("p4a_constants.php");
	require_once("p4a_control.php");
	require_once("p4a_datasource.php");

class p4a_ListView extends p4a_Control
{
	var $source;
	var $num_rows;
	var $data;
	var $columns;
	var $onClickColumn;
	var $onClickRow;
	var $onclickNext;
	var $onclickPrev;
	var $widthColumn;
	var $filter;

	function p4a_ListView( $_wc=0)
	{
		$this->type="listview";
		$this->num_rows = P4A_NUM_RECORDS_MULTIOCURRENCY;
		$this->widthColumn = $_wc;
	}

	function setSource( $_obj)
	{
		$this->source = $_obj;
		$this->next();
	}

	function setNumRows( $_nr)
	{
		$this->num_rows = $_nr;
	}

	function setColumn( $_cn, $_la)
	{
		$this->columns[$_cn]['label']=$_la;
		$this->columns[$_cn]['order']='';
	}
	
	function setDscn( $_cn, $_la, $_t, $_vis, $_invis)
	{
		$this->columns[$_vis]['label'] = $_la;
		$this->columns[$_vis]['order'] = '';
		$this->columns[$_vis]['dscn']['table'] = $_t;
		$this->columns[$_vis]['dscn']['invisible'] = $_invis;
		$this->columns[$_vis]['dscn']['related'] = $_cn;
	}

	function setonClickColumn( $_evt)
	{
		$this->onClickColumn = $_evt;
	}

	function setonClickRow( $_evt)
	{
		$this->onClickRow = $_evt;
	}

	function setonClickNext( $_evt, $_l)
	{
		$this->onClickNext['evt'] = $_evt;
		$this->onClickNext['lbl'] = $_l;
	}

	function setonClickPrev( $_evt, $_l)
	{
		$this->onClickPrev['evt'] = $_evt;
		$this->onClickPrev['lbl'] = $_l;
	}

	function getDscn()
	{
		if (isset($this->columns))
		for($i=0;$i<count($this->data);$i++)
			foreach( $this->columns as $n => $v)
				if (isset($v['dscn']))
				{
					$sql = "select * from ".$v['dscn']['table']." where ".$v['dscn']['invisible']." = '".$this->data[$i][$v['dscn']['related']]."'";
					$result = mysql_query( $sql);
					$num_results = mysql_num_rows($result);
					if ($num_results>0)
					{
						$rec = mysql_fetch_array( $result, MYSQL_ASSOC);
						$this->data[$i][$n] = $rec[$n];
					}
				}
	}

	function setFilter( $_f)
	{
		$this->source->setFilter( $_f);
		$this->data = $this->source->getRecords( $this->source->getPointer() - $this->num_rows, $this->num_rows);
		
		$this->getDscn();
	}

	function next()
	{
		$this->data = $this->source->getRecords( $this->source->getPointer(), $this->num_rows);

		$this->getDscn();
	}

	function previous()
	{
		$this->data = $this->source->getRecords( $this->source->getPointer() - 2 * $this->num_rows, $this->num_rows);

		$this->getDscn();
	}

	function refresh()
	{
		$this->data = $this->source->getRecords( $this->source->getPointer() - $this->num_rows, $this->num_rows);

		$this->getDscn();
	}


	function setOrder( $_cn, $_or=P4A_ORDER_ASC)
	{
		$this->columns[$_cn]['order']=$_or;
		$this->source->setOrder( $_cn." ".$_or);
		$this->data = $this->source->getRecords( $this->source->getPointer() - $this->num_rows, $this->num_rows);

		$this->getDscn();
	}

	function changeOrder( $_cn)
	{
		if ($this->columns[$_cn]['order']==P4A_ORDER_DESC)
			$this->columns[$_cn]['order']=P4A_ORDER_ASC;
		else
			$this->columns[$_cn]['order']=P4A_ORDER_DESC;

		$this->source->setOrder( $_cn." ".$this->columns[$_cn]['order']);
		$this->data = $this->source->getRecords( $this->source->getPointer() - $this->num_rows, $this->num_rows);

		$this->getDscn();
	}

	function convert( $_v)
	{
		// convierte entidades habituales html
		$_v = htmlentities( $_v, ENT_QUOTES);

		// convierte saltos de lĂ­nea en "\\n" aptos para javascript (usando innerHTML='...')

		$_v = preg_replace("(\r\n|\n|\r)", "\\n", $_v);
		
		return $_v;

	}

	function flush( $_ev=0)
	{
		$this->refresh();

		if (!$_ev)
			$da="";
		else
			$da="parent.";

		$js = "<script language='JavaScript'>\n";
		$js .= $da.$this->id_html.".className='clsListView';\n";
		$js .=$da.$this->id_html.".innerHTML='<table><tr>";

		foreach( $this->columns as $k => $v)
		{
			if (isset($v['dscn']))
				$js .= "<th>".$v['label']."</th>";
			else
			{
				if (isset($this->onClickColumn))
					$js .= "<th onclick=\"javascript:p4a_eventFrame.location.href=\'p4a_events.php?event=".$this->onClickColumn."&column=".$k."\'\">".$v['label']."</th>";
				else
					$js .= "<th>".$v['label']."</th>";
			}
		}

		$js .= "</tr>";

		for($i=0;$i<count($this->data);$i++)
		{
			if (isset($this->onClickRow))
				$js .= "<tr onclick=\"javascript:p4a_eventFrame.location.href=\'p4a_events.php?event=".$this->onClickRow."&rownumber=".$i."\'\">";
			else
				$js .= "<tr>";
			foreach( $this->columns as $k => $v)
				if (isset($this->data[$i][$k]))
				{
					if ($this->widthColumn>0)
						$js .= "<td width=\'".$this->widthColumn."px\'>".$this->convert( $this->data[$i][$k])."</td>";
					else
						$js .= "<td>".$this->convert( $this->data[$i][$k])."</td>";
				}
				else
				{
					if ($this->widthColumn>0)
						$js .= "<td width=\'".$this->widthColumn."px\'>----</td>";
					else
						$js .= "<td>----</td>";
				}

			$js .= "</tr>";
		}

		for($i=count($this->data);$i<$this->num_rows;$i++)
		{
			$js .= "<tr>";
			foreach( $this->columns as $k => $v)
				$js .= "<td>.</td>";
			$js .="</tr>";
		}

		$js .="<tr><td colspan=\"".count($this->columns)."\"><table class=\"clsListViewControls\"><tr>";

		if (isset($this->onClickPrev))
			$js .="<td onclick=\"javascript:p4a_eventFrame.location.href=\'p4a_events.php?event=".$this->onClickPrev['evt']."\'\">".$this->onClickPrev['lbl']."</td>";
		else
			$js .="<td></td>";
		if (isset($this->onClickNext))
			$js .="<td onclick=\"javascript:p4a_eventFrame.location.href=\'p4a_events.php?event=".$this->onClickNext['evt']."\'\">".$this->onClickNext['lbl']."</td>";
		else
			$js .="<td></td>";

		$js .="</tr></table></td></tr>";

		$js .= "</table>';\n";
		$js .= "</script>\n";

		return $js;
	}


}

?>
Return current item: PHP 4 Applications