mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
5c9044a007
API Introduce HTMLFragment as casting helper for HTMLText with shortcodes disabled API Introduce DBField::CDATA for XML file value encoding API RSSFeed now casts from the underlying model rather than by override API Introduce CustomMethods::getExtraMethodConfig() to allow metadata to be queried BUG Remove _call hack from VirtualPage API Remove FormField::$dontEscape API Introduce HTMLReadonlyField for non-editable readonly HTML API FormField::Field() now returns string in many cases rather than DBField instance. API Remove redundant *_val methods from ViewableData API ViewableData::obj() no longer has a $forceReturnObject parameter as it always returns an object BUG Fix issue with ViewableData caching incorrect field values after being modified. API Remove deprecated DB class methods API Enforce plain text left/right formfield titles
116 lines
3.3 KiB
PHP
116 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* 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.
|
|
*
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
*
|
|
* @package forms
|
|
* @subpackage fields-formattedinput
|
|
*/
|
|
class CurrencyField extends TextField {
|
|
/**
|
|
* allows the value to be set. removes the first character
|
|
* if it is not a number (probably a currency symbol)
|
|
*/
|
|
public function setValue($val) {
|
|
if(!$val) $val = 0.00;
|
|
$this->value = Config::inst()->get('SilverStripe\\ORM\\FieldType\\DBCurrency','currency_symbol') . number_format((double)preg_replace('/[^0-9.\-]/', '', $val), 2);
|
|
return $this;
|
|
}
|
|
/**
|
|
* Overwrite the datavalue before saving to the db ;-)
|
|
* return 0.00 if no value, or value is non-numeric
|
|
*/
|
|
public function dataValue() {
|
|
if($this->value) {
|
|
return preg_replace('/[^0-9.\-]/','', $this->value);
|
|
}else{
|
|
return 0.00;
|
|
}
|
|
}
|
|
|
|
public function Type() {
|
|
return 'currency text';
|
|
}
|
|
|
|
/**
|
|
* Create a new class for this field
|
|
*/
|
|
public function performReadonlyTransformation() {
|
|
return $this->castedCopy('CurrencyField_Readonly');
|
|
}
|
|
|
|
public function validate($validator) {
|
|
$currencySymbol = preg_quote(Config::inst()->get('SilverStripe\\ORM\\FieldType\\DBCurrency', 'currency_symbol'));
|
|
$regex = '/^\s*(\-?'.$currencySymbol.'?|'.$currencySymbol.'\-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/';
|
|
if(!empty ($this->value)
|
|
&& !preg_match($regex, $this->value)) {
|
|
|
|
$validator->validationError($this->name, _t('Form.VALIDCURRENCY', "Please enter a valid currency"),
|
|
"validation", false);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Readonly version of a {@link CurrencyField}.
|
|
* @package forms
|
|
* @subpackage fields-formattedinput
|
|
*/
|
|
class CurrencyField_Readonly extends ReadonlyField{
|
|
|
|
/**
|
|
* overloaded to display the correctly formated value for this datatype
|
|
*/
|
|
public function Field($properties = array()) {
|
|
if($this->value){
|
|
$val = Convert::raw2xml($this->value);
|
|
$val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
|
|
$valforInput = Convert::raw2att($val);
|
|
} else {
|
|
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
|
|
$valforInput = '';
|
|
}
|
|
return "<span class=\"readonly ".$this->extraClass()."\" id=\"" . $this->ID() . "\">$val</span>"
|
|
. "<input type=\"hidden\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
|
|
}
|
|
|
|
/**
|
|
* This already is a readonly field.
|
|
*/
|
|
public function performReadonlyTransformation() {
|
|
return clone $this;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Readonly version of a {@link CurrencyField}.
|
|
* @package forms
|
|
* @subpackage fields-formattedinput
|
|
*/
|
|
class CurrencyField_Disabled extends CurrencyField{
|
|
|
|
protected $disabled = true;
|
|
|
|
/**
|
|
* overloaded to display the correctly formated value for this datatype
|
|
*/
|
|
public function Field($properties = array()) {
|
|
if($this->value){
|
|
$val = Convert::raw2xml($this->value);
|
|
$val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
|
|
$valforInput = Convert::raw2att($val);
|
|
} else {
|
|
$valforInput = '';
|
|
}
|
|
return "<input class=\"text\" type=\"text\" disabled=\"disabled\""
|
|
. " name=\"".$this->name."\" value=\"".$valforInput."\" />";
|
|
}
|
|
}
|