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

Source for file abstract.php

Documentation is available at abstract.php

  1. <?php
  2. /**
  3.  * @version        $Id: abstract.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4.  * @package        Koowa_View
  5.  * @copyright    Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  6.  * @license        GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  7.  * @link         http://www.nooku.org
  8.  */
  9.  
  10. /**
  11.  * Abstract View Class
  12.  *
  13.  * @author        Johan Janssens <johan@nooku.org>
  14.  * @package        Koowa_View
  15.  * @uses        KMixinClass
  16.  * @uses         KTemplate
  17.  */
  18. abstract class KViewAbstract extends KObject
  19. {
  20.     /**
  21.      * Model identifier (com://APP/COMPONENT.model.NAME)
  22.      *
  23.      * @var    string|object 
  24.      */
  25.     protected $_model;
  26.     
  27.     /**
  28.      * Layout name
  29.      *
  30.      * @var string 
  31.      */
  32.     protected $_layout;
  33.     
  34.     /**
  35.      * The uniform resource locator
  36.      * 
  37.      * @var object 
  38.      */
  39.     protected $_baseurl;
  40.     
  41.     /**
  42.      * The output of the view
  43.      *
  44.      * @var string 
  45.      */
  46.     public $output = '';
  47.     
  48.     /**
  49.      * The mimetype
  50.      * 
  51.      * @var string 
  52.      */
  53.     public $mimetype = '';
  54.     
  55.     /**
  56.      * Constructor
  57.      *
  58.      * @param     object     An optional KConfig object with configuration options
  59.      */
  60.     public function __construct(KConfig $config null)
  61.     {
  62.         //If no config is passed create it
  63.         if(!isset($config)) $config new KConfig();
  64.         
  65.         parent::__construct($config);
  66.         
  67.         //set the base url
  68.         if(!$config->base_url instanceof KHttpUrl{
  69.             $this->_baseurl = KService::get('koowa:http.url'array('url' => $config->base_url));
  70.         else {
  71.             $this->_baseurl = $config->base_url;
  72.         }
  73.         
  74.         $this->output   = $config->output;
  75.         $this->mimetype = $config->mimetype;
  76.         
  77.         $this->setModel($config->model);
  78.         $this->setLayout($config->layout);
  79.     }
  80.  
  81.     /**
  82.      * Initializes the config for the object
  83.      *
  84.      * Called from {@link __construct()} as a first step of object instantiation.
  85.      *
  86.      * @param     object     An optional KConfig object with configuration options
  87.      * @return  void 
  88.      */
  89.     protected function _initialize(KConfig $config)
  90.     {
  91.         $config->append(array(
  92.             'model'       => $this->getName(),
  93.             'output'    => '',
  94.             'mimetype'    => '',
  95.             'layout'    => 'default',
  96.             'base_url'   => '',
  97.           ));
  98.       
  99.         parent::_initialize($config);
  100.     }
  101.     
  102.     /**
  103.      * Get the name
  104.      *
  105.      * @return     string     The name of the object
  106.      */
  107.     public function getName()
  108.     {
  109.         $total count($this->getIdentifier()->path);
  110.         return $this->getIdentifier()->path[$total 1];
  111.     }
  112.     
  113.     /**
  114.      * Get the format
  115.      *
  116.      * @return     string     The format of the view
  117.      */
  118.     public function getFormat()
  119.     {
  120.         return $this->getIdentifier()->name;
  121.     }
  122.  
  123.     /**
  124.      * Return the views output
  125.       *
  126.      * @return string     The output of the view
  127.      */
  128.     public function display()
  129.     {
  130.         return $this->output;
  131.     }
  132.     
  133.     /**
  134.      * Get the model object attached to the contoller
  135.      *
  136.      * @return    KModelAbstract 
  137.      */
  138.     public function getModel()
  139.     {
  140.         if(!$this->_model instanceof KModelAbstract
  141.         {
  142.             //Make sure we have a model identifier
  143.             if(!($this->_model instanceof KServiceIdentifier)) {
  144.                 $this->setModel($this->_model);
  145.             }
  146.           
  147.             $this->_model = $this->getService($this->_model);
  148.         }
  149.  
  150.         return $this->_model;
  151.     }
  152.     
  153.     /**
  154.      * Method to set a model object attached to the view
  155.      *
  156.      * @param    mixed    An object that implements KObjectServiceable, KServiceIdentifier object
  157.      *                      or valid identifier string
  158.      * @throws    KViewException    If the identifier is not a table identifier
  159.      * @return    KViewAbstract 
  160.      */
  161.     public function setModel($model)
  162.     {
  163.         if(!($model instanceof KModelAbstract))
  164.         {
  165.             if(is_string($model&& strpos($model'.'=== false 
  166.             {
  167.                 // Model names are always plural
  168.                 if(KInflector::isSingular($model)) {
  169.                     $model KInflector::pluralize($model);
  170.                 
  171.                 
  172.                 $identifier            clone $this->getIdentifier();
  173.                 $identifier->path    array('model');
  174.                 $identifier->name    $model;
  175.             }
  176.             else $identifier $this->getIdentifier($model);
  177.             
  178.             if($identifier->path[0!= 'model'{
  179.                 throw new KControllerException('Identifier: '.$identifier.' is not a model identifier');
  180.             }
  181.  
  182.             $model $identifier;
  183.         }
  184.         
  185.         $this->_model = $model;
  186.         
  187.         return $this;
  188.     }
  189.     
  190.      /**
  191.      * Get the layout.
  192.      *
  193.      * @return string The layout name
  194.      */
  195.     public function getLayout()
  196.     {
  197.         return $this->_layout;
  198.     }
  199.  
  200.    /**
  201.      * Sets the layout name to use
  202.      *
  203.      * @param    string  The template name.
  204.      * @return   KViewAbstract 
  205.      */
  206.     public function setLayout($layout)
  207.     {
  208.         $this->_layout = $layout;
  209.         return $this;
  210.     }
  211.  
  212.     /**
  213.      * Get a route based on a full or partial query string
  214.      * 
  215.      * option, view and layout can be ommitted. The following variations
  216.      * will all result in the same route
  217.      *
  218.      * - foo=bar
  219.      * - option=com_mycomp&view=myview&foo=bar
  220.      *
  221.      * In templates, use @route()
  222.      *
  223.      * @param    string    The query string used to create the route
  224.      * @param     boolean    If TRUE create a fully qualified route. Default TRUE.
  225.      * @return     string     The route
  226.      */
  227.     public function getRoute$route ''$fqr true)
  228.     {
  229.         //Parse route
  230.         $parts array();
  231.         parse_str(trim($route)$parts);
  232.         
  233.         //Check to see if there is component information in the route if not add it
  234.         if(!isset($parts['option'])) {
  235.             $parts['option''com_'.$this->getIdentifier()->package;
  236.         }
  237.  
  238.         //Add the view information to the route if it's not set
  239.         if(!isset($parts['view'])) 
  240.         {
  241.             $parts['view'$this->getName();
  242.             
  243.             //Add the layout information to the route if it's not set
  244.             if(!isset($parts['layout'])) {
  245.                 $parts['layout'$this->getLayout();
  246.             }
  247.         }
  248.         
  249.         //Add the format information to the route only if it's not 'html'
  250.         if(!isset($parts['format'])) {
  251.             $parts['format'$this->getIdentifier()->name;
  252.         }
  253.         
  254.          //Add the model state only for routes to the same view
  255.         if($parts['view'== $this->getName())
  256.         {
  257.             $state $this->getModel()->getState()->toArray();
  258.             $parts array_merge($state$parts);
  259.         }
  260.         
  261.         //Create the route 
  262.         $route KService::get('koowa:http.url'array('url' => JRoute::_('index.php?'.http_build_query($parts))));
  263.         
  264.         //Add the host and the schema
  265.         if($fqr)
  266.         {
  267.             $route->scheme $this->getBaseUrl()->scheme;
  268.             $route->host   $this->getBaseUrl()->host;
  269.         }
  270.         
  271.         return $route;
  272.     }
  273.     
  274.     /**
  275.      * Get the view base url
  276.      * 
  277.      * @return     object    KHttpUrl object
  278.      */
  279.     public function getBaseUrl()
  280.     {
  281.         return $this->_baseurl;
  282.     }
  283.         
  284.     /**    
  285.      * Returns the views output
  286.       *
  287.      * @return     string 
  288.      */
  289.     public function __toString()
  290.     {
  291.         return $this->display();
  292.     }
  293. }

Documentation generated on Fri, 24 May 2013 03:00:42 +0200 by phpDocumentor 1.4.3