2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-11 12:51:39 +02:00
|
|
|
|
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 {
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
/**
|
|
|
|
* Override locale for this field
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $locale = null;
|
|
|
|
|
|
|
|
public function setValue($value, $data = array()) {
|
|
|
|
require_once "Zend/Locale/Format.php";
|
|
|
|
|
|
|
|
// If passing in a non-string number, or a value
|
|
|
|
// directly from a dataobject then localise this number
|
|
|
|
if ((is_numeric($value) && !is_string($value)) ||
|
|
|
|
($value && $data instanceof DataObject)
|
|
|
|
){
|
|
|
|
$locale = new Zend_Locale($this->getLocale());
|
|
|
|
$this->value = Zend_Locale_Format::toNumber($value, array('locale' => $locale));
|
|
|
|
} else {
|
|
|
|
// If an invalid number, store it anyway, but validate() will fail
|
|
|
|
$this->value = $this->clean($value);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In some cases and locales, validation expects non-breaking spaces
|
|
|
|
*
|
|
|
|
* @param string $input
|
|
|
|
* @return string The input value, with all spaces replaced with non-breaking spaces
|
|
|
|
*/
|
|
|
|
protected function clean($input) {
|
|
|
|
$nbsp = html_entity_decode(' ', null, 'UTF-8');
|
|
|
|
return str_replace(' ', $nbsp, trim($input));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the current value is a valid number in the current locale
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isNumeric() {
|
|
|
|
require_once "Zend/Locale/Format.php";
|
|
|
|
$locale = new Zend_Locale($this->getLocale());
|
|
|
|
return Zend_Locale_Format::isNumber(
|
|
|
|
$this->clean($this->value),
|
|
|
|
array('locale' => $locale)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-08-04 04:20:57 +02:00
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(parent::getAttributes(), array(
|
|
|
|
'type' => 'number',
|
|
|
|
'step' => 'any' // allows entering float/decimal numbers like "1.2" instead of just integers
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
/**
|
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate(Validator $validator) {
|
|
|
|
if(!$this->value) {
|
2013-05-11 12:51:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-02 22:33:18 +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(
|
|
|
|
'NumericField.VALIDATION', "'{value}' is not a number, only numbers can be accepted for this field",
|
|
|
|
array('value' => $this->value)
|
|
|
|
),
|
|
|
|
"validation"
|
2013-05-11 12:51:39 +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
|
|
|
/**
|
|
|
|
* Extracts the number value from the localised string value
|
|
|
|
*
|
|
|
|
* @return string number value
|
|
|
|
*/
|
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";
|
|
|
|
if(!$this->isNumeric()) return 0;
|
|
|
|
$locale = new Zend_Locale($this->getLocale());
|
|
|
|
$number = Zend_Locale_Format::getNumber(
|
|
|
|
$this->clean($this->value),
|
|
|
|
array('locale' => $locale)
|
|
|
|
);
|
|
|
|
return $number;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
|
|
|
/**
|
2014-11-03 10:13:54 +01:00
|
|
|
* Returns a readonly version of this field
|
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
$field = new NumericField_Readonly($this->name, $this->title, $this->value);
|
|
|
|
$field->setForm($this->form);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:33:18 +02:00
|
|
|
/**
|
|
|
|
* Gets the current locale this field is set to
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLocale() {
|
|
|
|
return $this->locale ?: i18n::get_locale();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the locale for this field
|
|
|
|
*
|
|
|
|
* @param string $locale
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setLocale($locale) {
|
|
|
|
$this->locale = $locale;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2014-11-03 10:13:54 +01:00
|
|
|
class NumericField_Readonly extends ReadonlyField {
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2014-11-03 10:13:54 +01:00
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
return clone $this;
|
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2014-11-03 10:13:54 +01:00
|
|
|
public function Value() {
|
|
|
|
return Convert::raw2xml($this->value ? "$this->value" : "0");
|
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|