<?php
/**
* NOTE THIS IS THE DEBUG VERSION. THERE IS A VERSION WITHOUT COMMENTS
* ALSO PRESENT IN THIS FOLDER CALLED DataGrid.php AS OPPOSED TO
* DataGrid-debug.php
*
* @author Angelo Rodrigues
* @version 1.7b
* @copyright
* @link http://wheremy.feethavebeen.com/2010/07/datagrid-control-v2/
* @package Controls
*
*
* @example also available at http://wheremy.feethavebeen.com/2010/07/datagrid-control-v2/
*
* $DataGrid = new DataGrid();
* $data = array(
* array('id'=>0,'f_name'=>'Angelo','l_name'=>'Rodrigues'),
* array('id'=>1,'f_name'=>'Darth','l_name'=>'Vader'),
* array('id'=>2,'f_name'=>'Luke','l_name'=>'Skywalker'),
* array('id'=>3,'f_name'=>'Han','l_name'=>'Solo'),
* );
*
* $DataGrid->configure(
* array(
* 'primary_key' => 'id',
* 'sortable' => true,
* 'striped' => true,
* 'even_row' => null,
* 'odd_row' => 'striped'
* )
* );
*
* // This not only defines display fields, but the order as well
* $DataGrid->setDisplayFields(array('f_name'=>'First Name','l_name'=>'Last Name'));
*
* -- Needs to happen in head section!
* $headers = DataGrid->Headers();
* echo $headers['css'];
* echo $headers['js']['object'];
* echo $headers['js']['function'];
*
*
* $DataGrid->source($data);
*
*/
class DataGrid {
private $config_options; // Set at initialization and by calling configure()
private $data; // Multi-dimensional array
private $tableHeader; // Display name of field and order, optional
/**
*
* @param string $css -path to css file
* @param bool $jquery -since jQuery is supported, true uses jQuery for manips
* @param bool $xhtml -in case you want to use xhtml
* @return array
*
* Called anywhere, but you need to explicitly output the results.
* This is cause some people keep JS at the bottom and others at the top,
* so you can do what you want.
*
* @example
* $h = DataGrid::headers('./dg.css',true);
* echo $h['css'];
* echo $h['js']['object']; -MUST appear ABOVE function
* echo $j['js']['function'];
*/
public function Headers($dir = './', $css = 2, $jquery = false ,$xhtml = false) {
$dir = ($dir == '')?'./':$dir;
$css = DataGrid::CSS($dir,$css,$xhtml);
$js = $this->JS($dir,$jquery);
return array('css'=>$css,'js'=>$js);
}
/**
* @param string $path_to_file
* @param bool $xhtml
* @return string
*
* Optionally, you can just call the CSS
*/
public function CSS($path_to_file,$xhtml) {
$link = '<link rel="stylesheet" type="text/css" href="'.$path_to_file.'"';
if($xhtml) {
$link .= ' />';
}
else {
$link .= '>';
}
return $link;
}
/**
* @param bool $jquery
* @return array
*
* load just the JS if you define the CSS somewhere else
*/
public function JS($jquery = false) {
if($jquery) {
$file = 'dg-jquery.js';
}
else {
$file = 'dg-gen.js';
}
$link = '<script type="text/javascript" src="'.$file.'"></script>';
$tmp = '<script type="text/javascript">';
$e = array('table_header'=>$this->tableHeader,'config_options'=>$this->config_options);
$tmp = 'var DataGrid = '.json_encode($e).';';
$tmp .= '</script>';
return array('object'=>$tmp,'function'=>$link);
}
/**
* Constructor, just sets up the defaults. If your project
* is going to use the same options don't edit the defaults.
*
* Instead utilize setDefaultOptions() and
*/
public function __construct() {
$this->config_options = array(
'primary_key' => 'id',
'selectable' => false,
'select_class' => null,
'editable' => false,
'ajax_url' => null,
'striped' => false,
'even_row' => null,
'odd_row' => null,
);
$this->configure($this->setDefaultOptions());
$this->data = null;
$this->tableHeader = null;
}
/**
* This is provided to set up your project defaults
* without having to set them every time. Instead of
* modifying __construct() (which could update and
* eliminate your changes) Use the function below as
* outlined.
*
* You can still change individual options further via
* $this->configure() externally.
*
* @example
* private function setDefaultOptions() {
* return array(
* 'selectable' => 'row',
* 'editable' => true,
* 'even_row' => null,
* 'odd_row' => 'zebra',
* 'striped' => true,
* );
* }
*/
private function setDefaultOptions() {
}
/**
* @param array $array
*
* Sets the configuration options. If they aren't set
* by default, it will ignore them.
*/
public function configure($array) {
if(is_array($array)) {
foreach($array as $key=>$val) {
if(array_key_exists($key,$this->config_options)) {
$this->config_options[$key] = str_to_lower($val);
}
}
}
}
/**
* @param string $key
* @return any
*
* Will return the request key, or if not found (or empty)
* it will return the entire config_options.
*/
public function option($key = '') {
if(array_key_exists(str_to_lower($key),$this->config_options)) {
return $this->config_options[str_to_lower($key)];
}
return $this->config_options;
}
/**
* @param array $data
* @param string $table optional
*
* Sets the data source
*/
public function source($data) {
$this->data = $data;
}
/**
* @param array $array
*
* Defines the fields you want to display AND their order
*
* @example
* setDisplayFields(array(
* 'l_name'=>'Last Name',
* 'f_name'=>'First Name',
* ));
*
* Will display ONLY the following in this order, regardless
* of other fields
* +-----------+------------+
* | Last Name | First Name |
* +-----------+------------+
*/
public function setDisplayFields($array) {
$i = 0;
foreach($array as $field=>$display) {
$this->tableHeader[$field]['display'] = $display;
$this->tableHeader[$field]['index'] = $i;
$i++;
}
}
/**
* @param bool $return
* @return string optional
*
* Builds the Grid view and either displays it, or returns
* the data as a string.
*/
public function build($return = false) {
// Incase you didn't set it
if($this->tableHeader == null) {
foreach(array_keys($this->data[0]) as $index=>$display) {
$header[$display] = $display;
}
$this->setDisplayFields($header);
}
$tmp = '<table cellspacing="0" class="dg-main">';
$tmp .= buildHeader();
$tmp .= '<tbody>';
foreach($this->data as $row_num => $data) {
$tmp .= $this->buildRow($data,$row_num);
}
$tmp .= '</tbody></table>';
if($return) {
return $tmp;
}
echo $tmp;
}
/**
* @return string
*
* Builds the header row based on setDisplayFields();
*/
private function buildHeader() {
$tmp .= '<tr><thead>';
foreach($this->tableHeader as $field=>$value) {
$tmp .= '<th>'.$value['display'].'</th>';
}
$tmp .= '</thead></tr>';
return $tmp;
}
/**
* @param array $data
* @param int $row_num
* @return string
*
* Builds a row of data based on $data and $tableHeader
*/
private function buildRow($data,$row_num) {
$tmp = '<tr>';
$x = array(); // Holder to ensure we conform to your field layout
foreach($data as $field=>$value) {
if(array_key_exists($field,$this->tableHeader)) {
$x[$this->tableHeader[$field]['index']] = '<td class="dg-row '.$this->stripe($row_num).'" id="dg-'.$this->option('primary_key').':'.$this->field.'-'.$row_num.'">'.$value.'</td>';
}
$tmp .= implode('',$x);
}
$tmp .= '</tr>';
return $tmp;
}
/**
* @param int $row_num
*
* Just stripes the table if necessary
*/
private function stripe($row_num) {
if($this->option('striped')) {
if($row_num%2 == 0) {
return $this->option('even_row');
}
return $this->option('odd_row');
}
}
}
?>