Source for file grid_ctrl.class.php
Documentation is available at grid_ctrl.class.php
// *** Tangra (Application Framework and Tools for PHP)
* Contains class Grid_Ctrl
require_once(TANGRA_MAIN_DIR. 'grids/static_paginated_grid.class.php');
require_once(TANGRA_MAIN_DIR. 'db/db_connection.class.php');
require_once(TANGRA_MAIN_DIR. 'db/paginate_db_data.class.php');
require_once(TANGRA_MAIN_DIR. 'core/tangra_parameter_method.class.php');
require_once(TANGRA_MAIN_DIR. 'web_site/web_event_simple_int.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_key_already_exists.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_key_not_exists.class.php');
* Capture for setting page
* Method for passing set page command
* Holds array of additional parameters passed to the controller.
* Holds name of user defined method (if set)
* @param string $system_name
* @param Vars_Manager $vm Permanent Vars_Manager that will be used. Session of Thread VM
* @param integer $method Tangra_Parameter_Method::GET or Tangra_Parameter_Method::POST or Tangra_Parameter_Method::PI
function __construct($system_name, Vars_Manager $vm, $method = Tangra_Parameter_Method::GET) {
* This method will load page passed with <var>$page</var> or will load current_page
* @param DB_Connection $dbc DB_Connection object
* @param integer $page Page to be loaded. Default = NULL - will load current page
public function process(DB_Connection $dbc, $page = NULL) {
if ($total_pages - 1 < $page) {
$page = $total_pages - 1;
$rows = $this->$user_pp_method($dbc, $rows);
* Get TPLE Exports for the grid
$exports = $this->grid->get_tple_exports();
* Returns web event for set page command
* Returns grid's system name
* Returns reference to grid object
* Returns reference to Paginate_DB_Data object
* @return Paginate_DB_Data
* Sets Vars_Manager object
* @param Vars_Manager $vm
protected function set_vm(Vars_Manager $vm) {
* Returns reference to Vars_Manager object
* Sets method for receiving command
throw new Tangra_Exception('Invalid parameter $method: '. $method);
* Returns method for receiving commands
* Sets user defined method that will be called for postprocessing of the rows
* 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.
* Postprocessing method have to return an array containing new/modified data.
* Access to postprocessing method have to be "protected" in order grid_ctrl to be able to call it.
* $this->set_user_defined_post_processing_method('my_postprocessing');
* protected function my_posprocessing(DB_Connection $dbc, $rows) {
* // ..some processing - for example loading additional data per row, doing some calculation with existing data and populating new row cell, etc.
* @param string $method_name This method have to be defined in descendant class otherwise exception will be thrown
* @throws Tangra_Exception
throw new Tangra_Exception("Method $method_name is not defined.");
* Returns name of the user defined method that will be called for postprocessing of the rows
protected function init() {
// please note that page numbers are in human form, i.e. first page is 1, not 0
* Ensures that object returned by $this->create_grid(); will be instance of Grid
* @throws Tangra_Exception
if ($grid instanceof Grid) {
throw new Tangra_Exception('create_grid returned object that is not instance of class Static_Paginated_Grid.');
* @param unknown_type $system_name
* @throws Tangra_Exception
if (ereg("^[a-z]{1}[a-z0-9_]{1,50}$", $system_name)) {
throw new Tangra_Exception('Invalid grid name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.');
* Ensures that object returned by $this->create_paginate_db_data(); will beinstance of Paginate_DB_Data
* @throws Tangra_Exception
throw new Tangra_Exception('create_paginate_db_data() returned object that is not instance of class Paginate_DB_Data.');
* Have to be overwriten and sqls for the Paginate_DB_Data have to be set inside.
return array('sqls' => '', 'sqlc' => '');
* Adds new additional parameter
* Please note that this parameter will be also added to grid's external parameters.
* @throws TE_Key_Already_Exists
$this->grid->add_external_parameter($name, $value);
throw new TE_Key_Already_Exists('Parameter with that name already exists: '. $name);
* Sets additional_parameters at once
* @param array $params Simple array key => value pairs
* @throws Tangra_Exception
foreach($params as $key => $value) {
throw new Tangra_Exception('Expecting array.');
* Returns all additional parameters as array
* @return array Simple array, key => value pairs
* Returns value ot additional parameter
* @param boolean $forgiving
* @throws TE_Key_Not_Exists
throw new TE_Key_Not_Exists('Parameter with that name not exists: '. $name);
* Overoload this method, create your Grid object and return it
* Overload this method, create Paginate_DB_Data and return it
* Detects current page number.
* Will return current page number. If $page is passed - will return that value otherwise will
* retrieve current page from permanent VM.
if ($page !== false && $page != NULL ) {
* @param DB_Connection $dbc
* @param integer $page Rows for page $page will be retrieved
if (is_array($sql)) { //this if is used for backward compatability
* Sends retrieved rows and other data to the grid object
* @param integer $total_pages
* @param integer $total_rows
$this->grid->set_cp_and_tp($page, $total_pages);
$this->grid->set_total_rows($total_rows);
|