Source for file file.php

Documentation is available at file.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Layout
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('JPATH_BASE'or die;
  11.  
  12. /**
  13.  * Base class for rendering a display layout
  14.  * loaded from from a layout file
  15.  *
  16.  * @package     Joomla.Libraries
  17.  * @subpackage  Layout
  18.  * @see         http://docs.joomla.org/Sharing_layouts_across_views_or_extensions_with_JLayout
  19.  * @since       3.0
  20.  */
  21. class JLayoutFile extends JLayoutBase
  22. {
  23.     /**
  24.      * @var    string  Dot separated path to the layout file, relative to base path
  25.      * @since  3.0
  26.      */
  27.     protected $layoutId = '';
  28.  
  29.     /**
  30.      * @var    string  Base path to use when loading layout files
  31.      * @since  3.0
  32.      */
  33.     protected $basePath = null;
  34.  
  35.     /**
  36.      * @var    string  Full path to actual layout files, after possible template override check
  37.      * @since  3.0.3
  38.      */
  39.     protected $fullPath = null;
  40.  
  41.     /**
  42.      * Paths to search for layouts
  43.      *
  44.      * @var    array 
  45.      * @since  3.2
  46.      */
  47.     protected $includePaths = array();
  48.  
  49.     /**
  50.      * Method to instantiate the file-based layout.
  51.      *
  52.      * @param   string  $layoutId  Dot separated path to the layout file, relative to base path
  53.      * @param   string  $basePath  Base path to use when loading layout files
  54.      * @param   mixed   $options   Optional custom options to load. JRegistry or array format [@since 3.2]
  55.      *
  56.      * @since   3.0
  57.      */
  58.     public function __construct($layoutId$basePath null$options null)
  59.     {
  60.         // Initialise / Load options
  61.         $this->setOptions($options);
  62.  
  63.         // Main properties
  64.         $this->setLayout($layoutId);
  65.         $this->basePath = $basePath;
  66.  
  67.         // Init Enviroment
  68.         $this->setComponent($this->options->get('component''auto'));
  69.         $this->setClient($this->options->get('client''auto'));
  70.     }
  71.  
  72.     /**
  73.      * Method to render the layout.
  74.      *
  75.      * @param   object  $displayData  Object which properties are used inside the layout file to build displayed output
  76.      *
  77.      * @return  string  The necessary HTML to display the layout
  78.      *
  79.      * @since   3.0
  80.      */
  81.     public function render($displayData)
  82.     {
  83.         $layoutOutput '';
  84.  
  85.         // Check possible overrides, and build the full path to layout file
  86.         $path $this->getPath();
  87.  
  88.         if ($this->options->get('debug'false))
  89.         {
  90.             echo "<pre>" $this->renderDebugMessages("</pre>";
  91.         }
  92.  
  93.         // If there exists such a layout file, include it and collect its output
  94.         if (!empty($path))
  95.         {
  96.             ob_start();
  97.             include $path;
  98.             $layoutOutput ob_get_contents();
  99.             ob_end_clean();
  100.         }
  101.  
  102.         return $layoutOutput;
  103.     }
  104.  
  105.     /**
  106.      * Method to finds the full real file path, checking possible overrides
  107.      *
  108.      * @return  string  The full path to the layout file
  109.      *
  110.      * @since   3.0
  111.      */
  112.     protected function getPath()
  113.     {
  114.         JLoader::import('joomla.filesystem.path');
  115.  
  116.         if (is_null($this->fullPath&& !empty($this->layoutId))
  117.         {
  118.             $this->addDebugMessage('<strong>Layout:</strong> ' $this->layoutId);
  119.  
  120.             // Refresh paths
  121.             $this->refreshIncludePaths();
  122.  
  123.             $this->addDebugMessage('<strong>Include Paths:</strong> ' print_r($this->includePathstrue));
  124.  
  125.             $suffixes $this->options->get('suffixes'array());
  126.  
  127.             // Search for suffixed versions. Example: tags.j31.php
  128.             if (!empty($suffixes))
  129.             {
  130.                 $this->addDebugMessage('<strong>Suffixes:</strong> ' print_r($suffixestrue));
  131.  
  132.                 foreach ($suffixes as $suffix)
  133.                 {
  134.                     $rawPath  str_replace('.''/'$this->layoutId'.' $suffix '.php';
  135.                     $this->addDebugMessage('<strong>Searching layout for:</strong> ' $rawPath);
  136.  
  137.                     if ($this->fullPath = JPath::find($this->includePaths$rawPath))
  138.                     {
  139.                         $this->addDebugMessage('<strong>Found layout:</strong> ' $this->fullPath);
  140.  
  141.                         return $this->fullPath;
  142.                     }
  143.                 }
  144.             }
  145.  
  146.             // Standard version
  147.             $rawPath  str_replace('.''/'$this->layoutId'.php';
  148.             $this->addDebugMessage('<strong>Searching layout for:</strong> ' $rawPath);
  149.  
  150.             $this->fullPath = JPath::find($this->includePaths$rawPath);
  151.  
  152.             if ($this->fullPath = JPath::find($this->includePaths$rawPath))
  153.             {
  154.                 $this->addDebugMessage('<strong>Found layout:</strong> ' $this->fullPath);
  155.             }
  156.         }
  157.  
  158.         return $this->fullPath;
  159.     }
  160.  
  161.     /**
  162.      * Add one path to include in layout search. Proxy of addIncludePaths()
  163.      *
  164.      * @param   string  $path  The path to search for layouts
  165.      *
  166.      * @return  void 
  167.      *
  168.      * @since   3.2
  169.      */
  170.     public function addIncludePath($path)
  171.     {
  172.         $this->addIncludePaths($path);
  173.     }
  174.  
  175.     /**
  176.      * Add one or more paths to include in layout search
  177.      *
  178.      * @param   string  $paths  The path or array of paths to search for layouts
  179.      *
  180.      * @return  void 
  181.      *
  182.      * @since   3.2
  183.      */
  184.     public function addIncludePaths($paths)
  185.     {
  186.         if (!empty($paths))
  187.         {
  188.             if (is_array($paths))
  189.             {
  190.                 $this->includePaths = array_unique(array_merge($paths$this->includePaths));
  191.             }
  192.             else
  193.             {
  194.                 array_unshift($this->includePaths$paths);
  195.             }
  196.         }
  197.     }
  198.  
  199.     /**
  200.      * Remove one path from the layout search
  201.      *
  202.      * @param   string  $path  The path to remove from the layout search
  203.      *
  204.      * @return  void 
  205.      *
  206.      * @since   3.2
  207.      */
  208.     public function removeIncludePath($path)
  209.     {
  210.         $this->removeIncludePaths($path);
  211.     }
  212.  
  213.     /**
  214.      * Remove one or more paths to exclude in layout search
  215.      *
  216.      * @param   string  $paths  The path or array of paths to remove for the layout search
  217.      *
  218.      * @return  void 
  219.      *
  220.      * @since   3.2
  221.      */
  222.     public function removeIncludePaths($paths)
  223.     {
  224.         if (!empty($paths))
  225.         {
  226.             $paths = (array) $paths;
  227.  
  228.             $this->includePaths = array_diff($this->includePaths$paths);
  229.         }
  230.     }
  231.  
  232.     /**
  233.      * Validate that the active component is valid
  234.      *
  235.      * @param   string  $option  URL Option of the component. Example: com_content
  236.      *
  237.      * @return  boolean 
  238.      *
  239.      * @since   3.2
  240.      */
  241.     protected function validComponent($option null)
  242.     {
  243.         // By default we will validate the active component
  244.         $component ($option !== null$option $this->options->get('component'null);
  245.  
  246.         if (!empty($component))
  247.         {
  248.             // Valid option format
  249.             if (substr_count($component'com_'))
  250.             {
  251.                 // Latest check: component exists and is enabled
  252.                 return JComponentHelper::isEnabled($component);
  253.             }
  254.         }
  255.  
  256.         return false;
  257.     }
  258.  
  259.     /**
  260.      * Method to change the component where search for layouts
  261.      *
  262.      * @param   string  $option  URL Option of the component. Example: com_content
  263.      *
  264.      * @return  mixed  Component option string | null for none
  265.      *
  266.      * @since   3.2
  267.      */
  268.     public function setComponent($option)
  269.     {
  270.         $component null;
  271.  
  272.         switch ((string) $option)
  273.         {
  274.             case 'none':
  275.                 $component null;
  276.                 break;
  277.  
  278.             case 'auto':
  279.                 if (defined('JPATH_COMPONENT'))
  280.                 {
  281.                     $parts explode('/'JPATH_COMPONENT);
  282.                     $component end($parts);
  283.                 }
  284.  
  285.                 break;
  286.  
  287.             default:
  288.                 $component $option;
  289.                 break;
  290.         }
  291.  
  292.         // Extra checks
  293.         if (!$this->validComponent($component))
  294.         {
  295.             $component null;
  296.         }
  297.  
  298.         $this->options->set('component'$component);
  299.  
  300.         // Refresh include paths
  301.         $this->refreshIncludePaths();
  302.     }
  303.  
  304.     /**
  305.      * Function to initialise the application client
  306.      *
  307.      * @param   mixed  $client  Frontend: 'site' or 0 | Backend: 'admin' or 1
  308.      *
  309.      * @return  void 
  310.      *
  311.      * @since   3.2
  312.      */
  313.     public function setClient($client)
  314.     {
  315.         // Force string conversion to avoid unexpected states
  316.         switch ((string) $client)
  317.         {
  318.             case 'site':
  319.             case '0':
  320.                 $client 0;
  321.                 break;
  322.  
  323.             case 'admin':
  324.             case '1':
  325.                 $client 1;
  326.                 break;
  327.  
  328.             default:
  329.                 $client = (int) JFactory::getApplication()->isAdmin();
  330.                 break;
  331.         }
  332.  
  333.         $this->options->set('client'$client);
  334.  
  335.         // Refresh include paths
  336.         $this->refreshIncludePaths();
  337.     }
  338.  
  339.     /**
  340.      * Change the layout
  341.      *
  342.      * @param   string  $layoutId  Layout to render
  343.      *
  344.      * @return  void 
  345.      *
  346.      * @since   3.2
  347.      */
  348.     public function setLayout($layoutId)
  349.     {
  350.         $this->layoutId = $layoutId;
  351.         $this->fullPath = null;
  352.     }
  353.  
  354.     /**
  355.      * Refresh the list of include paths
  356.      *
  357.      * @return  void 
  358.      *
  359.      * @since   3.2
  360.      */
  361.     protected function refreshIncludePaths()
  362.     {
  363.         // Reset includePaths
  364.         $this->includePaths = array();
  365.  
  366.         // (1 - lower priority) Frontend base layouts
  367.         $this->addIncludePaths(JPATH_ROOT '/layouts');
  368.  
  369.         // (2) Standard Joomla! layouts overriden
  370.         $this->addIncludePaths(JPATH_THEMES '/' JFactory::getApplication()->getTemplate('/html/layouts');
  371.  
  372.         // Component layouts & overrides if exist
  373.         $component $this->options->get('component'null);
  374.  
  375.         if (!empty($component))
  376.         {
  377.             // (3) Component path
  378.             if ($this->options->get('client'== 0)
  379.             {
  380.                 $this->addIncludePaths(JPATH_SITE '/components/' $component '/layouts');
  381.             }
  382.             else
  383.             {
  384.                 $this->addIncludePaths(JPATH_ADMINISTRATOR '/components/' $component '/layouts');
  385.             }
  386.  
  387.             // (4) Component template overrides path
  388.             $this->addIncludePath(JPATH_THEMES '/' JFactory::getApplication()->getTemplate('/html/layouts/' $component);
  389.         }
  390.  
  391.         // (5 - highest priority) Received a custom high priority path ?
  392.         if (!is_null($this->basePath))
  393.         {
  394.             $this->addIncludePath(rtrim($this->basePathDIRECTORY_SEPARATOR));
  395.         }
  396.     }
  397.  
  398.     /**
  399.      * Change the debug mode
  400.      *
  401.      * @param   boolean  $debug  Enable / Disable debug
  402.      *
  403.      * @return  void 
  404.      *
  405.      * @since   3.2
  406.      */
  407.     public function setDebug($debug)
  408.     {
  409.         $this->options->set('debug'(boolean) $debug);
  410.     }
  411.  
  412.     /**
  413.      * Render a layout with the same include paths & options
  414.      *
  415.      * @param   object  $layoutId     Object which properties are used inside the layout file to build displayed output
  416.      * @param   mixed   $displayData  Data to be rendered
  417.      *
  418.      * @return  string  The necessary HTML to display the layout
  419.      *
  420.      * @since   3.2
  421.      */
  422.     public function sublayout($layoutId$displayData)
  423.     {
  424.         // Sublayouts are searched in a subfolder with the name of the current layout
  425.         if (!empty($this->layoutId))
  426.         {
  427.             $layoutId $this->layoutId . '.' $layoutId;
  428.         }
  429.  
  430.         $sublayout new static($layoutId$this->basePath$this->options);
  431.         $sublayout->includePaths $this->includePaths;
  432.  
  433.         return $sublayout->render($displayData);
  434.     }
  435. }

Documentation generated on Tue, 19 Nov 2013 15:03:07 +0100 by phpDocumentor 1.4.3