<?php
/**
 * @class pagination
 * @brief Generate json arrays to pass to the javascript pagination engine
 * @author Xavier Perrissoud
 * @date 29/08/2009
 * @version 0.1
 *
 * This class is a wrapper for the JavaScript pagination engine. It will help you creating and adding lines
 * to the result array, and to add cells to these lines.<br />
 * It will also allow you to set lines or cells attributes as you would do in a classic html table
*/
class pagination
{
	/**
	 * @brief Temporary created datas
	*/
	private $results = array();
	
	/**
	 * @brief Add a new line to the results array
	 * @param None
	 * @return Zero-based index of this new line
	 *
	 * You will have to call this method for every line (&lt;tr&gt;) you want to add to the result table.
	 * It will create an empty line, and return the zero-based index of the new line in the table.
	*/
	public function addLine()
	{
		// Create the new entry
		$tr = array();
		// Add the tr's options array to this entry
		$tr[] = array();
		// Add the tr's data's array to this entry
		$tr[] = array();
		// Add the entry to the results array
		$this->results[] = $tr;
		// Return the zero-based index of the new entry
		return $this->getLinesCount()-1;
	}
	
	/**
	 * @brief Set an attribute for a &lt;tr&gt; entry (such as id, class, style, ...)
	 * @param line_index Zero-based index of the line (returned by the addLine method)
	 * @param attr_name Name of the attribute to set
	 * @param attr_value Value of the attribute to set
	 * @return True if successfull, False otherwise
	 *
	 * Call this method to add properties to a &lt;tr&gt; entry of your table.<br />
	 * For example, if you want to set the class name of a line, just call this method with <tt>$attr_name = "class"</tt> and <tt>$attr_value = "class_name"</tt>.
	*/
	public function setLineAttribute( $line_index, $attr_name, $attr_value )
	{
		if ( $line_index >= $this->getLinesCount() ) return false;
		// Get the concerned line entry
		$tr = & $this->results[$line_index];
		// Get the line's options array
		$tr_opts = & $tr[0];
		// Create (or update) the line attribute
		$tr_opts[$attr_name] = $attr_value;
		// All is ok : we can return true
		return true;
	}
	
	/**
	 * @brief Get the number of lines already defined in the results array
	 * @param None
	 * @return Number of lines
	 *
	 * This method will return the total of lines added to the result array (total of calls to addLine ).
	*/
	public function getLinesCount()
	{
		return count($this->results);
	}
	
	/**
	 * @brief Add a new cell to the given line
	 * @param cell_value Text / Html code to put in the cell
	 * @param line_index Zero-based index of the line (if ommitted, the last line added is used)
	 * @return Zero-based index of the cell in the line
	 *
	 * You will have to call this method for every cell (&lt;td&gt;) entry that you want to add to a line.
	*/
	public function addCell( $cell_value, $line_index=null )
	{
		if ( is_null($line_index) ) $line_index = $this->getLinesCount()-1;
		// Create the new cell datas
		$new_cell = array( 'html' => $cell_value );
		// Get the line entry in the results array
		$tr = & $this->results[$line_index];
		// Get the line's cells array
		$tr_cells = & $tr[1];
		// Add the cell to the line
		$tr_cells[] = $new_cell;
		// Return the new cell index
		return $this->getCellsCount($line_index)-1;
	}
	
    /**
	 * @brief Set an attribute for a &lt;td&gt; entry (such as id, class, style, colspan, ...)
	 * @param line_index Zero-based index of the line
     * @param cell_index Zero-based index of the cell in the line (the cell must be created before with addCell)
	 * @param attr_name Name of the attribute to set/modify
	 * @param attr_value Value of the attribute
	 * @return True if successfull, false otherwise
	 *
	 * Call this method to add properties to a &lt;td&gt; entry of your table.<br />
	 * For example, if you want to set the class name of a cell, just call this method with <tt>$attr_name = "class"</tt> and <tt>$attr_value = "class_name"</tt>.
	*/
	public function setCellAttribute( $line_index, $cell_index, $attr_name, $attr_value )
	{
		// Check the $line_index and $cell_index values
		if ( ($line_index < 0) || ($line_index >= $this->getLinesCount())) return false; 
		if ( ($cell_index < 0) || ($cell_index >= $this->getCellsCount($line_index)) ) return false;
		// Get the line entry in the results array
		$tr = & $this->results[$line_index];
		// Get the line's cells array
		$tr_cells = & $tr[1];
		// Get the cell entry
		$td = & $tr_cells[$cell_index];
		// Set/Modify the cell attribute
		$td[$attr_name] = $attr_value;
		
		return true;
	}
    
    
	/**
	 * @brief Get the number of cells already defined in the given line
	 * @param line_index : Zero-based index of the line (if ommitted, the last line is used)
	 * @return Number of cells in the line
	 *
	*/
	public function getCellsCount( $line_index=null)
	{
		if ( is_null($line_index) ) $line_index = $this->getLinesCount()-1;
		// Get the line entry in the results array
		$tr = & $this->results[$line_index];
		// Return the number of cells
		return count($tr[1]);
	}
	
	/**
	 * @brief Get the created results array
	 * @param None
	 * @return json array of the result table datas
	 *
	*/
	public function getResult()
	{
		return json_encode($this->results);
	}
}
?>