Location: PHPKode > scripts > Bucci's Table Class > buccis-table-class/table.inc
<? class table {

/*=================================================================

/*--------------------------------

6/16/00 2:26 PM by jestevez

Copyright (C) 2000 Travel-Italy.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

--------------------------------*/

/*=================================================================

PLEASE SEE USER GUIDE FOR DOCUMENTATION

===================================================================*/

// OBJECT PROPERTIES

// This should be the path to the directory where you keep table templates, if you are going to use them.
// Your path must end with "/".
var $path_to_templates			= '';  // CHANGE ME!  
								

var $debugging = FALSE;			// Set to TRUE to have debugging information 
							// output to broswer and HTML source.

var $table;					// Holds HTML for table.
var $pair_string;				// Buffer which holds attributes before they are added to a tag.
var $pair_array;				// Buffer that holds name value pairs which will be tag attributes.

var $cellpadding;				// Used in <TABLE> tag.
var $cellspacing;				// Used in <TABLE> tag.
var $bgcolor;					// Used in <TABLE> tag.
var $border;					// Used in <TABLE> tag.
var $align;						// Used in <TABLE> tag.
var $width;					// Used in <TABLE> tag.

var $col_attribute;				// Multi-dimensional array.  Holds information which controls attribues of cells in a col.
var $row_attribute;				// Multi-dimensional array.  Holds information which controls attribues of cells in a row.

var $col = 0;					// Keeps track of current column in table.
var $row = 0;					// Keeps track of current row in table.

var $defaults = array
	(
	'debugging' 	=> FALSE, 
	'width' 		=> "100%",  
	'cellpadding' 	=> "1", 
	'cellspacing' 	=> "1", 
	'border' 		=> '1',
	'bgcolor' 		=> "#FFFFFF" 
	);


/* ================================================================= */

// CONSTRUCTOR

function table( $arguments = "" )

	//  Load paramters over defaults.
	//  Create the main "<table>" tag with appropriate attributes.

	{

	// Load defaults into corresponding object properties.
	while ( list( $key, $value ) = each ( $this->defaults ) )
		{
		if ( $key and $value )
			{
			$this->$key = $value;
			}
		}
			
	// Override defaults with passed parameters.
	if ( $arguments )
		{
		while ( list ( $key, $value ) = each ( $arguments ) )
			{	
			$this->$key = $value;
			}
		}
	
	}


/*=================================================================*/

// METHODS


function pass_thru ( $string )

	// Adds a string to the table, but doesn't wrap it in tags.
	// 5/27/00 7:05 PM by jestevez

	{
	$this->add ( $string );
	}

/*-----------------------------------------------------------------*/


function use_template ( $template )

	// Select a predefined table style.
	
	{
	$template_file_name =  $this->path_to_templates . $template . '.tpl';
	if ( is_readable ( $template_file_name ) )
		{
		include ( $template_file_name );
		}
	// You may want to put in code to handle
	// an error if the template isn't readable.
	
	}


/*-----------------------------------------------------------------*/


function add_col_attribute ( $name, $value, $loop = 0 )

	// Save information about attributes to be used in cells of a column.
	// Several arrays are used to hold the data.
	
	{
	$this->col_attribute_name[]		= $name;	// Save the name of the attribute.
	$this->col_attribute_value[] 		= $value;	// Each name has an associated array of values.
	$this->col_attribute_index[] 		= 0;		// The current position in the array of values.
	$this->col_attribute_loop_buffer[]	= $loop;	// Number of times we have left to loop the values.
	$this->col_attribute_loop[] 		= $loop;	// The total number of times to loop the values.
	}


/*-----------------------------------------------------------------*/


function add_row_attribute($name, $value, $loop = 0)

	// Save information about attributes to be used in cells of a row.

	{
	$this->row_attribute_name[]		= $name;		// Save the name of the attribute.
	$this->row_attribute_value[] 		= $value;		// Each name has an associated array of values.
	$this->row_attribute_index[] 		= 0;			// The current position in the array of values.
	$this->row_attribute_loop_buffer[] 	= $loop;		// Number of times we have left to loop the values.
	$this->row_attribute_loop[] 		= $loop;		// The total number of times to loop the values.
	}


/*-----------------------------------------------------------------*/


function add($s)
	// Add to the table.
	{
	$this->table .= $s;												
	}


/*-----------------------------------------------------------------*/


function start_table ()

	// Open the table tag.
	
	{

	// Get the table tag values from properties.
	$cellpadding	= $this->format_name_value_pair ( "CELLPADDING", $this->cellpadding );
	$cellspacing	= $this->format_name_value_pair ( "CELLSPACING", $this->cellspacing );
	$border 		= $this->format_name_value_pair ( "BORDER", $this->border );
	$bgcolor 		= $this->format_name_value_pair ( "BGCOLOR", $this->bgcolor );
	$width 		= $this->format_name_value_pair ( "WIDTH", $this->width );
		
	// Add beginning of table to the table.
	$this->add ( "\n\n" );
	$this->add ( "<!-- START TABLE -->" );
	$this->add ( "\n\n" );
	$this->add ( "<TABLE $cellpadding $cellspacing $border $bgcolor $width>" );
		
	}

/*-----------------------------------------------------------------*/

function debugging_message ( $debugging_message )

	// Add the message to ouput if debugging is turned on.
	
	{
	if ( $this->debugging )
		{
		$this->add ( $debugging_message );
		};
	}
	
/*-----------------------------------------------------------------*/

function finish_table ()
	
	// Close the table tag.
	
	{
	$this->add ( "\n\n" );
	$this->add ( "</TABLE>" );
	$this->add ( "\n\n" );
	$this->add ( "<!-- END TABLE -->" );
	$this->add ( "\n\n" );
	}

/*-----------------------------------------------------------------*/

function reset_col_pointer ()

	// Point to the first column in the table.
	
	{
	$this->col = 0;
	}

/*-----------------------------------------------------------------*/

function format_name_value_pair ( $name, $value )

	// Make sure a tag attribute is properly formated.
	
	{
	return ( strtoupper ( $name ) . "=\"$value\"");			
	}

/*-----------------------------------------------------------------*/

function is_not_valid_pair ( $name, $value )
	
	// Return TRUE if pair is not valid.
	
	{
	if( $name == "" or $value == "" )
		{
		return TRUE;
		}
	else
		{
		return FALSE;
		}
	}

/*-----------------------------------------------------------------*/

function send_pair_to_buffer ( $name, $value )

	// Make sure the pair is well-formed then send it to buffer.
	
	{
	// Make sure name and value are upper case.
	$upper_case_name	= strtoupper($name);
	$upper_case_value	= strtoupper($value);
	
	// Skip bad pairs.
	if ( $this->is_not_valid_pair ( $name, $value ) )
		{
		return TRUE;
		}

	// Send pair to correct buffer.
	switch($upper_case_name)
		{
		case 'NOWRAP':
			$this->pair_string .= ' NOWRAP';
			break;
		case 'BOLD':
			$this->content = '<B>' . $this->content . '</B>';
			break;
		default:
			$this->pair_array[$upper_case_name] = $upper_case_value;
		}

	}

/*-----------------------------------------------------------------*/

function add_cell_to_table ()

	// Add the built cell to the table.
	// Optinally add debugging information.

	{

	// Add formatting to output.
	$this->add("\n\t\t");

	// If debuggin is turned on, send additional information to HTML source code.
	$this->debugging_message ("<!-- ROW = " . $this->row . " and COL = " . $this->col . " -->\n\t\t");

	// Add the tag to open the cell and all the cell attributes.
	$this->add("<TD" . $this->pair_string . ">");

	// If debuggin is turned on, send additional information to screen.
	$this->debugging_message ("<small>index = ". $this->col . ", " . $this->row . "</small><br>");

	// Add the cell content.
	$this->add($this->content);

	// Add formatting to output
	// $this->add("\n\t\t");

	// Close the cell.
	$this->add("</TD>");

	}

/*-----------------------------------------------------------------*/

function make_array ( $any_data_type )

	// I know this function is silly.  Sorry.
	// Take any data type and make an array out of it.

	{
	if(! is_array ( $any_data_type ) )
		{
		return ( array ( $any_data_type ) );
		}
	else
		{
		return ( $any_data_type );
		}
	}

/*-----------------------------------------------------------------*/

function load_row_attributes ()
	
	// Find any row attribute that applies to this cell and send it to the buffer.

	{
	
	// Don't do anything if there aren't any row attributes.
	if ( ! $this->row_attribute_name )
		{
		return ( TRUE );
		}
		
	// Send all the attribute pairs for row this cell is in to the buffer.
	
	$row_attribute_count = count ( $this->row_attribute_name );
	
	for ( $i = 0; $i < $row_attribute_count ; $i++ )
		{

		$current_row_position 		= $this->row_attribute_index[ $i ];
		$current_row_attribute_value 	= $this->row_attribute_value[ $i ][ $current_row_position ];
		$current_row_attribute_name	= $this->row_attribute_name[ $i ];
		
		$this->send_pair_to_buffer ( $current_row_attribute_name, $current_row_attribute_value );
		
		}

	}
			
/*-----------------------------------------------------------------*/

function load_col_attributes ()

	// Find any column attribute that applies to this cell and send it to the buffer.
	
	{
	
	// Don't do anything if there aren't any column attributes.
	if ( ! $this->col_attribute_name )
		{
		return ( TRUE );
		}

	$col_attribute_count = count ( $this->col_attribute_name );
	
	for ( $i = 0; $i < $col_attribute_count; $i++ )
		{
		
		/*--------------------------------

		6/16/00 4:33 PM by jestevez

		This code is pretty gnarly because the names get long.
		It is an interesting exercise to rewrite this using "&" to link 
		the long names to short names.
		I decided not to release the easier to read and shorter
		code because it only works with PHP4.

		--------------------------------*/
		
		$last_col_index 	= count ( $this->col_attribute_value[ $i ] );
		$at_the_last_index 	=  $this->col_attribute_index[ $i ] == $last_col_index;
		
		
		if ( $at_the_last_index && --$this->col_attribute_loop_buffer[ $i ] )
			{
			$this->col_attribute_index[ $i ] = 0;
			}

		print $this->col_attribute_loop_buffer[ $i ];
		
		$col_attribute_name = $this->col_attribute_name[ $i ];
		$col_attribute_value = $this->col_attribute_value[ $i ][ $this->col_attribute_index[ $i ] ];

		$this->send_pair_to_buffer ( $col_attribute_name, $col_attribute_value );

		// Move to the next column.
		$this->col_attribute_index[ $i ]++;

		}

	}

/*-----------------------------------------------------------------*/

function load_local_attributes ( $input_array )

	//  Send the local cell attributes to the attribute buffer.
	
	{
	while( list ( $name, $value ) = each ( $input_array ))
		{
		$this->send_pair_to_buffer ( $name, $value );
		}
	}


/*-----------------------------------------------------------------*/


function build_attribute_string ()

	//  Add each pair in the array attribute buffer to the attribute string.
	
	{
	
	// Don't do anything if there isn't a pair_array
	if ( ! $this->pair_array )
		{
		return TRUE;
		}
		
	reset ( $this->pair_array );
	$pair_array_count = count ( $this->pair_array );
	for( $i = 0; $i < $pair_array_count; $i++ )
		{
		list( $name, $value ) = each ( $this->pair_array );
		$this->pair_string .= " $name='$value'";
		}
	}


/*-----------------------------------------------------------------*/


function add_cell ( $input )

	// Add a cell to a row.

	{
	
	// Make sure the input is an array.
	$input_array = $this->make_array ( $input );
		
	// Put the cell content into the cell content buffer.
	$this->content = $input_array[0];
		
	// Remove cell content from input array.
	unset($input_array[0]);
		
	$this->load_row_attributes ();
	$this->load_col_attributes ();
	$this->load_local_attributes ( $input_array );
	
	$this->build_attribute_string ();
		
	$this->add_cell_to_table ();
		
	$this->get_ready_for_next_cell ();

	}


/*-----------------------------------------------------------------*/


function get_ready_for_next_cell ()
	
	// Erase information about current cell and set the pointer to the next cell.
	
	{
	
	$this->pair_array 	= array();
	$this->pair_string	= '';
	$this->content		= '';
	
	$this->col++;
	
	}

/*-----------------------------------------------------------------*/

function add_multiple_rows ( $row_array )
	
	// Takes a multi-dimensional array of rows and adds it to the table.
	
	{
	$row_count = count ( $row_array );
	for ( $this_row = 0; $this_row < $row_count; $this_row++ )
		{
		$this->add_row ( $row_array[ $this_row ] );
		}
	}

/*-----------------------------------------------------------------*/


function add_row($cell_array)
	{

	// Reset to col zero.
	// We do this because every row starts with col zero.
		if ( $this->col_attribute_index )
			{
			reset ( $this->col_attribute_index );
			while ( list ( $name, $value ) = each ( $this->col_attribute_index ))
				{
				$this->col_attribute_index[ $name ] = 0;
				$this->col_attribute_loop_buffer[ $name ] = $this->col_attribute_loop[$name];
				}
			}
			
	// Build entire row.  As easy as ONE TWO THREE!
		
		//(1) Start the row.
		$this->add ( "\n\n\t<TR>\n" );
		
		//(2) Loop the cell array and add each cell in turn.
		$cell_count = count ( $cell_array );
		for ( $i = 0; $i < $cell_count; $i++ )
			{
			$this->add_cell ( $cell_array[$i] );
			}
			
		//(3) Finish the row.
		$this->add ( "\n\n\t</TR>" );
		
	// Reset our col pointer back to zero.
	$this->reset_col_pointer ();
	
	// Set our row counter to the next row.
	$this->row++;
	
	
	
	// Check our row attribute properties to see if, for each one, they should be reset, expired, or left alone.
		if ( $this->row_attribute_name )
	
			{
			$row_attribute_count = count ( $this->row_attribute_name );
			
			for ( $i = 0; $i < $row_attribute_count; $i++ )
				{
				$this->row_attribute_index[$i]++;
				$last_index 		= count ( $this->row_attribute_value[$i] );
				$current_index 	= $this->row_attribute_index[$i];
				$at_last_index 		= $current_index == $last_index;
				if ( $at_last_index && --$this->row_attribute_loop_buffer[$i] )
					{
					$this->row_attribute_index[$i] = 0;
					}
				}
			}
}


/*-----------------------------------------------------------------*/


function return_table()

	// Return the table to the caller.
	
	{
	return $this->table;
	}


}



?>
Return current item: Bucci's Table Class