2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Currency field.
|
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 {
|
|
|
|
/**
|
|
|
|
* allows the value to be set ( not including $ signs and number format...)
|
|
|
|
*/
|
|
|
|
function setValue($val) {
|
|
|
|
$this->value = '$' . number_format(ereg_replace('[^0-9.]',"",$val), 2);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Overwrite the datavalue before saving to the db ;-)
|
|
|
|
*/
|
|
|
|
function dataValue() {
|
|
|
|
if($this->value){
|
2007-11-19 02:12:35 +01:00
|
|
|
return preg_replace('/[^0-9.]/',"", $this->value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
return 0.00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create a new class for this field
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
|
|
|
|
return new CurrencyField_Readonly($this->name, $this->title, $this->value);
|
|
|
|
|
|
|
|
/*
|
|
|
|
$this is-a object and cant be passed as_a string of the first parameter of formfield constructor.
|
|
|
|
return new CurrencyField_Readonly($this);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see http://regexlib.com/REDetails.aspx?regexp_id=126
|
|
|
|
*/
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
|
|
|
|
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateCurrency: function(fieldName) {
|
|
|
|
var el = _CURRENT_FORM.elements[fieldName];
|
|
|
|
if(!el || !el.value) return true;
|
|
|
|
|
|
|
|
var value = \$F(el);
|
|
|
|
if(value.length > 0 && !value.match(/^\\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\$/)) {
|
|
|
|
validationError(el,"Please enter a valid currency.","validation",false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
|
|
|
|
Requirements::customScript($jsFunc, 'func_validateCurrency');
|
|
|
|
|
|
|
|
return "\$('$formID').validateCurrency('$this->name');";
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate() {
|
|
|
|
if(!empty ($this->value) && !preg_match('/^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $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
|
|
|
|
*/
|
|
|
|
function Field() {
|
|
|
|
if($this->value){
|
|
|
|
$val = $this->dontEscape ? ($this->reserveNL?Convert::raw2xml($this->value):$this->value) : Convert::raw2xml($this->value);
|
2007-11-19 02:12:35 +01:00
|
|
|
$val = '$' . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
}else {
|
|
|
|
$val = '<i>$0.00</i>';
|
|
|
|
}
|
|
|
|
$valforInput = $this->value ? Convert::raw2att($val) : "";
|
|
|
|
return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span><input type=\"hidden\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This already is a readonly field.
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|