PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/parameter.php

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