Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/document/html/html.php

Documentation is available at html.php

  1. <?php
  2. /**
  3. @version        $Id: html.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4. @package        Joomla.Framework
  5. @subpackage    Document
  6. @copyright    Copyright (C) 2005 - 2008 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. jimport('joomla.application.module.helper');
  19.  
  20. /**
  21.  * DocumentHTML class, provides an easy interface to parse and display an html document
  22.  *
  23.  * @author        Johan Janssens <johan.janssens@joomla.org>
  24.  * @package        Joomla.Framework
  25.  * @subpackage    Document
  26.  * @since        1.5
  27.  */
  28.  
  29. class JDocumentHTML extends JDocument
  30. {
  31.      /**
  32.      * Array of Header <link> tags
  33.      *
  34.      * @var     array 
  35.      * @access  private
  36.      */
  37.     var $_links array();
  38.  
  39.     /**
  40.      * Array of custom tags
  41.      *
  42.      * @var     string 
  43.      * @access  private
  44.      */
  45.     var $_custom array();
  46.  
  47.  
  48.     /**
  49.      * Class constructor
  50.      *
  51.      * @access protected
  52.      * @param    array    $options Associative array of options
  53.      */
  54.     function __construct($options array())
  55.     {
  56.         parent::__construct($options);
  57.  
  58.         //set document type
  59.         $this->_type 'html';
  60.  
  61.         //set mime type
  62.         $this->_mime 'text/html';
  63.  
  64.         //set default document metadata
  65.          $this->setMetaData('Content-Type'$this->_mime '; charset=' $this->_charset true );
  66.          $this->setMetaData('robots''index, follow' );
  67.     }
  68.  
  69.     /**
  70.      * Get the html document head data
  71.      *
  72.      * @access    public
  73.      * @return    array    The document head data in array form
  74.      */
  75.     function getHeadData()
  76.     {
  77.         $data array();
  78.         $data['title']        $this->title;
  79.         $data['description']$this->description;
  80.         $data['link']        $this->link;
  81.         $data['metaTags']    $this->_metaTags;
  82.         $data['links']        $this->_links;
  83.         $data['styleSheets']$this->_styleSheets;
  84.         $data['style']        $this->_style;
  85.         $data['scripts']    $this->_scripts;
  86.         $data['script']        $this->_script;
  87.         $data['custom']        $this->_custom;
  88.         return $data;
  89.     }
  90.  
  91.     /**
  92.      * Set the html document head data
  93.      *
  94.      * @access    public
  95.      * @param    array    $data    The document head data in array form
  96.      */
  97.     function setHeadData($data)
  98.     {
  99.         $this->title        = (isset($data['title'])) $data['title'$this->title;
  100.         $this->description    = (isset($data['description'])) $data['description'$this->description;
  101.         $this->link            = (isset($data['link'])) $data['link'$this->link;
  102.         $this->_metaTags    (isset($data['metaTags'])) $data['metaTags'$this->_metaTags;
  103.         $this->_links        (isset($data['links'])) $data['links'$this->_links;
  104.         $this->_styleSheets    (isset($data['styleSheets'])) $data['styleSheets'$this->_styleSheets;
  105.         $this->_style        (isset($data['style'])) $data['style'$this->_style;
  106.         $this->_scripts        (isset($data['scripts'])) $data['scripts'$this->_scripts;
  107.         $this->_script        (isset($data['script'])) $data['script'$this->_script;
  108.         $this->_custom        (isset($data['custom'])) $data['custom'$this->_custom;
  109.     }
  110.  
  111.      /**
  112.      * Adds <link> tags to the head of the document
  113.      *
  114.      * <p>$relType defaults to 'rel' as it is the most common relation type used.
  115.      * ('rev' refers to reverse relation, 'rel' indicates normal, forward relation.)
  116.      * Typical tag: <link href="index.php" rel="Start"></p>
  117.      *
  118.      * @access   public
  119.      * @param    string  $href        The link that is being related.
  120.      * @param    string  $relation   Relation of link.
  121.      * @param    string  $relType    Relation type attribute.  Either rel or rev (default: 'rel').
  122.      * @param    array   $attributes Associative array of remaining attributes.
  123.      * @return   void 
  124.      */
  125.     function addHeadLink($href$relation$relType 'rel'$attribs array())
  126.     {
  127.         $attribs JArrayHelper::toString($attribs);
  128.         $generatedTag '<link href="'.$href.'" '.$relType.'="'.$relation.'" '.$attribs;
  129.         $this->_links[$generatedTag;
  130.     }
  131.  
  132.      /**
  133.      * Adds a shortcut icon (favicon)
  134.      *
  135.      * <p>This adds a link to the icon shown in the favorites list or on
  136.      * the left of the url in the address bar. Some browsers display
  137.      * it on the tab, as well.</p>
  138.      *
  139.      * @param     string  $href        The link that is being related.
  140.      * @param     string  $type        File type
  141.      * @param     string  $relation    Relation of link
  142.      * @access    public
  143.      */
  144.     function addFavicon($href$type 'image/x-icon'$relation 'shortcut icon')
  145.     {
  146.         $href str_replace'\\''/'$href );
  147.         $this->_links['<link href="'.$href.'" rel="'.$relation.'" type="'.$type.'"';
  148.     }
  149.  
  150.     /**
  151.      * Adds a custom html string to the head block
  152.      *
  153.      * @param string The html to add to the head
  154.      * @access   public
  155.      * @return   void 
  156.      */
  157.  
  158.     function addCustomTag$html )
  159.     {
  160.         $this->_custom[trim$html );
  161.     }
  162.  
  163.     /**
  164.      * Get the contents of a document include
  165.      *
  166.      * @access public
  167.      * @param string     $type    The type of renderer
  168.      * @param string     $name     The name of the element to render
  169.      * @param array       $attribs Associative array of remaining attributes.
  170.      * @return     The output of the renderer
  171.      */
  172.     function getBuffer($type null$name null$attribs array())
  173.     {
  174.         $result null;
  175.  
  176.         // If no type is specified, return the whole buffer
  177.         if ($type === null{
  178.             return $this->_buffer;
  179.         }
  180.  
  181.         if(isset($this->_buffer[$type][$name])) {
  182.             $result $this->_buffer[$type][$name];
  183.         }
  184.  
  185.         // If the buffer has been explicitly turned off don't display or attempt to render
  186.         if ($result === false{
  187.             return null;
  188.         }
  189.  
  190.         if$renderer =$this->loadRenderer$type )) {
  191.             $result $renderer->render($name$attribs$result);
  192.         }
  193.  
  194.         return $result;
  195.     }
  196.  
  197.     /**
  198.      * Set the contents a document include
  199.      *
  200.      * @access public
  201.      * @param string     $type        The type of renderer
  202.      * @param string     $name        oke The name of the element to render
  203.      * @param string     $content    The content to be set in the buffer
  204.      */
  205.     function setBuffer($contents$type$name null)
  206.     {
  207.         $this->_buffer[$type][$name$contents;
  208.     }
  209.  
  210.     /**
  211.      * Outputs the template to the browser.
  212.      *
  213.      * @access public
  214.      * @param boolean     $cache        If true, cache the output
  215.      * @param array        $params        Associative array of attributes
  216.      * @return     The rendered data
  217.      */
  218.     function render$caching false$params array())
  219.     {
  220.         // check
  221.         $directory    = isset($params['directory']$params['directory''templates';
  222.         $template    JFilterInput::clean($params['template']'cmd');
  223.         $file        JFilterInput::clean($params['file']'cmd');
  224.  
  225.         if !file_exists$directory.DS.$template.DS.$file) ) {
  226.             $template 'system';
  227.         }
  228.  
  229.         // Parse the template INI file if it exists for parameters and insert
  230.         // them into the template.
  231.         if (is_readable$directory.DS.$template.DS.'params.ini' ) )
  232.         {
  233.             $content file_get_contents($directory.DS.$template.DS.'params.ini');
  234.             $params new JParameter($content);
  235.         }
  236.  
  237.         // Load the language file for the template
  238.         $lang =JFactory::getLanguage();
  239.         $lang->load'tpl_'.$template );
  240.  
  241.         // Assign the variables
  242.         $this->template $template;
  243.         $this->baseurl  JURI::base(true);
  244.         $this->params   $params;
  245.  
  246.         // load
  247.         $data $this->_loadTemplate($directory.DS.$template$file);
  248.  
  249.         // parse
  250.         $data $this->_parseTemplate($data);
  251.  
  252.         //output
  253.         parent::render();
  254.         return $data;
  255.     }
  256.  
  257.     /**
  258.      * Count the modules based on the given condition
  259.      *
  260.      * @access public
  261.      * @param  string     $condition    The condition to use
  262.      * @return integer  Number of modules found
  263.      */
  264.     function countModules($condition)
  265.     {
  266.         $result '';
  267.  
  268.         $words explode(' '$condition);
  269.         for($i 0$i count($words)$i+=2)
  270.         {
  271.             // odd parts (modules)
  272.             $name        strtolower($words[$i]);
  273.             $words[$i]    ((isset($this->_buffer['modules'][$name])) && ($this->_buffer['modules'][$name=== false)) count(JModuleHelper::getModules($name));
  274.         }
  275.  
  276.         $str 'return '.implode(' '$words).';';
  277.  
  278.         return eval($str);
  279.     }
  280.  
  281.     /**
  282.      * Load a template file
  283.      *
  284.      * @param string     $template    The name of the template
  285.      * @param string     $filename    The actual filename
  286.      * @return string The contents of the template
  287.      */
  288.     function _loadTemplate($directory$filename)
  289.     {
  290.         global $mainframe$option;
  291.  
  292.         if ($mainframe->getCfg('legacy'))
  293.         {
  294.             global $task$_VERSION$my$cur_template$database$acl$Itemid;
  295.  
  296.             //For backwards compatibility extract the config vars as globals
  297.             $registry =JFactory::getConfig();
  298.             foreach (get_object_vars($registry->toObject()) as $k => $v{
  299.                 $name 'mosConfig_'.$k;
  300.                 $$name $v;
  301.             }
  302.         }
  303.  
  304.         $contents '';
  305.  
  306.         //Check to see if we have a valid template file
  307.         if file_exists$directory.DS.$filename ) )
  308.         {
  309.             //store the file path
  310.             $this->_file $directory.DS.$filename;
  311.  
  312.             //get the file content
  313.             ob_start();
  314.             require_once $directory.DS.$filename;
  315.             $contents ob_get_contents();
  316.             ob_end_clean();
  317.         }
  318.  
  319.         // Try to find a favicon by checking the template and root folder
  320.         $path $directory DS;
  321.         $dirs array$pathJPATH_BASE DS );
  322.         foreach ($dirs as $dir )
  323.         {
  324.             $icon =   $dir 'favicon.ico';
  325.             if (file_exists$icon ))
  326.             {
  327.                 $path str_replaceJPATH_BASE DS''$dir );
  328.                 $path str_replace'\\''/'$path );
  329.                 $this->addFaviconJURI::base(true).'/'.$path 'favicon.ico' );
  330.                 break;
  331.             }
  332.         }
  333.  
  334.         return $contents;
  335.     }
  336.  
  337.     /**
  338.      * Parse a document template
  339.      *
  340.      * @access public
  341.      * @param string     $data        The data too parse
  342.      * @return The parsed contents of the template
  343.      */
  344.     function _parseTemplate($data)
  345.     {
  346.         $replace array();
  347.         $matches array();
  348.         if(preg_match_all('#<jdoc:include\ type="([^"]+)" (.*)\/>#iU'$data$matches))
  349.         {
  350.             $matches[0array_reverse($matches[0]);
  351.             $matches[1array_reverse($matches[1]);
  352.             $matches[2array_reverse($matches[2]);
  353.  
  354.             $count count($matches[1]);
  355.  
  356.             for($i 0$i $count$i++)
  357.             {
  358.                 $attribs JUtility::parseAttributes$matches[2][$i);
  359.                 $type  $matches[1][$i];
  360.  
  361.                 $name  = isset($attribs['name']$attribs['name'null;
  362.                 $replace[$i$this->getBuffer($type$name$attribs);
  363.             }
  364.  
  365.             $data str_replace($matches[0]$replace$data);
  366.         }
  367.  
  368.         return $data;
  369.     }
  370. }

Documentation generated on Tue, 29 Jan 2008 18:47:38 +0000 by phpDocumentor 1.3.1