Fixed bug #10861: Eval of double2 doesn't work
authorSteffen Kamper <info@sk-typo3.de>
Wed, 20 May 2009 15:45:50 +0000 (15:45 +0000)
committerSteffen Kamper <info@sk-typo3.de>
Wed, 20 May 2009 15:45:50 +0000 (15:45 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@5460 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_tcemain.php
t3lib/jsfunc.evalfield.js
tests/t3lib/t3lib_tcemain_testcase.php

index c2004e8..78c0b38 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,7 @@
 
 2009-05-20  Steffen Kamper  <info@sk-typo3.de>
 
+       * Fixed bug #10861: Eval of double2 doesn't work
        * Fixed bug #10994: allow using custom adapter for extJs
 
 2009-05-19  Sebastian Kurf├╝rst  <sebastian@typo3.org>
index 0d63f23..8c5b2a8 100644 (file)
@@ -1990,8 +1990,8 @@ class t3lib_TCEmain       {
         *
         * @param       string          Value to evaluate
         * @param       array           Array of evaluations to traverse.
-        * @param       string          Is-in string
-        * @return      string          Modified $value
+        * @param       string          Is-in string for 'is_in' evaluation
+        * @return      array           Modified $value in key 'value' or empty array
         */
        function checkValue_input_Eval($value,$evalArray,$is_in)        {
                $res = Array();
@@ -2014,16 +2014,19 @@ class t3lib_TCEmain     {
                                        }
                                break;
                                case 'double2':
-                                       $theDec = 0;
-                                       for ($a=strlen($value); $a>0; $a--)     {
-                                               if (substr($value,$a-1,1)=='.' || substr($value,$a-1,1)==',')   {
-                                                       $theDec = substr($value,$a);
-                                                       $value = substr($value,0,$a-1);
-                                                       break;
-                                               }
+                                       $value = preg_replace('/[^0-9,\.-]/', '', $value);
+                                       $negative = substr($value, 0, 1) == '-';
+                                       $value = strtr($value, array(',' => '.', '-' => ''));
+                                       if (strpos($value, '.') === false) {
+                                               $value .= '.0';
+                                       }
+                                       $valueArray = explode('.', $value);
+                                       $dec = array_pop($valueArray);
+                                       $value = join('', $valueArray) . '.' . $dec;
+                                       if ($negative) {
+                                               $value *= -1;
                                        }
-                                       $theDec = preg_replace('/[^0-9]/','',$theDec).'00';
-                                       $value = intval(str_replace(' ','',$value)).'.'.substr($theDec,0,2);
+                                       $value = number_format($value, 2, '.', '');
                                break;
                                case 'md5':
                                        if (strlen($value)!=32){$set=false;}
index 38fe9c7..e446cf3 100644 (file)
@@ -159,18 +159,21 @@ function evalFunc_getNumChars(value)      {
        return outVal;
 }
 function evalFunc_parseDouble(value)   {
-       var theVal = ''+value;
-       var dec=0;
-       if (!value)     return 0;
-       for (var a=theVal.length; a>0; a--)     {
-               if (theVal.substr(a-1,1)=='.' || theVal.substr(a-1,1)==',')     {
-                       dec = theVal.substr(a);
-                       theVal = theVal.substr(0,a-1);
-                       break;
-               }
+       var theVal = "" + value;
+       theVal = theVal.replace(/[^0-9,\.-]/g, "");
+       var negative = theVal.substring(0, 1) === '-';
+       theVal = theVal.replace(/-/g, "");
+       theVal = theVal.replace(/,/g, ".");
+       if (theVal.indexOf(".") == -1) {
+               theVal += ".0";
+       }
+       var parts = theVal.split(".");
+       var dec = parts.pop();
+       theVal = Number(parts.join("") + "." + dec);
+       if (negative) {
+           theVal *= -1;
        }
-       dec = this.getNumChars(dec)+'00';
-       theVal=this.parseInt(this.noSpace(theVal))+TS.decimalSign+dec.substr(0,2);
+       theVal = theVal.toFixed(2);
 
        return theVal;
 }
index 5562387..ee13002 100644 (file)
@@ -175,5 +175,27 @@ class t3lib_tcemain_testcase extends tx_phpunit_testcase {
                        $this->fixture->checkModifyAccessList('be_users')
                );
        }
+       
+       /**
+        * @test
+        */
+       public function evalCheckValueDouble2() {
+               $testData = array (
+                                               '-0,5' => '-0.50',
+                                               '1000' => '1000.00',
+                                               '1000,10' => '1000.10',
+                                               '1000,0' => '1000.00',
+                                               '600.000.000,00' => '600000000.00',
+                                               '60aaa00' => '6000.00',
+                                               );
+               foreach ($testData as $value => $expectedReturnValue){
+                       $returnValue = $this->fixture->checkValue_input_Eval($value, array('double2'), '');
+                       $this->assertSame(
+                       $returnValue['value'],
+                       $expectedReturnValue 
+                       );      
+               }
+       }       
+
 }
 ?>