Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/html/html/select.php

Documentation is available at select.php

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

Documentation generated on Sat, 14 Nov 2009 11:18:39 +0000 by phpDocumentor 1.3.1