[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

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


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