Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/document/document.php

Documentation is available at document.php

  1. <?php
  2. /**
  3. @version        $Id: document.php 10816 2008-08-27 04:17:00Z tcp $
  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. //Register the renderer class with the loader
  19. JLoader::register('JDocumentRenderer'dirname(__FILE__).DS.'renderer.php');
  20.  
  21. /**
  22.  * Document class, provides an easy interface to parse and display a document
  23.  *
  24.  * @abstract
  25.  * @package        Joomla.Framework
  26.  * @subpackage    Document
  27.  * @since        1.5
  28.  */
  29. class JDocument extends JObject
  30. {
  31.     /**
  32.      * Document title
  33.      *
  34.      * @var     string 
  35.      * @access  public
  36.      */
  37.     var $title = '';
  38.  
  39.     /**
  40.      * Document description
  41.      *
  42.      * @var     string 
  43.      * @access  public
  44.      */
  45.     var $description = '';
  46.  
  47.     /**
  48.      * Document full URL
  49.      *
  50.      * @var     string 
  51.      * @access  public
  52.      */
  53.     var $link = '';
  54.  
  55.     /**
  56.      * Document base URL
  57.      *
  58.      * @var     string 
  59.      * @access  public
  60.      */
  61.     var $base = '';
  62.  
  63.      /**
  64.      * Contains the document language setting
  65.      *
  66.      * @var     string 
  67.      * @access  public
  68.      */
  69.     var $language = 'en-gb';
  70.  
  71.     /**
  72.      * Contains the document direction setting
  73.      *
  74.      * @var     string 
  75.      * @access  public
  76.      */
  77.     var $direction = 'ltr';
  78.  
  79.     /**
  80.      * Document generator
  81.      *
  82.      * @var        string 
  83.      * @access    public
  84.      */
  85.      var $_generator = 'Joomla! 1.5 - Open Source Content Management';
  86.  
  87.     /**
  88.      * Document modified date
  89.      *
  90.      * @var        string 
  91.      * @access   private
  92.      */
  93.     var $_mdate '';
  94.  
  95.     /**
  96.      * Tab string
  97.      *
  98.      * @var        string 
  99.      * @access    private
  100.      */
  101.     var $_tab "\11";
  102.  
  103.     /**
  104.      * Contains the line end string
  105.      *
  106.      * @var        string 
  107.      * @access    private
  108.      */
  109.     var $_lineEnd "\12";
  110.  
  111.     /**
  112.      * Contains the character encoding string
  113.      *
  114.      * @var     string 
  115.      * @access  private
  116.      */
  117.     var $_charset 'utf-8';
  118.  
  119.     /**
  120.      * Document mime type
  121.      *
  122.      * @var        string 
  123.      * @access    private
  124.      */
  125.     var $_mime '';
  126.  
  127.     /**
  128.      * Document namespace
  129.      *
  130.      * @var        string 
  131.      * @access   private
  132.      */
  133.     var $_namespace '';
  134.  
  135.     /**
  136.      * Document profile
  137.      *
  138.      * @var        string 
  139.      * @access   private
  140.      */
  141.     var $_profile '';
  142.  
  143.     /**
  144.      * Array of linked scripts
  145.      *
  146.      * @var        array 
  147.      * @access   private
  148.      */
  149.     var $_scripts array();
  150.  
  151.     /**
  152.      * Array of scripts placed in the header
  153.      *
  154.      * @var  array 
  155.      * @access   private
  156.      */
  157.     var $_script array();
  158.  
  159.      /**
  160.      * Array of linked style sheets
  161.      *
  162.      * @var     array 
  163.      * @access  private
  164.      */
  165.     var $_styleSheets array();
  166.  
  167.     /**
  168.      * Array of included style declarations
  169.      *
  170.      * @var     array 
  171.      * @access  private
  172.      */
  173.     var $_style array();
  174.  
  175.     /**
  176.      * Array of meta tags
  177.      *
  178.      * @var     array 
  179.      * @access  private
  180.      */
  181.     var $_metaTags array();
  182.  
  183.     /**
  184.      * The rendering engine
  185.      *
  186.      * @var     object 
  187.      * @access  private
  188.      */
  189.     var $_engine null;
  190.  
  191.     /**
  192.      * The document type
  193.      *
  194.      * @var     string 
  195.      * @access  private
  196.      */
  197.     var $_type null;
  198.  
  199.     /**
  200.      * Array of buffered output
  201.      *
  202.      * @var        mixed (depends on the renderer)
  203.      * @access    private
  204.      */
  205.     var $_buffer null;
  206.  
  207.  
  208.     /**
  209.     * Class constructor
  210.     *
  211.     * @access protected
  212.     * @param    array    $options Associative array of options
  213.     */
  214.     function __construct$options array())
  215.     {
  216.         parent::__construct();
  217.  
  218.         if (array_key_exists('lineend'$options)) {
  219.             $this->setLineEnd($options['lineend']);
  220.         }
  221.  
  222.         if (array_key_exists('charset'$options)) {
  223.             $this->setCharset($options['charset']);
  224.         }
  225.  
  226.         if (array_key_exists('language'$options)) {
  227.             $this->setLanguage($options['language']);
  228.         }
  229.  
  230.          if (array_key_exists('direction'$options)) {
  231.             $this->setDirection($options['direction']);
  232.         }
  233.  
  234.         if (array_key_exists('tab'$options)) {
  235.             $this->setTab($options['tab']);
  236.         }
  237.  
  238.         if (array_key_exists('link'$options)) {
  239.             $this->setLink($options['link']);
  240.         }
  241.  
  242.         if (array_key_exists('base'$options)) {
  243.             $this->setBase($options['base']);
  244.         }
  245.     }
  246.  
  247.     /**
  248.      * Returns a reference to the global JDocument object, only creating it
  249.      * if it doesn't already exist.
  250.      *
  251.      * This method must be invoked as:
  252.      *         <pre>  $document = &JDocument::getInstance();</pre>
  253.      *
  254.      * @access public
  255.      * @param type $type The document type to instantiate
  256.      * @return object  The document object.
  257.      */
  258.     function &getInstance($type 'html'$attributes array())
  259.     {
  260.         static $instances;
  261.  
  262.         if (!isset$instances )) {
  263.             $instances array();
  264.         }
  265.  
  266.         $signature serialize(array($type$attributes));
  267.  
  268.         if (empty($instances[$signature]))
  269.         {
  270.             $type    preg_replace('/[^A-Z0-9_\.-]/i'''$type);
  271.             $path    dirname(__FILE__).DS.$type.DS.$type.'.php';
  272.             $ntype    null;
  273.  
  274.             // Check if the document type exists
  275.             if file_exists($path))
  276.             {
  277.                 // Default to the raw format
  278.                 $ntype    $type;
  279.                 $type    'raw';
  280.             }
  281.  
  282.             // Determine the path and class
  283.             $class 'JDocument'.$type;
  284.             if(!class_exists($class))
  285.             {
  286.                 $path    dirname(__FILE__).DS.$type.DS.$type.'.php';
  287.                 if (file_exists($path)) {
  288.                     require_once($path);
  289.                 else {
  290.                     JError::raiseError(500,JText::_('Unable to load document class'));
  291.                 }
  292.             }
  293.  
  294.             $instance    new $class($attributes);
  295.             $instances[$signature=$instance;
  296.  
  297.             if !is_null($ntype) )
  298.             {
  299.                 // Set the type to the Document type originally requested
  300.                 $instance->setType($ntype);
  301.             }
  302.         }
  303.  
  304.         return $instances[$signature];
  305.     }
  306.  
  307.     /**
  308.      * Set the document type
  309.      *
  310.      * @access    public
  311.      * @param    string $type 
  312.      */
  313.     function setType($type{
  314.         $this->_type $type;
  315.     }
  316.  
  317.      /**
  318.      * Returns the document type
  319.      *
  320.      * @access    public
  321.      * @return    string 
  322.      */
  323.     function getType({
  324.         return $this->_type;
  325.     }
  326.  
  327.     /**
  328.      * Get the document head data
  329.      *
  330.      * @access    public
  331.      * @return    array    The document head data in array form
  332.      */
  333.     function getHeadData({
  334.         // Impelemented in child classes
  335.     }
  336.  
  337.     /**
  338.      * Set the document head data
  339.      *
  340.      * @access    public
  341.      * @param    array    $data    The document head data in array form
  342.      */
  343.     function setHeadData($data{
  344.         // Impelemented in child classes
  345.     }
  346.  
  347.     /**
  348.      * Get the contents of the document buffer
  349.      *
  350.      * @access public
  351.      * @return     The contents of the document buffer
  352.      */
  353.     function getBuffer({
  354.         return $this->_buffer;
  355.     }
  356.  
  357.     /**
  358.      * Set the contents of the document buffer
  359.      *
  360.      * @access public
  361.      * @param string     $content    The content to be set in the buffer
  362.      */
  363.     function setBuffer($content{
  364.         $this->_buffer $content;
  365.     }
  366.  
  367.     /**
  368.      * Gets a meta tag.
  369.      *
  370.      * @param    string    $name            Value of name or http-equiv tag
  371.      * @param    bool    $http_equiv     META type "http-equiv" defaults to null
  372.      * @return    string 
  373.      * @access    public
  374.      */
  375.     function getMetaData($name$http_equiv false)
  376.     {
  377.         $result '';
  378.         $name strtolower($name);
  379.         if($name == 'generator'
  380.             $result $this->getGenerator();
  381.         elseif($name == 'description'{
  382.             $result $this->getDescription();
  383.         else {
  384.             if ($http_equiv == true{
  385.                 $result @$this->_metaTags['http-equiv'][$name];
  386.             else {
  387.                 $result @$this->_metaTags['standard'][$name];
  388.             }
  389.         }
  390.         return $result;
  391.     }
  392.  
  393.     /**
  394.      * Sets or alters a meta tag.
  395.      *
  396.      * @param string  $name            Value of name or http-equiv tag
  397.      * @param string  $content        Value of the content tag
  398.      * @param bool    $http_equiv     META type "http-equiv" defaults to null
  399.      * @return void 
  400.      * @access public
  401.      */
  402.     function setMetaData($name$content$http_equiv false)
  403.     {
  404.         $name strtolower($name);
  405.         if($name == 'generator'
  406.             $this->setGenerator($content);
  407.         elseif($name == 'description'{
  408.             $this->setDescription($content);
  409.         else {
  410.             if ($http_equiv == true{
  411.                 $this->_metaTags['http-equiv'][$name$content;
  412.             else {
  413.                 $this->_metaTags['standard'][$name$content;
  414.             }
  415.         }
  416.     }
  417.  
  418.      /**
  419.      * Adds a linked script to the page
  420.      *
  421.      * @param    string  $url        URL to the linked script
  422.      * @param    string  $type        Type of script. Defaults to 'text/javascript'
  423.      * @access   public
  424.      */
  425.     function addScript($url$type="text/javascript"{
  426.         $this->_scripts[$url$type;
  427.     }
  428.  
  429.     /**
  430.      * Adds a script to the page
  431.      *
  432.      * @access   public
  433.      * @param    string  $content   Script
  434.      * @param    string  $type    Scripting mime (defaults to 'text/javascript')
  435.      * @return   void 
  436.      */
  437.     function addScriptDeclaration($content$type 'text/javascript')
  438.     {
  439.         if (!isset($this->_script[strtolower($type)])) {
  440.             $this->_script[strtolower($type)$content;
  441.         else {
  442.             $this->_script[strtolower($type).= chr(13).$content;
  443.         }
  444.     }
  445.  
  446.     /**
  447.      * Adds a linked stylesheet to the page
  448.      *
  449.      * @param    string  $url    URL to the linked style sheet
  450.      * @param    string  $type   Mime encoding type
  451.      * @param    string  $media  Media type that this stylesheet applies to
  452.      * @access   public
  453.      */
  454.     function addStyleSheet($url$type 'text/css'$media null$attribs array())
  455.     {
  456.         $this->_styleSheets[$url]['mime']        $type;
  457.         $this->_styleSheets[$url]['media']        $media;
  458.         $this->_styleSheets[$url]['attribs']    $attribs;
  459.     }
  460.  
  461.      /**
  462.      * Adds a stylesheet declaration to the page
  463.      *
  464.      * @param    string  $content   Style declarations
  465.      * @param    string  $type        Type of stylesheet (defaults to 'text/css')
  466.      * @access   public
  467.      * @return   void 
  468.      */
  469.     function addStyleDeclaration($content$type 'text/css')
  470.     {
  471.         if (!isset($this->_style[strtolower($type)])) {
  472.             $this->_style[strtolower($type)$content;
  473.         else {
  474.             $this->_style[strtolower($type).= chr(13).$content;
  475.         }
  476.     }
  477.  
  478.      /**
  479.      * Sets the document charset
  480.      *
  481.      * @param   string   $type  Charset encoding string
  482.      * @access  public
  483.      * @return  void 
  484.      */
  485.     function setCharset($type 'utf-8'{
  486.         $this->_charset $type;
  487.     }
  488.  
  489.     /**
  490.      * Returns the document charset encoding.
  491.      *
  492.      * @access public
  493.      * @return string 
  494.      */
  495.     function getCharset({
  496.         return $this->_charset;
  497.     }
  498.  
  499.     /**
  500.      * Sets the global document language declaration. Default is English (en-gb).
  501.      *
  502.      * @access public
  503.      * @param   string   $lang 
  504.      */
  505.     function setLanguage($lang "en-gb"{
  506.         $this->language = strtolower($lang);
  507.     }
  508.  
  509.     /**
  510.      * Returns the document language.
  511.      *
  512.      * @return string 
  513.      * @access public
  514.      */
  515.     function getLanguage({
  516.         return $this->language;
  517.     }
  518.  
  519.     /**
  520.      * Sets the global document direction declaration. Default is left-to-right (ltr).
  521.      *
  522.      * @access public
  523.      * @param   string   $lang 
  524.      */
  525.     function setDirection($dir "ltr"{
  526.         $this->direction = strtolower($dir);
  527.     }
  528.  
  529.     /**
  530.      * Returns the document language.
  531.      *
  532.      * @return string 
  533.      * @access public
  534.      */
  535.     function getDirection({
  536.         return $this->direction;
  537.     }
  538.  
  539.     /**
  540.      * Sets the title of the document
  541.      *
  542.      * @param    string    $title 
  543.      * @access   public
  544.      */
  545.     function setTitle($title{
  546.         $this->title = $title;
  547.     }
  548.  
  549.     /**
  550.      * Return the title of the document.
  551.      *
  552.      * @return   string 
  553.      * @access   public
  554.      */
  555.     function getTitle({
  556.         return $this->title;
  557.     }
  558.  
  559.     /**
  560.      * Sets the base URI of the document
  561.      *
  562.      * @param    string    $base 
  563.      * @access   public
  564.      */
  565.     function setBase($base{
  566.         $this->base = $base;
  567.     }
  568.  
  569.     /**
  570.      * Return the base URI of the document.
  571.      *
  572.      * @return   string 
  573.      * @access   public
  574.      */
  575.     function getBase({
  576.         return $this->base;
  577.     }
  578.  
  579.     /**
  580.      * Sets the description of the document
  581.      *
  582.      * @param    string    $title 
  583.      * @access   public
  584.      */
  585.     function setDescription($description{
  586.         $this->description = $description;
  587.     }
  588.  
  589.     /**
  590.      * Return the title of the page.
  591.      *
  592.      * @return   string 
  593.      * @access   public
  594.      */
  595.     function getDescription({
  596.         return $this->description;
  597.     }
  598.  
  599.      /**
  600.      * Sets the document link
  601.      *
  602.      * @param   string   $url  A url
  603.      * @access  public
  604.      * @return  void 
  605.      */
  606.     function setLink($url{
  607.         $this->link = $url;
  608.     }
  609.  
  610.     /**
  611.      * Returns the document base url
  612.      *
  613.      * @access public
  614.      * @return string 
  615.      */
  616.     function getLink({
  617.         return $this->link;
  618.     }
  619.  
  620.      /**
  621.      * Sets the document generator
  622.      *
  623.      * @param   string 
  624.      * @access  public
  625.      * @return  void 
  626.      */
  627.     function setGenerator($generator{
  628.         $this->_generator = $generator;
  629.     }
  630.  
  631.     /**
  632.      * Returns the document generator
  633.      *
  634.      * @access public
  635.      * @return string 
  636.      */
  637.     function getGenerator({
  638.         return $this->_generator;
  639.     }
  640.  
  641.      /**
  642.      * Sets the document modified date
  643.      *
  644.      * @param   string 
  645.      * @access  public
  646.      * @return  void 
  647.      */
  648.     function setModifiedDate($date{
  649.         $this->_mdate $date;
  650.     }
  651.  
  652.     /**
  653.      * Returns the document modified date
  654.      *
  655.      * @access public
  656.      * @return string 
  657.      */
  658.     function getModifiedDate({
  659.         return $this->_mdate;
  660.     }
  661.  
  662.      /**
  663.      * Sets the document MIME encoding that is sent to the browser.
  664.      *
  665.      * <p>This usually will be text/html because most browsers cannot yet
  666.      * accept the proper mime settings for XHTML: application/xhtml+xml
  667.      * and to a lesser extent application/xml and text/xml. See the W3C note
  668.      * ({@link http://www.w3.org/TR/xhtml-media-types/}
  669.      * http://www.w3.org/TR/xhtml-media-types/}) for more details.</p>
  670.      *
  671.      * @param    string    $type 
  672.      * @access   public
  673.      * @return   void 
  674.      */
  675.     function setMimeEncoding($type 'text/html'{
  676.         $this->_mime strtolower($type);
  677.     }
  678.  
  679.      /**
  680.      * Sets the line end style to Windows, Mac, Unix or a custom string.
  681.      *
  682.      * @param   string  $style  "win", "mac", "unix" or custom string.
  683.      * @access  public
  684.      * @return  void 
  685.      */
  686.     function setLineEnd($style)
  687.     {
  688.         switch ($style{
  689.             case 'win':
  690.                 $this->_lineEnd "\15\12";
  691.                 break;
  692.             case 'unix':
  693.                 $this->_lineEnd "\12";
  694.                 break;
  695.             case 'mac':
  696.                 $this->_lineEnd "\15";
  697.                 break;
  698.             default:
  699.                 $this->_lineEnd $style;
  700.         }
  701.     }
  702.  
  703.     /**
  704.      * Returns the lineEnd
  705.      *
  706.      * @access    private
  707.      * @return    string 
  708.      */
  709.     function _getLineEnd({
  710.         return $this->_lineEnd;
  711.     }
  712.  
  713.     /**
  714.      * Sets the string used to indent HTML
  715.      *
  716.      * @param     string    $string     String used to indent ("\11", "\t", '  ', etc.).
  717.      * @access    public
  718.      * @return    void 
  719.      */
  720.     function setTab($string{
  721.         $this->_tab $string;
  722.     }
  723.  
  724.      /**
  725.      * Returns a string containing the unit for indenting HTML
  726.      *
  727.      * @access    private
  728.      * @return    string 
  729.      */
  730.     function _getTab({
  731.         return $this->_tab;
  732.     }
  733.  
  734.     /**
  735.     * Load a renderer
  736.     *
  737.     * @access    public
  738.     * @param    string    The renderer type
  739.     * @return    object 
  740.     * @since 1.5
  741.     */
  742.     function &loadRenderer$type )
  743.     {
  744.         $null    null;
  745.         $class    'JDocumentRenderer'.$type;
  746.  
  747.         if!class_exists$class ) )
  748.         {
  749.             $path dirname(__FILE__).DS.$this->_type.DS.'renderer'.DS.$type.'.php';
  750.             if(file_exists($path)) {
  751.                 require_once($path);
  752.             else {
  753.                 JError::raiseError(500,JText::_('Unable to load renderer class'));
  754.             }
  755.         }
  756.  
  757.         if !class_exists$class ) ) {
  758.             return $null;
  759.         }
  760.  
  761.         $instance new $class($this);
  762.         return $instance;
  763.     }
  764.  
  765.     /**
  766.      * Outputs the document
  767.      *
  768.      * @access public
  769.      * @param boolean     $cache        If true, cache the output
  770.      * @param boolean     $compress    If true, compress the output
  771.      * @param array        $params        Associative array of attributes
  772.      * @return     The rendered data
  773.      */
  774.     function render$cache false$params array())
  775.     {
  776.         JResponse::setHeader'Expires'gmdate'D, d M Y H:i:s'time(900 ' GMT' );
  777.         if ($mdate $this->getModifiedDate()) {
  778.             JResponse::setHeader'Last-Modified'$mdate /* gmdate( 'D, d M Y H:i:s', time() + 900 ) . ' GMT' */ );
  779.         }
  780.         JResponse::setHeader'Content-Type'$this->_mime .  '; charset=' $this->_charset);
  781.     }
  782. }

Documentation generated on Sat, 14 Nov 2009 11:12:19 +0000 by phpDocumentor 1.3.1