forms
[ class tree: forms ] [ index: forms ] [ all elements ]

Source for file HTMLSelect.class.php

Documentation is available at HTMLSelect.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package forms
  9.  * @subpackage items
  10.  */
  11. if!defined"CLASSPATH" ) )
  12. {
  13.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  14.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  15.   exit();
  16. }
  17.  
  18. ifdefined"LANG" ) )
  19.   /**
  20.    * Including language specific messages.
  21.    */
  22.   includeCLASSPATH."/forms/items/HTMLSelect/".LANG.".inc.php" );
  23. else
  24. {
  25.   echo "<h3>You have to define the constant LANG!</h3>\r\n";
  26.   echo "Example for german: define( 'LANG', 'de' );\r\n";
  27.   exit();
  28. }
  29.  
  30. /**
  31.  *
  32.  */
  33. require_onceCLASSPATH."forms/items/ABSTHTMLFormItem.class.php" );
  34. /**
  35.  *
  36.  */
  37. include_onceCLASSPATH."forms/items/HTMLHiddenInput.class.php" );
  38. /**
  39.  *
  40.  */
  41. include_onceCLASSPATH."RegExpConstants.inc.php" );
  42.  
  43. /**
  44.  * A class to generate select fields.
  45.  *
  46.  * @package    forms
  47.  * @subpackage items
  48.  * @version    0.3.45
  49.  * @author     Daniel Plücken <daniel@debakel.net>
  50.  * @license    http://www.gnu.org/copyleft/lesser.html
  51.  *              GNU Lesser General Public License
  52.  * @copyright  Copyright (C) 2003 Daniel Plücken <daniel@debakel.net>
  53.  *
  54.  *  This library is free software; you can redistribute it and/or
  55.  *  modify it under the terms of the GNU Lesser General Public
  56.  *  License as published by the Free Software Foundation; either
  57.  *  version 2.1 of the License.
  58.  *
  59.  *  This library is distributed in the hope that it will be useful,
  60.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  61.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  62.  *  GNU Lesser General Public License for more details.
  63.  *
  64.  *  You should have received a copy of the GNU Lesser General
  65.  *  Public License along with this library; if not, write to the
  66.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  67.  *  Boston, MA 02111-1307 USA
  68.  */
  69. class HTMLSelect extends ABSTHTMLFormItem
  70. {
  71.     /**
  72.      * The charset to be used on option-labels
  73.      *
  74.      * @var    string  $charset 
  75.      * @access private
  76.      */
  77.     var $charset = "ISO-8859-15";
  78.     /**
  79.      * @var    array   $label_arr 
  80.      * @access private
  81.      */
  82.     var $label_arr = array();
  83.     /**
  84.      * @var    array   $value_arr 
  85.      * @access private
  86.      */
  87.     var $value_arr = array();
  88.     /**
  89.      * @var    array   $style_class_arr 
  90.      * @access private
  91.      */
  92.     var $style_class_arr = array();
  93.     /**
  94.      * @var    array   $freestyle_arr 
  95.      * @access private
  96.      */
  97.     var $freestyle_arr = array();
  98.     /**
  99.      * @var    array   $on_change_order 
  100.      * @access public
  101.      */
  102.     var $on_change_order;
  103.     /**
  104.      * @var    array   $show_standard_label 
  105.      * @access public
  106.      */
  107.     var $show_standard_label = false;
  108.     /**
  109.     * @var    array   $standard_label 
  110.     * @access private
  111.     */
  112.     var $standard_label = STANDARD_LABEL;
  113.     /**
  114.      * @var    boolean $multiple 
  115.      * @access public
  116.      */
  117.     var $multiple = false;
  118.     /**
  119.      * @var    integer $size 
  120.      * @access private
  121.      */
  122.     var $size = 1;
  123.     /**
  124.      * @var    integer $show_standard_label_on_single_value 
  125.      * @access private
  126.      */
  127.     /**
  128.      * @var    boolean $show_despite_zero_label_count 
  129.      * @access public
  130.      */
  131.     var $show_despite_zero_label_count = false;
  132.     /**
  133.      * @var    string  $text_on_hidden 
  134.      * @access public
  135.      */
  136.     var $text_on_hidden = "";
  137.     /**
  138.      * This attribute is to prevent errors if the class MultRelationSelect is
  139.      * used. If it is set to true the disabling of this item is not possible.
  140.      *
  141.      * @var    string  $text_on_hidden 
  142.      * @access private
  143.      */
  144.     var $do_not_disable = false;
  145.     /**
  146.      * Enter description here...
  147.      *
  148.      * @access public
  149.      * @var string 
  150.      */
  151.     var $str_single_value_template = "";
  152.  
  153.  
  154.     /**
  155.      * Constructor
  156.      *
  157.      * @version 1.2
  158.      * @since   0.1.0
  159.      * @author  Daniel Plücken <daniel@debakel.net>
  160.      * @access  public
  161.      * @param   string  $name 
  162.      * @param   array   $label_arr 
  163.      * @param   array   $value_arr 
  164.      * @param   string  $selected_value 
  165.      * @param   string  $on_change_order 
  166.      * @param   string  $show_standard_label 
  167.      * @param   string  $show_standard_label_on_single_value 
  168.      */
  169.     function HTMLSelect(
  170.     $name                "select",
  171.     $label_arr           array(),
  172.     $value_arr           array(),
  173.     $selected_value      "",
  174.     $on_change_order     "",
  175.     $show_standard_label true,
  176.     $show_standard_label_on_single_value true
  177.     )
  178.     {
  179.         $this->name            = $name;
  180.         $this->id              = $name;
  181.         $this->label_arr       = $label_arr;
  182.         $this->value_arr       = $value_arr;
  183.         $this->value           = $selected_value;
  184.         $this->on_change_order = $on_change_order;
  185.  
  186.         $this->showStandardLabel(
  187.         $show_standard_label,
  188.         $show_standard_label_on_single_value
  189.                                  );
  190.     }
  191.  
  192.  
  193.  
  194.     /**
  195.      * Sets the charset for the optionlabels of this select.
  196.      *
  197.      * @version 1.0
  198.      * @since   0.3.3
  199.      * @author  Daniel Plücken <daniel@debakel.net>
  200.      * @see     http://www.php.net/manual/en/function.htmlentities.php
  201.      * @see     http://www.php.net/manual/en/function.html-entity-decode.php
  202.      * @access  public
  203.      * @param   string $string 
  204.      * @return  void 
  205.      */
  206.     function setCharset$string )
  207.     {
  208.         switch $string )
  209.         {
  210.             case "ISO-8859-1":
  211.             case "ISO8859-1":
  212.             case "ISO-8859-15":
  213.             case "ISO8859-15":
  214.             case "UTF-8":
  215.             case "cp866":
  216.             case "ibm866":
  217.             case "866":
  218.             case "cp1251":
  219.             case "Windows-1251":
  220.             case "win-1251":
  221.             case "1251":
  222.             case "cp1252":
  223.             case "Windows-1252":
  224.             case "1252":
  225.             case "KOI8-R":
  226.             case "koi8-ru":
  227.             case "koi8r":
  228.             case "BIG5":
  229.             case "950":
  230.             case "GB2312":
  231.             case "936":
  232.             case "BIG5-HKSCS":
  233.             case "Shift_JIS":
  234.             case "SJIS":
  235.             case "932":
  236.             case "EUC-JP":
  237.             case "EUCJP":
  238.                  $this->charset = $string;
  239.                  break;
  240.             default:
  241.                  break;
  242.         }
  243.     }
  244.  
  245.  
  246.  
  247.     /**
  248.      * Sets the javascript that should execute if the value of the select
  249.      * changes
  250.      *
  251.      * @version 1.0
  252.      * @since   0.1.2
  253.      * @author  Daniel Plücken <daniel@debakel.net>
  254.      * @access  public
  255.      * @param   string $string 
  256.      * @return  void 
  257.      */
  258.     function setChangeOrder$string )
  259.     $this->on_change_order = $string}
  260.  
  261.  
  262.  
  263.     /**
  264.      * Sets the labels of this select in order to the given array.
  265.      *
  266.      * @version 1.0
  267.      * @since   0.1.0
  268.      * @author  Daniel Plücken <daniel@debakel.net>
  269.      * @access  public
  270.      * @param   array $stringArray 
  271.      * @return  void 
  272.      */
  273.     function setLabels$stringArray )
  274.     $this->label_arr = $stringArray}
  275.  
  276.  
  277.  
  278.     /**
  279.      * Sets the values of this select in order to the given array.
  280.      *
  281.      * @version 1.1
  282.      * @since   0.1.0
  283.      * @author  Daniel Plücken <daniel@debakel.net>
  284.      * @access  public
  285.      * @param   array $array 
  286.      * @return  void 
  287.      */
  288.     function setValues$array )
  289.     {
  290.       $this->value_arr = $array;
  291.       $this->showStandardLabel(
  292.                      $this->show_standard_label,
  293.                      $this->show_standard_label_on_single_value
  294.                               );
  295.     }
  296.  
  297.  
  298.  
  299.     /**
  300.      * Sets the style classes of the options of this select in order to the
  301.      * given array.
  302.      *
  303.      * @version 1.0
  304.      * @since   0.3.2
  305.      * @author  Daniel Plücken <daniel@debakel.net>
  306.      * @access  public
  307.      * @param   array $array 
  308.      * @return  void 
  309.      */
  310.     function setStyles$array )
  311.     $this->style_class_arr = $array}
  312.  
  313.  
  314.  
  315.     /**
  316.      * Sets the style attributes of the options of this select in order to the
  317.      * given array.
  318.      *
  319.      * @version 1.0
  320.      * @since   0.3.2
  321.      * @author  Daniel Plücken <daniel@debakel.net>
  322.      * @access  public
  323.      * @param   array $array 
  324.      * @return  void 
  325.      */
  326.     function setFreeStyles$array )
  327.     $this->style_class_arr = $array}
  328.  
  329.  
  330.  
  331.     /**
  332.      * Adds an option to this this select.
  333.      *
  334.      * @version 1.0
  335.      * @since   0.2.1
  336.      * @author  Daniel Plücken <daniel@debakel.net>
  337.      * @access  public
  338.      * @param   string $value 
  339.      * @param   string $label 
  340.      * @return  void 
  341.      */
  342.     function addOption$value$label )
  343.     {
  344.       $this->label_arr[$label;
  345.       $this->value_arr[$value;
  346.     }
  347.  
  348.  
  349.  
  350.     /**
  351.      * Adds an option to this this select at a specified position.
  352.      *
  353.      * @version 1.1
  354.      * @since   0.2.35
  355.      * @author  Daniel Plücken <daniel@debakel.net>
  356.      * @access  public
  357.      * @param   string  $value 
  358.      * @param   string  $label 
  359.      * @param   string  $position Has to be greater then zero!
  360.      * @return  boolean 
  361.      */
  362.     function addOptionAt$value$label$position )
  363.     {
  364.         if intval$position )
  365.         {
  366.             if !is_array$this->label_arr ) )
  367.                $this->label_arr = array();
  368.             if !is_array$this->value_arr ) )
  369.                $this->value_arr = array();
  370.  
  371.             array_splice$this->label_arr($position-1)0array$label ) );
  372.             array_splice$this->value_arr($position-1)0array$value ) );
  373.  
  374.             return true;
  375.         }
  376.  
  377.         return false;
  378.     }
  379.  
  380.  
  381.  
  382.     /**
  383.      * Set the text that should appear if the select is hidden, cause of zero
  384.      * count of options.
  385.      *
  386.      * @version 1.0
  387.      * @since   0.2.6
  388.      * @author  Daniel Plücken <daniel@debakel.net>
  389.      * @access  public
  390.      * @param   string $value 
  391.      * @param   string $label 
  392.      * @return  void 
  393.      */
  394.     function setAlternativeTextOnHidden$string )
  395.     $this->text_on_hidden = $string}
  396.  
  397.  
  398.  
  399.     /**
  400.      * Sets the size-attribute of this select.
  401.      *
  402.      * @version 1.0
  403.      * @since   0.1.9
  404.      * @author  Daniel Plücken <daniel@debakel.net>
  405.      * @access  public
  406.      * @return  void 
  407.      */
  408.     function setSize$int )
  409.     {
  410.       ifpreg_match"!^".UNSIGNEDINTEGER."$!"$int ) )
  411.         $this->size = $int;
  412.       else
  413.       {
  414.         echo "<pre>\r\n";
  415.         echo "<b>".$int." is not a valid value for a select!</b>\r\n\r\n";
  416.         $debugArr debug_backtrace();
  417.         print_r$debugArr[count($debugArr)-1);
  418.         echo "</pre>\r\n";
  419.       }
  420.     }
  421.  
  422.  
  423.  
  424.     /**
  425.      * Sets this select to be able in selecting multiple values.
  426.      *
  427.      * @version 1.0
  428.      * @since   0.1.9
  429.      * @author  Daniel Plücken <daniel@debakel.net>
  430.      * @access  public
  431.      * @return  void 
  432.      */
  433.     function setMultiple()
  434.     $this->multiple = true}
  435.  
  436.  
  437.  
  438.     /**
  439.      * Sets the label of this select that schould be shown if no value is
  440.      * selected.
  441.      *
  442.      * @version 1.0
  443.      * @since   0.1.1
  444.      * @author  Daniel Plücken <daniel@debakel.net>
  445.      * @access  public
  446.      * @param   string $string 
  447.      * @return  void 
  448.      */
  449.     function setStandardLabel$string )
  450.     $this->standard_label = $string}
  451.  
  452.  
  453.  
  454.     /**
  455.      * Sets whether the standardlabel of this select schould be shown if no
  456.      * value is selected to.
  457.      *
  458.      * @version 1.2
  459.      * @since   0.1.1
  460.      * @author  Daniel Plücken <daniel@debakel.net>
  461.      * @access  public
  462.      * @param   boolean $boolean 
  463.      * @param   boolean $show_standard_label_on_single_value 
  464.      * @return  void 
  465.      */
  466.     function showStandardLabel(
  467.                      $boolean,
  468.                      $show_standard_label_on_single_value true
  469.                               )
  470.     {
  471.        if is_bool$boolean ) )
  472.           $this->show_standard_label = $boolean;
  473.  
  474.        if (
  475.             is_array$this->value_arr )
  476.          && !in_array$this->value$this->value_arr )
  477.           )
  478.           if !$this->show_standard_label )
  479.              $this->value = $this->value_arr[0];
  480.           else
  481.              $this->value = "";
  482.  
  483.        if is_bool$show_standard_label_on_single_value ) )
  484.           $this->show_standard_label_on_single_value =
  485.                      $show_standard_label_on_single_value;
  486.     }
  487.  
  488.  
  489.  
  490.     /**
  491.      * Sets whether the select schould be shown if no value is presented on it.
  492.      *
  493.      * @access  public
  494.      * @version 1.0
  495.      * @since   0.2.5
  496.      *
  497.      * @param   boolean $boolean 
  498.      *
  499.      * @return  void 
  500.      */
  501.     function showAltoughOnZeroLabelCount$boolean true )
  502.     {
  503.        if is_bool$boolean ) )
  504.        {  $this->show_despite_zero_label_count = $boolean}
  505.     }
  506.  
  507.  
  508.  
  509.     /**
  510.      * If the single value template is set, then the method get() will only
  511.      * output the parsed template instead of the selectbox. You can use the
  512.      * template variable [#label#] to place the single label in the template
  513.      * string.
  514.      *
  515.      * @access  public
  516.      * @version 1.0
  517.      * @since   0.3.45
  518.      *
  519.      * @param string $str_tpl 
  520.      *
  521.      * @return void 
  522.      */
  523.     function setSingleValueTemplate$str_tpl )
  524.     $this->str_single_value_template = $str_tpl}
  525.  
  526.  
  527.  
  528.     /**
  529.      * Returns a clone of this object.
  530.      *
  531.      * @version 1.0
  532.      * @since   0.1.2
  533.      * @author  Daniel Plücken <daniel@debakel.net>
  534.      * @access  public
  535.      * @param   string $newName 
  536.      * @param   mixed  $selectedValue 
  537.      * @param   string $on_change_order 
  538.      * @return  void 
  539.      */
  540.     function getClone(
  541.                         $newName,
  542.                         $selectedValue false,
  543.                         $on_change_order false
  544.                      )
  545.     {
  546.       $newName !empty$newName )
  547.                  ? $newName
  548.                  : $this->name;
  549.  
  550.       $newInstance new HTMLSelect(
  551.                                      $newName,
  552.                                      $this->label_arr,
  553.                                      $this->value_arr,
  554.                                      $this->selectedValue,
  555.                                      $this->on_change_order,
  556.                                      $this->show_standard_label
  557.                                    );
  558.  
  559.       $newInstance->setStandardLabel$this->getStandardLabel() );
  560.       if$selectedValue !== false )
  561.         $newInstance->setSelectedValue$selectedValue );
  562.       if$on_change_order !== false )
  563.         $newInstance->setChangeOrder$on_change_order );
  564.  
  565.       return $newInstance;
  566.     }
  567.  
  568.  
  569.  
  570.     /**
  571.      * Returns the array containing the labels of this select.
  572.      *
  573.      * @version 1.0
  574.      * @since   0.1.2
  575.      * @author  Daniel Plücken <daniel@debakel.net>
  576.      * @access  public
  577.      * @return  array 
  578.      */
  579.     function getLabelArr()
  580.     return $this->label_arr}
  581.  
  582.  
  583.  
  584.     /**
  585.      * Returns the array containing the valuess of this select.
  586.      *
  587.      * @version 1.0
  588.      * @since   0.1.2
  589.      * @author  Daniel Plücken <daniel@debakel.net>
  590.      * @access  public
  591.      * @return  array 
  592.      */
  593.     function getValueArr()
  594.     return $this->value_arr}
  595.  
  596.  
  597.  
  598.     /**
  599.      * Returns the selected label of this select.
  600.      *
  601.      * @access  public
  602.      * @version 1.01
  603.      * @since   0.1.2
  604.      *
  605.      * @return  string 
  606.      */
  607.     function getSelectedLabel()
  608.     {
  609.         foreach $this->value_arr as $value )
  610.         if $value == parent::getValue() )
  611.         {  return $value}
  612.     }
  613.  
  614.  
  615.  
  616.     /**
  617.      * Returns the selected value of this select.
  618.      *
  619.      * @access  public
  620.      * @version 1.0
  621.      * @since   0.1.2
  622.      *
  623.      * @return  string 
  624.      */
  625.     function getSelectedValue()
  626.     return parent::getValue()}
  627.  
  628.  
  629.  
  630.     /**
  631.      * Returns the javascript that should execute if the value this select
  632.      * changes.
  633.      *
  634.      * @version 1.0
  635.      * @since   0.1.2
  636.      * @author  Daniel Plücken <daniel@debakel.net>
  637.      * @access  public
  638.      * @return  string 
  639.      */
  640.     function getChangeOrder()
  641.     return $this->on_change_order}
  642.  
  643.  
  644.  
  645.     /**
  646.      * Returns the standard label of this select.
  647.      *
  648.      * @version 1.0
  649.      * @since   0.1.2
  650.      * @author  Daniel Plücken <daniel@debakel.net>
  651.      * @access  public
  652.      * @return  string 
  653.      */
  654.     function getStandardLabel()
  655.     return $this->standard_label}
  656.  
  657.  
  658.  
  659.    /**
  660.     * Returns an if-order of a javascript to check whether this item is NOT
  661.     * filled
  662.     *
  663.     * @version 1.1
  664.     * @since   0.1.7
  665.     * @author  Daniel Plücken <daniel@debakel.net>
  666.     * @access  public
  667.     * @return  string 
  668.     */
  669.    function getCheckOrder()
  670.    {
  671.      $outputString "document.".$this->parentform->name."."
  672.                                 .$this->name.".value == \"\"";
  673.  
  674.      return $outputString;
  675.    }
  676.  
  677.  
  678.  
  679.    /**
  680.     * Generates the sourcecode to build this object and returns it.
  681.     *
  682.     * @version 1.1
  683.     * @since   0.1.0
  684.     * @author  Daniel Plücken <daniel@debakel.net>
  685.     * @access  public
  686.     * @return  string 
  687.     */
  688.    function getPHPSource()
  689.    {
  690.       $out .= "unset( \x24label_arr );\r\n";
  691.       $out .= "unset( \x24value_arr );\r\n";
  692.       $cntLab count$this->label_arr );
  693.       $cntVal count$this->value_arr );
  694.  
  695.       if!empty$cntLab ) )
  696.       {
  697.          $out .= "\x24label_arr = array(\r\n";
  698.          for$i 0$i $cntLab $i++ )
  699.          {
  700.             $comma $cntLab != $i
  701.                      ? ","
  702.                      : "";
  703.             $out .= "                    \""
  704.                    .$this->label_arr[$i]."\"".$comma."\r\n";
  705.          }
  706.          $out .= "                 );\r\n";
  707.       }
  708.  
  709.       if!empty$cntVal ) )
  710.       {
  711.          $out .= "\x24value_arr = array(\r\n";
  712.          for$i 0$i $cntVal $i++ )
  713.          {
  714.             $comma $cntVal != $i
  715.                      ? ","
  716.                      : "";
  717.             $out .= "                    \""
  718.                    .$this->value_arr[$i]."\"".$comma."\r\n";
  719.          }
  720.          $out .= "                 );\r\n";
  721.       }
  722.  
  723.  
  724.       $out .= "unset( \x24value );\r\n";
  725.       $out .= "\x24value = \x24_POST[\"".$this->getName()."\"]\r\n"
  726.                    ."        .\x24_GET[\"".$this->getName()."\"];\r\n";
  727.       $out .= "\x24input[] = new HTMLSelect(\r\n"
  728.              ."                            \"".$this->getName()."\",\r\n"
  729.              ."                            \x24Llabel_arr\r\n"
  730.              ."                            \x24value_arr,\r\n"
  731.              ."                            \x24value,\r\n"
  732.              ."                            \"".$this->getChangeOrder()."\","
  733.                                               ."\r\n"
  734.              ."                            ".(
  735.       empty$this->show_standard_label )
  736.       ? "false"
  737.                                  : "true"
  738.                                              )."\r\n"
  739.              ."                         );\r\n";
  740.  
  741.       $tmp $this->getSelectedValue();
  742.       $out .= !empty$tmp )
  743.       ? "if( \x24value === \"\" )\r\n"
  744.                      ."  \x24input[count(\x24input)-1]"
  745.                        ."->setSelectedValue( \"".$tmp."\" );\r\n\r\n"
  746.                     : "\r\n";
  747.  
  748.       return $out;
  749.    }
  750.  
  751.  
  752.  
  753.    /**
  754.     * Returns an javascript-function to get the value of a given select.
  755.     *
  756.     * @static
  757.     * @version 1.02
  758.     * @since   0.1.8
  759.     * @author  Daniel Plücken <daniel@debakel.net>
  760.     * @param   boolean $withJSdeclaration 
  761.     * @access  public
  762.     * @return  string 
  763.     */
  764.    function getJS2getSelectedValue$withJSdeclaration true )
  765.    {
  766.        static $JSOutputted 0;
  767.  
  768.        if$JSOutputted )
  769.        return "";
  770.        else
  771.        {
  772.            $JSOutputted++;
  773.  
  774.            $out "  window.getSelectedValueOf = function( which )\r\n"
  775.               ."  {\r\n"
  776.               ."    try { cnt = which.options.length; } catch (e) { cnt = 0; }\r\n"
  777.               ."    for( i = 0; i < cnt; ++i )\r\n"
  778.               ."      if( which.options[i].selected == true )\r\n"
  779.               ."        return which.options[i].value;\r\n\r\n"
  780.               ."    return '';\r\n"
  781.               ."  }\r\n";
  782.  
  783.            if$withJSdeclaration === true )
  784.            {
  785.                $js new Javascript();
  786.                $js->add$out );
  787.  
  788.                $out $js->get();
  789.            }
  790.  
  791.            return $out;
  792.        }
  793.    }
  794.  
  795.  
  796.  
  797.    /**
  798.     * Returns an javascript-function to get the selectedoption-object of a given
  799.     * select.
  800.     *
  801.     * @static
  802.     * @version 1.02
  803.     * @since   0.3.4
  804.     * @author  Daniel Plücken <daniel@debakel.net>
  805.     * @param   boolean $withJSdeclaration 
  806.     * @access  public
  807.     * @return  string 
  808.     */
  809.    function getJS2getSelectedOption$withJSdeclaration true )
  810.    {
  811.        static $JSOutputted 0;
  812.  
  813.        if$JSOutputted )
  814.        return "";
  815.        else
  816.        {
  817.            $JSOutputted++;
  818.  
  819.            $out "  window.getSelectedOptionOf = function( which )\r\n"
  820.               ."  {\r\n"
  821.               ."    try { cnt = which.options.length; } catch (e) { cnt = 0; }\r\n"
  822.               ."    for( i = 0; i < cnt; ++i )\r\n"
  823.               ."      if( which.options[i].selected == true )\r\n"
  824.               ."        return which.options[i];\r\n\r\n"
  825.               ."    return null;\r\n"
  826.               ."  }\r\n";
  827.  
  828.            if$withJSdeclaration === true )
  829.            {
  830.                $js new Javascript();
  831.                $js->add$out );
  832.  
  833.                $out $js->get();
  834.            }
  835.  
  836.            return $out;
  837.        }
  838.    }
  839.  
  840.  
  841.  
  842.    /**
  843.     * Returns an javascript-function to get all values from a given select as
  844.     * an array.
  845.     *
  846.     * @static
  847.     * @version 1.15
  848.     * @since   0.2.0
  849.     * @author  Daniel Plücken <daniel@debakel.net>
  850.     * @param   boolean $withJSdeclaration 
  851.     * @access  public
  852.     * @return  string 
  853.     */
  854.    function getJS2getAllValuesFrom$withJSdeclaration true )
  855.    {
  856.        static $JSOutputted 0;
  857.  
  858.        if$JSOutputted )
  859.        return "";
  860.        else
  861.        {
  862.            $JSOutputted++;
  863.  
  864.            $out = <<<JSCRIPT
  865.  
  866.     window.getAllValuesFrom = function( which )
  867.     {
  868.         var arr = new Array();
  869.         try { cnt = which.options.length; } catch (e) { cnt = 0; }
  870.         for ( var i = 0; i < cnt; i++ )
  871.             arr[ arr.length ] = which.options[i].value;
  872.  
  873.         return arr;
  874.     }
  875.  
  876. JSCRIPT;
  877.  
  878.            if$withJSdeclaration === true )
  879.            {
  880.                $js new Javascript();
  881.                $js->add$out );
  882.  
  883.                $out $js->get();
  884.            }
  885.  
  886.            return $out;
  887.        }
  888.    }
  889.  
  890.  
  891.  
  892.    /**
  893.     * Returns an javascript-function to get the value of a given select.
  894.     *
  895.     * @static
  896.     * @version 1.11
  897.     * @since   0.1.8
  898.     * @author  Daniel Plücken <daniel@debakel.net>
  899.     * @param   boolean $withJSdeclaration 
  900.     * @access  public
  901.     * @return  string 
  902.     */
  903.    function getJS2selectValue$withJSdeclaration true )
  904.    {
  905.        static $JSOutputted 0;
  906.  
  907.        if$JSOutputted )
  908.        return "";
  909.        else
  910.        {
  911.            $JSOutputted++;
  912.  
  913.            $out "  window.selectValueOf = function( which, val )\r\n"
  914.               ."  {\r\n"
  915.               ."    try { cnt = which.length; } catch (e) { cnt = 0; }\r\n"
  916.               ."    for( i = 0; i < cnt; ++i )\r\n"
  917.               ."      if( which.options[i].value == val )\r\n"
  918.               ."        which.options[i].selected = true;\r\n\r\n"
  919.               ."  }\r\n";
  920.  
  921.            if$withJSdeclaration === true )
  922.            {
  923.                $js new Javascript();
  924.                $js->add$out );
  925.  
  926.                $out $js->get();
  927.            }
  928.  
  929.            return $out;
  930.        }
  931.    }
  932.  
  933.  
  934.  
  935.    /**
  936.     * Returns an javascript-function to move the selected items of a select to
  937.     * another.
  938.     *
  939.     * @static
  940.     * @version 1.02
  941.     * @since   0.1.9
  942.     * @author  Daniel Plücken <daniel@debakel.net>
  943.     * @param   boolean $withJSdeclaration 
  944.     * @access  public
  945.     * @return  string 
  946.     */
  947.    function getJS2moveItem$withJSdeclaration true )
  948.    {
  949.       static $JSOutputted 0;
  950.  
  951.       if$JSOutputted )
  952.         return "";
  953.       else
  954.       {
  955.         $JSOutputted++;
  956.  
  957.         $out = <<<JSCRIPT
  958.  
  959.     window.moveItemTo = function( fromWhich, toWhich )
  960.     {
  961.         // selectedIndicesArr
  962.         sIA = new Array();
  963.         // clonedItemsArr
  964.         cIA = new Array();
  965.  
  966.         var k = 0;
  967.         // Acquire the selected indices of the "from"-item.
  968.         for ( var i = 0; i < fromWhich.length; i++ )
  969.             if ( fromWhich.options[i].selected == true )
  970.             {
  971.                 cIA[k] = fromWhich.options[i].cloneNode(true);
  972.                 if (
  973.                      toWhich.length == 0
  974.                   || toWhich.options[toWhich.length-1].text
  975.                    < cIA[k].text
  976.                    )
  977.                     // Append if the "to"-item is empty or the last
  978.                     // entry of it is in lower order than the "from"-item
  979.                     toWhich.appendChild( cIA[k] );
  980.                 else
  981.                     // Insert into correct order.
  982.                     for ( j = 0; j < toWhich.length; j++ )
  983.                         if ( toWhich.options[j].text > cIA[k].text )
  984.                         {
  985.                             toWhich.insertBefore( cIA[k], toWhich.options[j] );
  986.                             break;
  987.                         }
  988.  
  989.  
  990.                     sIA[k++] = fromWhich.options[i];
  991.             }
  992.  
  993.             // Remove nodes
  994.             for ( i = 0; i < sIA.length; i++ )
  995.                 fromWhich.removeChild( sIA[i] );
  996.     }
  997.  
  998. JSCRIPT;
  999.  
  1000.         if$withJSdeclaration === true )
  1001.         {
  1002.           $js new Javascript();
  1003.           $js->add$out );
  1004.  
  1005.           $out $js->get();
  1006.         }
  1007.  
  1008.         return $out;
  1009.       }
  1010.    }
  1011.  
  1012.  
  1013.  
  1014.    /**
  1015.     * Returns a generated string based on the attributes of this object.
  1016.     *
  1017.     * @version 2.39
  1018.     * @since   0.1.0
  1019.     * @author  Daniel Plücken <daniel@debakel.net>
  1020.     * @access  public
  1021.     * @return  string 
  1022.     */
  1023.    function get()
  1024.    {
  1025.      # Alternative text on zero value/label.
  1026.      if (
  1027.           count$this->label_arr <= 0
  1028.        && !$this->show_despite_zero_label_count
  1029.         )
  1030.      {   $out $this->text_on_hidden}
  1031.      else
  1032.      # Template text on single value/label.
  1033.      if (
  1034.           count$this->label_arr == 1
  1035.        && !empty$this->str_single_value_template )
  1036.         )
  1037.      {
  1038.          $out preg_replace(
  1039.                               "!\[#label#\]!i"$this->getSelectedLabel(),
  1040.                               $this->str_single_value_template
  1041.                             );
  1042.          $tmp_h_inp =new HTMLHiddenInput$this->name$this->value );
  1043.          $out .= $tmp_h_inp->get();
  1044.      }
  1045.      # The selectbox output.
  1046.      else
  1047.      {
  1048.          $out  $this->getLabel();
  1049.          $out .= "<!--\r\n-->"
  1050.                 ."<select\r\n"
  1051.                 ."           name=\"".$this->name
  1052.                               .(
  1053.                                  !$this->manipulateable
  1054.                               || (
  1055.                                    // this is neccessary for the class
  1056.                                    // MultiRelationSelect
  1057.                                    !$this->do_not_disable
  1058.                                 // && !$this->multiple /* PERHAPS -> to proof */
  1059.                                 && count$this->label_arr == 1
  1060.                                 && !$this->show_standard_label_on_single_value
  1061.                                  )
  1062.                                  ? "_disabled_dummy"
  1063.                                  : ""
  1064.                                )."\"\r\n"
  1065.                 ."           id=\"".$this->id."\"\r\n";
  1066.  
  1067.          if !empty$this->on_change_order ) )
  1068.             $out .= "           onChange=\"".$this->on_change_order."\"\r\n";
  1069.          if !empty$this->mouseover ) )
  1070.             $out .= "           onMouseover=\"".$this->mouseover."\"\r\n";
  1071.          if !empty$this->mouseout ) )
  1072.             $out .= "           onMouseout=\"".$this->mouseout."\"\r\n";
  1073.          if !empty$this->mouseup ) )
  1074.             $out .= "           onMouseup=\"".$this->mouseup."\"\r\n";
  1075.          if !empty$this->style_class ) )
  1076.             $out .= "           class=\"".$this->style_class."\"\r\n";
  1077.          if !empty$this->freestyle ) )
  1078.             $out .= "           style=\"".$this->freestyle."\"\r\n";
  1079.  
  1080.          if (
  1081.               preg_match"!^".UNSIGNEDINTEGER."$!"$this->size )
  1082.            && $this->size > 0
  1083.             )
  1084.             $out .= "           size=\"".$this->size."\"\r\n";
  1085.  
  1086.          if $this->multiple )
  1087.             $out .= "           multiple=\"multiple\"\r\n";
  1088.  
  1089.          if (
  1090.               !$this->manipulateable
  1091.            || (
  1092.                   // this is neccessary for the class MultiRelationSelect
  1093.                   !$this->do_not_disable
  1094.                && (
  1095.                     !$this->enabled
  1096.                  || count$this->label_arr == 1
  1097.                  // && !$this->multiple /* PERHAPS -> to proof */
  1098.                  && !$this->show_standard_label_on_single_value
  1099.                   )
  1100.               )
  1101.             )
  1102.             $out .= "           disabled=\"disabled\"\r\n";
  1103.  
  1104.          $out .= "   >";
  1105.  
  1106.  
  1107.          if (
  1108.               count$this->label_arr == 1
  1109.            && !$this->show_standard_label_on_single_value
  1110.             )
  1111.          {
  1112.             $out .= "<!--\r\n    -->"
  1113.                    ."<option value=\"".$this->value_arr[0]."\">"
  1114.                    .htmlentities(
  1115.                        html_entity_decode(
  1116.                             $this->label_arr[0]// <- There is the variable ;)
  1117.                             ENT_COMPAT,
  1118.                             $this->charset
  1119.                                          ),
  1120.                        ENT_COMPAT,
  1121.                        $this->charset
  1122.                                 )
  1123.                    ."</option>";
  1124.  
  1125.             $_GET[$this->name$this->value_arr[0];
  1126.             $_POST[$this->name$this->value_arr[0];
  1127.             $_REQUEST[$this->name$this->value_arr[0];
  1128.          }
  1129.          else
  1130.          {
  1131.            if $this->show_standard_label )
  1132.               $out .= "<!--\r\n    --><option value=\"\">"
  1133.                          .$this->standard_label
  1134.                      ."</option>";
  1135.  
  1136.            if is_array$this->label_arr ) )
  1137.            {
  1138.               foreach $this->label_arr as $label )
  1139.                   if !is_array$label ) )
  1140.                   {
  1141.                     $value current$this->value_arr );
  1142.                     $style_class current$this->style_class_arr );
  1143.                     $freestyle current$this->freestyle_arr );
  1144.  
  1145.                     $out .= "<!--\r\n    --><option value=\"".$value."\"";
  1146.  
  1147.                     $out .= !empty$style_class )
  1148.                             ? " class=\"".$style_class."\""
  1149.                             : "";
  1150.  
  1151.                     $out .= !empty$freestyle )
  1152.                             ? " style=\"".$freestyle."\""
  1153.                             : "";
  1154.  
  1155.                     $out .= $value == $this->value
  1156.                             ? " selected=\"selected\""
  1157.                             : "";
  1158.  
  1159.                     $out .= ">"
  1160.                            .htmlentities(
  1161.                                html_entity_decode(
  1162.                                     $label// <- There is the variable ;)
  1163.                                     ENT_COMPAT,
  1164.                                     $this->charset
  1165.                                                  ),
  1166.                                ENT_COMPAT,
  1167.                                $this->charset
  1168.                                         )
  1169.                            ."</option>";
  1170.  
  1171.                     next$this->value_arr );
  1172.                     next$this->style_class_arr );
  1173.                     next$this->freestyle_arr );
  1174.                   }
  1175.                   else // TODO: OPTGROUP!
  1176.                   {
  1177.  
  1178.                   }
  1179.  
  1180.               reset$this->value_arr );
  1181.               reset$this->style_class_arr );
  1182.               reset$this->style_class_arr );
  1183.            }
  1184.          }
  1185.          $out .= "<!--\r\n--></select>";
  1186.  
  1187.          if !$this->manipulateable )
  1188.          {
  1189.             $tmp_h_inp =new HTMLHiddenInput$this->name$this->value );
  1190.             $out .= $tmp_h_inp->get();
  1191.          }
  1192.          else
  1193.          if (
  1194.               // this is neccessary for the class MultiRelationSelect
  1195.               !$this->do_not_disable
  1196.            // && !$this->multiple /* PERHAPS -> to proof */
  1197.            && count$this->label_arr == 1
  1198.            && !$this->show_standard_label_on_single_value
  1199.             )
  1200.          {
  1201.             $tmp_h_inp =new HTMLHiddenInput(
  1202.                                        $this->name,
  1203.                                        $this->value_arr[0]
  1204.                                              );
  1205.             $out .= $tmp_h_inp->get();
  1206.          }
  1207.  
  1208.      }
  1209.  
  1210.      return $out;
  1211.    }
  1212. // END of class HTMLSelect
  1213. ?>

Documentation generated on Thu, 05 Jun 2008 19:13:01 +0200 by phpDocumentor 1.4.1