tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file grid.class.php

Documentation is available at grid.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Grid
  8.  *
  9.  * @package  tangra_lib
  10.  * @subpackage grids
  11.  */
  12.  
  13.  
  14. /**
  15.  *
  16.  */
  17. require_once(TANGRA_MAIN_DIR.'web_site/web_context.class.php');
  18. /**
  19.  *
  20.  */
  21. require_once(TANGRA_MAIN_DIR.'exceptions/te_not_an_array.class.php');
  22. /**
  23.  *
  24.  */
  25. require_once(TANGRA_MAIN_DIR.'exceptions/te_grid_column_count_not_matched.class.php');
  26. /**
  27.  *
  28.  */
  29. require_once(TANGRA_MAIN_DIR.'exceptions/te_grid_no_such_row.class.php');
  30.  
  31.  
  32. /**
  33.  * Grids are used to present data in table format
  34.  * Grid is base class for all grids.
  35.  *
  36.  * @package  tangra_lib
  37.  * @subpackage grids
  38.  */
  39. abstract class Grid extends Tangra_Class {
  40.     /**
  41.      * Data rows
  42.      *
  43.      * @var array 
  44.      * @internal
  45.      */
  46.     protected $rows = array();
  47.  
  48.     /**
  49.      * Name of the grid
  50.      *
  51.      * @var string 
  52.      * @internal
  53.      */
  54.     private $name;
  55.  
  56.     /**
  57.      * How many coluns will grid have
  58.      *
  59.      * @var integer 
  60.      */
  61.     private $columns_number = 0;
  62.  
  63.  
  64.     /**
  65.      * Constructor
  66.      *
  67.      * @param string $name Name of the grid
  68.      * @param integer $columns_number How many coluns will grid have
  69.      */
  70.     function __construct($name$columns_number{
  71.         $this->set_name($name);
  72.         $this->set_columns_number($columns_number);
  73.     }
  74.  
  75.  
  76.     /**
  77.      * Sets grid name
  78.      *
  79.      * @param string $name 
  80.      * @throws Tangra_Exception
  81.      */
  82.     public function set_name($name{
  83.         if (ereg("^[a-z]{1}[a-z0-9_]{1,50}$"$name)) {
  84.             $this->name = strtolower($name);
  85.         else {
  86.             throw new Tangra_Exception('Invalid grid name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.');
  87.         }
  88.     }
  89.  
  90.  
  91.     /**
  92.      * Returns grid name
  93.      *
  94.      * @return string 
  95.      */
  96.     public function get_name({
  97.         return $this->name;
  98.     }
  99.  
  100.  
  101.     /**
  102.      * Sets how many columns grid will have
  103.      *
  104.      * @param integer $number 
  105.      */
  106.     public function set_columns_number($number{
  107.         tangra_if_not_int_throw_e($number);
  108.         $this->columns_number = $number;
  109.     }
  110.  
  111.  
  112.     /**
  113.      * Returns how many columns grid has
  114.      *
  115.      * @return integer 
  116.      */
  117.     public function get_columns_number({
  118.         return $this->columns_number;
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Adds new row
  124.      *
  125.      * @param array $row 
  126.      * @throws TE_Grid_Column_Count_Not_Matched, TE_Not_An_Array
  127.      */
  128.     public function add_row($row{
  129.         if (is_array($row)) {
  130.             if (count($row== $this->get_columns_number()) {
  131.                 array_push($this->rows$row);
  132.             else {
  133.                 throw new TE_Grid_Column_Count_Not_Matched('Columns in data: '.count($row).', Columns in grid: '.$this->get_columns_number());
  134.             }
  135.         else {
  136.             throw new TE_Not_An_Array($row);
  137.         }
  138.     }
  139.  
  140.  
  141.     /**
  142.      * Removes a row
  143.      *
  144.      * @param integer $row_num Row index
  145.      */
  146.     public function remove_row($row_num{
  147.         tangra_if_not_int_throw_e($row_num);
  148.         if (array_key_exists($row_num$this->rows)) {
  149.             unset($this->rows[$row_num]);
  150.             $this->rows = array_values($this->rows);
  151.         else {
  152.             $this->ask_for_forgivenes(new TE_Grid_No_Such_Row($row_num));
  153.         }
  154.     }
  155.  
  156.  
  157.     /**
  158.      * Sets all rows at once
  159.      *
  160.      * @param array $rows 
  161.      * @throws TE_Not_An_Array
  162.      */
  163.     public function set_rows(&$rows{
  164.         if (is_array($rows)) {
  165.             foreach($rows as $key => $row{
  166.                 if (is_array($row) ) {
  167.                     $this->add_row($row);
  168.                 else {
  169.                     throw new TE_Not_An_Array("Row: $key is not an array. Value = ".$row);
  170.                 }
  171.             }
  172.         else {
  173.             throw new TE_Not_An_Array($row);
  174.         }
  175.     }
  176.  
  177.  
  178.     /**
  179.      * Removes all rows
  180.      *
  181.      */
  182.     public function remove_all_rows({
  183.         $this->rows = array();
  184.     }
  185.  
  186.  
  187.     /**
  188.      * Returns reference to rows
  189.      *
  190.      * @return unknown 
  191.      */
  192.     public function &get_rows({
  193.         return $this->rows;
  194.     }
  195. }