PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/parameter.php

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