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

Source for file web_ctrl.class.php

Documentation is available at web_ctrl.class.php

  1. <?php
  2.  
  3. /**
  4.  * Contains class Web_Page
  5.  *
  6.  * @package tangra_lib
  7.  * @subpackage  web_site
  8.  */
  9.  
  10.  
  11. /**
  12.  *
  13.  */
  14. require_once(TANGRA_MAIN_DIR.'web_site/web_context.class.php');
  15. /**
  16.  *
  17.  */
  18. require_once(TANGRA_MAIN_DIR.'web_site/web_site_config.class.php');
  19. /**
  20.  *
  21.  */
  22. require_once(TANGRA_MAIN_DIR.'web_site/redirect_composer_local.class.php');
  23.  
  24.  
  25. /**
  26.  * Web_Ctrl is base class for Web_Page and Ajax_Ctrl.
  27.  *
  28.  * All objects that will be ran by site object must inherit it.
  29.  *
  30.  * @package tangra_lib
  31.  * @subpackage  web_site
  32.  */
  33. abstract class Web_Ctrl extends Tangra_Class {
  34.     const TRANSIT_VARS_PREFIX = '_transit_';
  35.  
  36.     /**
  37.      * Web_Site_Config holder
  38.      *
  39.      * @var Web_Site_Config 
  40.      * @internal
  41.      */
  42.     protected $_config;
  43.     /**
  44.      * TPLE_Exports holder
  45.      *
  46.      * @var TPLE_Exports 
  47.      * @internal
  48.      */
  49.     protected $_exports;
  50.  
  51.     /**
  52.      * Web_Context holder
  53.      *
  54.      * @var Web_Context 
  55.      * @internal
  56.      */
  57.     private $_context;
  58.  
  59.     /**
  60.      * Holds variables that are added as transit
  61.      *
  62.      * @var array 
  63.      * @internal
  64.      */
  65.     private $_transit_vars = array();
  66.  
  67.  
  68.     /**
  69.      * Constructor
  70.      *
  71.      */
  72.     function __construct({
  73.         $this->_exports = new TPLE_Exports();
  74.         $this->init_transit_vars();
  75.     }
  76.  
  77.  
  78.     /**
  79.      * Initializing the page
  80.      *
  81.      * @param Web_Context $context 
  82.      * @param Web_Site_Config $config 
  83.      */
  84.     public function _init(Web_Context $contextWeb_Site_Config $config{
  85.         $this->set_context($context);
  86.         $this->set_config($config);
  87.  
  88.         $this->capture_transit_vars();
  89.         $this->init();
  90.         $this->capture_transit_vars();
  91.     }
  92.  
  93.  
  94.     /**
  95.      * Place for user code for initialization.
  96.      * Must throw exception (TE_Page_Failed_To_Init) if init fails.
  97.      * On failure run() method will not be executed.
  98.      * Put in init() all code that:
  99.      * - checks for availability of resources
  100.      * - it is not essential for concrete page functionality
  101.      * Init method is useful for base page classes - if you have some functionality that
  102.      * is common for all (most) pages in your site - put it here and then inherit that base
  103.      *  page class. Don't forget to call parent::init() if you overload it
  104.      *
  105.      */
  106.     protected function init({
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Sets context
  112.      *
  113.      * @param Web_Context $context 
  114.      * @internal
  115.      */
  116.     private function set_context(Web_Context $context{
  117.         $this->_context = $context;
  118.     }
  119.  
  120.  
  121.     /**
  122.      * Returns reference to context
  123.      *
  124.      * @return Web_Context 
  125.      */
  126.     public function get__context({
  127.         return $this->_context;
  128.     }
  129.  
  130.  
  131.     /**
  132.      * Return exports object
  133.      *
  134.      * @return TPLE_Exports 
  135.      */
  136.     public function get__exports({
  137.         return $this->_exports;
  138.     }
  139.  
  140.  
  141.     /**
  142.      * Adds value key-value pair exports
  143.      *
  144.      * @param string $key 
  145.      * @param string $value 
  146.      */
  147.     public function export($key$value{
  148.         $this->_exports->add_pair($key$value);
  149.     }
  150.  
  151.  
  152.     /**
  153.      * Adds exports. This is bulk version of export()
  154.      *
  155.      * @param TPLE_exports $exports 
  156.      */
  157.     public function add_exports(TPLE_exports $exports{
  158.         $this->_exports->merge($exports);
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Sets $_config
  164.      *
  165.      * @param Web_Site_Config $config 
  166.      * @internal
  167.      */
  168.     private function set_config(Web_Site_Config $config{
  169.         $this->_config = $config;
  170.     }
  171.  
  172.  
  173.     /**
  174.      * Returns current web site configuration object.
  175.      *
  176.      * @return Web_Site_Config Current $WSC
  177.      */
  178.     public function get__config({
  179.         return $this->_config;
  180.     }
  181.  
  182.  
  183.     public function add_transit_var($var_name$var_value NULL{
  184.         if (ereg("^[a-z]{1}[a-z0-9_]{1,40}$"$var_name)) {
  185.             if (!array_key_exists($var_name$this->_transit_vars)) {
  186.                 $this->_transit_vars[$var_name= (string) $var_value;
  187.             else {
  188.                 throw new Tangra_Exception('Transit variable '.$var_name.' already exist.');
  189.             }
  190.         else {
  191.             throw new Tangra_Exception('Invalid name for transit variable: '.$var_name);
  192.         }
  193.     }
  194.  
  195.  
  196.     public function set_transit_var_value($var_name$var_value{
  197.         if (array_key_exists($var_name$this->_transit_vars)) {
  198.             $this->_transit_vars[$var_name= (string) $var_value;
  199.         else {
  200.             throw new Tangra_Exception('Transit variable "'.$var_name.'" not exist.');
  201.         }
  202.  
  203.     }
  204.  
  205.  
  206.     public function get_transit_var($var_name$forgiving false{
  207.         $ret false;
  208.  
  209.         if (array_key_exists($var_name$this->_transit_vars)) {
  210.             $ret $this->_transit_vars[$var_name];
  211.         else {
  212.             if (!$forgiving{
  213.                 throw new Tangra_Exception('Transit variable "'.$var_name.'" not exist.');
  214.             }
  215.         }
  216.  
  217.         return $ret;
  218.     }
  219.  
  220.  
  221.     public function get_transit_vars({
  222.         return $this->_transit_vars;
  223.     }
  224.  
  225.  
  226.     public function capture_transit_vars({
  227.         foreach($this->_transit_vars as $var_name => $var_value{
  228.             $context $this->get__context();
  229.             if ($context->exists_in_get(Web_Page::TRANSIT_VARS_PREFIX.$var_name)) {
  230.                 $this->set_transit_var_value($var_nameurldecode($context->get_from_get(Web_Ctrl::TRANSIT_VARS_PREFIX.$var_name)));
  231.             elseif($context->exists_in_post(Web_Page::TRANSIT_VARS_PREFIX.$var_name)) {
  232.                 $this->set_transit_var_value($var_nameurldecode($context->get_from_post(Web_Ctrl::TRANSIT_VARS_PREFIX.$var_name)));
  233.             }
  234.         }
  235.     }
  236.  
  237.  
  238.     public function set_transit_vars_as_output_rewrite_vars({
  239.         $_transit_vars_prepared $this->prepare_transit_vars();
  240.  
  241.         foreach($_transit_vars_prepared as $var_name => $var_value{
  242.             output_add_rewrite_var($var_name$var_value);
  243.         }
  244.     }
  245.  
  246.  
  247.     public function remove_transit_var($var_name{
  248.         if (array_key_exists($var_name$this->_transit_vars)) {
  249.             unset($this->_transit_vars[$var_name]);
  250.         }
  251.     }
  252.  
  253.  
  254.     protected function prepare_transit_vars({
  255.         $ret array();
  256.         foreach($this->_transit_vars as $var_name => $var_value{
  257.             if ($var_value != NULL{
  258.                 $ret[Web_Page::TRANSIT_VARS_PREFIX.$var_name$var_value;
  259.             }
  260.         }
  261.  
  262.         return $ret;
  263.     }
  264.  
  265.  
  266.     protected function create_redirect_composer($target_location$param_pairs array()) {
  267.         if (strtolower(substr($target_location04)) != 'http'{
  268.             $_transit_vars_prepared $this->prepare_transit_vars();
  269.  
  270.             $param_pairs_all array_merge($param_pairs$_transit_vars_prepared);
  271.  
  272.             $redir new Redirect_Composer_Local($this->get__context()$target_location$param_pairs_all);
  273.         else {
  274.             $redir new Redirect_Composer($target_location$param_pairs);
  275.         }
  276.  
  277.         return $redir;
  278.     }
  279.  
  280.  
  281.     /**
  282.      * Runs the controller
  283.      *
  284.      * @return Web_Page_View 
  285.      */
  286.     public function _run({
  287.         $view $this->run();
  288.  
  289.         $_transit_vars_prepared $this->prepare_transit_vars();
  290.         $view->set_exported_value('_transit_vars'$_transit_vars_preparedtrue);
  291.         $this->set_transit_vars_as_output_rewrite_vars();
  292.  
  293.         return $view;
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Here is the place to add your transit vars
  299.      *
  300.      * This method will be called by __constructor automatically. It is provided for convenience (i.e. otherwise you have to add transit vars in __constructor which means that you have to overload it, find the correct declaration, etc.)
  301.      *
  302.      * Example:
  303.      * <code>
  304.      * <?php
  305.      * protected function init_transit_vars() {
  306.      *         add_transit_var('category');
  307.      * }
  308.      * ?>
  309.      * </code>
  310.      *
  311.      */
  312.     protected function init_transit_vars({
  313.  
  314.     }
  315.  
  316.  
  317.     /**
  318.      * Put your main page functionalty in run() (if not otherwise specified like for example for WED pages)
  319.      *
  320.      * Must return instance of Web_Page_View
  321.      */
  322.     abstract public function run();
  323. }