Fix: json is a fuck... sh... 3 hours to find a bug ! (json fails

without any warning when converting non utf8 strings).
This commit is contained in:
Laurent Destailleur 2012-10-18 16:30:12 +02:00
parent 0152e226c1
commit 657d0197b6
3 changed files with 50 additions and 12 deletions

View File

@ -1480,7 +1480,7 @@ class Form
$outval.=$objRef.' ('.$objRefFourn.') - '; $outval.=$objRef.' ('.$objRefFourn.') - ';
$opt.=dol_trunc($objp->label,18).' - '; $opt.=dol_trunc($objp->label,18).' - ';
$outval.=dol_trunc($label,18).' - '; $outval.=dol_trunc($label,18).' - ';
if (! empty($objp->idprodfournprice)) if (! empty($objp->idprodfournprice))
{ {
$currencytext=$langs->trans("Currency".$conf->currency); $currencytext=$langs->trans("Currency".$conf->currency);
@ -1488,24 +1488,28 @@ class Form
if (dol_strlen($currencytext) > 10) $currencytext=$conf->currency; // If text is too long, we use the short code if (dol_strlen($currencytext) > 10) $currencytext=$conf->currency; // If text is too long, we use the short code
if (dol_strlen($currencytextnoent) > 10) $currencytextnoent=$conf->currency; // If text is too long, we use the short code if (dol_strlen($currencytextnoent) > 10) $currencytextnoent=$conf->currency; // If text is too long, we use the short code
$opt.= price($objp->fprice).' '.$currencytext."/".$objp->quantity;
$outval.= price($objp->fprice).' '.$currencytextnoent."/".$objp->quantity;
$outqty=$objp->quantity; $outqty=$objp->quantity;
$outdiscount=$objp->remise_percent; $outdiscount=$objp->remise_percent;
if ($objp->quantity == 1) if ($objp->quantity == 1)
{ {
$opt.= strtolower($langs->trans("Unit")); $opt.= price($objp->fprice).' '.$currencytext."/";
$outval.=strtolower($langs->transnoentities("Unit")); $outval.= price($objp->fprice).' '.$currencytextnoent."/";
$opt.= $langs->trans("Unit"); // Do not use strtolower because it breaks utf8 encoding
$outval.=$langs->transnoentities("Unit");
} }
else else
{ {
$opt.= strtolower($langs->trans("Units")); $opt.= price($objp->fprice).' '.$currencytext."/".$objp->quantity;
$outval.=strtolower($langs->transnoentities("Units")); $outval.= price($objp->fprice).' '.$currencytextnoent."/".$objp->quantity;
$opt.= $langs->trans("Units"); // Do not use strtolower because it breaks utf8 encoding
$outval.=$langs->transnoentities("Units");
} }
if ($objp->quantity >= 1) if ($objp->quantity >= 1)
{ {
$opt.=" (".price($objp->unitprice).' '.$currencytext."/".strtolower($langs->trans("Unit")).")"; $opt.=" (".price($objp->unitprice).' '.$currencytext."/".$langs->trans("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
$outval.=" (".price($objp->unitprice).' '.$currencytextnoent."/".strtolower($langs->transnoentities("Unit")).")"; $outval.=" (".price($objp->unitprice).' '.$currencytextnoent."/".$langs->transnoentities("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
} }
if ($objp->remise_percent >= 1) if ($objp->remise_percent >= 1)
{ {
@ -1529,13 +1533,22 @@ class Form
$outval.=$langs->transnoentities("NoPriceDefinedForThisSupplier"); $outval.=$langs->transnoentities("NoPriceDefinedForThisSupplier");
} }
$opt .= "</option>\n"; $opt .= "</option>\n";
// Add new entry // Add new entry
// "key" value of json key array is used by jQuery automatically as selected value // "key" value of json key array is used by jQuery automatically as selected value
// "label" value of json key array is used by jQuery automatically as text for combo box // "label" value of json key array is used by jQuery automatically as text for combo box
$outselect.=$opt; $outselect.=$opt;
array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false))); array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
// Exemple of var_dump $outjson
// array(1) {[0]=>array(6) {[key"]=>string(1) "2" ["value"]=>string(3) "ppp"
// ["label"]=>string(76) "ppp (<strong>f</strong>ff2) - ppp - 20,00 Euros/1unité (20,00 Euros/unité)"
// ["qty"]=>string(1) "1" ["discount"]=>string(1) "0" ["disabled"]=>bool(false)
//}
//var_dump($outval); var_dump(utf8_check($outval)); var_dump(json_encode($outval));
//$outval=array('label'=>'ppp (<strong>f</strong>ff2) - ppp - 20,00 Euros/ Unité (20,00 Euros/unité)');
//var_dump($outval); var_dump(utf8_check($outval)); var_dump(json_encode($outval));
$i++; $i++;
} }
$outselect.='</select>'; $outselect.='</select>';

View File

@ -446,6 +446,30 @@ function dol_escape_htmltag($stringtoescape,$keepb=0)
return dol_htmlentities($tmp,ENT_COMPAT,'UTF-8'); return dol_htmlentities($tmp,ENT_COMPAT,'UTF-8');
} }
/**
* Convert a string to lower. Never use strtolower because it does not works with UTF8 strings.
*
* @param string $utf8_string String to encode
* @return string String converted
*/
function dol_strtolower($utf8_string)
{
return mb_strtolower($utf8_string, "UTF-8");
}
/**
* Convert a string to upper. Never use strtolower because it does not works with UTF8 strings.
*
* @param string $utf8_string String to encode
* @return string String converted
*/
function dol_strtoupper($utf8_string)
{
return mb_strtoupper($utf8_string, "UTF-8");
}
/** /**
* Write log message into outputs. Possible outputs can be: * Write log message into outputs. Possible outputs can be:
* A file if SYSLOG_FILE_ON defined: file name is then defined by SYSLOG_FILE * A file if SYSLOG_FILE_ON defined: file name is then defined by SYSLOG_FILE

View File

@ -33,7 +33,8 @@ if (! function_exists('json_encode'))
*/ */
function json_encode($elements) function json_encode($elements)
{ {
return dol_json_encode($elements); return 'ttt';
//return dol_json_encode($elements);
} }
} }