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

Source for file web_page.class.php

Documentation is available at web_page.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Web_Page
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage  web_site
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'web_site/web_ctrl.class.php');
  17.  
  18. /**
  19.  *
  20.  */
  21. require_once('web_page_view.class.php');
  22. /**
  23.  *
  24.  */
  25. require_once('just_headers_view.class.php');
  26.  
  27. /**
  28.  *
  29.  */
  30. require_once(TANGRA_MAIN_DIR.'exceptions/te_web_page_view_not_exists.class.php');
  31. /**
  32.  *
  33.  */
  34. require_once(TANGRA_MAIN_DIR.'exceptions/te_web_page_view_already_exists.class.php');
  35.  
  36.  
  37. /**
  38.  * Class Web_Page represents the abstract concept of web page
  39.  *
  40.  * @package tangra_lib
  41.  * @subpackage  web_site
  42.  */
  43. abstract class Web_Page extends Web_Ctrl {
  44.  
  45.     /**
  46.      * Page name
  47.      *
  48.      * @var string 
  49.      * @internal
  50.      */
  51.     private $page_name;
  52.     /**
  53.      * Views for this page. Array of Web_Page_View
  54.      *
  55.      * @var array 
  56.      * @internal
  57.      */
  58.     private $views = array();
  59.     /**
  60.      * current URL of the page
  61.      *
  62.      * @var string 
  63.      */
  64.     private $page_url_name;
  65.  
  66.     /**
  67.      * Constructor
  68.      *
  69.      * @param string $page_name 
  70.      */
  71.     function __construct($page_name{
  72.         parent::__construct();
  73.         $this->set_page_name($page_name);
  74.     }
  75.  
  76.  
  77.     /**
  78.      * Initializing the page
  79.      *
  80.      * @param Web_Context $context 
  81.      * @param Web_Site_Config $config 
  82.      */
  83.     public function _init(Web_Context $contextWeb_Site_Config $config{
  84.         $this->page_url_name = $this->capture_page_url_name($context$config);
  85.         parent::_init($context$config);
  86.         $this->export_uni_back();
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Returns page name
  92.      *
  93.      * @return string 
  94.      */
  95.     public function get_page_name({
  96.         return $this->page_name;
  97.     }
  98.  
  99.  
  100.     /**
  101.      * Returns page URL
  102.      *
  103.      * @return unknown 
  104.      */
  105.     public function get_page_url_name({
  106.         return $this->page_url_name;
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Sets page name
  112.      *
  113.      * @param string $page_name 
  114.      * @throws  Tangra_Exception
  115.      * @internal
  116.      * @throws Tangra_Exception
  117.      */
  118.     private function set_page_name($page_name{
  119.         if (ereg("^[a-z]{1}[a-z0-9_/\\]{0,100}$"$page_name)) {
  120.             $this->page_name = $page_name;
  121.         else {
  122.             throw new Tangra_Exception('Invalid page name ($page_name). Must conform ^[a-z]{1}[a-z0-9_/\\]{1,100}[a-z0-9_]{1}$. Current value: '.$page_name);
  123.         }
  124.     }
  125.  
  126.  
  127.     /**
  128.      * Adds view
  129.      *
  130.      * @param Web_Page_View $page_view 
  131.      * @throws TE_Web_Page_View_Already_Exists
  132.      */
  133.     protected function add_view(Web_Page_View $page_view{
  134.         if (!array_key_exists($page_view->get_name()$this->views)) {
  135.             $this->views[$page_view->get_name()$page_view;
  136.         else {
  137.             throw new TE_Web_Page_View_Already_Exists($page_view->get_name());
  138.         }
  139.     }
  140.  
  141.  
  142.     /**
  143.      * Returns view
  144.      *
  145.      * @param string $name Name of the view
  146.      * @return Web_Page_View 
  147.      * @throws TE_Web_Page_View_Not_Exists
  148.      */
  149.     protected function get_view($name 'default'{
  150.         if (array_key_exists($name$this->views)) {
  151.             $ret $this->views[$name];
  152.         else {
  153.             throw new TE_Web_Page_View_Not_Exists($name);
  154.         }
  155.  
  156.         return $ret;
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Captures current URL from context
  162.      *
  163.      * @return string 
  164.      * @internal
  165.      */
  166.     private function capture_page_url_name(Web_Context $contextWeb_Site_Config $config{
  167.         $et false;
  168.  
  169.         $script_name $context->get_from_server('SCRIPT_NAME');
  170.         $relative_docroot $config->get_document_root_relative();
  171.  
  172.         $ret substr($script_namestrlen($relative_docroot));
  173.  
  174.  
  175.         return $ret;
  176.     }
  177.  
  178.  
  179.  
  180.     /**
  181.      * Creates and returns Just_Headers_View with redirect (Location: ) header
  182.      *
  183.      * If $target starts with "http" redirect will be with absolute URL, otherwise will be relative URL.
  184.      *
  185.      * Please note that $params_pairs will be merged with existing (if any) transit variables.
  186.      *
  187.      * @param stirng $target_location target page to redirect to. Example: 'admin/index.php'
  188.      * @param array $param_pairs key => value pairs of parameters that will be added to redirect. Example: array('confirmed' => 1)
  189.      * @throws Tangra_Exception
  190.      */
  191.     protected function create_redirect_view($target_location$param_pairs array()) {
  192.         $target_location trim($target_location);
  193.         if ($target_location{
  194.             $redir_view new Just_Headers_View();
  195.  
  196.             $redir $this->create_redirect_composer($target_location$param_pairs);
  197.             $redir_view->add_http_header($redir->get_location());
  198.         else {
  199.             throw new Tangra_Exception('$target is empty or invalid.');
  200.         }
  201.  
  202.  
  203.         return $redir_view;
  204.     }
  205.  
  206.  
  207.     /**
  208.      * Detects referer (if local) and exports it as _uni_back
  209.      *
  210.      * Useful for creating back links that work the same way as browser window but with _tm variable set correctly (if available).
  211.      *    @internal
  212.      */
  213.     private function export_uni_back({
  214.         if (array_key_exists('HTTP_REFERER'$_SERVER)) {
  215.             $parsed_url parse_url($_SERVER['HTTP_REFERER']);
  216.  
  217.             $config $this->get__config();
  218.             if ($parsed_url['host'== $config->get_current_host()) {
  219.     //            $lang_pos = strpos($parsed_url['path'], '_lang-');
  220.     //            if ($lang_pos !== false) {
  221.     //                $parsed_url['path'] = substr($parsed_url['path'], 0, $lang_pos -1);
  222.     //            }
  223.  
  224.                 if (array_key_exists('query'$parsed_url)) {
  225.                     if ($parsed_url['query']{
  226.                         $tm_pos strpos($parsed_url['query']'&_tm=');
  227.                         if ($tm_pos !== false{
  228.                             $tm_full substr($parsed_url['query']$tm_pos37);
  229.                             $parsed_url['query'str_replace($tm_full''$parsed_url['query']);
  230.                         }
  231.                     }
  232.                 else {
  233.                     $parsed_url['query''';
  234.                 }
  235.  
  236.                 $this->export('_uni_back'$parsed_url['path'].($parsed_url['query''?'.$parsed_url['query':''));
  237.             }
  238.         }
  239.     }
  240.  
  241.  
  242.     /**
  243.      * End classes must implement this function and it must be used always for creating views (i.e. no new Some_View())
  244.      *
  245.      * @param string $name 
  246.      */
  247.     abstract public function create_view($name 'default');
  248. }