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

Source for file sp_grid_wlc.class.php

Documentation is available at sp_grid_wlc.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class SP_Grid_WLC
  8.  *
  9.  * @package  tangra_lib
  10.  * @subpackage grids
  11.  */
  12.  
  13.  
  14. /**
  15.  *
  16.  */
  17. require_once(TANGRA_MAIN_DIR.'grids/static_paginated_grid.class.php');
  18. /**
  19.  *
  20.  */
  21. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  22. /**
  23.  *
  24.  */
  25. require_once(TANGRA_MAIN_DIR.'exceptions/te_grid_invalid_number_values_for_link_cols.class.php');
  26.  
  27.  
  28.  
  29. /**
  30.  * Static Grid with link columns and paginator
  31.  *
  32.  * @package  tangra_lib
  33.  * @subpackage grids
  34.  */
  35.     /**
  36.      * Link columns
  37.      *
  38.      * @var array 
  39.      * @internal
  40.      */
  41.     private $link_cols = array();
  42.  
  43.     /**
  44.      * Link columns capture values
  45.      *
  46.      * @var unknown_type 
  47.      * @internal
  48.      */
  49.     private $link_cols_capture_values_rows = array();
  50.  
  51.  
  52.     /**
  53.      * Adds new column
  54.      *
  55.      * Adds new linl column to the grid with name <var>$name</var>,
  56.      * href link pointing to <var>$target</var> and capture <var>$capture</var>.
  57.      * Generated link will look like: <a href="$target?$capture=$link_column_capture">Something</a>
  58.      *
  59.      * @param string $name Column name
  60.      * @param string $target_page Target page
  61.      * @param string $capture Label for the capture
  62.      */
  63.     public function add_link_col($name$target_page$capture{
  64.         if (!array_key_exists($name$this->link_cols)) {
  65.             $this->link_cols[$name]['target_page'$target_page;
  66.             $this->link_cols[$name]['capture'$capture;
  67.         else {
  68.             throw new TE_Key_Already_Exists($name);
  69.         }
  70.     }
  71.  
  72.  
  73.     /**
  74.      * Returns array containing web events that grid may generate
  75.      *
  76.      * @return array 
  77.      */
  78.     public function get_web_events({
  79.         $ret parent::get_web_events();
  80.  
  81.         foreach($this->link_cols as $link_col{
  82.             //matches only event targeted at current page
  83.             if ($link_col['target_page'== $this->get_web_page()) {
  84.                 $ret [new Web_Event_Simple_Int($link_col['capture']Tangra_Parameter_Method::GET$link_col['capture']);
  85.             }
  86.         }
  87.  
  88.         return $ret;
  89.     }
  90.  
  91.  
  92.     /**
  93.      * Returns TPLE exports for the grid
  94.      *
  95.      * @return TPLE_Exports 
  96.      */
  97.     public function get_tple_exports({
  98.         $exports parent::get_tple_exports();
  99.  
  100.         $exports->add_pair($this->get_name().'_prows_count'count($this->prepare_rows()));
  101.  
  102.         return $exports;
  103.     }
  104.  
  105.  
  106.     /**
  107.      * Returns HTML for the grid
  108.      *
  109.      * @param string $path Path to template file
  110.      * @param string $language Navive language code
  111.      * @return string 
  112.      */
  113.     public function get_static_html($path$language{
  114.         $tple new Tple();
  115.         $tple->set_delimiters('{@''@}');
  116.         $tple->assign('cols_number'$this->get_columns_number());
  117.         $tple->assign('grid_name'$this->get_name());
  118.         $tple->assign('link_cols'$this->link_cols);
  119.         $tple->assign('language'$language);
  120.         $html $tple->fetch($path.'/sp_grid_wlc.tpl');
  121.  
  122.         return $html;
  123.     }
  124.  
  125.  
  126.     /**
  127.      * Adds link columns capture values
  128.      *
  129.      * Link columns capture values are values that will be added at the end of the link.
  130.      * Example:
  131.      * <a href="somepage.php?obj=5">
  132.      * somepage.php is <var>$target</var> @see SP_Grid_WLC::add_link_col()
  133.      * obj is the capture label
  134.      * 5 is the link column capture value - normally this is the id of the record but you
  135.      * may use different column @see SP_Grid_WLC::set_rows_and_link_cols_rows_from_raw_rows
  136.      *
  137.      * @param arary $row 
  138.      */
  139.     public function add_link_cols_capture_values_row($row{
  140.         $have count($row);
  141.         $must_have count($this->link_cols);
  142.         if ($have == $must_have{
  143.             $this->link_cols_capture_values_rows[$row;
  144.         else {
  145.             throw new TE_Grid_Invalid_Number_Values_For_Link_Cols($must_have$have);
  146.         }
  147.     }
  148.  
  149.  
  150.     /**
  151.      * Sets all link columns capture values at once
  152.      *
  153.      * @param array $rows 
  154.      */
  155.     public function set_link_cols_capture_values_rows($rows{
  156.         foreach($rows as $row{
  157.             $this->add_link_cols_capture_values_row($row);
  158.         }
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Prepares row to be used in a convinient way by this class
  164.      *
  165.      * @return string 
  166.      * @internal
  167.      * @throws TE_Key_Not_Exists
  168.      */
  169.     protected function prepare_rows({
  170.         $ret array();
  171.         foreach($this->rows as $row_key => $row{
  172.             if (array_key_exists($row_key$this->link_cols_capture_values_rows)) {
  173.                 $ret[$row_key]['cols'$row;
  174.                 $ret[$row_key]['lc_vals'$this->link_cols_capture_values_rows[$row_key];
  175.             else {
  176.                 throw new TE_Key_Not_Exists($row_key);
  177.             }
  178.         }
  179.  
  180.         return $ret;
  181.     }
  182.  
  183.  
  184.  
  185.     /**
  186.      * Sets all data rows and link column capture rows using "raw rows"
  187.      *
  188.      * @param array $raw_rows Array of rows as returned by the SELECT form DB
  189.      * @param integer $use_col_num Which column to be used for value of link columns capture value
  190.      * @param boolean $skip_used_col If true used column will be  skipped and will not exists in final array
  191.      */
  192.     public function set_rows_and_link_cols_rows_from_raw_rows($raw_rows$use_col_num 0$skip_used_col false{
  193.         tangra_if_not_int_throw_e($use_col_num);
  194.  
  195.         foreach($raw_rows as $row{
  196.             $c 0;
  197.             for($i 0$i <= (count($row1);$i++{
  198.                 if ($use_col_num == $i{
  199.  
  200.                     //preparing one row of link_cols_rows
  201.                     $link_cols_row array();
  202.                     foreach($this->link_cols as $key => $link_col{
  203.                         $link_cols_row[$key$row[$use_col_num];
  204.                     }
  205.                     $this->add_link_cols_capture_values_row($link_cols_row);
  206.  
  207.                     //setting row
  208.                     if ($skip_used_col{
  209.                         unset($row[$use_col_num]);
  210.                         $row $this->shift_down($row);
  211.                         $this->add_row($row);
  212.                     else {
  213.                         $this->add_row($row);
  214.                     }
  215.                 }
  216.             }
  217.         }
  218.     }
  219.  
  220.  
  221.     /**
  222.      * Sets all rows at once
  223.      *
  224.      * Shorhand for setting all rows with using column 0 and skipping used column
  225.      * This function is convinient when you select from database like:
  226.      * "Select obj.id, obj.name, obj.description where...".
  227.      *
  228.      * @param array $rows 
  229.      * @see SP_Grid_WLC::set_rows_and_link_cols_rows_from_raw_rows
  230.      */
  231.     public function set_rows(&$rows{
  232.         $this->set_rows_and_link_cols_rows_from_raw_rows($rows0true);
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Shifts down the array in order to have continiuos array index starting with 0
  238.      *
  239.      * @param array $row 
  240.      * @return array 
  241.      * @internal
  242.      */
  243.     private function shift_down($row{
  244.         $ret array();
  245.  
  246.         if ($row{
  247.             $c 0;
  248.             foreach($row as $item{
  249.                 $ret[$c$item;
  250.                 $c++;
  251.             }
  252.         }
  253.  
  254.         return $ret;
  255.     }
  256.  
  257. }