dbscript
[ class tree: dbscript ] [ index: dbscript ] [ all elements ]

Source for file view.php

Documentation is available at view.php

  1. <?php
  2.  
  3.   /** 
  4.    * dbscript for PHP 4 & 5 - restful crud framework
  5.    * @version 0.1.2 -- 19-Feb-2007
  6.    * @author Brian Hendrickson <brian@dbscript.net>
  7.    * @link http://dbscript.net/
  8.    * @copyright Copyright 2007 Brian Hendrickson
  9.    * @package dbscript
  10.    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11.    */
  12.  
  13.   /**
  14.    * View
  15.    * 
  16.    * More info...
  17.    * {@link http://dbscript.net/view}
  18.    * 
  19.    * @package dbscript
  20.    * @author Brian Hendrickson <brian@dbscript.net>
  21.    * @access public
  22.    * @version 0.1.2
  23.    */
  24.  
  25. class View {
  26.   
  27.   var $negotiator;
  28.   var $controller;
  29.   var $collection;
  30.   var $named_vars;
  31.   var $layout;
  32.   var $extension;
  33.   var $header_sent;
  34.   
  35.   function View({
  36.     
  37.     $this->named_vars = array();
  38.     $this->header_sent = false;
  39.     
  40.     $db =db_object();
  41.     $request =request_object();
  42.     $env =environment();
  43.     
  44.     $Entry =$db->get_table'entries' );    
  45.     
  46.     if isset$request->resource ))
  47.       $this->collection = new Collection$request->resource );
  48.     else
  49.       $this->collection = new Collectionnull );
  50.     
  51.     $this->named_vars['db'=$db;
  52.     $this->named_vars['request'=$request;
  53.     $this->named_vars['collection'=$this->collection;
  54.     $this->named_vars['response'=$this;
  55.     
  56.     $this->controller = $request->controller;
  57.     
  58.     // check for a controller file in db/controllers/[resource].php
  59.     if isset$request->resource )) {
  60.       $cont controller_path($request->resource ".php";
  61.       if file_exists$cont ))
  62.         $this->controller = $request->resource ".php";
  63.     }
  64.     
  65.     if file_existscontroller_path($this->controller ))
  66.       require_oncecontroller_path($this->controller );
  67.     
  68.     if (!(isset($env['content_types'])))
  69.       trigger_error'Sorry, the content_types array was not found in the configuration file'E_USER_ERROR );
  70.     
  71.     $this->negotiator = HTTP_Negotiate::choose$env['content_types');
  72.     
  73.   }
  74.   
  75.   function render&$request {
  76.     
  77.     // boot.php calls $response->render()
  78.  
  79.     $db =db_object();
  80.     
  81.     $ext $this->pick_template_extension$request );
  82.     
  83.     $view $request->get_template_path$ext );
  84.     
  85.     $action $request->action;
  86.     
  87.     if (!(function_exists($action)))
  88.       $action 'index';
  89.     
  90.     if file_exists$view ) ) {
  91.       
  92.       if (function_exists$action )) {
  93.         trigger_before$action$request$db );
  94.         $result $actionarray_merge$this->named_vars$db->get_objects() ));
  95.         trigger_after$action$request$db );
  96.         if is_array$result ))
  97.           extract$result );
  98.       }
  99.  
  100.       $content_type 'Content-Type: ' $this->pick_content_type$ext );
  101.       if ($this->pick_content_charset$ext ))
  102.         $content_type .= '; charset=' $this->pick_content_charset$ext );
  103.       header$content_type );
  104.       $this->header_sent = true;
  105.       include$view );
  106.  
  107.     else {
  108.  
  109.       $this->render_partial&$request$request->action );
  110.  
  111.     }
  112.     
  113.   }
  114.   
  115.   function render_partial&$request$template {
  116.     
  117.     // content_for_layout() passes the $request->action as $template
  118.     
  119.     $ext $this->pick_template_extension$request$template );
  120.     
  121.     $view $request->get_template_path$ext$template );
  122.     
  123.     if ($template == 'get')
  124.       $template 'index';
  125.     
  126.     $action "_" $template;
  127.     
  128.     $db =db_object();
  129.     
  130.     if function_exists$action )) {
  131.       trigger_before$action$request$db );
  132.       $result $actionarray_merge$this->named_vars$db->get_objects() ));
  133.       trigger_after$action$request$db );
  134.       if is_array$result ))
  135.         extract$result );
  136.     }
  137.     
  138.     if file_exists$view ) ) {
  139.       if (!($this->header_sent)) {
  140.         $content_type 'Content-Type: ' $this->pick_content_type$ext );
  141.         if ($this->pick_content_charset$ext ))
  142.           $content_type .= '; charset=' $this->pick_content_charset$ext );
  143.         header$content_type );
  144.         $this->header_sent = true;
  145.       }
  146.       include$view );
  147.     }
  148.       
  149.   }
  150.   
  151.   function negotiate_content&$request$template {
  152.     foreach $this->negotiator as $client_wants {
  153.       if template_exists$request$client_wants['id']$template ))
  154.         return $client_wants['id'];
  155.     }
  156.   }
  157.     
  158.   function pick_content_type$ext {
  159.     $variants content_types();
  160.     foreach($variants as $content_type{
  161.       if ($content_type['id'== $ext && $content_type['type'!= null)
  162.         return $content_type['type'];
  163.     }
  164.     return false;
  165.   }
  166.  
  167.   function pick_content_charset$ext {
  168.     $variants content_types();
  169.     foreach($variants as $content_type{
  170.       if ($content_type['id'== $ext)
  171.         return $content_type['charset'];
  172.     }
  173.     return false;
  174.   }
  175.   
  176.   function set_content_encoding$ext {
  177.     $variants content_types();
  178.     foreach($variants as $content_type{
  179.       if (($content_type['id'== $ext&& $content_type['encoding'])
  180.         header"Content-Encoding: "$content_type['encoding');
  181.     }
  182.   }
  183.   
  184.   function pick_template_extension&$request$template null {
  185.     
  186.     if (isset($request->params['client_wants']))
  187.       return $request->params['client_wants'];
  188.       
  189.     $ext $this->negotiate_content$request$template );
  190.     
  191.     if (!$ext{
  192.       
  193.       // if ( content-negotiation fails ) go to html
  194.       
  195.       $variants array(
  196.         array(
  197.           'id' => 'html',
  198.           'qs' => 1.000,
  199.           'type' => 'text/html',
  200.           'encoding' => null,
  201.           'charset' => 'iso-8859-1',
  202.           'language' => 'en',
  203.           'size' => 3000
  204.         )
  205.       );
  206.       
  207.       $this->negotiator = $variants;
  208.       
  209.       $ext 'html';
  210.       
  211.     }
  212.     
  213.     return $ext;
  214.     
  215.   }
  216.   
  217. }
  218.  
  219. ?>

Documentation generated on Mon, 19 Feb 2007 10:25:04 -0800 by phpDocumentor 1.3.1