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

Source for file grid_ctrl.class.php

Documentation is available at grid_ctrl.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class Grid_Ctrl
  9.  *
  10.  * @package  tangra_lib
  11.  * @subpackage grids
  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.'db/db_connection.class.php');
  22. /**
  23.  *
  24.  */
  25. require_once(TANGRA_MAIN_DIR.'db/paginate_db_data.class.php');
  26.  
  27. /**
  28.  *
  29.  */
  30. require_once(TANGRA_MAIN_DIR.'core/tangra_parameter_method.class.php');
  31.  
  32. /**
  33.  *
  34.  */
  35. require_once(TANGRA_MAIN_DIR.'web_site/web_event_simple_int.class.php');
  36.  
  37. /**
  38.  *
  39.  */
  40. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  41.  
  42. /**
  43.  *
  44.  */
  45. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_not_exists.class.php');
  46.  
  47. /**
  48.  * Controller for grids
  49.  *
  50.  * @package  tangra_lib
  51.  * @subpackage grids
  52.  */
  53. abstract class Grid_Ctrl extends Tangra_Class {
  54.     /**
  55.      * Capture for setting page
  56.      *
  57.      */
  58.     const SET_PAGE_CAPTURE = 'page';
  59.  
  60.     /**
  61.      * Grid object
  62.      *
  63.      * @var Grid 
  64.      * @internal
  65.      */
  66.     protected $grid;
  67.  
  68.     /**
  69.      * Paginator object
  70.      *
  71.      * @var Paginate_DB_Data 
  72.      * @internal
  73.      */
  74.     protected $paginate_db_data;
  75.  
  76.     /**
  77.      * Controller name
  78.      *
  79.      * @var unknown_type 
  80.      * @internal
  81.      */
  82.     private $system_name;
  83.  
  84.     /**
  85.      * Vars manager
  86.      *
  87.      * @var Vars_Manager 
  88.      * @internal
  89.      */
  90.     private $vm;
  91.  
  92.     /**
  93.      * Method for passing set page command
  94.      *
  95.      * @var unknown_type 
  96.      * @internal
  97.      */
  98.     private $method;
  99.  
  100.     /**
  101.      * Holds array of additional parameters passed to the controller.
  102.      *
  103.      * @var array 
  104.      */
  105.     protected $additional_parameters = array();
  106.  
  107.  
  108.     /**
  109.      * Holds name of user defined method (if set)
  110.      *
  111.      * @var string 
  112.      */
  113.  
  114.  
  115.     /**
  116.      * Constructor
  117.      *
  118.      * @param string $system_name 
  119.      * @param Vars_Manager $vm Permanent Vars_Manager that will be used. Session of Thread VM
  120.      * @param integer $method Tangra_Parameter_Method::GET or Tangra_Parameter_Method::POST or Tangra_Parameter_Method::PI
  121.      */
  122.     function __construct($system_nameVars_Manager $vm$method Tangra_Parameter_Method::GET{
  123.         $this->set_system_name($system_name);
  124.         $this->_create_grid();
  125.         $this->_create_paginate_db_data();
  126.         $this->set_vm($vm);
  127.  
  128.         $this->set_method($method);
  129.  
  130.         $this->init();
  131.     }
  132.  
  133.  
  134.     /**
  135.      * Process the grid
  136.      *
  137.      * This method will load page passed with <var>$page</var> or will load current_page
  138.      *
  139.      * @param DB_Connection $dbc DB_Connection object
  140.      * @param integer $page Page to be loaded. Default = NULL - will load current page
  141.      */
  142.     public function process(DB_Connection $dbc$page NULL{
  143.         $page $this->detect_page($page);
  144.         $rows $this->retrieve_rows($dbc$page);
  145.  
  146.         $total_pages $this->paginate_db_data->get_total_pages();
  147.         if ($total_pages $page{
  148.             $this->set_page($page);
  149.             if ($total_pages{
  150.                 $page $total_pages 1;
  151.             else {
  152.                 $page 0;
  153.             }
  154.  
  155.             $rows $this->retrieve_rows($dbc$page);
  156.         }
  157.  
  158.         $user_pp_method $this->get_user_defined_post_processing_method();
  159.         if ($user_pp_method{
  160.             $rows $this->$user_pp_method($dbc$rows);
  161.         }
  162.  
  163.         $this->send_data_to_grid($rows$page$this->paginate_db_data->get_total_pages()$this->paginate_db_data->get_total_rows());
  164.     }
  165.  
  166.  
  167.     /**
  168.      * Get TPLE Exports for the grid
  169.      *
  170.      * @return TPLE_Exports 
  171.      */
  172.     public function get_tple_exports({
  173.         $exports $this->grid->get_tple_exports();
  174.         $exports->add_pair($this->get_system_name().'_additional_parameters'$this->get_additional_parameters());
  175.  
  176.         return $exports;
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Returns web event for set page command
  182.      *
  183.      * @return Web_Event_Si 
  184.      */
  185.     public function get_grid_set_page_event({
  186.         $set_page_event new Web_Event_Simple_Int($this->get_system_name().'_grid_'.Grid_Ctrl::SET_PAGE_CAPTURE,
  187.                                                  $this->get_method(),
  188.                                                  $this->get_system_name().'_grid_'.Grid_Ctrl::SET_PAGE_CAPTURE);
  189.  
  190.         return $set_page_event;
  191.     }
  192.  
  193.  
  194.     /**
  195.      * Returns grid's system name
  196.      *
  197.      * @return unknown 
  198.      */
  199.     public function get_system_name({
  200.         return $this->system_name;
  201.     }
  202.  
  203.  
  204.     /**
  205.      * Returns reference to grid object
  206.      *
  207.      * @return Grid 
  208.      */
  209.     public function get_grid({
  210.         return $this->grid;
  211.     }
  212.  
  213.  
  214.     /**
  215.      * Returns reference to Paginate_DB_Data object
  216.      *
  217.      * @return Paginate_DB_Data 
  218.      */
  219.     public function get_paginate_db_data({
  220.         return $this->paginate_db_data;
  221.     }
  222.  
  223.  
  224.     /**
  225.      * Sets Vars_Manager object
  226.      *
  227.      * @param Vars_Manager $vm 
  228.      */
  229.     protected function set_vm(Vars_Manager $vm{
  230.         $this->vm = $vm;
  231.     }
  232.  
  233.  
  234.     /**
  235.      * Returns reference to Vars_Manager object
  236.      *
  237.      * @return unknown 
  238.      * @internal
  239.      */
  240.     protected function get_vm({
  241.         return $this->vm;
  242.     }
  243.  
  244.  
  245.  
  246.  
  247.     /**
  248.      * Sets method for receiving command
  249.      *
  250.      * @param integer $method 
  251.      * @internal
  252.      */
  253.     protected function set_method($method{
  254.         switch ($method{
  255.             case Tangra_Parameter_Method::GET:
  256.                 $this->method = $method;
  257.                 break;
  258.             case Tangra_Parameter_Method::POST:
  259.                 $this->method = $method;
  260.                 break;
  261.             case Tangra_Parameter_Method::PI:
  262.                 $this->method = $method;
  263.                 break;
  264.             default:
  265.                 throw new Tangra_Exception('Invalid parameter $method: '.$method);
  266.                 break;
  267.         }
  268.     }
  269.  
  270.  
  271.     /**
  272.      * Returns method for receiving commands
  273.      *
  274.      * @return unknown 
  275.      */
  276.     public function get_method({
  277.         return $this->method;
  278.     }
  279.  
  280.     /**
  281.      * Sets user defined method that will be called for postprocessing of the rows
  282.      *
  283.      * Two parameters will be passed to postprocessing method: $dbc and $rows where $dbc is DB_Connection instance and $rows contains all rows of the current page of the grid.
  284.      * Postprocessing method have to return an array containing new/modified data.
  285.      * Access to postprocessing method have to be "protected" in order grid_ctrl to be able to call it.
  286.      *
  287.      * Example:
  288.      * $this->set_user_defined_post_processing_method('my_postprocessing');
  289.      *
  290.      * protected function my_posprocessing(DB_Connection $dbc, $rows) {
  291.      *        // ..some processing - for example loading additional data per row, doing some calculation with existing data and populating new row cell, etc.
  292.      *         return $new_rows;
  293.      * }
  294.      *
  295.      * @param string $method_name This method have to be defined in descendant class otherwise exception will be thrown
  296.      * @throws Tangra_Exception
  297.      */
  298.     public function set_user_defined_post_processing_method($method_name{
  299.         if (is_callable(array($this$method_name))) {
  300.             $this->user_defined_post_processing_method = $method_name;
  301.         else {
  302.             throw new Tangra_Exception("Method $method_name is not defined.");
  303.         }
  304.     }
  305.  
  306.     /**
  307.      * Returns name of the user defined method that will be called for postprocessing of the rows
  308.      *
  309.      * @return string 
  310.      */
  311.     public function get_user_defined_post_processing_method({
  312.         return $this->user_defined_post_processing_method;
  313.     }
  314.  
  315.  
  316.     /**
  317.      * Initializes the object
  318.      *
  319.      */
  320.     protected function init({
  321.         $vm $this->get_vm();
  322.  
  323.         if (!$vm->is_var_registered($this->get_system_name())) {
  324.             $vm->register_var($this->get_system_name());
  325.             $tmp 0;
  326.             $vm->set_var($this->get_system_name()$tmp);
  327.         }
  328.     }
  329.  
  330.  
  331.     /**
  332.      * Sets current page
  333.      *
  334.      * @param integer $page 
  335.      */
  336.     protected function set_page($page{
  337.         tangra_if_not_int_throw_e($page);
  338.  
  339.         // please note that page numbers are in human form, i.e. first page is 1, not 0
  340.         $vm $this->get_vm();
  341.         if ($page{
  342.             $page_num $page 1;
  343.         else {
  344.             $page_num 0;
  345.         }
  346.  
  347.         $vm $this->get_vm();
  348.         $vm->set_var($this->get_system_name()$page_num);
  349.     }
  350.  
  351.     /**
  352.      * Ensures that object returned by $this->create_grid(); will be instance of Grid
  353.      *
  354.      * @throws Tangra_Exception
  355.      */
  356.     protected function _create_grid({
  357.         $grid $this->create_grid();
  358.  
  359.         if ($grid instanceof Grid{
  360.             $this->grid = $grid;
  361.         else {
  362.             throw new Tangra_Exception('create_grid returned object that is not instance of class Static_Paginated_Grid.');
  363.         }
  364.     }
  365.  
  366.  
  367.     /**
  368.      * Sets system name
  369.      *
  370.      * @param unknown_type $system_name 
  371.      * @throws Tangra_Exception
  372.      * @internal
  373.      */
  374.     protected function set_system_name($system_name{
  375.         if (ereg("^[a-z]{1}[a-z0-9_]{1,50}$"$system_name)) {
  376.             $this->system_name = strtolower($system_name);
  377.         else {
  378.             throw new Tangra_Exception('Invalid grid name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.');
  379.         }
  380.     }
  381.  
  382.  
  383.     /**
  384.      * Ensures that object returned by $this->create_paginate_db_data(); will beinstance of Paginate_DB_Data
  385.      *
  386.      * @throws Tangra_Exception
  387.      */
  388.     protected function _create_paginate_db_data({
  389.         $pdbd $this->create_paginate_db_data();
  390.  
  391.         if ($pdbd instanceof Paginate_DB_Data{
  392.             $this->paginate_db_data = $pdbd;
  393.         else {
  394.             throw new Tangra_Exception('create_paginate_db_data() returned object that is not instance of class Paginate_DB_Data.');
  395.         }
  396.     }
  397.  
  398.  
  399.     /**
  400.      * Have to be overwriten and sqls for the Paginate_DB_Data have to be set inside.
  401.      *
  402.      */
  403.     protected function prepare_sqls({
  404.         return array('sqls' => '''sqlc' => '');
  405.     }
  406.  
  407.  
  408.     /**
  409.      * Adds new additional parameter
  410.      *
  411.      * Please note that this parameter will be also added to grid's external parameters.
  412.      *
  413.      * @param string $name 
  414.      * @param mixed $value 
  415.      * @throws TE_Key_Already_Exists
  416.      */
  417.     public function add_additional_parameter($name$value{
  418.         if (!array_key_exists($name$this->additional_parameters)) {
  419.             $this->additional_parameters[$name$value;
  420.             $this->grid->add_external_parameter($name$value);
  421.         else {
  422.             throw new TE_Key_Already_Exists('Parameter with that name already exists: '.$name);
  423.         }
  424.     }
  425.  
  426.  
  427.     /**
  428.      * Sets additional_parameters at once
  429.      *
  430.      * @param array $params Simple array key => value pairs
  431.      * @throws  Tangra_Exception
  432.      */
  433.     public function set_additional_parameters($params{
  434.         if (is_array($params)) {
  435.             foreach($params as $key => $value{
  436.                 $this->add_additional_parameter($key$value);
  437.             }
  438.         else {
  439.             throw new Tangra_Exception('Expecting array.');
  440.         }
  441.     }
  442.  
  443.  
  444.     /**
  445.      * Returns all additional parameters as array
  446.      *
  447.      * @return array Simple array, key => value pairs
  448.      */
  449.     public function get_additional_parameters({
  450.         return $this->additional_parameters;
  451.     }
  452.  
  453.  
  454.     /**
  455.      * Returns value ot additional parameter
  456.      *
  457.      * @param string $name 
  458.      * @param  boolean $forgiving 
  459.      * @throws TE_Key_Not_Exists
  460.      *
  461.      */
  462.     protected function get_additional_parameter($name$forgiving false{
  463.         if (array_key_exists($name$this->additional_parameters)) {
  464.             $ret $this->additional_parameters[$name];
  465.         else {
  466.             if (!$forgiving{
  467.                 throw new TE_Key_Not_Exists('Parameter with that name not exists: '.$name);
  468.             else {
  469.                 $ret false;
  470.             }
  471.         }
  472.  
  473.         return $ret;
  474.     }
  475.  
  476.  
  477.     /**
  478.      * Overoload this method, create your Grid object and return it
  479.      *
  480.      */
  481.     abstract protected function create_grid();
  482.  
  483.     /**
  484.      * Overload this method, create Paginate_DB_Data and return it
  485.      *
  486.      */
  487.     abstract protected function create_paginate_db_data();
  488.  
  489.  
  490.     /**
  491.      * Detects current page number.
  492.      *
  493.      * Will return current page number. If $page is passed - will return that value otherwise will
  494.      * retrieve current page from permanent VM.
  495.      *
  496.      * @param integer $page 
  497.      * @return integer 
  498.      */
  499.     protected function detect_page($page{
  500.         $vm $this->get_vm();
  501.  
  502.         if ($page !== false && $page != NULL {
  503.             $this->set_page($page);
  504.         }
  505.  
  506.         $page $vm->get_var($this->get_system_name());
  507.  
  508.         return $page;
  509.     }
  510.  
  511.  
  512.     /**
  513.      * Retrieves rows from DB
  514.      *
  515.      * @param DB_Connection $dbc 
  516.      * @param integer $page     Rows for page $page will be retrieved
  517.      * @return array 
  518.      */
  519.     protected function retrieve_rows(DB_Connection $dbc$page{
  520.         $sql $this->prepare_sqls();
  521.         if (is_array($sql)) //this if is used for backward compatability
  522.             if ($sql['sqls']{
  523.                 $this->paginate_db_data->set_sql_select($sql['sqls']);
  524.             }
  525.             if ($sql['sqlc']{
  526.                 $this->paginate_db_data->set_sql_count($sql['sqlc']);
  527.             }
  528.         }
  529.  
  530.         $rows $this->paginate_db_data->fetch_page($page$dbc);
  531.  
  532.         return $rows;
  533.     }
  534.  
  535.  
  536.     /**
  537.      * Sends retrieved rows and other data to the grid object
  538.      *
  539.      * @param array $rows 
  540.      * @param integer $page 
  541.      * @param integer $total_pages 
  542.      * @param integer $total_rows 
  543.      */
  544.     protected function send_data_to_grid($rows$page$total_pages$total_rows{
  545.         $this->grid->set_rows($rows);
  546.  
  547.         $this->grid->set_cp_and_tp($page$total_pages);
  548.         $this->grid->set_total_rows($total_rows);
  549.     }
  550. }