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

Source for file NumberTextInput.class.php

Documentation is available at NumberTextInput.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.  
  12. if!defined"CLASSPATH" ) )
  13. {
  14.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  15.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  16.   exit();
  17. }
  18.  
  19. /**
  20.  *
  21.  */
  22. require_once CLASSPATH."forms/items/HTMLTextInput.class.php";
  23.  
  24. /**
  25.  *
  26.  */
  27. require_once CLASSPATH."html/JavaScript.class.php";
  28.  
  29. /**
  30.  * A class to generate text input fields.
  31.  *
  32.  * @package    forms
  33.  * @subpackage items
  34.  * @version    0.1.82
  35.  * @static
  36.  * @author     Daniel Plücken <daniel@debakel.net>
  37.  * @license    http://www.gnu.org/copyleft/lesser.html
  38.  *              GNU Lesser General Public License
  39.  * @copyright  Copyright (C) 2003 Daniel Pluecken <daniel@debakel.net>
  40.  *
  41.  *  This library is free software; you can redistribute it and/or
  42.  *  modify it under the terms of the GNU Lesser General Public
  43.  *  License as published by the Free Software Foundation; either
  44.  *  version 2.1 of the License.
  45.  *
  46.  *  This library is distributed in the hope that it will be useful,
  47.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  48.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  49.  *  GNU Lesser General Public License for more details.
  50.  *
  51.  *  You should have received a copy of the GNU Lesser General
  52.  *  Public License along with this library; if not, write to the
  53.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  54.  *  Boston, MA 02111-1307 USA
  55.  */
  56. {
  57.    /**
  58.     * @var char $decimalchar 
  59.     * @access public
  60.     */
  61.    var $decimalchar = ",";
  62.    /**
  63.     * @var char $thousandseperatorchar 
  64.     * @access public
  65.     */
  66.    var $thousandseperatorchar = ".";
  67.    /**
  68.     * @var char $thousandseperatorchar 
  69.     * @access public
  70.     */
  71.    var $count_of_decimals = 2;
  72.  
  73.    /**
  74.     * Constructor
  75.     *
  76.     * @version 1.1
  77.     * @since   0.1.0
  78.     * @author  Daniel Plücken <daniel@debakel.net>
  79.     * @access  public
  80.     * @param   string  $name 
  81.     * @param   string  $value 
  82.     * @param   integer $size 
  83.     * @param   integer $maxchar 
  84.     * @param   integer $count_of_decimals 
  85.     * @param   string  $decimalchar 
  86.     * @param   string  $thousandseperatorchar 
  87.     */
  88.    function NumberTextInput(
  89.                              $name,
  90.                              $value "",
  91.                              $size "",
  92.                              $maxchars "",
  93.                              $count_of_decimals 2,
  94.                              $decimalchar ",",
  95.                              $thousandseperatorchar "."
  96.                            )
  97.    {
  98.      $this->count_of_decimals = $count_of_decimals;
  99.      $this->decimalchar = $decimalchar;
  100.      $this->thousandseperatorchar = $thousandseperatorchar;
  101.  
  102.      parent::HTMLTextInput$name$value$size$maxchars );
  103.    }
  104.  
  105.  
  106.  
  107.    /**
  108.     * Stores how many decimal numbers should be shown.
  109.     *
  110.     * @version 1.0
  111.     * @since   0.1.4
  112.     * @author  Daniel Plücken <daniel@debakel.net>
  113.     * @access  public
  114.     * @param   integer $int 
  115.     * @return  void 
  116.     */
  117.    function setDecimalCount$int )
  118.    $this->count_of_decimals = $int}
  119.  
  120.  
  121.  
  122.    /**
  123.     * Returns a javascript to probe if the value, that was typed in the
  124.     * textfield, is a number.
  125.     *
  126.     *
  127.     * @version 1.71
  128.     * @since   0.1.0
  129.     * @author  Daniel Plücken <daniel@debakel.net>
  130.     * @access  public
  131.     * @return  void 
  132.     */
  133.    function getJSFunctionsToCheckNumber()
  134.    {
  135.        global $JSTOCHECKNUMBER;
  136.  
  137.        ifempty$JSTOCHECKNUMBER ) )
  138.        {
  139.           $JSTOCHECKNUMBER true;
  140.  
  141.           $out  "\r\n\r\n<script language=\"javascript\">\r\n";
  142.           $out .= "<!--\r\n\r\n";
  143.  
  144.           $out .= JavaScript::getStrRepeat();
  145.  
  146.           $out .= "  window.checkNumberText = function( which, decimals )\r\n";
  147.           $out .= "  {\r\n";
  148.           $out .= "     string = which.value.replace( /(?:[^-0-9".$this
  149.                                    ->decimalchar."]|".$this->decimalchar
  150.                                                    ."0*$|^0*)/g, \"\" );\r\n";
  151.           $out .= "     string = string.replace( /-+/, \"-\" );\r\n\r\n";
  152.           $out .= "     string = string.replace( /^(-?)"
  153.                                                ."(".$this ->decimalchar.")"
  154.                                                ."(.+)$/, "
  155.                                              ."\"$10$2$3\" );\r\n\r\n";
  156.           $tmp "/(^-)?[^0-9]*(\d*)"
  157.                 ."[^0-9".$this ->decimalchar."]*"
  158.                 ."(".$this->decimalchar."?)"
  159.                 ."[^0-9]*(\d*)[^0-9]*$/";
  160.           $out .= "     while( !/^-?\d*,?\d*$/.test( string ) )\r\n";
  161.           $out .= "       string = string.replace( ".$tmp.", "
  162.                                                   ."\"$1$2$3$4\" );\r\n\r\n";
  163.  
  164.           $out .= "     string = string.replace( /^".$this
  165.                                    ->decimalchar."/, \"0$1\" );\r\n\r\n";
  166.           $out .= "     pattern = /".$this->decimalchar."/;\r\n";
  167.           $out .= "     if( pattern.test( string ) )\r\n";
  168.           $out .= "       reppattern = /(-?)(\d|\d\d|\d\d\d)((\d\d\d)+("
  169.                                     .$this->decimalchar."\d*)$)/;\r\n";
  170.           $out .= "     else\r\n";
  171.           $out .= "       reppattern = /(-?)(\d|\d\d|\d\d\d)((\d\d\d)+)$/;\r\n";
  172.           $out .= "     while( reppattern.test( string ) )\r\n";
  173.           $out .= "       string = string.replace( reppattern, \"$1$2"
  174.                              .$this->thousandseperatorchar."$3\" );\r\n\r\n";
  175.           $out .= "     if( string == \"\" || string == \"-\" )\r\n";
  176.           $out .= "       string = \"0\";\r\n\r\n";
  177.           $out .= "     string = string.replace( /,+$/, \"\" );\r\n\r\n";
  178.           $out .= "     which.value = string;\r\n\r\n";
  179.           $out .= "     if( decimals == 0 )\r\n";
  180.           $out .= "       while( "
  181.                  ."(pos = which.value.indexOf('".$this->decimalchar."')) != -1 "
  182.                              .")\r\n";
  183.           $out .= "       {\r\n";
  184.           $out .= "         which.value = which.value.substr( 0, pos );\r\n";
  185.           $out .= "       }\r\n";
  186.           $out .= "     else\r\n";
  187.           $out .= "     if ( "
  188.                       ."which.value.length "
  189.                     ."- (pos = which.value.indexOf('".$this->decimalchar."')) "
  190.                     ."> decimals "
  191.                     ."&& pos != -1"
  192.                          .")\r\n";
  193.           $out .= "        which.value = which.value.substr( "
  194.                                  ."0, pos + decimals + 1 "
  195.                                                          .");\r\n\r\n";
  196.  
  197.           $out .= "     if ( which.value.indexOf('".$this->decimalchar."') == -1 "
  198.                         ."&& decimals > 0 )\r\n";
  199.           $out .= "        which.value += '".$this->decimalchar."' "
  200.                                       ."+ str_repeat( '0', decimals );\r\n";
  201.  
  202.           $out .= "     if ( which.value.indexOf('".$this->decimalchar."') != -1 )\r\n";
  203.           $out .= "     while ( "
  204.                         ."which.value.length "
  205.                       ."- which.value.indexOf('".$this->decimalchar."') "
  206.                       ."<= decimals "
  207.                             .")\r\n";
  208.           $out .= "        which.value += '0';\r\n";
  209.           $out .= "     return true;\r\n\r\n";
  210.           $out .= "  }\r\n";
  211.           $out .= "// -->\r\n";
  212.           $out .= "</script>\r\n\r\n\r\n";
  213.        }
  214.        return $out;
  215.    }
  216.  
  217.  
  218.  
  219.    /**
  220.     * Returns an if-order of a javascript to check whether the value of this
  221.     * item has the correct format.
  222.     *
  223.     * @version 1.3
  224.     * @since   0.1.2
  225.     * @author  Daniel Plücken <daniel@debakel.net>
  226.     * @access  public
  227.     * @param   string $formName 
  228.     * @return  string 
  229.     */
  230.    function getValidationOrder$formName "" )
  231.    {
  232.       $out "checkNumberText( document."
  233.                   .(
  234.                      empty$formName )
  235.                      ? $this->parentform->getName()
  236.                      : $formName
  237.                    )."."
  238.                   .$this->getName()
  239.                                  .", ".$this->count_of_decimals." )";
  240.  
  241.       return $out;
  242.    }
  243.  
  244.  
  245.    /**
  246.     * Returns an if-order of a javascript to check whether this item is NOT
  247.     * filled
  248.     *
  249.     * @version 1.0
  250.     * @since   0.1.3
  251.     * @author  Daniel Plücken <daniel@debakel.net>
  252.     * @access  public
  253.     * @return  string 
  254.     */
  255.    function getCheckOrder()
  256.    {
  257.       $out "document.".$this->parentform->getName()."."
  258.                         .$this->name.".value == \"0\"";
  259.  
  260.       return $out;
  261.    }
  262.  
  263.  
  264.  
  265.    /**
  266.     * Generates the sourcecode to build this object and returns it.
  267.     *
  268.     * @version 1.1
  269.     * @since   0.1.0
  270.     * @author  Daniel Plücken <daniel@debakel.net>
  271.     * @access  public
  272.     * @return  string 
  273.     */
  274.    function getPHPSource()
  275.    {
  276.       $out  "unset( \x24value );\r\n";
  277.       $out .= "\x24value = \x24_POST[\"".$this->getName()."\"]\r\n"
  278.              ."        .\x24_GET[\"".$this->getName()."\"];\r\n";
  279.       $out .= "\x24input[] = new NumberTextInput(\r\n"
  280.            ."                                 \"".$this->getName()."\",\r\n"
  281.            ."                                 \x24value,\r\n"
  282.            ."                                 \"".$this->getSize()."\",\r\n"
  283.            ."                                 \"".$this->getMaxChars()."\"\r\n"
  284.            ."                              );\r\n";
  285.  
  286.       $tmp $this->getValue();
  287.       $out .= !empty$tmp )
  288.               ? "if( \x24value === \"\" )\r\n"
  289.                ."  \x24input[count(\x24input)-1]"
  290.                   ."->setValue( \"".$tmp."\" );\r\n\r\n"
  291.               : "\r\n";
  292.  
  293.       return $out;
  294.    }
  295.  
  296.  
  297.  
  298.    /**
  299.     * Returns a generated string based on the attributes of this object.
  300.     *
  301.     * @version 1.41
  302.     * @since   0.1.0
  303.     * @author  Daniel Plücken <daniel@debakel.net>
  304.     * @access  public
  305.     * @return  string 
  306.     */
  307.    function get()
  308.    {
  309.       $dbl_tmp doubleval$this->value );
  310.       $str_tmp $this->value;
  311.  
  312.       # Converting the value to type of double, if it isn't already a double
  313.       # value
  314.       if (
  315.            preg_replace"![^0-9]+\.[^0-9]+0+$!"""$dbl_tmp )
  316.         != preg_replace(
  317.                          "![^0-9]+".preg_quote$this->thousandseperatorchar )
  318.                         ."[^0-9]+0+$!"""$str_tmp
  319.                        )
  320.          )
  321.       {
  322.          $this->value = str_replace(
  323.                                      $this->thousandseperatorchar,
  324.                                      ""$this->value
  325.                                    );
  326.          $this->value = str_replace$this->decimalchar"."$this->value );
  327.       }
  328.  
  329.       $this->value = number_format(
  330.                               $this->value,
  331.                               $this->count_of_decimals,
  332.                               $this->decimalchar,
  333.                               $this->thousandseperatorchar
  334.                                   );
  335.  
  336.       parent::setOnBlur(
  337.                 "checkNumberText( this, ".$this->count_of_decimals." );"
  338.                .$this->blur
  339.                        );
  340.  
  341.       $out  $this->getJSFunctionsToCheckNumber();
  342.       $out .= parent::get();
  343.  
  344.       return  $out;
  345.    }
  346. // END of class NumberTextInput
  347. ?>

Documentation generated on Thu, 05 Jun 2008 19:14:37 +0200 by phpDocumentor 1.4.1