Index: branches/5.2.x/core/kernel/utility/formatters/formatter.php =================================================================== diff -u -N -r14244 -r14596 --- branches/5.2.x/core/kernel/utility/formatters/formatter.php (.../formatter.php) (revision 14244) +++ branches/5.2.x/core/kernel/utility/formatters/formatter.php (.../formatter.php) (revision 14596) @@ -1,6 +1,6 @@ '.$field_type.' (in TypeCast method), please use float instead', E_USER_NOTICE); + trigger_error('Invalid field type ' . $field_type . ' (in TypeCast method), please use float instead', E_USER_NOTICE); $field_type = 'float'; } - $type_ok = preg_match('#int|integer|double|float|real|numeric|string#', $field_type); - if ($field_type == 'string') { - if (!$this->Application->isAdmin && isset($options['allow_html']) && $options['allow_html']) { + elseif ( $field_type == 'string' ) { + if ( !$this->Application->isAdmin && isset($options['allow_html']) && $options['allow_html'] ) { // this allows to revert htmlspecialchars call for each field submitted on front-end $value = kUtil::unhtmlentities($value); } + return $value; } - static $comma = null; - static $thousands = null; - if (is_null($comma) || is_null($thousands)) { - $lang =& $this->Application->recallObject('lang.current'); - $comma = $lang->GetDBField('DecimalPoint'); - $thousands = $lang->GetDBField('ThousandSep'); - } + $value = $this->formatNumber($value); + $type_ok = preg_match('#int|integer|double|float|real|numeric|string#', $field_type); - $value = str_replace($thousands, '', $value); - $value = str_replace($comma, '.', $value); - - if ($value != '' && $type_ok) - { + if ( $value != '' && $type_ok ) { $ret = is_numeric($value); - if($ret) - { - $f = 'is_'.$field_type; + + if ($ret) { + $f = 'is_' . $field_type; settype($value, $field_type); $ret = $f($value); } @@ -104,21 +96,62 @@ return $ret ? $value : false; } - function TypeCastArray($src, &$object) + /** + * Formats number, according to regional settings + * + * @param string $number + * @return float + */ + function formatNumber($number) { - $dst = array(); + static $comma = null, $thousands = null; + + if ( !isset($comma) || !isset($thousands) ) { + $lang =& $this->Application->recallObject('lang.current'); + /* @var $lang LanguagesItem */ + + $comma = $lang->GetDBField('DecimalPoint'); + $thousands = $lang->GetDBField('ThousandSep'); + } + + $number = str_replace($thousands, '', $number); + $number = str_replace($comma, '.', $number); + + return $number; + } + + /** + * Applies type casting on each array element + * @param Array $src + * @param kDBItem|kDBList|kDBBase $object + * @return Array + * @access public + */ + public function TypeCastArray($src, &$object) + { + $dst = array (); + foreach ($src as $id => $row) { - $tmp_row = array(); + $tmp_row = array (); foreach ($row as $fld => $value) { $field_options = $object->GetFieldOptions($fld); $tmp_row[$fld] = $this->TypeCast($value, $field_options); } $dst[$id] = $tmp_row; } + return $dst; } -//function Format($value, $options, &$errors) + /** + * Formats value of a given field + * + * @param string $value + * @param string $field_name + * @param kDBItem|kDBList|kDBBase $object + * @param string $format + * @return string + */ function Format($value, $field_name, &$object, $format = null) { if ( is_null($value) ) { @@ -148,6 +181,8 @@ if (preg_match('#int|integer|double|float|real|numeric#', $options['type'])) { $lang =& $this->Application->recallObject('lang.current'); + /* @var $lang LanguagesItem */ + return $lang->formatNumber($value); } elseif ($options['type'] == 'string') { @@ -162,17 +197,19 @@ * * @param mixed $value * @param string $field_name - * @param kDBItem $object + * @param kDBItem|kDBList|kDBBase $object * @return mixed + * @access public */ - function Parse($value, $field_name, &$object) + public function Parse($value, $field_name, &$object) { if ($value == '') { return NULL; } $options = $object->GetFieldOptions($field_name); - $tc_value = $this->TypeCast($value,$options); + $tc_value = $this->TypeCast($value, $options); + if ($tc_value === false) { return $value; // for leaving badly formatted date on the form } @@ -216,12 +253,14 @@ * Used for split fields like timestamp -> date, time * Called from DBItem to update sub fields values after loading item * - * @param unknown_type $field - * @param unknown_type $value - * @param unknown_type $options - * @param unknown_type $object + * @param string $field + * @param string $value + * @param Array $options + * @param kDBItem|kDBList|kDBBase $object + * @return void + * @access public */ - function UpdateSubFields($field, $value, &$options, &$object) + public function UpdateSubFields($field, $value, &$options, &$object) { } @@ -233,26 +272,25 @@ * @param string $field * @param mixed $value * @param Array $options - * @param kDBItem $object + * @param kDBItem|kDBList|kDBBase $object */ function UpdateMasterFields($field, $value, &$options, &$object) { } -/* function GetErrorMsg($pseudo_error, $options) + /** + * Return sample value, that can be entered in this field + * + * @param string $field + * @param Array $options + * @param kDBItem|kDBList|kDBBase $object + * @return string + * @access public + */ + public function GetSample($field, &$options, &$object) { - if ( isset($options['error_msgs'][$pseudo_error]) ) { - return $options['error_msgs'][$pseudo_error]; - } - else { - return $this->ErrorMsgs[$pseudo_error]; - } - }*/ - - function GetSample($field, &$options, &$object) - { - if (isset($options['sample_value'])) return $options['sample_value']; + return isset($options['sample_value']) ? $options['sample_value'] : ''; } } \ No newline at end of file