<?php
/**
 * Data Table helper class to render a formatted html Data Table
 * 
 * @link http://www.oldauntie.org
 * @author Maggie Marple <oldauntie@oldauntie.org>
 * @version 1.0
 */
class Datatable {
	var $id;
	var $aActions = array ();
	var $aHeader = array ();
	var $aFooter = array ();
	var $aRows = array ();
	var $script;
	var $rows;
	var $footer;
	var $header;

	var $output;
	
	// const ACTION_DELETE = 'delete';
	// const ACTION_EDIT = 'edit';
	/**
	 * 
	 * @param string $id
	 */
	function Datatable($id = 'myDataTable') {
		$this->id = $id;
	}
	
	/**
	 * 
	 * @param unknown $actionName
	 * @param unknown $actionURL
	 * @param array $actionsFields
	 */
	function addAction($actionName, $actionURL, $actionsFields = array()) {
		$this->aActions [$actionName] = array (
				$actionURL,
				$actionsFields 
		);
	}
	
	/**
	 * 
	 * @param array $array
	 */
	function addRows($array = array()) {
		$this->aRows = $array;
	}
	
	/**
	 * 
	 * @return string
	 */
	private function renderRows() {
		if( empty ( $this->aRows ) == false ){
			
			foreach ( $this->aRows as $row ) {
				$this->rows .= '<tr><td>' . implode ( '<td>', $row );
				
				// build actions button / querystring
				if( empty ( $this->aActions ) == false ){
					// render Action cell building the action URL dinamically
					$this->rows .= '<td>';
					foreach ( $this->aActions as $action_name => $action_data ) {
						
						$params = array ();
						foreach ( $action_data [1] as $action_data_field ) {
							$params[] = $row[ $action_data_field ];
						}
						
						$url = vsprintf ( $action_data [0], $params );
						$this->rows .= '<a href="' . $url . '"><button>' . $action_name . '</button></a>';
					}
					$this->rows .= '</td>';
				}
				
				$this->rows .= '</tr>' . "\n";
			}
		}
		
		return $this->rows;
	}
	
	/**
	 * Add footer to a table. Usually used by renderFooter to create <tfoot> section
	 * 
	 * @param array $array
	 */
	function addFooter($array = array()) {
		$this->aFooter = $array;
	}
	
	/**
	 * Add header to a table. Usually used by renderHeader to create <thead> section
	 * 
	 * @param array $array
	 */
	function addHeader($array = array()) {
		$this->aHeader = $array;
	}
	
	/**
	 * Create <tfoot> section into a table
	 */
	private function renderFooter() {
		// if the footer array is not empty (i.e. some column have been added), creates the table output for the <tfoot> tag
		if (empty ( $this->aFooter ) == false) {
			$this->footer = '<tfoot><tr><th>' . implode ( '</th><th>', $this->aFooter );
			if (empty ( $this->aActions ) == false) {
				$this->footer .= '<th>actions</th>';
			}
			$this->footer .= '</tr></tfoot>';
		}
		
		// return the html output
		return $this->footer;
	}
	
	/**
	 * Create <thead> section into a table
	 */
	private function renderHeader() {
		if(empty ( $this->aHeader ) == false) {
			$this->header = '<thead><tr><th>' . implode ( '</th><th>', $this->aHeader );
			if (empty ( $this->aActions ) == false) {
				$this->header .= '<th>actions</th>';
			}
			$this->header .= '</tr></thead>';
		}
		
		return $this->header;
	}
	
	
	function render() {
		$this->output = "<script>
				$(document).ready(function() {
				    $('#" . $this->id . "').DataTable();
				} );
				</script>" . "\n\n";
		
		$this->output .= '<table id="' . $this->id . '" class="table table-striped table-bordered">' . "\n";
		$this->output .= $this->renderHeader () . "\n";
		$this->output .= $this->renderFooter () . "\n";
		$this->output .= '<tbody>' . "\n";
		$this->output .= $this->renderRows () . "\n";
		$this->output .= "\n" . '</tbody>' . "\n";
		$this->output .= '</table>' . "\n";
	}
	
	function output(){
		if( empty( $this->aRows ) == false ){
			$aRows = $this->aRows;
			$aHeader = array_keys( array_shift( $aRows ) );
			$this->addHeader( $aHeader );
			unset( $aRows );
		}
		
		$this->render();
		return $this->output;
	}
}
