2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-06-26 15:03:11 +02:00
|
|
|
* Renders a text field, validating its input as a currency.
|
|
|
|
* Limited to US-centric formats, including a hardcoded currency
|
|
|
|
* symbol and decimal separators.
|
|
|
|
* See {@link MoneyField} for a more flexible implementation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
2009-04-02 19:17:04 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CurrencyField extends TextField {
|
|
|
|
/**
|
2010-01-18 01:56:29 +01:00
|
|
|
* allows the value to be set. removes the first character
|
|
|
|
* if it is not a number (probably a currency symbol)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($val) {
|
2010-04-12 23:59:55 +02:00
|
|
|
if(!$val) $val = 0.00;
|
2015-11-10 02:38:51 +01:00
|
|
|
$this->value = Config::inst()->get('Currency','currency_symbol') . number_format((double)preg_replace('/[^0-9.\-]/', '', $val), 2);
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Overwrite the datavalue before saving to the db ;-)
|
2010-01-18 01:56:29 +01:00
|
|
|
* return 0.00 if no value, or value is non-numeric
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataValue() {
|
2010-10-19 02:32:18 +02:00
|
|
|
if($this->value) {
|
|
|
|
return preg_replace('/[^0-9.\-]/','', $this->value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
return 0.00;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return 'currency text';
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new class for this field
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2012-12-13 13:51:28 +01:00
|
|
|
return $this->castedCopy('CurrencyField_Readonly');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2015-11-10 02:38:51 +01:00
|
|
|
$currencySymbol = preg_quote(Config::inst()->get('Currency','currency_symbol'));
|
|
|
|
$regex = '/^\s*(\-?'.$currencySymbol.'?|'.$currencySymbol.'\-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/';
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!empty ($this->value)
|
2015-11-10 02:38:51 +01:00
|
|
|
&& !preg_match($regex, $this->value)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
|
|
|
$validator->validationError($this->name, _t('Form.VALIDCURRENCY', "Please enter a valid currency"),
|
|
|
|
"validation", false);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-10 01:34:18 +01:00
|
|
|
* Readonly version of a {@link CurrencyField}.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CurrencyField_Readonly extends ReadonlyField{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* overloaded to display the correctly formated value for this datatype
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->value){
|
2011-09-14 10:06:01 +02:00
|
|
|
$val = $this->dontEscape ? $this->value : Convert::raw2xml($this->value);
|
2008-01-10 04:28:13 +01:00
|
|
|
$val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
|
2012-04-12 02:11:22 +02:00
|
|
|
} else {
|
|
|
|
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
$valforInput = $this->value ? Convert::raw2att($val) : "";
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<span class=\"readonly ".$this->extraClass()."\" id=\"" . $this->id() . "\">$val</span>"
|
|
|
|
. "<input type=\"hidden\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* This already is a readonly field.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
return clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-10-16 02:49:34 +02:00
|
|
|
/**
|
|
|
|
* Readonly version of a {@link CurrencyField}.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
|
|
|
class CurrencyField_Disabled extends CurrencyField{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
protected $disabled = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 02:49:34 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* overloaded to display the correctly formated value for this datatype
|
2008-10-16 02:49:34 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2008-10-16 02:49:34 +02:00
|
|
|
if($this->value){
|
2011-09-14 10:06:01 +02:00
|
|
|
$val = $this->dontEscape ? $this->value : Convert::raw2xml($this->value);
|
2008-10-16 02:49:34 +02:00
|
|
|
$val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
|
2012-04-12 02:11:22 +02:00
|
|
|
} else {
|
|
|
|
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
|
2008-10-16 02:49:34 +02:00
|
|
|
}
|
|
|
|
$valforInput = $this->value ? Convert::raw2att($val) : "";
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<input class=\"text\" type=\"text\" disabled=\"disabled\""
|
|
|
|
. " name=\"".$this->name."\" value=\"".$valforInput."\" />";
|
2008-10-16 02:49:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|