Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/application/component/view.php

Documentation is available at view.php

  1. <?php
  2. /**
  3.  * @version        $Id: view.php 6634 2007-02-15 18:27:18Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Application
  6.  * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Base class for a Joomla View
  20.  *
  21.  * Class holding methods for displaying presentation data.
  22.  *
  23.  * @abstract
  24.  * @package        Joomla.Framework
  25.  * @subpackage    Application
  26.  * @author        Johan Janssens <[email protected]>
  27.  * @author        Louis Landry <[email protected]>
  28.  * @author         Andrew Eddie
  29.  * @since        1.5
  30.  */
  31. class JView extends JObject
  32. {
  33.     /**
  34.      * The name of the view
  35.      *
  36.      * @var        array 
  37.      * @access protected
  38.      */
  39.     var $_name = null;
  40.  
  41.     /**
  42.      * Registered models
  43.      *
  44.      * @var        array 
  45.      * @access protected
  46.      */
  47.     var $_models = array();
  48.  
  49.     /**
  50.      * The default model
  51.      *
  52.      * @var    string 
  53.      * @access protected
  54.      */
  55.     var $_defaultModel = null;
  56.  
  57.     /**
  58.      * Layout name
  59.      *
  60.      * @var        string 
  61.      * @access     protected
  62.      */
  63.     var $_layout = 'default';
  64.  
  65.    /**
  66.     * The set of search directories for resources (templates)
  67.     *
  68.     * @var array 
  69.     * @access protected
  70.     */
  71.     var $_path = array(
  72.         'template' => array()
  73.     );
  74.  
  75.    /**
  76.     * The name of the default template source file.
  77.     *
  78.     * @var string 
  79.     * @access private
  80.     */
  81.     var $_template null;
  82.  
  83.    /**
  84.     * The output of the template script.
  85.     *
  86.     * @var string 
  87.     * @access private
  88.     */
  89.     var $_output null;
  90.  
  91.     /**
  92.     * Array of callbacks used to escape output.
  93.     *
  94.     * @var array 
  95.     * @access private
  96.     */
  97.     var $_escape array('htmlspecialchars');
  98.  
  99.     /**
  100.      * Constructor
  101.      *
  102.      * @access    protected
  103.      */
  104.     function __construct($config array())
  105.     {
  106.         //set the view name
  107.         if (empty$this->_name ))
  108.         {
  109.             if (isset($config['name']))  {
  110.                 $this->_name = $config['name'];
  111.             }
  112.             else
  113.             {
  114.                 $r null;
  115.                 if (!preg_match('/View((view)*(.*(view)?.*))$/i'get_class($this)$r)) {
  116.                     JError::raiseError (500"JView::__construct() : Can't get or parse class name.");
  117.                 }
  118.                 $this->_name = strtolower$r[3);
  119.             }
  120.         }
  121.  
  122.         // set the default template search path
  123.         if (isset($config['template_path'])) {
  124.             // user-defined dirs
  125.             $this->_setPath('template'$config['template_path']);
  126.         else {
  127.             $this->_setPath('template'null);
  128.         }
  129.  
  130.         // set the layout
  131.         if (isset($config['layout'])) {
  132.             $this->setLayout($config['layout']);
  133.         }
  134.     }
  135.  
  136.    /**
  137.     * Execute and display a template script.
  138.     *
  139.     * @param string $tpl The name of the template file to parse;
  140.     *  automatically searches through the template paths.
  141.     *
  142.     * @throws object An JError object.
  143.     * @see fetch()
  144.     */
  145.     function display($tpl null)
  146.     {
  147.         $result $this->loadTemplate($tpl);
  148.         if (JError::isError($result)) {
  149.             return $result;
  150.         }
  151.  
  152.         echo $result;
  153.     }
  154.  
  155.     /**
  156.     * Assigns variables to the view script via differing strategies.
  157.     *
  158.     * This method is overloaded; you can assign all the properties of
  159.     * an object, an associative array, or a single value by name.
  160.     *
  161.     * You are not allowed to set variables that begin with an underscore;
  162.     * these are either private properties for JView or private variables
  163.     * within the template script itself.
  164.     *
  165.     * <code>
  166.     * $view =& new JView();
  167.     *
  168.     * // assign directly
  169.     * $view->var1 = 'something';
  170.     * $view->var2 = 'else';
  171.     *
  172.     * // assign by name and value
  173.     * $view->assign('var1', 'something');
  174.     * $view->assign('var2', 'else');
  175.     *
  176.     * // assign by assoc-array
  177.     * $ary = array('var1' => 'something', 'var2' => 'else');
  178.     * $view->assign($obj);
  179.     *
  180.     * // assign by object
  181.     * $obj = new stdClass;
  182.     * $obj->var1 = 'something';
  183.     * $obj->var2 = 'else';
  184.     * $view->assign($obj);
  185.     *
  186.     * </code>
  187.     *
  188.     * @access public
  189.     * @return bool True on success, false on failure.
  190.     */
  191.     function assign()
  192.     {
  193.         // get the arguments; there may be 1 or 2.
  194.         $arg0 @func_get_arg(0);
  195.         $arg1 @func_get_arg(1);
  196.  
  197.         // assign by object
  198.         if (is_object($arg0))
  199.         {
  200.             // assign public properties
  201.             foreach (get_object_vars($arg0as $key => $val)
  202.             {
  203.                 if (substr($key01!= '_'{
  204.                     $this->$key $val;
  205.                 }
  206.             }
  207.             return true;
  208.         }
  209.  
  210.         // assign by associative array
  211.         if (is_array($arg0))
  212.         {
  213.             foreach ($arg0 as $key => $val)
  214.             {
  215.                 if (substr($key01!= '_'{
  216.                     $this->$key $val;
  217.                 }
  218.             }
  219.             return true;
  220.         }
  221.  
  222.         // assign by string name and mixed value.
  223.  
  224.         // we use array_key_exists() instead of isset() becuase isset()
  225.         // fails if the value is set to null.
  226.         if (is_string($arg0&& substr($arg001!= '_' && func_num_args(1)
  227.         {
  228.             $this->$arg0 $arg1;
  229.             return true;
  230.         }
  231.  
  232.         // $arg0 was not object, array, or string.
  233.         return false;
  234.     }
  235.  
  236.  
  237.     /**
  238.     * Assign variable for the view (by reference).
  239.     *
  240.     * You are not allowed to set variables that begin with an underscore;
  241.     * these are either private properties for JView or private variables
  242.     * within the template script itself.
  243.     *
  244.     * <code>
  245.     * $view = new JView();
  246.     *
  247.     * // assign by name and value
  248.     * $view->assignRef('var1', $ref);
  249.     *
  250.     * // assign directly
  251.     * $view->ref =& $var1;
  252.     * </code>
  253.     *
  254.     * @access public
  255.     *
  256.     * @param string $key The name for the reference in the view.
  257.     * @param mixed &$val The referenced variable.
  258.     *
  259.     * @return bool True on success, false on failure.
  260.     */
  261.  
  262.     function assignRef($key&$val)
  263.     {
  264.         if (is_string($key&& substr($key01!= '_')
  265.         {
  266.             $this->$key =$val;
  267.             return true;
  268.         }
  269.  
  270.         return false;
  271.     }
  272.  
  273.     /**
  274.     * Applies escaping to a value.
  275.     *
  276.     * You can override the predefined escaping callbacks by passing
  277.     * added parameters as replacement callbacks.
  278.     *
  279.     * <code>
  280.     * // use predefined callbacks
  281.     * $result = $view->escape($value);
  282.     *
  283.     * // use replacement callbacks
  284.     * $result = $view->escape(
  285.     *     $value,
  286.     *     'stripslashes',
  287.     *     'htmlspecialchars',
  288.     *     array('StaticClass', 'method'),
  289.     *     array($object, $method)
  290.     * );
  291.     * </code>
  292.     *
  293.     * @access public
  294.     * @param mixed $value The value to be escaped.
  295.     * @return mixed 
  296.     */
  297.     function escape($value)
  298.     {
  299.         // were custom callbacks passed?
  300.         if (func_num_args(== 1)
  301.         {
  302.             // no, only a value was passed.
  303.             // loop through the predefined callbacks.
  304.             foreach ($this->_escape as $func)
  305.             {
  306.                 // this if() shaves 0.001sec off of 300 calls.
  307.                 if (is_string($func)) {
  308.                     $value $func($value);
  309.                 else {
  310.                     $value call_user_func($func$value);
  311.                 }
  312.             }
  313.         }
  314.         else
  315.         {
  316.             // yes, use the custom callbacks
  317.             $callbacks func_get_args();
  318.  
  319.             // drop $value
  320.             array_shift($callbacks);
  321.  
  322.             // loop through custom callbacks.
  323.             foreach ($callbacks as $func)
  324.             {
  325.                 // this if() shaves 0.001sec off of 300 calls.
  326.                 if (is_string($func)) {
  327.                     $value $func($value);
  328.                 else {
  329.                     $value call_user_func($func$value);
  330.                 }
  331.             }
  332.  
  333.         }
  334.  
  335.         return $value;
  336.     }
  337.  
  338.     /**
  339.      * Method to get data from a registered model
  340.      *
  341.      * @access    public
  342.      * @param    string    The name of the method to call on the model
  343.      * @param    string    The name of the model to reference [optional]
  344.      * @return mixed    The return value of the method
  345.      */
  346.     function &get$method$model null )
  347.     {
  348.         $result false;
  349.  
  350.         // If $model is null we use the default model
  351.         if (is_null($model)) {
  352.             $model $this->_defaultModel;
  353.         else {
  354.             $model strtolower$model );
  355.         }
  356.  
  357.         // First check to make sure the model requested exists
  358.         if (isset$this->_models[$model))
  359.         {
  360.             // Model exists, lets build the method name
  361.             $method 'get'.ucfirst($method);
  362.  
  363.             // Does the method exist?
  364.             if (method_exists($this->_models[$model]$method))
  365.             {
  366.                 // The method exists, lets call it and return what we get
  367.                 $result $this->_models[$model]->$method();
  368.             }
  369.             else
  370.             {
  371.                 // Method wasn't found... throw a warning and return false
  372.                 JError::raiseWarning0"Unknown Method $model::$method() was not found");
  373.                 $result false;
  374.             }
  375.         }
  376.         else
  377.         {
  378.             // degrade to JObject::get
  379.             $result parent::get$method$model );
  380.         }
  381.  
  382.         return $result;
  383.     }
  384.  
  385.     /**
  386.      * Method to add a model to the view.  We support a multiple model single
  387.      * view system by which models are referenced by classname.  A caveat to the
  388.      * classname referencing is that any classname prepended by JModel will be
  389.      * referenced by the name without JModel, eg. JModelCategory is just
  390.      * Category.
  391.      *
  392.      * @access    public
  393.      * @param    object    $model        The model to add to the view.
  394.      * @param    boolean    $default    Is this the default model?
  395.      * @return    object                The added model
  396.      */
  397.     function &setModel&$model$default false )
  398.     {
  399.         $name strtolower(get_class($model));
  400.         $this->_models[$name&$model;
  401.  
  402.         if ($default{
  403.             $this->_defaultModel = $name;
  404.         }
  405.         return $model;
  406.     }
  407.  
  408.     /**
  409.      * Method to get the model object
  410.      *
  411.      * @access    public
  412.      * @param    string    $name    The name of the model (optional)
  413.      * @return    mixed            JModel object
  414.      */
  415.     function &getModel$name null )
  416.     {
  417.         if ($name === null{
  418.             $name $this->_defaultModel;
  419.         }
  420.         return $this->_models[strtolower$name )];
  421.     }
  422.  
  423.     /**
  424.     * Sets the layout name to use
  425.     *
  426.     * @access public
  427.     * @param string $template The template name.
  428.     */
  429.  
  430.     function setLayout($layout)
  431.     {
  432.         $this->_layout = $layout;
  433.     }
  434.  
  435.     /**
  436.     * Get the layout.
  437.     *
  438.     * @access public
  439.     * @return string The layout name
  440.     */
  441.  
  442.     function getLayout()
  443.     {
  444.         return $this->_layout;
  445.     }
  446.  
  447.     /**
  448.      * Adds to the stack of view script paths in LIFO order.
  449.      *
  450.      * @param string|arrayThe directory (-ies) to add.
  451.      * @return void 
  452.      */
  453.     function addTemplatePath($path)
  454.     {
  455.         $this->_addPath('template'$path);
  456.     }
  457.  
  458.     /**
  459.     * Clears then sets the callbacks to use when calling JView::escape().
  460.     *
  461.     * Each parameter passed to this function is treated as a separate
  462.     * callback.  For example:
  463.     *
  464.     * <code>
  465.     * $view->setEscape(
  466.     *     'stripslashes',
  467.     *     'htmlspecialchars',
  468.     *     array('StaticClass', 'method'),
  469.     *     array($object, $method)
  470.     * );
  471.     * </code>
  472.     *
  473.     * @access public
  474.     */
  475.     function setEscape()
  476.     {
  477.         $this->_escape = (array) @func_get_args();
  478.     }
  479.  
  480.  
  481.     /**
  482.     * Adds to the callbacks used when calling JView::escape().
  483.     *
  484.     * Each parameter passed to this function is treated as a separate
  485.     * callback.  For example:
  486.     *
  487.     * <code>
  488.     * $savant->addEscape(
  489.     *     'stripslashes',
  490.     *     'htmlspecialchars',
  491.     *     array('StaticClass', 'method'),
  492.     *     array($object, $method)
  493.     * );
  494.     * </code>
  495.     *
  496.     * @access public
  497.     *
  498.     * @return void 
  499.     *
  500.     */
  501.     function addEscape()
  502.     {
  503.         $args = (array) @func_get_args();
  504.         $this->_escape array_merge($this->_escape$args);
  505.     }
  506.  
  507.     /**
  508.      * Load a template file -- first look in the templates folder for an override
  509.      *
  510.      * @access    protected
  511.      * @param string $_tpl The name of the template source file ...
  512.      *  automatically searches the template paths and compiles as needed.
  513.      * @return string The output of the the template script.
  514.      */
  515.     function loadTemplate$tpl null)
  516.     {
  517.         global $mainframe$option;
  518.  
  519.         // clear prior output
  520.         $this->_output null;
  521.  
  522.         //create the template file name based on the layout
  523.         $file = isset($tpl$this->_layout.'_'.$tpl $this->_layout;
  524.         // clean the file name
  525.         $file preg_replace'#[^\w_\.]#'''$file );
  526.  
  527.         // load the template script
  528.         jimport('joomla.filesystem.path');
  529.         $this->_template JPath::find($this->_path['template']$this->_createFileName('template'array('name' => $file)));
  530.         if ($this->_template == false)
  531.         {
  532.             $file2 = isset($tpl'default_'.$tpl 'default';
  533.             $this->_template JPath::find($this->_path['template']$this->_createFileName('template'array('name' => $file2)));
  534.         }
  535.  
  536.         if ($this->_template != false)
  537.         {
  538.             // unset so as not to introduce into template scope
  539.             unset($tpl);
  540.             unset($file);
  541.  
  542.             // never allow a 'this' property
  543.             if (isset($this->this)) {
  544.                 unset($this->this);
  545.             }
  546.  
  547.             // start capturing output into a buffer
  548.             ob_start();
  549.             // include the requested template filename in the local scope
  550.             // (this will execute the view logic).
  551.             include $this->_template;
  552.  
  553.             // done with the requested template; get the buffer and
  554.             // clear it.
  555.             $this->_output ob_get_contents();
  556.             ob_end_clean();
  557.  
  558.             return $this->_output;
  559.         }
  560.         else {
  561.             return JError::raiseError500'Layout "' $file '" not found' );
  562.         }
  563.     }
  564.  
  565.    /**
  566.     * Sets an entire array of search paths for templates or resources.
  567.     *
  568.     * @access protected
  569.     * @param string $type The type of path to set, typically 'template'.
  570.     * @param string|array$path The new set of search paths.  If null or
  571.     *  false, resets to the current directory only.
  572.     */
  573.     function _setPath($type$path)
  574.     {
  575.         global $mainframe$option;
  576.  
  577.         // clear out the prior search dirs
  578.         $this->_path[$typearray();
  579.  
  580.         // always add the fallback directories as last resort
  581.         switch (strtolower($type))
  582.         {
  583.             case 'template':
  584.                 // the current directory
  585.                 $this->_addPath($typeJPATH_COMPONENT.DS.'views'.DS.$this->_name.DS.'tmpl');
  586.  
  587.                 // set the alternative template searh dir
  588.                 if (isset($mainframe)) {
  589.                     $fallback JPATH_BASE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.$option.DS.$this->_name;
  590.                     $this->_addPath('template'$fallback);
  591.                 }
  592.                 break;
  593.         }
  594.  
  595.         // actually add the user-specified directories
  596.         $this->_addPath($type$path);
  597.     }
  598.  
  599.    /**
  600.     * Adds to the search path for templates and resources.
  601.     *
  602.     * @access protected
  603.     * @param string|array$path The directory or stream to search.
  604.     */
  605.     function _addPath($type$path)
  606.     {
  607.         // just force to array
  608.         settype($path'array');
  609.  
  610.         // loop through the path directories
  611.         foreach ($path as $dir)
  612.         {
  613.             // no surrounding spaces allowed!
  614.             $dir trim($dir);
  615.  
  616.             // add trailing separators as needed
  617.             if (substr($dir-1!= DIRECTORY_SEPARATOR{
  618.                 // directory
  619.                 $dir .= DIRECTORY_SEPARATOR;
  620.             }
  621.  
  622.             // add to the top of the search dirs
  623.             array_unshift($this->_path[$type]$dir);
  624.         }
  625.     }
  626.  
  627.     /**
  628.      * Create the filename for a resource
  629.      *
  630.      * @access private
  631.      * @param string     $type  The resource type to create the filename for
  632.      * @param array     $parts An associative array of filename information
  633.      * @return string The filename
  634.      * @since 1.5
  635.      */
  636.     function _createFileName($type$parts array())
  637.     {
  638.         $filename '';
  639.  
  640.         switch($type)
  641.         {
  642.             case 'template' :
  643.                  $filename strtolower($parts['name']).'.php';
  644.                 break;
  645.  
  646.         }
  647.         return $filename;
  648.     }
  649. }
  650. ?>

Documentation generated on Mon, 05 Mar 2007 21:31:28 +0000 by phpDocumentor 1.3.1