Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
form-processor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mattwire
form-processor
Commits
64fe3117
Commit
64fe3117
authored
5 years ago
by
jaapjansma
Browse files
Options
Downloads
Patches
Plain Diff
Added formatted decimal type
parent
07f9c2ad
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+1
-0
1 addition, 0 deletions
CHANGELOG.md
Civi/FormProcessor/Type/Factory.php
+1
-0
1 addition, 0 deletions
Civi/FormProcessor/Type/Factory.php
Civi/FormProcessor/Type/FormattedDecimalType.php
+87
-0
87 additions, 0 deletions
Civi/FormProcessor/Type/FormattedDecimalType.php
with
89 additions
and
0 deletions
CHANGELOG.md
+
1
−
0
View file @
64fe3117
...
...
@@ -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
===========
...
...
This diff is collapsed.
Click to expand it.
Civi/FormProcessor/Type/Factory.php
+
1
−
0
View file @
64fe3117
...
...
@@ -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'
)));
...
...
This diff is collapsed.
Click to expand it.
Civi/FormProcessor/Type/FormattedDecimalType.php
0 → 100644
+
87
−
0
View file @
64fe3117
<?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
)))
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment