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

Source for file web_page_view.class.php

Documentation is available at web_page_view.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Web_Page_View
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage  web_site
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'web_site/tple_exports.class.php');
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'web_site/web_page/web_page.class.php');
  21. /**
  22.  *
  23.  */
  24. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_not_exists.class.php');
  25.  
  26.  
  27. /**
  28.  * View for Web_page
  29.  *
  30.  * @package tangra_lib
  31.  * @subpackage  web_site
  32.  */
  33. abstract class Web_Page_View extends Tangra_Class {
  34.     /**
  35.      * Web_Page holder
  36.      *
  37.      * @var Web_Page 
  38.      * @internal
  39.      */
  40.     private $page;
  41.  
  42.     /**
  43.      * HTTP headers array
  44.      *
  45.      * @var array 
  46.      * @internal
  47.      */
  48.     private $http_headers = array();
  49.  
  50.     /**
  51.      * TPLE_Exports holder
  52.      *
  53.      * @var TPLE_Exports 
  54.      */
  55.     private $exports;
  56.  
  57.     /**
  58.      * Name of the view
  59.      *
  60.      * @var string 
  61.      */
  62.     private $name;
  63.  
  64.  
  65.     /**
  66.      * Holds additional CSSes that will be exported to template
  67.      *
  68.      * @var array 
  69.      */
  70.     private $additional_csses = array();
  71.  
  72.  
  73.     /**
  74.      * Holds additional Javascript files that will be exported to template
  75.      *
  76.      * @var array 
  77.      */
  78.     private $additional_jses;
  79.  
  80.  
  81.     /**
  82.      * Constructor
  83.      *
  84.      * @param Web_Page $page Page tha view is for
  85.      * @param string $name Name of the view
  86.      */
  87.     function __construct(Web_Page $page$name 'default'{
  88.         $this->page = $page;
  89.         $this->set_name($name);
  90.         $this->init_exports();
  91.     }
  92.  
  93.  
  94.     protected function init_exports({
  95.         $this->exports = new TPLE_Exports;
  96.     }
  97.  
  98.     /**
  99.      * Returns page object
  100.      *
  101.      */
  102.     public function get_page({
  103.         return $this->page;
  104.     }
  105.  
  106.  
  107.     /**
  108.      * Returns view name
  109.      *
  110.      * @return string 
  111.      */
  112.     public function get_name({
  113.         return $this->name;
  114.     }
  115.  
  116.  
  117.     /**
  118.      * Returns exports object
  119.      *
  120.      * @return TPLE_Exports 
  121.      */
  122.     public function get__exports({
  123.         return $this->exports;
  124.     }
  125.  
  126.  
  127.     /**
  128.      * Returns page HTML
  129.      *
  130.      */
  131.     abstract public function &fetch_content();
  132.  
  133.  
  134.     /**
  135.      * Shows page HTML
  136.      *
  137.      */
  138.     public function show({
  139.         foreach($this->http_headers as $h{
  140.             header($h);
  141.         }
  142.  
  143.         $this->exports->add_pair('_additional_csses'$this->get_additional_csses());
  144.         $this->exports->add_pair('_additional_jses'$this->get_additional_jses());
  145.  
  146.         print($this->fetch_content());
  147.     }
  148.  
  149.  
  150.     /**
  151.      * Sets TPLE exports
  152.      *
  153.      * @param TPLE_exports $exports 
  154.      */
  155.     public function set_exports(TPLE_exports $exports{
  156.         $this->exports = $exports;
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Adds exports
  162.      *
  163.      * @param TPLE_exports $exports 
  164.      */
  165.     public function add_exports(TPLE_exports $exports{
  166.         $this->exports->merge($exports);
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Returns value of exported pair.
  172.      *
  173.      * If $forgiving is set to false will throw exception TE_Key_Not_Exists if such key does not exist.
  174.      * If $forgiving is set to true will return NULL if such key does not exist.
  175.      *
  176.      * @param string $key 
  177.      * @param boolean $forgiving 
  178.      * @return mixed 
  179.      * @see Web_Page_View::set_exported_value()
  180.      */
  181.     public function get_exported_value($key$forgiving true{
  182.         $ret $this->exports->get_pair_value($key$forgiving);
  183.  
  184.         return $ret;
  185.     }
  186.  
  187.     /**
  188.      * Changes the value of exported pair.
  189.      *
  190.      * If $create_if_not_exists is set to true this function will create export pair if it does not exist.
  191.      * If $create_if_not_exists is set to false exception will be thrown if pair does not exist.
  192.      *
  193.      * The purpose of methods set_exported_value and get_exported_value is to give chance to the view class to format values.
  194.      * For example, if Page object exported 'total' => 0.54, view object can use number_format() function to change
  195.      * the appearance of the value (first it will get value with $var = get_exported_value('some_key'), then will
  196.      * set_exported_value('some_key', number_format($var, 2, ','))).
  197.      *
  198.      * @param string $key 
  199.      * @param mixed $value 
  200.      * @param boolean $create_if_not_exists 
  201.      * @throws TE_Key_Not_Exists
  202.      */
  203.     public function set_exported_value($key$value$create_if_not_exist false{
  204.         if ($this->exports->key_exists($keytrue)) {
  205.             $this->exports->update_pair_value($key$value);
  206.         else {
  207.             if ($create_if_not_exist{
  208.                 $this->exports->add_pair($key$value);
  209.             else {
  210.                 throw new TE_Key_Not_Exists('Key not exists: '.$key);
  211.             }
  212.         }
  213.     }
  214.  
  215.  
  216.     /**
  217.      * Adds HTTP header
  218.      *
  219.      * @param unknown_type $http_header 
  220.      */
  221.     public function add_http_header($http_header{
  222.         array_push($this->http_headers$http_header);
  223.     }
  224.  
  225.  
  226.     /**
  227.      * Return HTTP headers
  228.      *
  229.      * @return array 
  230.      */
  231.     public function get_http_headers({
  232.         return $this->http_headers;
  233.     }
  234.  
  235.  
  236.     public function add_css($path_to_css_file{
  237.         $this->additional_csses[$path_to_css_file$path_to_css_file;
  238.     }
  239.  
  240.  
  241.     public function get_additional_csses({
  242.         return $this->additional_csses;
  243.     }
  244.  
  245.  
  246.     public function add_js($path_to_js_file{
  247.         $this->additional_jses[$path_to_js_file$path_to_js_file;
  248.     }
  249.  
  250.  
  251.     public function get_additional_jses({
  252.         return $this->additional_jses;
  253.     }
  254.  
  255.  
  256.     /**
  257.      * Adds value key-value pair exports
  258.      *
  259.      * @param string $key 
  260.      * @param string $value 
  261.      */
  262.     public function export($key$value{
  263.         $this->exports->add_pair($key$value);
  264.     }
  265.  
  266.  
  267.  
  268.     /**
  269.      * Sets name of the view
  270.      *
  271.      * @param string $name 
  272.      * @internal
  273.      */
  274.     private function set_name($name{
  275.         if (ereg("^[a-z]{1}[a-z0-9_]{1,100}$"$name)) {
  276.             $this->name = $name;
  277.         else {
  278.             throw new Tangra_Exception('Invalid view name ($page_name). Must conform ^[a-z]{1}[a-z0-9_]{1,100}$. Current value: '.$name);
  279.         }
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Sets page object
  285.      *
  286.      * @param Web_Page $page 
  287.      */
  288.     private function set_page($page{
  289.         $this->page = $page;
  290.     }
  291.  
  292. }