PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/parameter.php

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