Skip to content
Snippets Groups Projects
Commit 64fe3117 authored by jaapjansma's avatar jaapjansma
Browse files

Added formatted decimal type

parent 07f9c2ad
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ Version 1.3
* Added getoptions to the FormProcessor and FormProcessorDefaults API.
* Added a type with a list of weekdays
* Added field type for time
* Added field type for formatted decimal numbers
Version 1.2
===========
......
......@@ -19,6 +19,7 @@
public function __construct() {
$this->addType(new GenericType('Integer', E::ts('Numeric (no-decimal)')));
$this->addType(new GenericType('Float', E::ts('Numeric (with-decimal)')));
$this->addType(new FormattedDecimalType(E::ts('Formatted decimal number')));
$this->addType(new GenericType('String', E::ts('Short text')));
$this->addType(new GenericType('Text', E::ts('Long text')));
$this->addType(new DateType(E::ts('Date')));
......
<?php
/**
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
namespace Civi\FormProcessor\Type;
use \Civi\FormProcessor\Config\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
use CRM_FormProcessor_ExtensionUtil as E;
class FormattedDecimalType extends AbstractType {
public function __construct($label) {
$this->name = 'FormattedDecimal';
$this->label = $label;
}
public function validateValue($value, $allValues=array()) {
$value = $this->normalizeValue($value);
if (\CRM_Utils_Type::validate($value, 'Float', false) === NULL) {
return false;
}
return true;
}
/**
* Returns the type number from CRM_Utils_Type
*/
public function getCrmType() {
return \CRM_Utils_Type::T_FLOAT;
}
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('decimal_point', 'String', E::ts('Decimal point'), true, '.', null, null),
new Specification('thousand_sep', 'String', E::ts('Thousand separator'), false, ',', null, null),
new Specification('decimals', 'Integer', E::ts('Decimals'), true, 2, null, null)
));
}
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
$decimal_point = $this->configuration->doesConfigExists('decimal_point') ? $this->configuration->get('decimal_point') : null;
$thousand_sep = $this->configuration->doesConfigExists('thousand_sep') ? $this->configuration->get('thousand_sep') : '';
$decimals = $this->configuration->doesConfigExists('decimals') ? $this->configuration->get('decimals') : 0;
return number_format($value, $decimals, $decimal_point, $thousand_sep);
}
/**
* Normalize the input value
*
* @param $value
* @return float|mixed
*/
public function normalizeValue($value) {
// Taken from: https://www.php.net/manual/en/function.floatval.php#114486
$dotPos = strrpos($value, '.');
$commaPos = strrpos($value, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
if (!$sep) {
return floatval(preg_replace("/[^0-9]/", "", $value));
}
return floatval(
preg_replace("/[^0-9]/", "", substr($value, 0, $sep)) . '.' .
preg_replace("/[^0-9]/", "", substr($value, $sep+1, strlen($value)))
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment