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

Source for file HTMLRadioGroup.class.php

Documentation is available at HTMLRadioGroup.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>\n";
  14.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  15.   exit();
  16. }
  17.  
  18. /**
  19.  *
  20.  */
  21. require_onceCLASSPATH."forms/items/ABSTHTMLFormItem.class.php" );
  22.  
  23. /**
  24.  * @package    forms
  25.  * @subpackage items
  26.  * @abstract
  27.  * @version    0.2.23
  28.  * @author     Daniel Plücken <daniel@debakel.net>
  29.  * @license    http://www.gnu.org/copyleft/lesser.html
  30.  *              GNU Lesser General Public License
  31.  * @copyright  Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
  32.  *
  33.  *  This library is free software; you can redistribute it and/or
  34.  *  modify it under the terms of the GNU Lesser General Public
  35.  *  License as published by the Free Software Foundation; either
  36.  *  version 2.1 of the License.
  37.  *
  38.  *  This library is distributed in the hope that it will be useful,
  39.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  40.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41.  *  GNU Lesser General Public License for more details.
  42.  *
  43.  *  You should have received a copy of the GNU Lesser General
  44.  *  Public License along with this library; if not, write to the
  45.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  46.  *  Boston, MA 02111-1307 USA
  47.  */
  48. {
  49.    /**
  50.     * @var array 
  51.     * @public
  52.     */
  53.    var $radioButtonsLabels = array();
  54.    /**
  55.     * @var string 
  56.     * @public
  57.     */
  58.    var $radioButtonsLabelsInlineStyle = "display: inline";
  59.    /**
  60.     * @var array 
  61.     * @public
  62.     */
  63.    var $radioButtonsValues = array();
  64.    /**
  65.     * @var array 
  66.     * @public
  67.     */
  68.    /**
  69.     * Stores whether the radio buttons should arrange horizontal; Arrangement
  70.     * with or without breaks.
  71.     *
  72.     * @var array 
  73.     * @public
  74.     */
  75.    var $horizontal = false;
  76.  
  77.  
  78.    /**
  79.     * Constructor
  80.     *
  81.     * @version 1.2
  82.     * @since   0.1.0
  83.     * @author  Daniel Plücken <daniel@debakel.net>
  84.     * @access  public
  85.     * @param   string         $name          Name of the radio button group.
  86.     * @param   array          $labelArr      Array with the label of each radio
  87.     *                                         button.
  88.     * @param   array          $valueArr      Array with the value of each radio
  89.     *                                         button.
  90.     * @param   string         $selectedValue Value to be select.
  91.     * @param   string         $horizontal    Whether the radio buttons should
  92.     *                                         arrange horizontal.
  93.     * @return  HTMLRadioGroup 
  94.     */
  95.    function HTMLRadioGroup(
  96.                             $name          "",
  97.                             $labelArr      array(),
  98.                             $valueArr      array(),
  99.                             $selectedValue "",
  100.                             $horizontal    false
  101.                           )
  102.    {
  103.         if(
  104.            is_array$labelArr )
  105.         || is_array$valueArr )
  106.          )
  107.         {
  108.           $this->radioButtonsLabels = $labelArr;
  109.           $this->radioButtonsValues = $valueArr;
  110.         }
  111.  
  112.         $this->name       = $name;
  113.         $this->id         = $name;
  114.         $this->value      = $selectedValue;
  115.         $this->horizontal = $horizontal;
  116.     }
  117.  
  118.  
  119.  
  120.    /**
  121.     * Adds a new radiobutton to the group.
  122.     *
  123.     * @version 1.1
  124.     * @since   0.1.1
  125.     * @author  Daniel Plücken <daniel@debakel.net>
  126.     * @access  public
  127.     * @param   array $label 
  128.     * @param   array $value 
  129.     * @or
  130.     * @param   string $label 
  131.     * @param   string $value 
  132.     * @return  void 
  133.     */
  134.    function addButton$label ""$value "" )
  135.    {
  136.        if(
  137.            is_array$label )
  138.         || is_array$value )
  139.          )
  140.        {
  141.          for$i 0$i maxcount$label )count$value ) )$i++ )
  142.          {
  143.            $this->radioButtonsLabels[$label[$i];
  144.            $this->radioButtonsValues[$value[$i];
  145.          }
  146.        }
  147.        else
  148.        {
  149.          $this->radioButtonsLabels[$label;
  150.          $this->radioButtonsValues[$value;
  151.        }
  152.    }
  153.  
  154.  
  155.  
  156.    /**
  157.     * Adding an array to this object, that carries the javascript for the
  158.     * onMouseup-tag of each radiobutton.
  159.     *
  160.     * @version 1.0
  161.     * @since   0.1.8
  162.     * @author  Daniel Plücken <daniel@debakel.net>
  163.     * @access  public
  164.     * @param   array  $array  Array with the javascripts of each radio button
  165.     *                          they should execute on left mouse button up.
  166.     * @return  void 
  167.     */
  168.    function setMouseUpArray$array )
  169.    {
  170.       if is_array$array ) )
  171.          $this->radioButtonsOnMouseUp = $array;
  172.    }
  173.  
  174.  
  175.  
  176.    /**
  177.     * Returns a string which contains a function of a javascript to check the
  178.     * radiobutton which value is the given one.
  179.     *
  180.     * @version 1.0
  181.     * @since   0.1.6
  182.     * @static
  183.     * @author  Daniel Plücken <daniel@debakel.net>
  184.     * @access  public
  185.     * @return  string 
  186.     */
  187.    function getJSFunctions()
  188.    {
  189.      $out  "  function setRadio( field, wert )\r\n";
  190.      $out .= "  {\r\n";
  191.      $out .= "   Ã¼for( i = 0; i < field.length; i++ )\r\n";
  192.      $out .= "       if( field[i].value == wert )\r\n";
  193.      $out .= "         field[i].checked = true;\r\n";
  194.      $out .= "  }\r\n";
  195.  
  196.      return $out;
  197.    }
  198.  
  199.  
  200.  
  201.    /**
  202.     * Returns an if-order of a javascript to check whether this item is NOT
  203.     * filled
  204.     *
  205.     * @version 1.2
  206.     * @since   0.1.4
  207.     * @author  Daniel Plücken <daniel@debakel.net>
  208.     * @access  public
  209.     * @param   string $formName 
  210.     * @return  string 
  211.     */
  212.    function getCheckOrder()
  213.    {
  214.       for$i 0$i count$this->radioButtonsValues )$i++ )
  215.         if$i == )
  216.           $out "document.".$this->parentform->name."."
  217.                 .$this->name."[0].checked != true";
  218.         else
  219.           $out .= "\r\n     && document.".$this->parentform->name."."
  220.                               .$this->name."[".$i."].checked != true";
  221.  
  222.       return $out;
  223.    }
  224.  
  225.  
  226.  
  227.  
  228.    /**
  229.     * Set the value of inline styles of the labels for the radiobuttons of the
  230.     * group.
  231.     *
  232.     * @version 1.0
  233.     * @since   0.2.22
  234.     * @author  Daniel Plücken <daniel@debakel.net>
  235.     * @access  public
  236.     * @param   string $string 
  237.     * @return  void 
  238.     */
  239.    function setLabelFreeStyle$string )
  240.    $this->radioButtonsLabelsInlineStyle = $string}
  241.  
  242.  
  243.  
  244.    /**
  245.     * Generates the sourcecode to build this object and returns it.
  246.     *
  247.     * @version 1.0
  248.     * @since   0.1.5
  249.     * @author  Daniel Plücken <daniel@debakel.net>
  250.     * @access  public
  251.     * @return  string 
  252.     */
  253.    function getPHPSource()
  254.    {
  255.       $out .= "unset( \x24LabelArr );\r\n";
  256.       $out .= "unset( \x24ValueArr );\r\n";
  257.       $cntLab count$this->radioButtonsLabels );
  258.       $cntVal count$this->radioButtonsValues );
  259.  
  260.       if!empty$cntLab ) )
  261.       {
  262.          $out .= "\x24LabelArr = array(\r\n";
  263.          for$i 0$i $cntLab $i++ )
  264.          {
  265.             $comma $cntLab != $i )
  266.                      ? ","
  267.                      : "";
  268.             $out .= "                    \""
  269.                    .$this->radioButtonsLabels[$i]."\"".$comma."\r\n";
  270.          }
  271.          $out .= "                 );\r\n";
  272.       }
  273.  
  274.       if!empty$cntVal ) )
  275.       {
  276.          $out .= "\x24ValueArr = array(\r\n";
  277.          for$i 0$i $cntVal $i++ )
  278.          {
  279.             $comma $cntVal != $i )
  280.                      ? ","
  281.                      : "";
  282.             $out .= "                    \""
  283.                    .$this->radioButtonsValues[$i]."\"".$comma."\r\n";
  284.          }
  285.          $out .= "                 );\r\n";
  286.       }
  287.  
  288.  
  289.       $out .= "unset( \x24value );\r\n";
  290.       $out .= "\x24value = \x24_POST[\"".$this->getName()."\"]\r\n"
  291.                    ."        .\x24_GET[\"".$this->getName()."\"];\r\n";
  292.       $out .= "\x24input[] = new HTMLRadioGroup(\r\n"
  293.              ."                                \"".$this->getName()."\",\r\n"
  294.              ."                                \x24LabelArr,\r\n"
  295.              ."                                \x24ValueArr,\r\n"
  296.              ."                                \x24value,\r\n"
  297.              ."                                ".empty$this->horizontal )
  298.                                                    ? "false"
  299.                                                    : "true"
  300.                                                  )."\r\n"
  301.              ."                             );\r\n";
  302.  
  303.       /*
  304.       $tmp = $this->getSelectedValue();
  305.       $out .= !empty( $tmp )
  306.                     ? "if( \x24value === \"\" )\r\n"
  307.                      ."  \x24input[count(\x24input)-1]->setSelectedValue( \"".$tmp."\" );\r\n"
  308.                     : "\r\n";
  309.       */
  310.  
  311.       return $out;
  312.    }
  313.  
  314.  
  315.  
  316.    /**
  317.     * Returns a generated string based on the attributes of this object.
  318.     *
  319.     * @version 1.72
  320.     * @since   0.1.0
  321.     * @author  Daniel Plücken <daniel@debakel.net>
  322.     * @access  public
  323.     * @param   string forminput POST or GET or REQUEST
  324.     * @return  string 
  325.     */
  326.    function get$forminput "" )
  327.    {
  328.      $this->idExists$this->idtrue );
  329.  
  330.      $out "";
  331.      for$i 0$i count$this->radioButtonsValues )$i++ )
  332.      {
  333.        $out .= "<input type=\"radio\"\r\n"
  334.               ."       name=\"".$this->name."\"\r\n"
  335.               ."       id=\"".$this->id."_".$i."\"";
  336.        $out .= !empty$this->style_class )
  337.                         ? "\r\n       class=\""
  338.                                    .$this->style_class."\""
  339.                         : "";
  340.        $out .= !empty$this->freestyle )
  341.                         ? "\r\n       style=\""
  342.                                    .$this->freestyle."\""
  343.                         : "";
  344.        $out .= !empty$this->radioButtonsValues[$i)
  345.                         ? "\r\n       value=\""
  346.                                    .$this->radioButtonsValues[$i]."\""
  347.                         : "";
  348.        $out .= !empty$this->radioButtonsOnMouseUp[$i)
  349.                         ? "\r\n       onMouseup=\""
  350.                                    .$this->radioButtonsOnMouseUp[$i]."\""
  351.                         : "";
  352.        $out .= (
  353.                  $this->enabled
  354.               && $this->manipulateable
  355.                )
  356.                ? ""
  357.                : " disabled=\"disabled\"";
  358.  
  359.        if(
  360.            $forminput == "POST"
  361.         || $forminput == "GET"
  362.         || $forminput == "REQUEST"
  363.          )
  364.          $out .= "<? if( \x24_".$forminput."['".$this->name."'] "
  365.                     ."== '".$this->radioButtonsValues[$i]."' ) "
  366.                      ."echo ' checked'; ?>";
  367.        else
  368.          $out .= (
  369.                             $this->value == $this->radioButtonsValues[$i]
  370.                          || (
  371.                               $this->value == "on"
  372.                            && $this->radioButtonsValues[$i== 0
  373.                             )
  374.                           )
  375.                           ? "\r\n       checked=\"checked\""
  376.                           : "";
  377.  
  378.        $out .= "\r\n/>".(
  379.                      !empty$this->radioButtonsLabels[$i)
  380.                      ? "<label for=\"".$this->id."_".$i."\""
  381.                       .(
  382.                          !empty$this->radioButtonsLabelsInlineStyle )
  383.                          ? " style=\"{$this->radioButtonsLabelsInlineStyle}\""
  384.                          : ""
  385.                        )
  386.                       .">"
  387.                       .$this->radioButtonsLabels[$i]."</label>"
  388.                      : ""
  389.                         );
  390.  
  391.        if(
  392.            $i != count$this->radioButtonsValues )
  393.         && !$this->
  394.          )
  395.          $out .= "<br>\r\n";
  396.      }
  397.  
  398.      return $out;
  399.    }
  400. } // END of class HTMLRadioGroup

Documentation generated on Thu, 05 Jun 2008 19:12:51 +0200 by phpDocumentor 1.4.1