Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/html/parameter.php

Documentation is available at parameter.php

  1. <?php
  2. /**
  3. @version        $Id: parameter.php 6743 2007-02-28 10:07:23Z tcp $
  4. @package        Joomla.Framework
  5. @subpackage    Parameter
  6. @copyright    Copyright (C) 2005 - 2007 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.registry.registry' );
  19.  
  20. /**
  21.  * Parameter handler
  22.  *
  23.  * @author         Johan Janssens <[email protected]>
  24.  * @package     Joomla.Framework
  25.  * @subpackage        Parameter
  26.  * @since        1.5
  27.  */
  28. class JParameter extends JRegistry
  29. {
  30.     /**
  31.      * The raw params string
  32.      *
  33.      * @access    private
  34.      * @var string 
  35.      */
  36.     var $_raw null;
  37.  
  38.     /**
  39.      * The xml params element
  40.      *
  41.      * @access    private
  42.      * @var object 
  43.      */
  44.     var $_xml null;
  45.  
  46.     /**
  47.     * loaded elements
  48.     *
  49.     * @access    private
  50.     * @var        array 
  51.     */
  52.     var $_elements array();
  53.  
  54.     /**
  55.     * directories, where element types can be stored
  56.     *
  57.     * @access    private
  58.     * @var        array 
  59.     */
  60.     var $_elementDirs array();
  61.  
  62.     /**
  63.      * Constructor
  64.      *
  65.      * @access protected
  66.      * @param string The raw parms text
  67.      * @param string Path to the xml setup file
  68.      * @var string The type of setup file
  69.      */
  70.     function __construct($data$path '')
  71.     {
  72.         if!defined'JPARAMETER_INCLUDE_PATH' ) ) {
  73.             define'JPARAMETER_INCLUDE_PATH'dirname__FILE__ ).DS.'parameter'.DS.'element' );
  74.         }
  75.  
  76.         parent::__construct('_default');
  77.  
  78.         if (trim$data )) {
  79.             $this->loadINI($data);
  80.         }
  81.  
  82.         if ($path{
  83.             $this->loadSetupFile($path);
  84.         }
  85.  
  86.         $this->_raw $data;
  87.     }
  88.  
  89.     /**
  90.      * Set a value
  91.      *
  92.      * @access public
  93.      * @param string The name of the param
  94.      * @param string The value of the parameter
  95.      * @return string The set value
  96.      */
  97.     function set($key$value ''$group '_default')
  98.     {
  99.         $this->setValue($group.'.'.$key(string) $value);
  100.         return $this->getValue($group.'.'.$key);
  101.     }
  102.  
  103.     /**
  104.      * Get a value
  105.      *
  106.      * @access public
  107.      * @param string The name of the param
  108.      * @param mixed The default value if not found
  109.      * @return string 
  110.      */
  111.     function get($key$default ''$group '_default')
  112.     {
  113.         $value $this->getValue($group.'.'.$key);
  114.         $result (empty($value&& ($value !== 0&& ($value !== '0')) $default $value;
  115.         return $result;
  116.     }
  117.  
  118.     /**
  119.      * Sets a default value if not alreay assigned
  120.      *
  121.      * @access public
  122.      * @param string The name of the param
  123.      * @param string The value of the parameter
  124.      * @return string The set value
  125.      */
  126.     function def($key$value ''{
  127.         return $this->set($key$this->get($key(string) $value));
  128.     }
  129.  
  130.     /**
  131.      * Sets the XML object from custom xml files
  132.      * @access public
  133.      * @param object An XML object
  134.      */
  135.     function setXML&$xml )
  136.     {
  137.         if (is_object$xml ))
  138.         {
  139.             if ($group $xml->attributes'group' )) {
  140.                 $this->_xml[$group$xml;
  141.             else {
  142.                 $this->_xml['_default'$xml;
  143.             }
  144.             if ($dir $xml->attributes'addparameterdir' )) {
  145.                 $this->addParameterDirJPATH_ROOT str_replace('/'DS$dir) );
  146.             }
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * Bind data to the parameter
  152.      *
  153.      * @param mixed $data Array or Object
  154.      * @return boolean True if the data was successfully bound
  155.      * @access public
  156.      * @since 1.5
  157.      */
  158.     function bind($data$group '_default')
  159.     {
  160.         if is_array($data) ) {
  161.             return $this->loadArray($data$group);
  162.         elseif is_object($data) ) {
  163.             return $this->loadObject($data$group);
  164.         else {
  165.             return $this->loadINI($data$group);
  166.         }
  167.  
  168.         return false;
  169.     }
  170.  
  171.     /**
  172.      * Render
  173.      *
  174.      * @access public
  175.      * @param string The name of the control, or the default text area if a setup file is not found
  176.      * @return string HTML
  177.      */
  178.     function render($name 'params'$group '_default')
  179.     {
  180.         if (!isset($this->_xml[$group])) {
  181.             return false;
  182.         }
  183.  
  184.         $params $this->getParams($name$group);
  185.  
  186.         $html array ();
  187.         $html['<table width="100%" class="paramlist" cellspacing="1">';
  188.  
  189.         if ($description $this->_xml[$group]->attributes('description')) {
  190.             // add the params description to the display
  191.             $desc    JText::_($description);
  192.             $html[]    '<tr><td class="paramlist_description" colspan="2">'.$desc.'</td></tr>';
  193.         }
  194.  
  195.         foreach ($params as $param)
  196.         {
  197.             $html['<tr>';
  198.  
  199.             $html['<td width="40%" class="paramlist_key"><span class="editlinktip">'.$param[0].'</span></td>';
  200.             $html['<td class="paramlist_value">'.$param[1].'</td>';
  201.  
  202.             $html['</tr>';
  203.         }
  204.  
  205.         if (count($params1{
  206.             $html["<tr><td colspan=\"2\"><i>".JText::_('There are no Parameters for this item')."</i></td></tr>";
  207.         }
  208.  
  209.         $html['</table>';
  210.  
  211.         return implode("\n"$html);
  212.     }
  213.  
  214.     /**
  215.      * Render all parameters to an array
  216.      *
  217.      * @access public
  218.      * @param string The name of the control, or the default text area if a setup file is not found
  219.      * @return array of all parameters, each as array Any array of the label, the form element and the tooltip
  220.      */
  221.     function renderToArray($name 'params'$group '_default')
  222.     {
  223.         if (!isset($this->_xml[$group])) {
  224.             return false;
  225.         }
  226.         $results array();
  227.         foreach ($this->_xml[$group]->children(as $param)  {
  228.             $result $this->getParam($param$name);
  229.             $results[$result[5]] $result;
  230.         }
  231.         return $results;
  232.     }
  233.  
  234.     /**
  235.      * Return number of params to render
  236.      *
  237.      * @access public
  238.      * @return mixed    Boolean falst if no params exist or integer number of params that exist
  239.      */
  240.     function getNumParams($group '_default'
  241.     {
  242.         if (!isset($this->_xml[$group]|| !count($this->_xml[$group]->children())) {
  243.             return false;
  244.         else {
  245.             return count($this->_xml[$group]->children());
  246.         }
  247.     }
  248.  
  249.     /**
  250.      * Get the number of params in each group
  251.      *
  252.      * @access public
  253.      * @return array of all group names as key and param count as value
  254.      */
  255.     function getGroups()
  256.     {
  257.         if (!is_array($this->_xml)) {
  258.             return false;
  259.         }
  260.         $results array();
  261.         foreach ($this->_xml as $name => $group)  {
  262.             $results[$name$this->getNumParams($name);
  263.         }
  264.         return $results;
  265.     }
  266.  
  267.     /**
  268.      * Render all parameters
  269.      *
  270.      * @access public
  271.      * @param string The name of the control, or the default text area if a setup file is not found
  272.      * @return array of all parameters, each as array Any array of the label, the form element and the tooltip
  273.      */
  274.     function getParams($name 'params'$group '_default')
  275.     {
  276.         if (!isset($this->_xml[$group])) {
  277.             return false;
  278.         }
  279.         $results array();
  280.         foreach ($this->_xml[$group]->children(as $param)  {
  281.             $results[$this->getParam($param$name$group);
  282.         }
  283.         return $results;
  284.     }
  285.  
  286.     /**
  287.      * Render a parameter type
  288.      *
  289.      * @param object param tag node
  290.      * @param string The control name
  291.      * @return array Any array of the label, the form element and the tooltip
  292.      */
  293.     function getParam(&$node$control_name 'params'$group '_default')
  294.     {
  295.         //get the type of the parameter
  296.         $type $node->attributes('type');
  297.  
  298.         //remove any occurance of a mos_ prefix
  299.         $type str_replace('mos_'''$type);
  300.  
  301.         $element =$this->loadElement($type);
  302.  
  303.         /**
  304.          * error happened
  305.          */
  306.         if ($element === false)
  307.         {
  308.             $result array();
  309.             $result[0$node->attributes('name');
  310.             $result[1JText::_('Element not defined for type').' = '.$type;
  311.             $result[5$result[0];
  312.             return $result;
  313.         }
  314.  
  315.         //get value
  316.         $value $this->get($node->attributes('name')$node->attributes('default'));
  317.  
  318.         return $element->render($node$value$control_name);
  319.     }
  320.  
  321.     /**
  322.     * Loads an xml setup file and parses it
  323.     *
  324.     * @access    public
  325.     * @param    string    path to xml setup file
  326.     * @return    object 
  327.     * @since 1.5
  328.     */
  329.     function loadSetupFile($path)
  330.     {
  331.         $result false;
  332.  
  333.         if ($path)
  334.         {
  335.             $xml JFactory::getXMLParser('Simple');
  336.  
  337.             if ($xml->loadFile($path))
  338.             {
  339.                 if ($params $xml->document->params{
  340.                     foreach ($params as $param)
  341.                     {
  342.                         $this->setXML$param );
  343.                         $result true;
  344.                     }
  345.                 }
  346.             }
  347.         }
  348.         else
  349.         {
  350.             $result true;
  351.         }
  352.  
  353.         return $result;
  354.     }
  355.  
  356.     /**
  357.     * Loads a element type
  358.     *
  359.     * @access    public
  360.     * @param    string    elementType
  361.     * @return    object 
  362.     * @since 1.5
  363.     */
  364.     function &loadElement$type$new false )
  365.     {
  366.         $false false;
  367.         $signature md5$type  );
  368.  
  369.         if( (isset$this->_elements[$signature&& !is_a($this->_elements[$signature]'__PHP_Incomplete_Class'))  && $new === false {
  370.             return    $this->_elements[$signature];
  371.         }
  372.  
  373.         if!class_exists'JElement' ) ) {
  374.             jimport('joomla.html.parameter.element');
  375.         }
  376.  
  377.         $elementClass    =    'JElement'.$type;
  378.         if!class_exists$elementClass ) )
  379.         {
  380.             ifisset$this->_elementDirs ) ) {
  381.                 $dirs $this->_elementDirs;
  382.             else {
  383.                 $dirs array();
  384.             }
  385.  
  386.             array_push$dirs$this->getIncludePath());
  387.  
  388.             $found false;
  389.             foreach$dirs as $dir )
  390.             {
  391.                 $elementFile    sprintf'%s'.DS.'%s.php'$dirstr_replace'_'DS$type ) );
  392.  
  393.                 if (@include_once $elementFile)
  394.                 {
  395.                     $found true;
  396.                     break;
  397.                 }
  398.             }
  399.  
  400.             if!$found {
  401.                 return $false;
  402.             }
  403.         }
  404.  
  405.         if!class_exists$elementClass ) ) {
  406.             return $false;
  407.         }
  408.  
  409.         $this->_elements[$signaturenew $elementClass($this);
  410.  
  411.         return $this->_elements[$signature];
  412.     }
  413.  
  414.     /**
  415.     * Add a directory where JParameter should search for element types
  416.     *
  417.     * You may either pass a string or an array of directories.
  418.     *
  419.     * JParameter will be searching for a element type in the same
  420.     * order you added them. If the parameter type cannot be found in
  421.     * the custom folders, it will look in
  422.     * JParameter/types.
  423.     *
  424.     * @access    public
  425.     * @param    string|array   directory or directories to search.
  426.     * @since 1.5
  427.     */
  428.     function addParameterDir$dir )
  429.     {
  430.         ifis_array$dir ) ) {
  431.             $this->_elementDirs array_merge$this->_elementDirs$dir );
  432.         else {
  433.             array_push$this->_elementDirs$dir );
  434.         }
  435.     }
  436.  
  437.    /**
  438.     * Get the include path
  439.     *
  440.     * @access    public
  441.     * @return   string 
  442.     * @since 1.5
  443.     */
  444.     function getIncludePath({
  445.         return    JPARAMETER_INCLUDE_PATH;
  446.     }
  447. }
  448. ?>

Documentation generated on Mon, 05 Mar 2007 21:13:11 +0000 by phpDocumentor 1.3.1