2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Currency field.
|
2009-04-02 19:17:04 +02:00
|
|
|
*
|
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
function setValue($val) {
|
2010-04-12 23:59:55 +02:00
|
|
|
if(!$val) $val = 0.00;
|
2010-10-19 02:32:18 +02:00
|
|
|
$this->value = '$' . 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
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
function Type() {
|
|
|
|
return 'currency text';
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new class for this field
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
2008-10-16 02:49:34 +02:00
|
|
|
$field = new CurrencyField_Readonly($this->name, $this->title, $this->value);
|
|
|
|
$field -> addExtraClass($this->extraClass());
|
|
|
|
return $field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-04-06 06:03:33 +02:00
|
|
|
function validate($validator) {
|
2010-10-19 02:32:18 +02:00
|
|
|
if(!empty ($this->value) && !preg_match('/^\s*(\-?\$?|\$\-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/', $this->value)) {
|
2007-10-25 04:47:45 +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{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* overloaded to display the correctly formated value for this datatype
|
|
|
|
*/
|
2012-04-11 07:33:36 +02:00
|
|
|
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) : "";
|
2008-10-16 02:49:34 +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
|
|
|
}
|
2011-12-21 17:35:42 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* This already is a readonly field.
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
return clone $this;
|
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{
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
protected $disabled = true;
|
|
|
|
|
2008-10-16 02:49:34 +02:00
|
|
|
/**
|
|
|
|
* overloaded to display the correctly formated value for this datatype
|
|
|
|
*/
|
2012-04-11 07:33:36 +02:00
|
|
|
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) : "";
|
|
|
|
return "<input class=\"text\" type=\"text\" disabled=\"disabled\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|