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

/com_joomleague/administrator/components/com_joomleague/helpers/jlparameter.php

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