Guest User

koda

a guest
Nov 17th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. <?php
  2. /**
  3. * @version $Id: select.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage HTML
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. defined('JPATH_BASE') or die();
  15. /**
  16. * Utility class for creating HTML select lists
  17. *
  18. * @static
  19. * @package Joomla.Framework
  20. * @subpackage HTML
  21. * @since 1.5
  22. */
  23. class JHTMLSelect
  24. {
  25. /**
  26. * @param string The value of the option
  27. * @param string The text for the option
  28. * @param string The returned object property name for the value
  29. * @param string The returned object property name for the text
  30. * @return object
  31. */
  32. function option( $value, $text='', $value_name='value', $text_name='text', $disable=false )
  33. {
  34. $obj = new stdClass;
  35. $obj->$value_name = $value;
  36. $obj->$text_name = trim( $text ) ? $text : $value;
  37. $obj->disable = $disable;
  38. return $obj;
  39. }
  40.  
  41. /**
  42. * @param string The text for the option
  43. * @param string The returned object property name for the value
  44. * @param string The returned object property name for the text
  45. * @return object
  46. */
  47. function optgroup( $text, $value_name = 'value', $text_name = 'text' )
  48. {
  49. $obj = new stdClass;
  50. $obj->$value_name = '<OPTGROUP>';
  51. $obj->$text_name = $text;
  52. return $obj;
  53. }
  54.  
  55. /**
  56. * Generates just the option tags for an HTML select list
  57. *
  58. * @param array An array of objects
  59. * @param string The name of the object variable for the option value
  60. * @param string The name of the object variable for the option text
  61. * @param mixed The key that is selected (accepts an array or a string)
  62. * @returns string HTML for the select list
  63. */
  64. function options( $arr, $key = 'value', $text = 'text', $selected = null, $translate = false )
  65. {
  66. $html = '';
  67.  
  68. foreach ($arr as $i => $option)
  69. {
  70. $element =& $arr[$i]; // since current doesn't return a reference, need to do this
  71.  
  72. $isArray = is_array( $element );
  73. $extra = '';
  74. if ($isArray)
  75. {
  76. $k = $element[$key];
  77. $t = $element[$text];
  78. $id = ( isset( $element['id'] ) ? $element['id'] : null );
  79. if(isset($element['disable']) && $element['disable']) {
  80. $extra .= ' disabled="disabled"';
  81. }
  82. }
  83. else
  84. {
  85. $k = $element->$key;
  86. $t = $element->$text;
  87. $id = ( isset( $element->id ) ? $element->id : null );
  88. if(isset( $element->disable ) && $element->disable) {
  89. $extra .= ' disabled="disabled"';
  90. }
  91. }
  92.  
  93. // This is real dirty, open to suggestions,
  94. // barring doing a propper object to handle it
  95. if ($k === '<OPTGROUP>') {
  96. $html .= '<optgroup label="' . $t . '">';
  97. } else if ($k === '</OPTGROUP>') {
  98. $html .= '</optgroup>';
  99. }
  100. else
  101. {
  102. //if no string after hypen - take hypen out
  103. $splitText = explode( ' - ', $t, 2 );
  104. $t = $splitText[0];
  105. if(isset($splitText[1])){ $t .= ' - '. $splitText[1]; }
  106.  
  107. //$extra = '';
  108. //$extra .= $id ? ' id="' . $arr[$i]->id . '"' : '';
  109. if (is_array( $selected ))
  110. {
  111. foreach ($selected as $val)
  112. {
  113. $k2 = is_object( $val ) ? $val->$key : $val;
  114. if ($k == $k2)
  115. {
  116. $extra .= ' selected="selected"';
  117. break;
  118. }
  119. }
  120. } else {
  121. $extra .= ( (string)$k == (string)$selected ? ' selected="selected"' : '' );
  122. }
  123.  
  124. //if flag translate text
  125. if ($translate) {
  126. $t = JText::_( $t );
  127. }
  128.  
  129. // ensure ampersands are encoded
  130. $k = JFilterOutput::ampReplace($k);
  131. $t = JFilterOutput::ampReplace($t);
  132.  
  133. $html .= '<option value="'. $k .'" '. $extra .'>' . $t . '</option>';
  134. }
  135. }
  136.  
  137. return $html;
  138. }
  139.  
  140. /**
  141. * Generates an HTML select list
  142. *
  143. * @param array An array of objects
  144. * @param string The value of the HTML name attribute
  145. * @param string Additional HTML attributes for the <select> tag
  146. * @param string The name of the object variable for the option value
  147. * @param string The name of the object variable for the option text
  148. * @param mixed The key that is selected (accepts an array or a string)
  149. * @returns string HTML for the select list
  150. */
  151. function genericlist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false )
  152. {
  153. if ( is_array( $arr ) ) {
  154. reset( $arr );
  155. }
  156.  
  157. if (is_array($attribs)) {
  158. $attribs = JArrayHelper::toString($attribs);
  159. }
  160.  
  161. $id = $name;
  162.  
  163. if ( $idtag ) {
  164. $id = $idtag;
  165. }
  166.  
  167. $id = str_replace('[','',$id);
  168. $id = str_replace(']','',$id);
  169.  
  170. $html = '<select name="'. $name .'" id="'. $id .'" '. $attribs .'>';
  171. $html .= JHTMLSelect::Options( $arr, $key, $text, $selected, $translate );
  172. $html .= '</select>';
  173.  
  174. return $html;
  175. }
  176.  
  177. /**
  178. * Generates a select list of integers
  179. *
  180. * @param int The start integer
  181. * @param int The end integer
  182. * @param int The increment
  183. * @param string The value of the HTML name attribute
  184. * @param string Additional HTML attributes for the <select> tag
  185. * @param mixed The key that is selected
  186. * @param string The printf format to be applied to the number
  187. * @returns string HTML for the select list
  188. */
  189. function integerlist( $start, $end, $inc, $name, $attribs = null, $selected = null, $format = "" )
  190. {
  191. $start = intval( $start );
  192. $end = intval( $end );
  193. $inc = intval( $inc );
  194. $arr = array();
  195.  
  196. for ($i=$start; $i <= $end; $i+=$inc)
  197. {
  198. $fi = $format ? sprintf( "$format", $i ) : "$i";
  199. $arr[] = JHTML::_('select.option', $fi, $fi );
  200. }
  201.  
  202. return JHTML::_('select.genericlist', $arr, $name, $attribs, 'value', 'text', $selected );
  203. }
  204.  
  205. /**
  206. * Generates an HTML radio list
  207. *
  208. * @param array An array of objects
  209. * @param string The value of the HTML name attribute
  210. * @param string Additional HTML attributes for the <select> tag
  211. * @param mixed The key that is selected
  212. * @param string The name of the object variable for the option value
  213. * @param string The name of the object variable for the option text
  214. * @returns string HTML for the select list
  215. */
  216. function radiolist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false )
  217. {
  218. reset( $arr );
  219. $html = '';
  220.  
  221. if (is_array($attribs)) {
  222. $attribs = JArrayHelper::toString($attribs);
  223. }
  224.  
  225. $id_text = $name;
  226. if ( $idtag ) {
  227. $id_text = $idtag;
  228. }
  229.  
  230. for ($i=0, $n=count( $arr ); $i < $n; $i++ )
  231. {
  232. $k = $arr[$i]->$key;
  233. $t = $translate ? JText::_( $arr[$i]->$text ) : $arr[$i]->$text;
  234. $id = ( isset($arr[$i]->id) ? @$arr[$i]->id : null);
  235.  
  236. $extra = '';
  237. $extra .= $id ? " id=\"" . $arr[$i]->id . "\"" : '';
  238. if (is_array( $selected ))
  239. {
  240. foreach ($selected as $val)
  241. {
  242. $k2 = is_object( $val ) ? $val->$key : $val;
  243. if ($k == $k2)
  244. {
  245. $extra .= " selected=\"selected\"";
  246. break;
  247. }
  248. }
  249. } else {
  250. $extra .= ((string)$k == (string)$selected ? " checked=\"checked\"" : '');
  251. }
  252. $html .= "\n\t<input type=\"radio\" name=\"$name\" id=\"$id_text$k\" value=\"".$k."\"$extra $attribs />";
  253. $html .= "\n\t<label for=\"$id_text$k\">$t</label>";
  254. }
  255. $html .= "\n";
  256. return $html;
  257. }
  258.  
  259. /**
  260. * Generates a yes/no radio list
  261. *
  262. * @param string The value of the HTML name attribute
  263. * @param string Additional HTML attributes for the <select> tag
  264. * @param mixed The key that is selected
  265. * @returns string HTML for the radio list
  266. */
  267. function booleanlist( $name, $attribs = null, $selected = null, $yes='yes', $no='no', $id=false )
  268. {
  269. $arr = array(
  270. JHTML::_('select.option', '0', JText::_( $no ) ),
  271. JHTML::_('select.option', '1', JText::_( $yes ) )
  272. );
  273. return JHTML::_('select.radiolist', $arr, $name, $attribs, 'value', 'text', (int) $selected, $id );
  274. }
  275. }
  276.  
  277.  
Advertisement
Add Comment
Please, Sign In to add comment