<?php
/*
*
* TableGear for PHP (An Intuitive Data Table Management Class)
*
* Version: 1.5.2
* Documentation: AndrewPlummer.com (http://www.andrewplummer.com/code/tablegear/)
* License: MIT-style License
*
* Copyright (c) 2009 Andrew Plummer
*
*
*/
$tgTableID = 0;
class TableGear
{
var $processHTTP = true; // Process data submitted by HTTP
var $indent = 0; // HTML indent base
var $autoHeaders = true; // Automatically get the headers from field names
var $readableHeaders = true; // Creates readable headers from camelCase and underscore field names.
var $noDataMessage = "- No Data -"; // The message to display when no data is available
var $newRowLabel = "New Row";
var $addNewRows = true;
var $_curIndent = 0; // For HTML output
var $_hasTags = false; // For HTML output
function TableGear($options)
{
global $tgTableID;
if($options["editable"]) $this->form = array("url" => $_SERVER["REQUEST_URI"], "method" => "post", "submit" => "Update");
$tgTableID++;
$this->table = array("id" => "tgTable");
$this->headers = array("EDIT" => "Edit Row", "DELETE" => "Delete Row");
$this->_setOptions($options);
if($tgTableID > 1) $this->table["id"] .= $tgTableID;
if($this->database) $this->connect();
if($this->processHTTP) $this->_checkSubmit();
if(!$this->database["noAutoQuery"]) $this->fetchDataArray();
$this->_checkColumnShift();
}
/* Functions for working with the database */
function connect()
{
$db = $this->database;
if(!$db["database"] || !$db["username"]) trigger_error("Database info required!", E_USER_ERROR);
if(!$db["table"]) trigger_error("Database table must be specified.", E_USER_ERROR);
$server = ($db["server"]) ? $db["server"] : "localhost";
$this->connection = mysql_connect($server, $db["username"], $db["password"]);
mysql_select_db($db["database"], $this->connection);
}
function query($query)
{
//echo "QUERY: $query<br/>"; // Leave for debug
if(!$this->connection) trigger_error("No database connection established!", E_USER_ERROR);
$result = mysql_query($query, $this->connection);
$this->_affectedRows = mysql_affected_rows($this->connection);
if(!$result){
$this->database["error"] = mysql_error();
return false;
} elseif($result && $result != 1){
$data = array();
while($row = mysql_fetch_assoc($result)) array_push($data, $row);
return $data;
} else {
return true;
}
}
function fetchDataArray($query = null)
{
if(!$query && !$this->database["table"]) return;
$table = $this->database["table"];
$key = $this->_getPrimaryKey();
if(!$query){
if(!$this->database["table"]) return;
if($_GET["sort"]){
$sort = $_GET["sort"];
$desc = $_GET["desc"] ? " DESC" : " ASC";
} elseif($this->database["sort"]){
list($sort, $params) = $this->_getParams($this->database["sort"]);
$desc = ($params == "desc") ? " DESC" : " ASC";
} else {
$sort = $key;
}
$columns = $this->database["columns"] ? implode(",", $this->database["columns"]) : "*";
$query = "SELECT SQL_CALC_FOUND_ROWS $columns FROM $table ORDER BY $sort$desc";
}
if($this->pagination){
$page = $this->pagination["currentPage"] = ($_GET["page"]) ? $_GET["page"] : 1;
if(!$this->pagination["perPage"]) $this->pagination["perPage"] = 10;
$min = ($page - 1) * $this->pagination["perPage"];
$perPage = $this->pagination["perPage"];
$query .= " LIMIT $min, $perPage";
}
$data = $this->query($query);
if($this->pagination){
$result = mysql_query("SELECT FOUND_ROWS() AS total");
$row = mysql_fetch_assoc($result);
$this->totalRows = $row["total"];
$this->pagination["totalPages"] = ceil($this->totalRows / $this->pagination["perPage"]);
}
if(!$data) return;
$this->data = array();
foreach($data as $row){
$entry = array();
$entry["key"] = $row[$key];
unset($row[$key]);
$entry["data"] = $row;
array_push($this->data, $entry);
}
}
function _getPrimaryKey()
{
if($this->database["key"]) return $this->database["key"];
$table = $this->database["table"];
$columns = $this->query("SHOW KEYS FROM $table WHERE `Key_name`='PRIMARY'");
$key = $columns[0]["Column_name"];
if(!$key) trigger_error("Primary key is required for table $table.", E_USER_ERROR);
$this->database["key"] = $key;
if($this->database["columns"] && !array_search($key, $this->database["columns"])) array_push($this->database["columns"], $key);
return $key;
}
/* Functions for working with the data */
function injectColumn($array, $position = "first", $fieldName = null)
{
if(!$this->data) return;
foreach($this->data as $rowIndex => $row){
$data = $row["data"];
$column = count($data) + 1;
if($fieldName) $data[$fieldName] = $array[$rowIndex];
else $data[$column] = $array[$rowIndex];
$this->data[$rowIndex]["data"] = $data;
}
$col = $fieldName ? $fieldName : $column;
$this->shiftColumn($col, $position);
}
function shiftColumn($col, $pos)
{
if(is_numeric($col)){
$keys = array_keys($this->data[0]["data"]);
$col = $keys[$col-1];
}
if(!is_numeric($pos)) list($pos, $params) = $this->_getParams($pos);
foreach($this->data as $rowIndex => $row){
$new = array();
$currentColumn = 1;
if($pos == "first"){
$new[$col] = $row["data"][$col];
$currentColumn++;
}
foreach($row["data"] as $field => $data){
if($pos == "before" && $params == $field){
$new[$col] = $row["data"][$col];
$currentColumn++;
}
if($pos == $currentColumn){
$new[$col] = $row["data"][$col];
$currentColumn++;
}
if($field != $col){
$new[$field] = $data;
$currentColumn++;
}
if($pos == "after" && $params == $field){
$new[$col] = $row["data"][$col];
$currentColumn++;
}
}
if($pos == "last"){
$new[$col] = $row["data"][$col];
}
$this->data[$rowIndex]["data"] = $new;
}
}
function _fetchHeaders()
{
$headers = array();
if($this->form && $this->editable) array_push($headers, array("field" => "EDIT", "html" => $this->headers["EDIT"], "attrib" => array("class" => $this->_checkColumnClass("EDIT"))));
if(count($this->data) > 0){
$firstRow = reset($this->data);
$column = 1;
foreach($firstRow["data"] as $field => $data){
$sortable = $this->_testForOption("sortable", $field, $column) ? true : false;
$sortType = $this->_getSortType($field);
$class = $this->_addClass("sortable", null, $sortable);
$class = $this->_addClass($sortType, $class);
if($this->headers[$field]) $userHeader = $this->headers[$field];
elseif($this->headers[$column]) $userHeader = $this->headers[$column];
else $userHeader = null;
$html = null;
if(is_array($userHeader)){
$html = $userHeader["html"];
$class = $this->_addClass($userHeader["class"], $class);
} elseif(is_string($userHeader)){
$html = $userHeader;
}
if(!$html && $this->autoHeaders) $html = $this->_autoFormatHeader($field);
if($this->pagination && $this->pagination["totalPages"] != 1){
$desc = ($_GET["sort"] == $field && !$_GET["desc"]) ? "true" : null;
$href = $this->_modifyURIParams(array("sort" => $field, "desc" => $desc, "page" => null));
if($_GET["sort"] == $field){
$carat = array("tag" => "span", "attrib" => array("class" => "carat"));
if($_GET["desc"]){
$desc = null;
$carat["html"] = "▼";
} else {
$desc = "true";
$carat["html"] = "▲";
}
$html = array(array("tag" => "span", "html" => $html), $carat);
}
$link = array("tag" => "a", "html" => $html, "attrib" => array("href" => $href));
$header = array("html" => $link);
} else {
$header = array("html" => $html, "attrib"