Location: PHPKode > projects > Dbscript > db/library/dbscript/view.php
<?php

  /** 
   * dbscript for PHP 4 & 5 - restful crud framework
   * @version 0.3.0 -- 10-Jun-2007
   * @author Brian Hendrickson <hide@address.com>
   * @link http://dbscript.net/
   * @copyright Copyright 2007 Brian Hendrickson
   * @package dbscript
   * @license http://www.opensource.org/licenses/mit-license.php MIT License
   */

  /**
   * View
   * 
   * More info...
   * {@link http://dbscript.net/view}
   * 
   * @package dbscript
   * @author Brian Hendrickson <hide@address.com>
   * @access public
   * @version 0.3.0
   */

class View {
  
  var $negotiator;
  var $controller;
  var $collection;
  var $named_vars;
  var $layout;
  var $extension;
  var $header_sent;
  
  function View() {
    
    $this->named_vars = array();
    $this->header_sent = false;
    
    $db =& db_object();
    $request =& request_object();
    $env =& environment();
    
    $Entry =& $db->get_table( 'entries' );    
    
    if ( isset( $request->resource ))
      $this->collection = new Collection( $request->resource );
    else
      $this->collection = new Collection( null );
    
    $this->named_vars['db'] =& $db;
    $this->named_vars['request'] =& $request;
    $this->named_vars['collection'] =& $this->collection;
    $this->named_vars['response'] =& $this;

    if ( isset( $request->resource ))
      $this->named_vars['resource'] =& $db->get_table( $request->resource );
    else
      $this->named_vars['resource'] = false;
    
    $this->controller = $request->controller;
    
    // check for a controller file in db/controllers/[resource].php
    if ( isset( $request->resource )) {
      $cont = controller_path() . $request->resource . ".php";
      if ( file_exists( $cont ))
        $this->controller = $request->resource . ".php";
    }
    
    if ( file_exists( controller_path() . $this->controller ))
      require_once( controller_path() . $this->controller );
    
    if (!(isset($env['content_types'])))
      trigger_error( 'Sorry, the content_types array was not found in the configuration file', E_USER_ERROR );
    
    $this->negotiator = HTTP_Negotiate::choose( $env['content_types'] );
    
  }
  
  function render( &$request ) {
    
    // boot.php calls $response->render()

    $db =& db_object();
    
    $ext = $this->pick_template_extension( $request );
    
    $view = $request->get_template_path( $ext );
    
    $action = $request->action;
    
    if (!(function_exists($action)))
      $action = 'index';
    
    if ( file_exists( $view ) ) {
      
      if (function_exists( $action )) {
        trigger_before( $action, $request, $db );
        $result = $action( array_merge( $this->named_vars, $db->get_objects() ));
        trigger_after( $action, $request, $db );
        if ( is_array( $result ))
          extract( $result );
      }
      
      $content_type = 'Content-Type: ' . $this->pick_content_type( $ext );
      if ($this->pick_content_charset( $ext ))
        $content_type .= '; charset=' . $this->pick_content_charset( $ext );
        
      if ( !$this->header_sent ) {
        
        header( $content_type );
        $this->header_sent = true;
        include( $view );
        
      } else {
        
        $this->render_partial( $request, $request->action );
        
      }

    } else {

      $this->render_partial( $request, $request->action );

    }
    
  }
  
  function render_partial( &$request, $template ) {
    
    // content_for_layout() passes the $request->action as $template
    
    $ext = $this->pick_template_extension( $request, $template );
    
    $view = $request->get_template_path( $ext, $template );
    
    if ($template == 'get')
      $template = 'index';
    
    if (file_exists($view))
      $action = "_" . $template;
    else
      $action = $template;
    
    $db =& db_object();
    
    if ( function_exists( $action )) {
      trigger_before( $action, $request, $db );
      $result = $action( array_merge( $this->named_vars, $db->get_objects() ));
      trigger_after( $action, $request, $db );
      if ( is_array( $result ))
        extract( $result );
    }
    
    if ( file_exists( $view ) ) {
      if (!($this->header_sent)) {
        $content_type = 'Content-Type: ' . $this->pick_content_type( $ext );
        if ($this->pick_content_charset( $ext ))
          $content_type .= '; charset=' . $this->pick_content_charset( $ext );
        header( $content_type );
        $this->header_sent = true;
      }
      include( $view );
    }
      
  }
  
  function negotiate_content( &$request, $template ) {
    foreach ( $this->negotiator as $client_wants ) {
      if ( template_exists( $request, $client_wants['id'], $template ))
        return $client_wants['id'];
    }
  }
    
  function pick_content_type( $ext ) {
    $variants = content_types();
    foreach($variants as $content_type) {
      if ($content_type['id'] == $ext && $content_type['type'] != null)
        return $content_type['type'];
    }
    return false;
  }

  function pick_content_charset( $ext ) {
    $variants = content_types();
    foreach($variants as $content_type) {
      if ($content_type['id'] == $ext)
        return $content_type['charset'];
    }
    return false;
  }
  
  function set_content_encoding( $ext ) {
    $variants = content_types();
    foreach($variants as $content_type) {
      if (($content_type['id'] == $ext) && $content_type['encoding'])
        header( "Content-Encoding: ". $content_type['encoding'] );
    }
  }
  
  function pick_template_extension( &$request, $template = null ) {
    
    if (isset($request->params['client_wants']))
      return $request->params['client_wants'];
      
    $ext = $this->negotiate_content( $request, $template );
    
    if (!$ext) {
      
      // if ( content-negotiation fails ) go to html
      
      $variants = array(
        array(
          'id' => 'html',
          'qs' => 1.000,
          'type' => 'text/html',
          'encoding' => null,
          'charset' => 'iso-8859-1',
          'language' => 'en',
          'size' => 3000
        )
      );
      
      $this->negotiator = $variants;
      
      $ext = 'html';
      
    }
    
    return $ext;
    
  }
  
}

?>
Return current item: Dbscript