PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/fof/form/field/list.php

https://gitlab.com/ricardosanchez/prueba
PHP | 382 lines | 225 code | 61 blank | 96 comment | 38 complexity | 61d3c481e7f583cae278daf9611e99d7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @subpackage form
  5. * @copyright Copyright (C) 2010 - 2014 Akeeba Ltd. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // Protect from unauthorized access
  9. defined('FOF_INCLUDED') or die;
  10. JFormHelper::loadFieldClass('list');
  11. /**
  12. * Form Field class for FOF
  13. * Supports a generic list of options.
  14. *
  15. * @package FrameworkOnFramework
  16. * @since 2.0
  17. */
  18. class FOFFormFieldList extends JFormFieldList implements FOFFormField
  19. {
  20. protected $static;
  21. protected $repeatable;
  22. /** @var FOFTable The item being rendered in a repeatable form field */
  23. public $item;
  24. /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
  25. public $rowid;
  26. /**
  27. * Method to get certain otherwise inaccessible properties from the form field object.
  28. *
  29. * @param string $name The property name for which to the the value.
  30. *
  31. * @return mixed The property value or null.
  32. *
  33. * @since 2.0
  34. */
  35. public function __get($name)
  36. {
  37. switch ($name)
  38. {
  39. case 'static':
  40. if (empty($this->static))
  41. {
  42. $this->static = $this->getStatic();
  43. }
  44. return $this->static;
  45. break;
  46. case 'repeatable':
  47. if (empty($this->repeatable))
  48. {
  49. $this->repeatable = $this->getRepeatable();
  50. }
  51. return $this->repeatable;
  52. break;
  53. default:
  54. return parent::__get($name);
  55. }
  56. }
  57. /**
  58. * Get the rendering of this field type for static display, e.g. in a single
  59. * item view (typically a "read" task).
  60. *
  61. * @since 2.0
  62. *
  63. * @return string The field HTML
  64. */
  65. public function getStatic()
  66. {
  67. $class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
  68. return '<span id="' . $this->id . '" ' . $class . '>' .
  69. htmlspecialchars(self::getOptionName($this->getOptions(), $this->value), ENT_COMPAT, 'UTF-8') .
  70. '</span>';
  71. }
  72. /**
  73. * Get the rendering of this field type for a repeatable (grid) display,
  74. * e.g. in a view listing many item (typically a "browse" task)
  75. *
  76. * @since 2.0
  77. *
  78. * @return string The field HTML
  79. */
  80. public function getRepeatable()
  81. {
  82. $show_link = false;
  83. $link_url = '';
  84. $class = $this->element['class'] ? (string) $this->element['class'] : '';
  85. if ($this->element['show_link'] == 'true')
  86. {
  87. $show_link = true;
  88. }
  89. if ($this->element['url'])
  90. {
  91. $link_url = $this->element['url'];
  92. }
  93. else
  94. {
  95. $show_link = false;
  96. }
  97. if ($show_link && ($this->item instanceof FOFTable))
  98. {
  99. $link_url = $this->parseFieldTags($link_url);
  100. }
  101. else
  102. {
  103. $show_link = false;
  104. }
  105. $html = '<span class="' . $this->id . ' ' . $class . '">';
  106. if ($show_link)
  107. {
  108. $html .= '<a href="' . $link_url . '">';
  109. }
  110. $html .= htmlspecialchars(self::getOptionName($this->getOptions(), $this->value), ENT_COMPAT, 'UTF-8');
  111. if ($show_link)
  112. {
  113. $html .= '</a>';
  114. }
  115. $html .= '</span>';
  116. return $html;
  117. }
  118. /**
  119. * Gets the active option's label given an array of JHtml options
  120. *
  121. * @param array $data The JHtml options to parse
  122. * @param mixed $selected The currently selected value
  123. * @param string $optKey Key name
  124. * @param string $optText Value name
  125. *
  126. * @return mixed The label of the currently selected option
  127. */
  128. public static function getOptionName($data, $selected = null, $optKey = 'value', $optText = 'text')
  129. {
  130. $ret = null;
  131. foreach ($data as $elementKey => &$element)
  132. {
  133. if (is_array($element))
  134. {
  135. $key = $optKey === null ? $elementKey : $element[$optKey];
  136. $text = $element[$optText];
  137. }
  138. elseif (is_object($element))
  139. {
  140. $key = $optKey === null ? $elementKey : $element->$optKey;
  141. $text = $element->$optText;
  142. }
  143. else
  144. {
  145. // This is a simple associative array
  146. $key = $elementKey;
  147. $text = $element;
  148. }
  149. if (is_null($ret))
  150. {
  151. $ret = $text;
  152. }
  153. elseif ($selected == $key)
  154. {
  155. $ret = $text;
  156. }
  157. }
  158. return $ret;
  159. }
  160. /**
  161. * Method to get the field options.
  162. *
  163. * Ordering is disabled by default. You can enable ordering by setting the
  164. * 'order' element in your form field. The other order values are optional.
  165. *
  166. * - order What to order. Possible values: 'name' or 'value' (default = false)
  167. * - order_dir Order direction. Possible values: 'asc' = Ascending or 'desc' = Descending (default = 'asc')
  168. * - order_case_sensitive Order case sensitive. Possible values: 'true' or 'false' (default = false)
  169. *
  170. * @return array The field option objects.
  171. *
  172. * @since Ordering is available since FOF 2.1.b2.
  173. */
  174. protected function getOptions()
  175. {
  176. // Ordering is disabled by default for backward compatibility
  177. $order = false;
  178. // Set default order direction
  179. $order_dir = 'asc';
  180. // Set default value for case sensitive sorting
  181. $order_case_sensitive = false;
  182. if ($this->element['order'] && $this->element['order'] !== 'false')
  183. {
  184. $order = $this->element['order'];
  185. }
  186. if ($this->element['order_dir'])
  187. {
  188. $order_dir = $this->element['order_dir'];
  189. }
  190. if ($this->element['order_case_sensitive'])
  191. {
  192. // Override default setting when the form element value is 'true'
  193. if ($this->element['order_case_sensitive'] == 'true')
  194. {
  195. $order_case_sensitive = true;
  196. }
  197. }
  198. // Create a $sortOptions array in order to apply sorting
  199. $i = 0;
  200. $sortOptions = array();
  201. foreach ($this->element->children() as $option)
  202. {
  203. $name = JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname));
  204. $sortOptions[$i] = new stdClass;
  205. $sortOptions[$i]->option = $option;
  206. $sortOptions[$i]->value = $option['value'];
  207. $sortOptions[$i]->name = $name;
  208. $i++;
  209. }
  210. // Only order if it's set
  211. if ($order)
  212. {
  213. jimport('joomla.utilities.arrayhelper');
  214. FOFUtilsArray::sortObjects($sortOptions, $order, $order_dir == 'asc' ? 1 : -1, $order_case_sensitive, false);
  215. }
  216. // Initialise the options
  217. $options = array();
  218. // Get the field $options
  219. foreach ($sortOptions as $sortOption)
  220. {
  221. $option = $sortOption->option;
  222. $name = $sortOption->name;
  223. // Only add <option /> elements.
  224. if ($option->getName() != 'option')
  225. {
  226. continue;
  227. }
  228. $tmp = JHtml::_('select.option', (string) $option['value'], $name, 'value', 'text', ((string) $option['disabled'] == 'true'));
  229. // Set some option attributes.
  230. $tmp->class = (string) $option['class'];
  231. // Set some JavaScript option attributes.
  232. $tmp->onclick = (string) $option['onclick'];
  233. // Add the option object to the result set.
  234. $options[] = $tmp;
  235. }
  236. // Do we have a class and method source for our options?
  237. $source_file = empty($this->element['source_file']) ? '' : (string) $this->element['source_file'];
  238. $source_class = empty($this->element['source_class']) ? '' : (string) $this->element['source_class'];
  239. $source_method = empty($this->element['source_method']) ? '' : (string) $this->element['source_method'];
  240. $source_key = empty($this->element['source_key']) ? '*' : (string) $this->element['source_key'];
  241. $source_value = empty($this->element['source_value']) ? '*' : (string) $this->element['source_value'];
  242. $source_translate = empty($this->element['source_translate']) ? 'true' : (string) $this->element['source_translate'];
  243. $source_translate = in_array(strtolower($source_translate), array('true','yes','1','on')) ? true : false;
  244. $source_format = empty($this->element['source_format']) ? '' : (string) $this->element['source_format'];
  245. if ($source_class && $source_method)
  246. {
  247. // Maybe we have to load a file?
  248. if (!empty($source_file))
  249. {
  250. $source_file = FOFTemplateUtils::parsePath($source_file, true);
  251. if (FOFPlatform::getInstance()->getIntegrationObject('filesystem')->fileExists($source_file))
  252. {
  253. include_once $source_file;
  254. }
  255. }
  256. // Make sure the class exists
  257. if (class_exists($source_class, true))
  258. {
  259. // ...and so does the option
  260. if (in_array($source_method, get_class_methods($source_class)))
  261. {
  262. // Get the data from the class
  263. if ($source_format == 'optionsobject')
  264. {
  265. $options = array_merge($options, $source_class::$source_method());
  266. }
  267. else
  268. {
  269. // Get the data from the class
  270. $source_data = $source_class::$source_method();
  271. // Loop through the data and prime the $options array
  272. foreach ($source_data as $k => $v)
  273. {
  274. $key = (empty($source_key) || ($source_key == '*')) ? $k : $v[$source_key];
  275. $value = (empty($source_value) || ($source_value == '*')) ? $v : $v[$source_value];
  276. if ($source_translate)
  277. {
  278. $value = JText::_($value);
  279. }
  280. $options[] = JHtml::_('select.option', $key, $value, 'value', 'text');
  281. }
  282. }
  283. }
  284. }
  285. }
  286. reset($options);
  287. return $options;
  288. }
  289. /**
  290. * Replace string with tags that reference fields
  291. *
  292. * @param string $text Text to process
  293. *
  294. * @return string Text with tags replace
  295. */
  296. protected function parseFieldTags($text)
  297. {
  298. $ret = $text;
  299. // Replace [ITEM:ID] in the URL with the item's key value (usually:
  300. // the auto-incrementing numeric ID)
  301. $keyfield = $this->item->getKeyName();
  302. $replace = $this->item->$keyfield;
  303. $ret = str_replace('[ITEM:ID]', $replace, $ret);
  304. // Replace the [ITEMID] in the URL with the current Itemid parameter
  305. $ret = str_replace('[ITEMID]', JFactory::getApplication()->input->getInt('Itemid', 0), $ret);
  306. // Replace other field variables in the URL
  307. $fields = $this->item->getTableFields();
  308. foreach ($fields as $fielddata)
  309. {
  310. $fieldname = $fielddata->Field;
  311. if (empty($fieldname))
  312. {
  313. $fieldname = $fielddata->column_name;
  314. }
  315. $search = '[ITEM:' . strtoupper($fieldname) . ']';
  316. $replace = $this->item->$fieldname;
  317. $ret = str_replace($search, $replace, $ret);
  318. }
  319. return $ret;
  320. }
  321. }