tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file form_field_money.class.php

Documentation is available at form_field_money.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class Form_Field_Date
  9.  *
  10.  * @package tangra_lib
  11.  * @subpackage form
  12.  */
  13.  
  14.  
  15. /**
  16.  *
  17.  */
  18. require_once(TANGRA_MAIN_DIR.'form/fields/text/form_field_text.class.php');
  19.  
  20.  
  21. /**
  22.  * Represents text field that accepts money value
  23.  *
  24.  * Please note that currency sign is not accepted
  25.  *
  26.  * @package tangra_lib
  27.  * @subpackage form
  28.  */
  29. class Form_Field_Money extends Form_Field_Text {
  30.     /**
  31.      * Separator for thousands
  32.      *
  33.      * @var string 
  34.      * @internal
  35.      */
  36.     private $thousands_separator;
  37.     /**
  38.      * Decimal separator
  39.      *
  40.      * @var string 
  41.      * @internal
  42.      */
  43.     private $floating_separator;
  44.  
  45.  
  46.     /**
  47.      * Enter description here...
  48.      *
  49.      * @param string $name Name of the field
  50.      * @param boolean $required Is field required. Default is false
  51.      * @param integer $maxlength  Maximum length of the text. Default is 100
  52.      * @param string $value Default value
  53.      * @param string $thousands_separator Thousands separator
  54.      * @param string $floating_separator Decimal separator
  55.      */
  56.     function __construct($name$required false$maxlength 100$value NULL$thousands_separator ' '$floating_separator '.'{
  57.         parent::__construct($name$required$maxlength$value);
  58.  
  59.         $this->set_separators($thousands_separator$floating_separator);
  60.         $this->add_potential_error('invalid_money_value');
  61.     }
  62.  
  63.  
  64.     /**
  65.      * Sets separators
  66.      *
  67.      * @param string $thousands_separator Thousands separator
  68.      * @param string $floating_separator Decimal separator
  69.      */
  70.     public function set_separators($thousands_separator$floating_separator{
  71.         if ($thousands_separator == $floating_separator{
  72.             throw new Tangra_Exception('Thousands separator and Floating separator can not be the same = '$thousands_separator);
  73.         }
  74.  
  75.         if ($thousands_separator != ',' && $thousands_separator != ' ' && $thousands_separator != '.' && $thousands_separator != ''{
  76.             throw new Tangra_Exception('Invalid thousands separator = "'.$thousands_separator.'". Must be "," (comma) or " "(space) or empty.');
  77.         }
  78.  
  79.         $this->thousands_separator = $thousands_separator;
  80.  
  81.         if ($floating_separator != '.' && $floating_separator != ","{
  82.             throw new Tangra_Exception('Invalid floating separator = "'.$floating_separator.'". Must be "."(dot) or ","(comma)');
  83.         }
  84.  
  85.         $this->floating_separator = $floating_separator;
  86.  
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Returns thousands separator
  92.      *
  93.      * @return string 
  94.      */
  95.     public function get_thousands_separator({
  96.         return $this->thousands_separator;
  97.     }
  98.  
  99.  
  100.     /**
  101.      * Returns decimal separator
  102.      *
  103.      * @return unknown 
  104.      */
  105.     public function get_floating_separator({
  106.         return $this->floating_separator;
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Alias of get_floating_separator
  112.      *
  113.      * @return unknown 
  114.      */
  115.     public function get_decimal_separator({
  116.         return $this->get_floating_separator();
  117.     }
  118.  
  119.  
  120.     /**
  121.      * Performs basic check for validity
  122.      *
  123.      * @return boolean 
  124.      * @internal
  125.      */
  126.     public function basic_check({
  127.         $has_errors parent::basic_check();
  128.  
  129.  
  130.         if (!$has_errors{
  131.             if ($this->get_html_value(!= NULL and $this->get_html_value(!= ''{
  132.                 if (!($this->convert_money_to_int($this->get_html_value()) !== false)) {
  133.                     $this->set_error('invalid_money_value');
  134.                     $has_errors true;
  135.                 else {
  136.                     if ($this->convert_money_to_int($this->get_html_value()) == && $this->get_required()) {
  137.                         $this->set_error('mandatory_missing');
  138.                         $has_errors true;
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         return $has_errors;
  145.     }
  146.  
  147.     /**
  148.      * Transfers data from html_value to value
  149.      * @internal
  150.      */
  151.     protected function translate_value_html2app({
  152.         $this->set_value($this->convert_money_to_int($this->get_html_value()));
  153.     }
  154.  
  155.  
  156.     /**
  157.      * Converts money value to integer
  158.      *
  159.      * @param string $value 
  160.      * @return integer 
  161.      * @internal
  162.      */
  163.     protected function convert_money_to_int($value{
  164.         //TODO - make it to accept value without leading 0, e.g. ".45"
  165.         $ret false;
  166.  
  167.         //removing thousends separator
  168.         $value strtr($valuearray($this->get_thousands_separator(=> ''));
  169.  
  170.         $floating_separator $this->get_floating_separator();
  171.  
  172.         $fsp strpos($value$floating_separator);
  173.  
  174.         if ($fsp{
  175.             // ensuring that floating separator is present only one time
  176.             if (!strpos($value$floating_separator$fsp 1)) {
  177.  
  178.                 $base substr($value0$fsp);
  179.                 $fraction substr($value$fsp 1);
  180.  
  181.                 if (strlen($fraction== 1{
  182.                     $ret $base.$fraction.'0';
  183.                 else {
  184.                     if (strlen($fraction== 2{
  185.                         $ret $base.$fraction;
  186.                     }
  187.                 }
  188.             }
  189.         else {
  190.             if (tangra_is_int($value)) {
  191.                 $ret $value.'00';
  192.             }
  193.         }
  194.  
  195.  
  196.         return $ret;
  197.     }
  198.  
  199. }