Source for file view.php

Documentation is available at view.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  view
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. /**
  12.  * FrameworkOnFramework View class. The View is the MVC component which gets the
  13.  * raw data from a Model and renders it in a way that makes sense. The usual
  14.  * rendering is HTML, but you can also output JSON, CSV, XML, or even media
  15.  * (images, videos, ...) and documents (Word, PDF, Excel...).
  16.  *
  17.  * @package  FrameworkOnFramework
  18.  * @since    1.0
  19.  */
  20. abstract class FOFView extends JObject
  21. {
  22.     /**
  23.      * The name of the view
  24.      *
  25.      * @var    array 
  26.      */
  27.     protected $_name = null;
  28.  
  29.     /**
  30.      * Registered models
  31.      *
  32.      * @var    array 
  33.      */
  34.     protected $_models = array();
  35.  
  36.     /**
  37.      * The base path of the view
  38.      *
  39.      * @var    string 
  40.      */
  41.     protected $_basePath = null;
  42.  
  43.     /**
  44.      * The default model
  45.      *
  46.      * @var    string 
  47.      */
  48.     protected $_defaultModel = null;
  49.  
  50.     /**
  51.      * Layout name
  52.      *
  53.      * @var    string 
  54.      */
  55.     protected $_layout = 'default';
  56.  
  57.     /**
  58.      * Layout extension
  59.      *
  60.      * @var    string 
  61.      */
  62.     protected $_layoutExt = 'php';
  63.  
  64.     /**
  65.      * Layout template
  66.      *
  67.      * @var    string 
  68.      */
  69.     protected $_layoutTemplate = '_';
  70.  
  71.     /**
  72.      * The set of search directories for resources (templates)
  73.      *
  74.      * @var array 
  75.      */
  76.     protected $_path = array('template' => array()'helper' => array());
  77.  
  78.     /**
  79.      * The name of the default template source file.
  80.      *
  81.      * @var string 
  82.      */
  83.     protected $_template = null;
  84.  
  85.     /**
  86.      * The output of the template script.
  87.      *
  88.      * @var string 
  89.      */
  90.     protected $_output = null;
  91.  
  92.     /**
  93.      * Callback for escaping.
  94.      *
  95.      * @var string 
  96.      * @deprecated 13.3
  97.      */
  98.     protected $_escape = 'htmlspecialchars';
  99.  
  100.     /**
  101.      * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8)
  102.      *
  103.      * @var string 
  104.      */
  105.     protected $_charset = 'UTF-8';
  106.  
  107.     /**
  108.      * The available renderer objects we can use to render views
  109.      *
  110.      * @var    array  Contains objects of the FOFRenderAbstract class
  111.      */
  112.     public static $renderers array();
  113.  
  114.     /**
  115.      * Cache of the configuration array
  116.      *
  117.      * @var    array 
  118.      */
  119.     protected $config = array();
  120.  
  121.     /**
  122.      * The input object of this view
  123.      *
  124.      * @var    FOFInput 
  125.      */
  126.     protected $input = null;
  127.  
  128.     /**
  129.      * The chosen renderer object
  130.      *
  131.      * @var    FOFRenderAbstract 
  132.      */
  133.     protected $rendererObject = null;
  134.  
  135.     /**
  136.      * Should I run the pre-render step?
  137.      *
  138.      * @var    boolean 
  139.      */
  140.     protected $doPreRender = true;
  141.  
  142.     /**
  143.      * Should I run the post-render step?
  144.      *
  145.      * @var    boolean 
  146.      */
  147.     protected $doPostRender = true;
  148.  
  149.     /**
  150.      * Public constructor. Instantiates a FOFView object.
  151.      *
  152.      * @param   array  $config  The configuration data array
  153.      */
  154.     public function __construct($config array())
  155.     {
  156.         // Make sure $config is an array
  157.         if (is_object($config))
  158.         {
  159.             $config = (array) $config;
  160.         }
  161.         elseif (!is_array($config))
  162.         {
  163.             $config array();
  164.         }
  165.  
  166.         // Get the input
  167.         if (array_key_exists('input'$config))
  168.         {
  169.             if ($config['input'instanceof FOFInput)
  170.             {
  171.                 $this->input = $config['input'];
  172.             }
  173.             else
  174.             {
  175.                 $this->input = new FOFInput($config['input']);
  176.             }
  177.         }
  178.         else
  179.         {
  180.             $this->input = new FOFInput;
  181.         }
  182.  
  183.         parent::__construct($config);
  184.  
  185.         $component 'com_foobar';
  186.  
  187.         // Get the component name
  188.         if (array_key_exists('input'$config))
  189.         {
  190.             if ($config['input'instanceof FOFInput)
  191.             {
  192.                 $tmpInput $config['input'];
  193.             }
  194.             else
  195.             {
  196.                 $tmpInput new FOFInput($config['input']);
  197.             }
  198.  
  199.             $component $tmpInput->getCmd('option''');
  200.         }
  201.         else
  202.         {
  203.             $tmpInput $this->input;
  204.         }
  205.  
  206.         if (array_key_exists('option'$config))
  207.         {
  208.             if ($config['option'])
  209.             {
  210.                 $component $config['option'];
  211.             }
  212.         }
  213.  
  214.         $config['option'$component;
  215.  
  216.         // Get the view name
  217.         $view null;
  218.         if (array_key_exists('input'$config))
  219.         {
  220.             $view $tmpInput->getCmd('view''');
  221.         }
  222.  
  223.         if (array_key_exists('view'$config))
  224.         {
  225.             if ($config['view'])
  226.             {
  227.                 $view $config['view'];
  228.             }
  229.         }
  230.  
  231.         $config['view'$view;
  232.  
  233.         // Set the component and the view to the input array
  234.  
  235.         if (array_key_exists('input'$config))
  236.         {
  237.             $tmpInput->set('option'$config['option']);
  238.             $tmpInput->set('view'$config['view']);
  239.         }
  240.  
  241.         // Set the view name
  242.  
  243.         if (array_key_exists('name'$config))
  244.         {
  245.             $this->_name = $config['name'];
  246.         }
  247.         else
  248.         {
  249.             $this->_name = $config['view'];
  250.         }
  251.  
  252.         $tmpInput->set('view'$this->_name);
  253.         $config['input'$tmpInput;
  254.         $config['name'$this->_name;
  255.         $config['view'$this->_name;
  256.  
  257.         // Get the component directories
  258.         $componentPaths FOFPlatform::getInstance()->getComponentBaseDirs($config['option']);
  259.  
  260.         // Set the charset (used by the variable escaping functions)
  261.  
  262.         if (array_key_exists('charset'$config))
  263.         {
  264.             JLog::add('Setting a custom charset for escaping is deprecated. Override FOFView::escape() instead.'JLog::WARNING'deprecated');
  265.             $this->_charset = $config['charset'];
  266.         }
  267.  
  268.         // User-defined escaping callback
  269.  
  270.         if (array_key_exists('escape'$config))
  271.         {
  272.             $this->setEscape($config['escape']);
  273.         }
  274.  
  275.         // Set a base path for use by the view
  276.  
  277.         if (array_key_exists('base_path'$config))
  278.         {
  279.             $this->_basePath = $config['base_path'];
  280.         }
  281.         else
  282.         {
  283.             $this->_basePath = $componentPaths['main'];
  284.         }
  285.  
  286.         // Set the default template search path
  287.  
  288.         if (array_key_exists('template_path'$config))
  289.         {
  290.             // User-defined dirs
  291.             $this->_setPath('template'$config['template_path']);
  292.         }
  293.         else
  294.         {
  295.             $altView FOFInflector::isSingular($this->getName()) FOFInflector::pluralize($this->getName()) FOFInflector::singularize($this->getName());
  296.             $this->_setPath('template'$this->_basePath . '/views/' $altView '/tmpl');
  297.             $this->_addPath('template'$this->_basePath . '/views/' $this->getName('/tmpl');
  298.         }
  299.  
  300.         // Set the default helper search path
  301.  
  302.         if (array_key_exists('helper_path'$config))
  303.         {
  304.             // User-defined dirs
  305.             $this->_setPath('helper'$config['helper_path']);
  306.         }
  307.         else
  308.         {
  309.             $this->_setPath('helper'$this->_basePath . '/helpers');
  310.         }
  311.  
  312.         // Set the layout
  313.  
  314.         if (array_key_exists('layout'$config))
  315.         {
  316.             $this->setLayout($config['layout']);
  317.         }
  318.         else
  319.         {
  320.             $this->setLayout('default');
  321.         }
  322.  
  323.         $this->config = $config;
  324.  
  325.         if (!FOFPlatform::getInstance()->isCli())
  326.         {
  327.             $this->baseurl JURI::base(true);
  328.  
  329.             $fallback FOFPlatform::getInstance()->getTemplateOverridePath($component'/' $this->getName();
  330.             $this->_addPath('template'$fallback);
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * Loads a template given any path. The path is in the format:
  336.      * [admin|site]:com_foobar/viewname/templatename
  337.      * e.g. admin:com_foobar/myview/default
  338.      *
  339.      * This function searches for Joomla! version override templates. For example,
  340.      * if you have run this under Joomla! 3.0 and you try to load
  341.      * admin:com_foobar/myview/default it will automatically search for the
  342.      * template files default.j30.php, default.j3.php and default.php, in this
  343.      * order.
  344.      *
  345.      * @param   string  $path         See above
  346.      * @param   array   $forceParams  A hash array of variables to be extracted in the local scope of the template file
  347.      *
  348.      * @return  boolean  False if loading failed
  349.      */
  350.     public function loadAnyTemplate($path ''$forceParams array())
  351.     {
  352.         // Automatically check for a Joomla! version specific override
  353.         $throwErrorIfNotFound true;
  354.  
  355.         $suffixes FOFPlatform::getInstance()->getTemplateSuffixes();
  356.  
  357.         foreach ($suffixes as $suffix)
  358.         {
  359.             if (substr($path-strlen($suffix)) == $suffix)
  360.             {
  361.                 $throwErrorIfNotFound false;
  362.                 break;
  363.             }
  364.         }
  365.  
  366.         if ($throwErrorIfNotFound)
  367.         {
  368.             foreach ($suffixes as $suffix)
  369.             {
  370.                 $result $this->loadAnyTemplate($path $suffix$forceParams);
  371.  
  372.                 if ($result !== false)
  373.                 {
  374.                     return $result;
  375.                 }
  376.             }
  377.         }
  378.  
  379.         $layoutTemplate $this->getLayoutTemplate();
  380.  
  381.         // Parse the path
  382.         $templateParts $this->_parseTemplatePath($path);
  383.  
  384.         // Get the paths
  385.         $componentPaths FOFPlatform::getInstance()->getComponentBaseDirs($templateParts['component']);
  386.         $templatePath FOFPlatform::getInstance()->getTemplateOverridePath($templateParts['component']);
  387.  
  388.         // Get the default paths
  389.         $paths array();
  390.         $paths[$templatePath '/' $templateParts['view'];
  391.         $paths[($templateParts['admin'$componentPaths['admin'$componentPaths['site']'/views/' $templateParts['view''/tmpl';
  392.  
  393.         if (isset($this->_path|| property_exists($this'_path'))
  394.         {
  395.             $paths array_merge($paths$this->_path['template']);
  396.         }
  397.         elseif (isset($this->path|| property_exists($this'path'))
  398.         {
  399.             $paths array_merge($paths$this->path['template']);
  400.         }
  401.  
  402.         // Look for a template override
  403.  
  404.         if (isset($layoutTemplate&& $layoutTemplate != '_' && $layoutTemplate != $template)
  405.         {
  406.             $apath array_shift($paths);
  407.             array_unshift($pathsstr_replace($template$layoutTemplate$apath));
  408.         }
  409.  
  410.         $filetofind $templateParts['template''.php';
  411.         JLoader::import('joomla.filesystem.path');
  412.         $this->_tempFilePath JPath::find($paths$filetofind);
  413.  
  414.         if ($this->_tempFilePath)
  415.         {
  416.             // Unset from local scope
  417.             unset($template);
  418.             unset($layoutTemplate);
  419.             unset($paths);
  420.             unset($path);
  421.             unset($filetofind);
  422.  
  423.             // Never allow a 'this' property
  424.  
  425.             if (isset($this->this))
  426.             {
  427.                 unset($this->this);
  428.             }
  429.  
  430.             // Force parameters into scope
  431.  
  432.             if (!empty($forceParams))
  433.             {
  434.                 extract($forceParams);
  435.             }
  436.  
  437.             // Start capturing output into a buffer
  438.             ob_start();
  439.  
  440.             // Include the requested template filename in the local scope (this will execute the view logic).
  441.             include $this->_tempFilePath;
  442.  
  443.             // Done with the requested template; get the buffer and clear it.
  444.             $this->_output = ob_get_contents();
  445.             ob_end_clean();
  446.  
  447.             return $this->_output;
  448.         }
  449.         else
  450.         {
  451.             if ($throwErrorIfNotFound)
  452.             {
  453.                 return new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND'$path)500);
  454.             }
  455.  
  456.             return false;
  457.         }
  458.     }
  459.  
  460.     /**
  461.      * Overrides the default method to execute and display a template script.
  462.      * Instead of loadTemplate is uses loadAnyTemplate which allows for automatic
  463.      * Joomla! version overrides. A little slice of awesome pie!
  464.      *
  465.      * @param   string  $tpl  The name of the template file to parse
  466.      *
  467.      * @return  mixed  A string if successful, otherwise a JError object.
  468.      */
  469.     public function display($tpl null)
  470.     {
  471.         FOFPlatform::getInstance()->setErrorHandling(E_ALL'ignore');
  472.  
  473.         $result $this->loadTemplate($tpl);
  474.  
  475.         if ($result instanceof Exception)
  476.         {
  477.             JError::raiseError($result->getCode()$result->getMessage());
  478.  
  479.             return $result;
  480.         }
  481.  
  482.         echo $result;
  483.     }
  484.  
  485.     /**
  486.      * Assigns variables to the view script via differing strategies.
  487.      *
  488.      * This method is overloaded; you can assign all the properties of
  489.      * an object, an associative array, or a single value by name.
  490.      *
  491.      * You are not allowed to set variables that begin with an underscore;
  492.      * these are either private properties for FOFView or private variables
  493.      * within the template script itself.
  494.      *
  495.      * @return  boolean  True on success, false on failure.
  496.      *
  497.      * @deprecated  13.3 Use native PHP syntax.
  498.      */
  499.     public function assign()
  500.     {
  501.         JLog::add(__METHOD__ . ' is deprecated. Use native PHP syntax.'JLog::WARNING'deprecated');
  502.  
  503.         // Get the arguments; there may be 1 or 2.
  504.         $arg0 @func_get_arg(0);
  505.         $arg1 @func_get_arg(1);
  506.  
  507.         // Assign by object
  508.  
  509.         if (is_object($arg0))
  510.         {
  511.             // Assign public properties
  512.             foreach (get_object_vars($arg0as $key => $val)
  513.             {
  514.                 if (substr($key01!= '_')
  515.                 {
  516.                     $this->$key $val;
  517.                 }
  518.             }
  519.  
  520.             return true;
  521.         }
  522.  
  523.         // Assign by associative array
  524.  
  525.         if (is_array($arg0))
  526.         {
  527.             foreach ($arg0 as $key => $val)
  528.             {
  529.                 if (substr($key01!= '_')
  530.                 {
  531.                     $this->$key $val;
  532.                 }
  533.             }
  534.  
  535.             return true;
  536.         }
  537.  
  538.         // Assign by string name and mixed value. We use array_key_exists() instead of isset()
  539.         // because isset() fails if the value is set to null.
  540.  
  541.         if (is_string($arg0&& substr($arg001!= '_' && func_num_args(1)
  542.         {
  543.             $this->$arg0 $arg1;
  544.  
  545.             return true;
  546.         }
  547.  
  548.         // $arg0 was not object, array, or string.
  549.         return false;
  550.     }
  551.  
  552.     /**
  553.      * Assign variable for the view (by reference).
  554.      *
  555.      * You are not allowed to set variables that begin with an underscore;
  556.      * these are either private properties for FOFView or private variables
  557.      * within the template script itself.
  558.      *
  559.      * @param   string  $key   The name for the reference in the view.
  560.      * @param   mixed   &$val  The referenced variable.
  561.      *
  562.      * @return  boolean  True on success, false on failure.
  563.      *
  564.      * @deprecated  13.3  Use native PHP syntax.
  565.      */
  566.     public function assignRef($key&$val)
  567.     {
  568.         JLog::add(__METHOD__ . ' is deprecated. Use native PHP syntax.'JLog::WARNING'deprecated');
  569.  
  570.         if (is_string($key&& substr($key01!= '_')
  571.         {
  572.             $this->$key &$val;
  573.  
  574.             return true;
  575.         }
  576.  
  577.         return false;
  578.     }
  579.  
  580.     /**
  581.      * Escapes a value for output in a view script.
  582.      *
  583.      * If escaping mechanism is either htmlspecialchars or htmlentities, uses
  584.      * {@link $_encoding} setting.
  585.      *
  586.      * @param   mixed  $var  The output to escape.
  587.      *
  588.      * @return  mixed  The escaped value.
  589.      */
  590.     public function escape($var)
  591.     {
  592.         if (in_array($this->_escapearray('htmlspecialchars''htmlentities')))
  593.         {
  594.             return call_user_func($this->_escape$varENT_COMPAT$this->_charset);
  595.         }
  596.  
  597.         return call_user_func($this->_escape$var);
  598.     }
  599.  
  600.     /**
  601.      * Method to get data from a registered model or a property of the view
  602.      *
  603.      * @param   string  $property  The name of the method to call on the model or the property to get
  604.      * @param   string  $default   The name of the model to reference or the default value [optional]
  605.      *
  606.      * @return  mixed  The return value of the method
  607.      */
  608.     public function get($property$default null)
  609.     {
  610.         // If $model is null we use the default model
  611.         if (is_null($default))
  612.         {
  613.             $model $this->_defaultModel;
  614.         }
  615.         else
  616.         {
  617.             $model strtolower($default);
  618.         }
  619.  
  620.         // First check to make sure the model requested exists
  621.         if (isset($this->_models[$model]))
  622.         {
  623.             // Model exists, let's build the method name
  624.             $method 'get' ucfirst($property);
  625.  
  626.             // Does the method exist?
  627.             if (method_exists($this->_models[$model]$method))
  628.             {
  629.                 // The method exists, let's call it and return what we get
  630.                 $result $this->_models[$model]->$method();
  631.  
  632.                 return $result;
  633.             }
  634.         }
  635.  
  636.         // Degrade to JObject::get
  637.         $result parent::get($property$default);
  638.  
  639.         return $result;
  640.     }
  641.  
  642.     /**
  643.      * Method to get the model object
  644.      *
  645.      * @param   string  $name  The name of the model (optional)
  646.      *
  647.      * @return  mixed  FOFModel object
  648.      */
  649.     public function getModel($name null)
  650.     {
  651.         if ($name === null)
  652.         {
  653.             $name $this->_defaultModel;
  654.         }
  655.  
  656.         return $this->_models[strtolower($name)];
  657.     }
  658.  
  659.     /**
  660.      * Get the layout.
  661.      *
  662.      * @return  string  The layout name
  663.      */
  664.     public function getLayout()
  665.     {
  666.         return $this->_layout;
  667.     }
  668.  
  669.     /**
  670.      * Get the layout template.
  671.      *
  672.      * @return  string  The layout template name
  673.      */
  674.     public function getLayoutTemplate()
  675.     {
  676.         return $this->_layoutTemplate;
  677.     }
  678.  
  679.     /**
  680.      * Method to get the view name
  681.      *
  682.      * The model name by default parsed using the classname, or it can be set
  683.      * by passing a $config['name'] in the class constructor
  684.      *
  685.      * @return  string  The name of the model
  686.      */
  687.     public function getName()
  688.     {
  689.         if (empty($this->_name))
  690.         {
  691.             $classname get_class($this);
  692.             $viewpos strpos($classname'View');
  693.  
  694.             if ($viewpos === false)
  695.             {
  696.                 throw new Exception(JText::_('JLIB_APPLICATION_ERROR_VIEW_GET_NAME')500);
  697.             }
  698.  
  699.             $this->_name = strtolower(substr($classname$viewpos 4));
  700.         }
  701.  
  702.         return $this->_name;
  703.     }
  704.  
  705.     /**
  706.      * Method to add a model to the view.
  707.      *
  708.      * @param   FOFMOdel  $model    The model to add to the view.
  709.      * @param   boolean   $default  Is this the default model?
  710.      * @param   String    $name     optional index name to store the model
  711.      *
  712.      * @return  object   The added model.
  713.      */
  714.     public function setModel($model$default false$name null)
  715.     {
  716.         if (is_null($name))
  717.         {
  718.             $name $model->getName();
  719.         }
  720.  
  721.         $name strtolower($name);
  722.  
  723.         $this->_models[$name$model;
  724.  
  725.         if ($default)
  726.         {
  727.             $this->_defaultModel = $name;
  728.         }
  729.  
  730.         return $model;
  731.     }
  732.  
  733.     /**
  734.      * Sets the layout name to use
  735.      *
  736.      * @param   string  $layout  The layout name or a string in format <template>:<layout file>
  737.      *
  738.      * @return  string  Previous value.
  739.      */
  740.     public function setLayout($layout)
  741.     {
  742.         $previous $this->_layout;
  743.  
  744.         if (strpos($layout':'=== false)
  745.         {
  746.             $this->_layout = $layout;
  747.         }
  748.         else
  749.         {
  750.             // Convert parameter to array based on :
  751.             $temp explode(':'$layout);
  752.             $this->_layout = $temp[1];
  753.  
  754.             // Set layout template
  755.             $this->_layoutTemplate = $temp[0];
  756.         }
  757.  
  758.         return $previous;
  759.     }
  760.  
  761.     /**
  762.      * Allows a different extension for the layout files to be used
  763.      *
  764.      * @param   string  $value  The extension.
  765.      *
  766.      * @return  string   Previous value
  767.      */
  768.     public function setLayoutExt($value)
  769.     {
  770.         $previous $this->_layoutExt;
  771.  
  772.         if ($value preg_replace('#[^A-Za-z0-9]#'''trim($value)))
  773.         {
  774.             $this->_layoutExt = $value;
  775.         }
  776.  
  777.         return $previous;
  778.     }
  779.  
  780.     /**
  781.      * Sets the _escape() callback.
  782.      *
  783.      * @param   mixed  $spec  The callback for _escape() to use.
  784.      *
  785.      * @return  void 
  786.      *
  787.      * @deprecated  2.1  Override FOFView::escape() instead.
  788.      */
  789.     public function setEscape($spec)
  790.     {
  791.         JLog::add(__METHOD__ . ' is deprecated. Override FOFView::escape() instead.'JLog::WARNING'deprecated');
  792.  
  793.         $this->_escape = $spec;
  794.     }
  795.  
  796.     /**
  797.      * Adds to the stack of view script paths in LIFO order.
  798.      *
  799.      * @param   mixed  $path  A directory path or an array of paths.
  800.      *
  801.      * @return  void 
  802.      */
  803.     public function addTemplatePath($path)
  804.     {
  805.         $this->_addPath('template'$path);
  806.     }
  807.  
  808.     /**
  809.      * Adds to the stack of helper script paths in LIFO order.
  810.      *
  811.      * @param   mixed  $path  A directory path or an array of paths.
  812.      *
  813.      * @return  void 
  814.      */
  815.     public function addHelperPath($path)
  816.     {
  817.         $this->_addPath('helper'$path);
  818.     }
  819.  
  820.     /**
  821.      * Overrides the built-in loadTemplate function with an FOF-specific one.
  822.      * Our overriden function uses loadAnyTemplate to provide smarter view
  823.      * template loading.
  824.      *
  825.      * @param   string   $tpl     The name of the template file to parse
  826.      * @param   boolean  $strict  Should we use strict naming, i.e. force a non-empty $tpl?
  827.      *
  828.      * @return  mixed  A string if successful, otherwise a JError object
  829.      */
  830.     public function loadTemplate($tpl null$strict false)
  831.     {
  832.         $paths FOFPlatform::getInstance()->getViewTemplatePaths(
  833.             $this->input->getCmd('option'''),
  834.             $this->input->getCmd('view'''),
  835.             $this->getLayout(),
  836.             $tpl,
  837.             $strict
  838.         );
  839.  
  840.         foreach ($paths as $path)
  841.         {
  842.             $result $this->loadAnyTemplate($path);
  843.  
  844.             if (!($result instanceof Exception))
  845.             {
  846.                 break;
  847.             }
  848.         }
  849.  
  850.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'&& ($result instanceof Exception))
  851.         {
  852.             JError::raiseError($result->getCode()$result->getMessage());
  853.         }
  854.  
  855.         return $result;
  856.     }
  857.  
  858.     /**
  859.      * Parses a template path in the form of admin:/component/view/layout or
  860.      * site:/component/view/layout to an array which can be used by
  861.      * loadAnyTemplate to locate and load the view template file.
  862.      *
  863.      * @param   string  $path  The template path to parse
  864.      *
  865.      * @return  array  A hash array with the parsed path parts
  866.      */
  867.     private function _parseTemplatePath($path '')
  868.     {
  869.         $parts array(
  870.             'admin'         => 0,
  871.             'component'     => $this->config['option'],
  872.             'view'         => $this->config['view'],
  873.             'template'     => 'default'
  874.         );
  875.  
  876.         if (substr($path06== 'admin:')
  877.         {
  878.             $parts['admin'1;
  879.             $path substr($path6);
  880.         }
  881.         elseif (substr($path05== 'site:')
  882.         {
  883.             $path substr($path5);
  884.         }
  885.  
  886.         if (empty($path))
  887.         {
  888.             return;
  889.         }
  890.  
  891.         $pathparts explode('/'$path3);
  892.  
  893.         switch (count($pathparts))
  894.         {
  895.             case 3:
  896.                 $parts['component'array_shift($pathparts);
  897.  
  898.             case 2:
  899.                 $parts['view'array_shift($pathparts);
  900.  
  901.             case 1:
  902.                 $parts['template'array_shift($pathparts);
  903.                 break;
  904.         }
  905.  
  906.         return $parts;
  907.     }
  908.  
  909.     /**
  910.      * Get the renderer object for this view
  911.      *
  912.      * @return  FOFRenderAbstract 
  913.      */
  914.     public function &getRenderer()
  915.     {
  916.         if (!($this->rendererObject instanceof FOFRenderAbstract))
  917.         {
  918.             $this->rendererObject = $this->findRenderer();
  919.         }
  920.  
  921.         return $this->rendererObject;
  922.     }
  923.  
  924.     /**
  925.      * Sets the renderer object for this view
  926.      *
  927.      * @param   FOFRenderAbstract  &$renderer  The render class to use
  928.      *
  929.      * @return  void 
  930.      */
  931.     public function setRenderer(FOFRenderAbstract &$renderer)
  932.     {
  933.         $this->rendererObject = $renderer;
  934.     }
  935.  
  936.     /**
  937.      * Finds a suitable renderer
  938.      *
  939.      * @return  FOFRenderAbstract 
  940.      */
  941.     protected function findRenderer()
  942.     {
  943.         JLoader::import('joomla.filesystem.folder');
  944.  
  945.         // Try loading the stock renderers shipped with FOF
  946.  
  947.         if (empty(self::$renderers|| !class_exists('FOFRenderJoomla'false))
  948.         {
  949.             $path dirname(__FILE__'/../render/';
  950.             $renderFiles JFolder::files($path'.php');
  951.  
  952.             if (!empty($renderFiles))
  953.             {
  954.                 foreach ($renderFiles as $filename)
  955.                 {
  956.                     if ($filename == 'abstract.php')
  957.                     {
  958.                         continue;
  959.                     }
  960.  
  961.                     @include_once $path '/' $filename;
  962.  
  963.                     $camel FOFInflector::camelize($filename);
  964.                     $className 'FOFRender' ucfirst(FOFInflector::getPart($camel0));
  965.                     $o new $className;
  966.  
  967.                     self::registerRenderer($o);
  968.                 }
  969.             }
  970.         }
  971.  
  972.         // Try to detect the most suitable renderer
  973.         $o null;
  974.         $priority 0;
  975.  
  976.         if (!empty(self::$renderers))
  977.         {
  978.             foreach (self::$renderers as $r)
  979.             {
  980.                 $info $r->getInformation();
  981.  
  982.                 if (!$info->enabled)
  983.                 {
  984.                     continue;
  985.                 }
  986.  
  987.                 if ($info->priority $priority)
  988.                 {
  989.                     $priority $info->priority;
  990.                     $o $r;
  991.                 }
  992.             }
  993.         }
  994.  
  995.         // Return the current renderer
  996.         return $o;
  997.     }
  998.  
  999.     /**
  1000.      * Registers a renderer object with the view
  1001.      *
  1002.      * @param   FOFRenderAbstract  &$renderer  The render object to register
  1003.      *
  1004.      * @return  void 
  1005.      */
  1006.     public static function registerRenderer(FOFRenderAbstract &$renderer)
  1007.     {
  1008.         self::$renderers[$renderer;
  1009.     }
  1010.  
  1011.     /**
  1012.      * Sets the pre-render flag
  1013.      *
  1014.      * @param   boolean  $value  True to enable the pre-render step
  1015.      *
  1016.      * @return  void 
  1017.      */
  1018.     public function setPreRender($value)
  1019.     {
  1020.         $this->doPreRender = $value;
  1021.     }
  1022.  
  1023.     /**
  1024.      * Sets the post-render flag
  1025.      *
  1026.      * @param   boolean  $value  True to enable the post-render step
  1027.      *
  1028.      * @return  void 
  1029.      */
  1030.     public function setPostRender($value)
  1031.     {
  1032.         $this->doPostRender = $value;
  1033.     }
  1034.  
  1035.     /**
  1036.      * Load a helper file
  1037.      *
  1038.      * @param   string  $hlp  The name of the helper source file automatically searches the helper paths and compiles as needed.
  1039.      *
  1040.      * @return  void 
  1041.      */
  1042.     public function loadHelper($hlp null)
  1043.     {
  1044.         // Clean the file name
  1045.         $file preg_replace('/[^A-Z0-9_\.-]/i'''$hlp);
  1046.  
  1047.         // Load the template script using the default Joomla! features
  1048.         JLoader::import('joomla.filesystem.path');
  1049.         $helper JPath::find($this->_path['helper']$this->_createFileName('helper'array('name' => $file)));
  1050.  
  1051.         if ($helper == false)
  1052.         {
  1053.             $componentPaths FOFPlatform::getInstance()->getComponentBaseDirs($this->config['option']);
  1054.             $path $componentPaths['main''/helpers';
  1055.             $helper JPath::find($path$this->_createFileName('helper'array('name' => $file)));
  1056.  
  1057.             if ($helper == false)
  1058.             {
  1059.                 $path $path $componentPaths['alt''/helpers';
  1060.                 $helper JPath::find($path$this->_createFileName('helper'array('name' => $file)));
  1061.             }
  1062.         }
  1063.  
  1064.         if ($helper != false)
  1065.         {
  1066.             // Include the requested template filename in the local scope
  1067.             include_once $helper;
  1068.         }
  1069.     }
  1070.  
  1071.     /**
  1072.      * Returns the view's option (component name) and view name in an
  1073.      * associative array.
  1074.      *
  1075.      * @return  array 
  1076.      */
  1077.     public function getViewOptionAndName()
  1078.     {
  1079.         return array(
  1080.             'option' => $this->config['option'],
  1081.             'view'     => $this->config['view'],
  1082.         );
  1083.     }
  1084.  
  1085.     /**
  1086.      * Sets an entire array of search paths for templates or resources.
  1087.      *
  1088.      * @param   string  $type  The type of path to set, typically 'template'.
  1089.      * @param   mixed   $path  The new search path, or an array of search paths.  If null or false, resets to the current directory only.
  1090.      *
  1091.      * @return  void 
  1092.      */
  1093.     protected function _setPath($type$path)
  1094.     {
  1095.         // Clear out the prior search dirs
  1096.         $this->_path[$typearray();
  1097.  
  1098.         // Actually add the user-specified directories
  1099.         $this->_addPath($type$path);
  1100.  
  1101.         // Always add the fallback directories as last resort
  1102.         switch (strtolower($type))
  1103.         {
  1104.             case 'template':
  1105.                 // Set the alternative template search dir
  1106.  
  1107.                 if (!FOFPlatform::getInstance()->isCli())
  1108.                 {
  1109.                     $fallback FOFPlatform::getInstance()->getTemplateOverridePath($this->input->getCmd('option''')) '/' $this->getName();
  1110.                     $this->_addPath('template'$fallback);
  1111.                 }
  1112.  
  1113.                 break;
  1114.         }
  1115.     }
  1116.  
  1117.     /**
  1118.      * Adds to the search path for templates and resources.
  1119.      *
  1120.      * @param   string  $type  The type of path to add.
  1121.      * @param   mixed   $path  The directory or stream, or an array of either, to search.
  1122.      *
  1123.      * @return  void 
  1124.      */
  1125.     protected function _addPath($type$path)
  1126.     {
  1127.         // Just force to array
  1128.         settype($path'array');
  1129.  
  1130.         // Loop through the path directories
  1131.         foreach ($path as $dir)
  1132.         {
  1133.             // No surrounding spaces allowed!
  1134.             $dir trim($dir);
  1135.  
  1136.             // Add trailing separators as needed
  1137.             if (substr($dir-1!= DIRECTORY_SEPARATOR)
  1138.             {
  1139.                 // Directory
  1140.                 $dir .= DIRECTORY_SEPARATOR;
  1141.             }
  1142.  
  1143.             // Add to the top of the search dirs
  1144.             array_unshift($this->_path[$type]$dir);
  1145.         }
  1146.     }
  1147.  
  1148.     /**
  1149.      * Create the filename for a resource
  1150.      *
  1151.      * @param   string  $type   The resource type to create the filename for
  1152.      * @param   array   $parts  An associative array of filename information
  1153.      *
  1154.      * @return  string  The filename
  1155.      */
  1156.     protected function _createFileName($type$parts array())
  1157.     {
  1158.         $filename '';
  1159.  
  1160.         switch ($type)
  1161.         {
  1162.             case 'template':
  1163.                 $filename strtolower($parts['name']'.' $this->_layoutExt;
  1164.                 break;
  1165.  
  1166.             default:
  1167.                 $filename strtolower($parts['name']'.php';
  1168.                 break;
  1169.         }
  1170.  
  1171.         return $filename;
  1172.     }
  1173. }

Documentation generated on Tue, 19 Nov 2013 15:18:16 +0100 by phpDocumentor 1.4.3