Share Pastebin
Guest
Public paste!

DataGrid V2

By: a guest | Jul 15th, 2010 | Syntax: PHP | Size: 8.34 KB | Hits: 143 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2. /**
  3.  * NOTE THIS IS THE DEBUG VERSION. THERE IS A VERSION WITHOUT COMMENTS
  4.  * ALSO PRESENT IN THIS FOLDER CALLED DataGrid.php AS OPPOSED TO
  5.  * DataGrid-debug.php
  6.  *
  7.  * @author      Angelo Rodrigues
  8.  * @version     1.7b
  9.  * @copyright  
  10.  * @link        http://wheremy.feethavebeen.com/2010/07/datagrid-control-v2/
  11.  * @package     Controls
  12.  *
  13.  *
  14.  * @example     also available at http://wheremy.feethavebeen.com/2010/07/datagrid-control-v2/    
  15.  *
  16.  * $DataGrid = new DataGrid();
  17.  * $data = array(
  18.  *   array('id'=>0,'f_name'=>'Angelo','l_name'=>'Rodrigues'),
  19.  *   array('id'=>1,'f_name'=>'Darth','l_name'=>'Vader'),
  20.  *   array('id'=>2,'f_name'=>'Luke','l_name'=>'Skywalker'),
  21.  *   array('id'=>3,'f_name'=>'Han','l_name'=>'Solo'),
  22.  * );
  23.  *
  24.  * $DataGrid->configure(
  25.  *   array(
  26.  *     'primary_key' => 'id',
  27.  *     'sortable' => true,
  28.  *     'striped' => true,
  29.  *     'even_row' => null,
  30.  *     'odd_row' => 'striped'
  31.  *     )
  32.  * );
  33.  *
  34.  * // This not only defines display fields, but the order as well
  35.  * $DataGrid->setDisplayFields(array('f_name'=>'First Name','l_name'=>'Last Name'));
  36.  *
  37.  * -- Needs to happen in head section!
  38.  * $headers = DataGrid->Headers();
  39.  * echo $headers['css'];
  40.  * echo $headers['js']['object'];
  41.  * echo $headers['js']['function'];
  42.  *
  43.  *
  44.  * $DataGrid->source($data);
  45.  *
  46.  */
  47. class DataGrid {
  48.  
  49.   private $config_options;   // Set at initialization and by calling configure()
  50.   private $data;             // Multi-dimensional array
  51.   private $tableHeader;      // Display name of field and order, optional
  52.  
  53.   /**
  54.    *
  55.    * @param string $css  -path to css file
  56.    * @param bool $jquery -since jQuery is supported, true uses jQuery for manips
  57.    * @param bool $xhtml  -in case you want to use xhtml
  58.    * @return array
  59.    *
  60.    * Called anywhere, but you need to explicitly output the results.
  61.    * This is cause some people keep JS at the bottom and others at the top,
  62.    * so you can do what you want.
  63.    *
  64.    * @example
  65.    * $h = DataGrid::headers('./dg.css',true);
  66.    * echo $h['css'];
  67.    * echo $h['js']['object'];    -MUST appear ABOVE function
  68.    * echo $j['js']['function'];
  69.    */
  70.   public function Headers($dir = './', $css = 2, $jquery = false ,$xhtml = false) {
  71.     $dir = ($dir == '')?'./':$dir;
  72.     $css = DataGrid::CSS($dir,$css,$xhtml);
  73.     $js = $this->JS($dir,$jquery);
  74.     return array('css'=>$css,'js'=>$js);
  75.   }
  76.  
  77.   /**
  78.    * @param string $path_to_file
  79.    * @param bool $xhtml
  80.    * @return string
  81.    *
  82.    * Optionally, you can just call the CSS
  83.    */
  84.   public function CSS($path_to_file,$xhtml) {
  85.     $link = '<link rel="stylesheet" type="text/css" href="'.$path_to_file.'"';
  86.     if($xhtml) {
  87.       $link .= ' />';
  88.     }
  89.     else {
  90.       $link .= '>';
  91.     }
  92.     return $link;
  93.   }
  94.  
  95.   /**
  96.    * @param bool $jquery
  97.    * @return array
  98.    *
  99.    * load just the JS if you define the CSS somewhere else
  100.    */
  101.   public function JS($jquery = false) {
  102.     if($jquery) {
  103.       $file = 'dg-jquery.js';
  104.     }
  105.     else {
  106.       $file = 'dg-gen.js';
  107.     }
  108.     $link = '<script type="text/javascript" src="'.$file.'"></script>';
  109.     $tmp = '<script type="text/javascript">';
  110.     $e = array('table_header'=>$this->tableHeader,'config_options'=>$this->config_options);
  111.     $tmp = 'var DataGrid = '.json_encode($e).';';
  112.     $tmp .= '</script>';
  113.    
  114.     return array('object'=>$tmp,'function'=>$link);
  115.   }
  116.  
  117.   /**
  118.    * Constructor, just sets up the defaults. If your project
  119.    * is going to use the same options don't edit the defaults.
  120.    *
  121.    * Instead utilize setDefaultOptions() and
  122.    */
  123. public function __construct() {
  124.     $this->config_options = array(
  125.       'primary_key' => 'id',      
  126.       'selectable' => false,        
  127.       'select_class' => null,    
  128.       'editable' => false,  
  129.       'ajax_url' => null,  
  130.       'striped' => false,      
  131.       'even_row' => null,
  132.       'odd_row' => null,    
  133.     );    
  134.    
  135.     $this->configure($this->setDefaultOptions());
  136.     $this->data = null;
  137.     $this->tableHeader = null;
  138.   }
  139.  
  140.   /**
  141.    * This is provided to set up your project defaults
  142.    * without having to set them every time. Instead of
  143.    * modifying __construct() (which could update and
  144.    * eliminate your changes) Use the function below as
  145.    * outlined.
  146.    *
  147.    * You can still change individual options further via
  148.    * $this->configure() externally.
  149.    *
  150.    * @example
  151.    * private function setDefaultOptions() {
  152.    *  return array(
  153.    *    'selectable' => 'row',
  154.    *    'editable' => true,
  155.    *    'even_row' => null,
  156.    *    'odd_row' => 'zebra',
  157.    *    'striped' => true,
  158.    *  );
  159.    * }
  160.    */
  161.   private function setDefaultOptions() {
  162.    
  163.   }
  164.  
  165.   /**
  166.    * @param array $array
  167.    *
  168.    * Sets the configuration options. If they aren't set
  169.    * by default, it will ignore them.
  170.    */
  171.   public function configure($array) {
  172.     if(is_array($array)) {
  173.       foreach($array as $key=>$val) {
  174.         if(array_key_exists($key,$this->config_options)) {
  175.           $this->config_options[$key] = str_to_lower($val);
  176.         }
  177.       }
  178.     }
  179.   }
  180.  
  181.   /**
  182.    * @param string $key
  183.    * @return any
  184.    *
  185.    * Will return the request key, or if not found (or empty)
  186.    * it will return the entire config_options.
  187.    */
  188.   public function option($key = '') {
  189.     if(array_key_exists(str_to_lower($key),$this->config_options)) {
  190.       return $this->config_options[str_to_lower($key)];
  191.     }
  192.     return $this->config_options;
  193.   }
  194.  
  195.   /**
  196.    * @param array $data
  197.    * @param string $table optional
  198.    *
  199.    * Sets the data source
  200.    */
  201.   public function source($data) {
  202.     $this->data = $data;
  203.   }
  204.  
  205.   /**
  206.    * @param array $array
  207.    *
  208.    * Defines the fields you want to display AND their order
  209.    *
  210.    * @example
  211.    * setDisplayFields(array(
  212.    *   'l_name'=>'Last Name',
  213.    *   'f_name'=>'First Name',
  214.    * ));
  215.    *
  216.    * Will display ONLY the following in this order, regardless
  217.    * of other fields
  218.    * +-----------+------------+
  219.    * | Last Name | First Name |
  220.    * +-----------+------------+
  221.    */
  222.   public function setDisplayFields($array) {
  223.     $i = 0;
  224.     foreach($array as $field=>$display) {
  225.       $this->tableHeader[$field]['display'] = $display;
  226.       $this->tableHeader[$field]['index'] = $i;
  227.       $i++;
  228.     }
  229.   }
  230.  
  231.   /**
  232.    * @param bool $return
  233.    * @return string optional
  234.    *
  235.    * Builds the Grid view and either displays it, or returns
  236.    * the data as a string.
  237.    */
  238.   public function build($return = false) {
  239.     // Incase you didn't set it
  240.     if($this->tableHeader == null) {
  241.       foreach(array_keys($this->data[0]) as $index=>$display) {
  242.         $header[$display] = $display;
  243.       }
  244.       $this->setDisplayFields($header);
  245.     }
  246.     $tmp = '<table cellspacing="0" class="dg-main">';
  247.     $tmp .= buildHeader();
  248.     $tmp .= '<tbody>';
  249.     foreach($this->data as $row_num => $data) {
  250.       $tmp .= $this->buildRow($data,$row_num);
  251.     }
  252.     $tmp .= '</tbody></table>';
  253.    
  254.     if($return) {
  255.       return $tmp;
  256.     }
  257.     echo $tmp;
  258.   }
  259.  
  260.   /**
  261.    * @return string
  262.    *
  263.    * Builds the header row based on setDisplayFields();
  264.    */
  265.   private function buildHeader() {
  266.     $tmp .= '<tr><thead>';
  267.     foreach($this->tableHeader as $field=>$value) {
  268.       $tmp .= '<th>'.$value['display'].'</th>';
  269.     }
  270.     $tmp .= '</thead></tr>';
  271.     return $tmp;
  272.   }
  273.  
  274.   /**
  275.    * @param array $data
  276.    * @param int $row_num
  277.    * @return string
  278.    *
  279.    * Builds a row of data based on $data and $tableHeader
  280.    */
  281.   private function buildRow($data,$row_num) {
  282.     $tmp = '<tr>';
  283.     $x = array();     // Holder to ensure we conform to your field layout
  284.     foreach($data as $field=>$value) {
  285.       if(array_key_exists($field,$this->tableHeader)) {
  286.         $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>';
  287.       }
  288.       $tmp .= implode('',$x);
  289.     }
  290.     $tmp .= '</tr>';
  291.     return $tmp;
  292.   }
  293.  
  294.   /**
  295.    * @param int $row_num
  296.    *
  297.    * Just stripes the table if necessary
  298.    */
  299.   private function stripe($row_num) {
  300.     if($this->option('striped')) {
  301.       if($row_num%2 == 0) {
  302.         return $this->option('even_row');
  303.       }
  304.       return $this->option('odd_row');
  305.     }
  306.   }
  307. }
  308. ?>