2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-11 12:51:39 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-05-11 12:51:39 +02:00
|
|
|
* Text input field with validation for numeric values. Supports validating
|
2014-04-02 22:33:18 +02:00
|
|
|
* the numeric value as to the {@link i18n::get_locale()} value, or an
|
|
|
|
* overridden locale specific to this field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-05-11 12:51:39 +02:00
|
|
|
class NumericField extends TextField {
|
2016-03-21 11:12:23 +01:00
|
|
|
|
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_DECIMAL;
|
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* Override locale for this field.
|
|
|
|
*
|
2014-04-02 22:33:18 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $locale = null;
|
|
|
|
|
2015-04-27 04:34:52 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*
|
|
|
|
* @throws Zend_Locale_Exception
|
|
|
|
*/
|
2014-04-02 22:33:18 +02:00
|
|
|
public function setValue($value, $data = array()) {
|
|
|
|
require_once "Zend/Locale/Format.php";
|
|
|
|
|
|
|
|
// If passing in a non-string number, or a value
|
2015-04-27 04:34:52 +02:00
|
|
|
// directly from a DataObject then localise this number
|
|
|
|
|
|
|
|
if(is_int($value) || is_float($value) || $data instanceof DataObject) {
|
2014-04-02 22:33:18 +02:00
|
|
|
$locale = new Zend_Locale($this->getLocale());
|
2015-04-27 04:34:52 +02:00
|
|
|
|
|
|
|
$this->value = Zend_Locale_Format::toNumber(
|
|
|
|
$value,
|
|
|
|
array('locale' => $locale)
|
|
|
|
);
|
2014-04-02 22:33:18 +02:00
|
|
|
} else {
|
|
|
|
$this->value = $this->clean($value);
|
|
|
|
}
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* In some cases and locales, validation expects non-breaking spaces.
|
|
|
|
*
|
|
|
|
* Returns the value, with all spaces replaced with non-breaking spaces.
|
2014-04-02 22:33:18 +02:00
|
|
|
*
|
|
|
|
* @param string $input
|
2015-04-27 04:34:52 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2014-04-02 22:33:18 +02:00
|
|
|
*/
|
|
|
|
protected function clean($input) {
|
2015-04-27 04:34:52 +02:00
|
|
|
$replacement = html_entity_decode(' ', null, 'UTF-8');
|
|
|
|
|
|
|
|
return str_replace(' ', $replacement, trim($input));
|
2014-04-02 22:33:18 +02:00
|
|
|
}
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* Determine if the current value is a valid number in the current locale.
|
|
|
|
*
|
2014-04-02 22:33:18 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isNumeric() {
|
|
|
|
require_once "Zend/Locale/Format.php";
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
$locale = new Zend_Locale($this->getLocale());
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
return Zend_Locale_Format::isNumber(
|
|
|
|
$this->clean($this->value),
|
|
|
|
array('locale' => $locale)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-27 04:34:52 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return 'numeric text';
|
|
|
|
}
|
2012-04-11 05:55:07 +02:00
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
/**
|
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2014-11-12 03:19:12 +01:00
|
|
|
if(!$this->value) {
|
2013-05-11 12:51:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-27 04:34:52 +02:00
|
|
|
if($this->isNumeric()) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-11 12:51:39 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
_t(
|
2015-04-27 04:34:52 +02:00
|
|
|
'NumericField.VALIDATION',
|
|
|
|
"'{value}' is not a number, only numbers can be accepted for this field",
|
2014-04-02 22:33:18 +02:00
|
|
|
array('value' => $this->value)
|
|
|
|
),
|
|
|
|
"validation"
|
2013-05-11 12:51:39 +02:00
|
|
|
);
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* Extracts the number value from the localised string value.
|
|
|
|
*
|
|
|
|
* @return string
|
2014-04-02 22:33:18 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataValue() {
|
2014-04-02 22:33:18 +02:00
|
|
|
require_once "Zend/Locale/Format.php";
|
2015-04-27 04:34:52 +02:00
|
|
|
|
|
|
|
if(!$this->isNumeric()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
$locale = new Zend_Locale($this->getLocale());
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
$number = Zend_Locale_Format::getNumber(
|
|
|
|
$this->clean($this->value),
|
|
|
|
array('locale' => $locale)
|
|
|
|
);
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
return $number;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-11-03 03:55:23 +01:00
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* Creates a read-only version of the field.
|
|
|
|
*
|
|
|
|
* @return NumericField_Readonly
|
2014-11-03 10:13:54 +01:00
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation() {
|
2015-04-27 04:34:52 +02:00
|
|
|
$field = new NumericField_Readonly(
|
|
|
|
$this->name,
|
|
|
|
$this->title,
|
|
|
|
$this->value
|
|
|
|
);
|
|
|
|
|
2014-11-03 10:13:54 +01:00
|
|
|
$field->setForm($this->form);
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-11-03 10:13:54 +01:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* Gets the current locale this field is set to.
|
|
|
|
*
|
2014-04-02 22:33:18 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLocale() {
|
2015-04-27 04:34:52 +02:00
|
|
|
if($this->locale) {
|
|
|
|
return $this->locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i18n::get_locale();
|
2014-04-02 22:33:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 04:34:52 +02:00
|
|
|
* Override the locale for this field.
|
2014-04-02 22:33:18 +02:00
|
|
|
*
|
|
|
|
* @param string $locale
|
2015-04-27 04:34:52 +02:00
|
|
|
*
|
2014-04-02 22:33:18 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setLocale($locale) {
|
|
|
|
$this->locale = $locale;
|
2015-04-27 04:34:52 +02:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2015-04-27 04:34:52 +02:00
|
|
|
/**
|
|
|
|
* Readonly version of a numeric field.
|
|
|
|
*
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
|
|
|
*/
|
2014-11-03 10:13:54 +01:00
|
|
|
class NumericField_Readonly extends ReadonlyField {
|
2015-04-27 04:34:52 +02:00
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
2014-11-03 10:13:54 +01:00
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
return clone $this;
|
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2015-04-27 04:34:52 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-11-03 10:13:54 +01:00
|
|
|
public function Value() {
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->value ?: '0';
|
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
public function getValueCast() {
|
|
|
|
return 'Decimal';
|
2015-04-27 04:34:52 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|