2009-05-26 00:21:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2010-11-13 03:05:34 +01:00
|
|
|
* A form field that can save into a {@link Money} database field.
|
|
|
|
* See {@link CurrencyField} for a similiar implementation
|
|
|
|
* that can save into a single float database field without indicating the currency.
|
|
|
|
*
|
2009-05-26 00:21:02 +02:00
|
|
|
* @author Ingo Schommer, SilverStripe Ltd. (<firstname>@silverstripe.com)
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
|
|
|
class MoneyField extends FormField {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $_locale
|
|
|
|
*/
|
|
|
|
protected $_locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limit the currencies
|
|
|
|
* @var array $allowedCurrencies
|
|
|
|
*/
|
|
|
|
protected $allowedCurrencies;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormField
|
|
|
|
*/
|
|
|
|
protected $fieldAmount = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormField
|
|
|
|
*/
|
|
|
|
protected $fieldCurrency = null;
|
|
|
|
|
2012-01-02 17:09:30 +01:00
|
|
|
function __construct($name, $title = null, $value = "") {
|
2009-05-26 00:21:02 +02:00
|
|
|
// naming with underscores to prevent values from actually being saved somewhere
|
|
|
|
$this->fieldAmount = new NumericField("{$name}[Amount]", _t('MoneyField.FIELDLABELAMOUNT', 'Amount'));
|
2010-10-19 03:34:31 +02:00
|
|
|
$this->fieldCurrency = $this->FieldCurrency($name);
|
2010-04-14 06:39:51 +02:00
|
|
|
|
2012-01-02 17:09:30 +01:00
|
|
|
parent::__construct($name, $title, $value);
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function Field() {
|
|
|
|
return "<div class=\"fieldgroup\">" .
|
|
|
|
"<div class=\"fieldgroupField\">" . $this->fieldCurrency->SmallFieldHolder() . "</div>" .
|
|
|
|
"<div class=\"fieldgroupField\">" . $this->fieldAmount->SmallFieldHolder() . "</div>" .
|
|
|
|
"</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-19 03:34:31 +02:00
|
|
|
* @param string $name - Name of field
|
2009-05-26 00:21:02 +02:00
|
|
|
* @return FormField
|
|
|
|
*/
|
2010-10-19 03:34:31 +02:00
|
|
|
protected function FieldCurrency($name) {
|
2009-05-26 00:21:02 +02:00
|
|
|
$allowedCurrencies = $this->getAllowedCurrencies();
|
|
|
|
if($allowedCurrencies) {
|
|
|
|
$field = new DropdownField(
|
2010-10-19 03:34:31 +02:00
|
|
|
"{$name}[Currency]",
|
2009-05-26 00:21:02 +02:00
|
|
|
_t('MoneyField.FIELDLABELCURRENCY', 'Currency'),
|
2010-10-04 06:27:34 +02:00
|
|
|
ArrayLib::is_associative($allowedCurrencies) ? $allowedCurrencies : array_combine($allowedCurrencies,$allowedCurrencies)
|
2009-05-26 00:21:02 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$field = new TextField(
|
2010-10-19 03:34:31 +02:00
|
|
|
"{$name}[Currency]",
|
2009-05-26 00:21:02 +02:00
|
|
|
_t('MoneyField.FIELDLABELCURRENCY', 'Currency')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValue($val) {
|
|
|
|
$this->value = $val;
|
|
|
|
|
|
|
|
if(is_array($val)) {
|
2009-05-27 02:59:07 +02:00
|
|
|
$this->fieldCurrency->setValue($val['Currency']);
|
|
|
|
$this->fieldAmount->setValue($val['Amount']);
|
2009-05-26 00:21:02 +02:00
|
|
|
} elseif($val instanceof Money) {
|
2009-05-27 02:59:07 +02:00
|
|
|
$this->fieldCurrency->setValue($val->getCurrency());
|
|
|
|
$this->fieldAmount->setValue($val->getAmount());
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
2009-05-26 03:08:47 +02:00
|
|
|
|
|
|
|
// @todo Format numbers according to current locale, incl.
|
|
|
|
// decimal and thousands signs, while respecting the stored
|
|
|
|
// precision in the database without truncating it during display
|
|
|
|
// and subsequent save operations
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
|
|
|
|
2009-06-30 05:16:28 +02:00
|
|
|
/**
|
|
|
|
* 30/06/2009 - Enhancement:
|
|
|
|
* SaveInto checks if set-methods are available and use them
|
|
|
|
* instead of setting the values in the money class directly. saveInto
|
|
|
|
* initiates a new Money class object to pass through the values to the setter
|
|
|
|
* method.
|
|
|
|
*
|
|
|
|
* (see @link MoneyFieldTest_CustomSetter_Object for more information)
|
|
|
|
*/
|
2012-04-11 07:00:57 +02:00
|
|
|
function saveInto(DataObjectInterface $dataObject) {
|
2009-05-26 00:21:02 +02:00
|
|
|
$fieldName = $this->name;
|
2009-06-30 05:16:28 +02:00
|
|
|
if($dataObject->hasMethod("set$fieldName")) {
|
2012-03-27 06:57:42 +02:00
|
|
|
$dataObject->$fieldName = DBField::create_field('Money', array(
|
2009-06-30 05:16:28 +02:00
|
|
|
"Currency" => $this->fieldCurrency->Value(),
|
|
|
|
"Amount" => $this->fieldAmount->Value()
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$dataObject->$fieldName->setCurrency($this->fieldCurrency->Value());
|
|
|
|
$dataObject->$fieldName->setAmount($this->fieldAmount->Value());
|
|
|
|
}
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a readonly version of this field.
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Implement removal of readonly state with $bool=false
|
|
|
|
* @todo Set readonly state whenever field is recreated, e.g. in setAllowedCurrencies()
|
|
|
|
*/
|
|
|
|
function setReadonly($bool) {
|
|
|
|
parent::setReadonly($bool);
|
|
|
|
|
2012-01-02 16:19:22 +01:00
|
|
|
$this->fieldAmount->setReadonly($bool);
|
|
|
|
$this->fieldCurrency->setReadonly($bool);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2012-01-02 16:19:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setDisabled($bool) {
|
|
|
|
parent::setDisabled($bool);
|
|
|
|
|
|
|
|
$this->fieldAmount->setDisabled($bool);
|
|
|
|
$this->fieldCurrency->setDisabled($bool);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $arr
|
|
|
|
*/
|
|
|
|
function setAllowedCurrencies($arr) {
|
|
|
|
$this->allowedCurrencies = $arr;
|
|
|
|
|
|
|
|
// @todo Has to be done twice in case allowed currencies changed since construction
|
|
|
|
$oldVal = $this->fieldCurrency->Value();
|
2010-10-19 03:34:31 +02:00
|
|
|
$this->fieldCurrency = $this->FieldCurrency($this->name);
|
2009-05-26 00:21:02 +02:00
|
|
|
$this->fieldCurrency->setValue($oldVal);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getAllowedCurrencies() {
|
|
|
|
return $this->allowedCurrencies;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLocale($locale) {
|
|
|
|
$this->_locale = $locale;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLocale() {
|
|
|
|
return $this->_locale;
|
|
|
|
}
|
|
|
|
}
|