/libraries/joomla/html/parameter.php

https://bitbucket.org/joomla/joomla-platform/ · PHP · 404 lines · 193 code · 50 blank · 161 comment · 37 complexity · dc8f04e0da7f6d5a35ae6a2b421ef8ba MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.registry.registry');
  11. // Register the element class with the loader.
  12. JLoader::register('JElement', dirname(__FILE__).'/parameter/element.php');
  13. /**
  14. * Parameter handler
  15. *
  16. * @package Joomla.Platform
  17. * @subpackage Parameter
  18. * @since 11.1
  19. */
  20. class JParameter extends JRegistry
  21. {
  22. /**
  23. * The raw params string
  24. *
  25. * @var string
  26. * @since 11.1
  27. */
  28. protected $_raw = null;
  29. /**
  30. * The xml params element
  31. *
  32. * @var object
  33. * @since 11.1
  34. */
  35. protected $_xml = null;
  36. /**
  37. * Loaded elements
  38. *
  39. * @var array
  40. * @since 11.1
  41. */
  42. protected $_elements = array();
  43. /**
  44. * Directories, where element types can be stored
  45. *
  46. * @var array
  47. * @since 11.1
  48. */
  49. protected $_elementPath = array();
  50. /**
  51. * Constructor
  52. *
  53. * @param string The raw parms text.
  54. * @param string Path to the xml setup file.
  55. * @since 11.1
  56. */
  57. public function __construct($data = '', $path = '')
  58. {
  59. parent::__construct('_default');
  60. // Set base path.
  61. $this->_elementPath[] = dirname(__FILE__).'/parameter/element';
  62. if ($data = trim($data)) {
  63. if (strpos($data, '{') === 0) {
  64. $this->loadJSON($data);
  65. } else {
  66. $this->loadINI($data);
  67. }
  68. }
  69. if ($path) {
  70. $this->loadSetupFile($path);
  71. }
  72. $this->_raw = $data;
  73. }
  74. /**
  75. * Sets a default value if not alreay assigned.
  76. *
  77. * @param string The name of the parameter.
  78. * @param string An optional value for the parameter.
  79. * @param string An optional group for the parameter.
  80. *
  81. * @return string The value set, or the default if the value was not previously set (or null).
  82. * @since 11.1
  83. */
  84. public function def($key, $default = '', $group = '_default')
  85. {
  86. $value = $this->get($key, (string) $default, $group);
  87. return $this->set($key, $value);
  88. }
  89. /**
  90. * Sets the XML object from custom xml files.
  91. *
  92. * @param JSimpleXMLElement An XML object.
  93. * @since 11.1
  94. */
  95. public function setXML(&$xml)
  96. {
  97. if (is_object($xml)) {
  98. if ($group = $xml->attributes('group')) {
  99. $this->_xml[$group] = $xml;
  100. } else {
  101. $this->_xml['_default'] = $xml;
  102. }
  103. if ($dir = $xml->attributes('addpath')) {
  104. $this->addElementPath(JPATH_ROOT . str_replace('/', DS, $dir));
  105. }
  106. }
  107. }
  108. /**
  109. * Bind data to the parameter.
  110. *
  111. * @param mixed An array or object.
  112. * @param string An optional group that the data should bind to. The default group is used if not supplied.
  113. * @return boolean True if the data was successfully bound, false otherwise.
  114. * @since 11.1
  115. */
  116. public function bind($data, $group = '_default')
  117. {
  118. if (is_array($data)) {
  119. return $this->loadArray($data, $group);
  120. } elseif (is_object($data)) {
  121. return $this->loadObject($data, $group);
  122. } else {
  123. return $this->loadJSON($data, $group);
  124. }
  125. }
  126. /**
  127. * Render the form control.
  128. *
  129. * @param string An optional name of the HTML form control. The default is 'params' if not supplied.
  130. * @param string An optional group to render. The default group is used if not supplied.
  131. *
  132. * @return string HTML
  133. * @since 11.1
  134. */
  135. public function render($name = 'params', $group = '_default')
  136. {
  137. if (!isset($this->_xml[$group])) {
  138. return false;
  139. }
  140. $params = $this->getParams($name, $group);
  141. $html = array ();
  142. if ($description = $this->_xml[$group]->attributes('description')) {
  143. // Add the params description to the display
  144. $desc = JText::_($description);
  145. $html[] = '<p class="paramrow_desc">'.$desc.'</p>';
  146. }
  147. foreach ($params as $param) {
  148. if ($param[0]) {
  149. $html[] = $param[0];
  150. $html[] = $param[1];
  151. } else {
  152. $html[] = $param[1];
  153. }
  154. }
  155. if (count($params) < 1) {
  156. $html[] = "<p class=\"noparams\">".JText::_('JLIB_HTML_NO_PARAMETERS_FOR_THIS_ITEM')."</p>";
  157. }
  158. return implode(PHP_EOL, $html);
  159. }
  160. /**
  161. * Render all parameters to an array.
  162. *
  163. * @param string An optional name of the HTML form control. The default is 'params' if not supplied.
  164. * @param string An optional group to render. The default group is used if not supplied.
  165. *
  166. * @return array
  167. * @since 11.1
  168. */
  169. public function renderToArray($name = 'params', $group = '_default')
  170. {
  171. if (!isset($this->_xml[$group])) {
  172. return false;
  173. }
  174. $results = array();
  175. foreach ($this->_xml[$group]->children() as $param) {
  176. $result = $this->getParam($param, $name, $group);
  177. $results[$result[5]] = $result;
  178. }
  179. return $results;
  180. }
  181. /**
  182. * Return the number of parameters in a group.
  183. *
  184. * @param string An optional group. The default group is used if not supplied.
  185. *
  186. * @return mixed False if no params exist or integer number of parameters that exist.
  187. * @since 11.1
  188. */
  189. public function getNumParams($group = '_default')
  190. {
  191. if (!isset($this->_xml[$group]) || !count($this->_xml[$group]->children())) {
  192. return false;
  193. } else {
  194. return count($this->_xml[$group]->children());
  195. }
  196. }
  197. /**
  198. * Get the number of params in each group.
  199. *
  200. * @return array Array of all group names as key and parameters count as value.
  201. * @since 11.1
  202. */
  203. public function getGroups()
  204. {
  205. if (!is_array($this->_xml)) {
  206. return false;
  207. }
  208. $results = array();
  209. foreach ($this->_xml as $name => $group) {
  210. $results[$name] = $this->getNumParams($name);
  211. }
  212. return $results;
  213. }
  214. /**
  215. * Render all parameters.
  216. *
  217. * @param string An optional name of the HTML form control. The default is 'params' if not supplied.
  218. * @param string An optional group to render. The default group is used if not supplied.
  219. *
  220. * @return array An array of all parameters, each as array of the label, the form element and the tooltip.
  221. * @since 11.1
  222. */
  223. public function getParams($name = 'params', $group = '_default')
  224. {
  225. if (!isset($this->_xml[$group])) {
  226. return false;
  227. }
  228. $results = array();
  229. foreach ($this->_xml[$group]->children() as $param) {
  230. $results[] = $this->getParam($param, $name, $group);
  231. }
  232. return $results;
  233. }
  234. /**
  235. * Render a parameter type.
  236. *
  237. * @param object A parameter XML element.
  238. * @param string An optional name of the HTML form control. The default is 'params' if not supplied.
  239. * @param string An optional group to render. The default group is used if not supplied.
  240. *
  241. * @return array Any array of the label, the form element and the tooltip.
  242. * @since 11.1
  243. */
  244. public function getParam(&$node, $control_name = 'params', $group = '_default')
  245. {
  246. // Get the type of the parameter.
  247. $type = $node->attributes('type');
  248. $element = $this->loadElement($type);
  249. // Check for an error.
  250. if ($element === false) {
  251. $result = array();
  252. $result[0] = $node->attributes('name');
  253. $result[1] = JText::_('Element not defined for type').' = '.$type;
  254. $result[5] = $result[0];
  255. return $result;
  256. }
  257. // Get value.
  258. $value = $this->get($node->attributes('name'), $node->attributes('default'), $group);
  259. return $element->render($node, $value, $control_name);
  260. }
  261. /**
  262. * Loads an xml setup file and parses it.
  263. *
  264. * @param string A path to the XML setup file.
  265. * @return object
  266. * @since 11.1
  267. */
  268. public function loadSetupFile($path)
  269. {
  270. $result = false;
  271. if ($path) {
  272. $xml = JFactory::getXMLParser('Simple');
  273. if ($xml->loadFile($path)) {
  274. if ($params = $xml->document->params) {
  275. foreach ($params as $param) {
  276. $this->setXML($param);
  277. $result = true;
  278. }
  279. }
  280. }
  281. } else {
  282. $result = true;
  283. }
  284. return $result;
  285. }
  286. /**
  287. * Loads an element type.
  288. *
  289. * @param string The element type.
  290. * @param boolean False (default) to reuse parameter elements; true to load the parameter element type again.
  291. *
  292. * @return object
  293. * @since 11.1
  294. */
  295. public function loadElement($type, $new = false)
  296. {
  297. $signature = md5($type);
  298. if ((isset($this->_elements[$signature]) && !($this->_elements[$signature] instanceof __PHP_Incomplete_Class)) && $new === false) {
  299. return $this->_elements[$signature];
  300. }
  301. $elementClass = 'JElement'.$type;
  302. if (!class_exists($elementClass)) {
  303. if (isset($this->_elementPath)) {
  304. $dirs = $this->_elementPath;
  305. } else {
  306. $dirs = array();
  307. }
  308. $file = JFilterInput::getInstance()->clean(str_replace('_', DS, $type).'.php', 'path');
  309. jimport('joomla.filesystem.path');
  310. if ($elementFile = JPath::find($dirs, $file)) {
  311. include_once $elementFile;
  312. } else {
  313. $false = false;
  314. return $false;
  315. }
  316. }
  317. if (!class_exists($elementClass)) {
  318. $false = false;
  319. return $false;
  320. }
  321. $this->_elements[$signature] = new $elementClass($this);
  322. return $this->_elements[$signature];
  323. }
  324. /**
  325. * Add a directory where JParameter should search for element types.
  326. *
  327. * You may either pass a string or an array of directories.
  328. *
  329. * JParameter will be searching for a element type in the same
  330. * order you added them. If the parameter type cannot be found in
  331. * the custom folders, it will look in
  332. * JParameter/types.
  333. *
  334. * @param string|array Directory or directories to search.
  335. * @since 11.1
  336. */
  337. public function addElementPath($path)
  338. {
  339. // Just force path to array.
  340. settype($path, 'array');
  341. // Loop through the path directories.
  342. foreach ($path as $dir) {
  343. // No surrounding spaces allowed!
  344. $dir = trim($dir);
  345. // Add trailing separators as needed.
  346. if (substr($dir, -1) != DIRECTORY_SEPARATOR) {
  347. // Directory
  348. $dir .= DIRECTORY_SEPARATOR;
  349. }
  350. // Add to the top of the search dirs.
  351. array_unshift($this->_elementPath, $dir);
  352. }
  353. }
  354. }