[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/document/html/ -> html.php (source)

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


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1