2009-10-23 03:29:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* NullableField is a field that wraps other fields when you want to allow the user to specify whether the value of
|
|
|
|
* the field is null or not.
|
2009-10-23 03:29:55 +02:00
|
|
|
*
|
|
|
|
* The classic case is to wrap a TextField so that the user can distinguish between an empty string and a null string.
|
|
|
|
* $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
|
|
|
|
*
|
|
|
|
* It displays the field that is wrapped followed by a checkbox that is used to specify if the value is null or not.
|
|
|
|
* It uses the Title of the wrapped field for its title.
|
|
|
|
* When a form is submitted the field tests the value of the "is null" checkbox and sets its value accordingly.
|
|
|
|
* You can retrieve the value of the wrapped field from the NullableField as follows:
|
|
|
|
* $field->Value() or $field->dataValue()
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* You can specify the label to use for the "is null" checkbox. If you want to use I8N for this label then specify it
|
|
|
|
* like this:
|
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null");
|
|
|
|
*
|
|
|
|
* @author Pete Bacon Darwin
|
2010-04-23 02:11:41 +02:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
|
|
|
class NullableField extends FormField {
|
|
|
|
/**
|
|
|
|
* The field that holds the value of this field
|
|
|
|
* @var FormField
|
|
|
|
*/
|
|
|
|
protected $valueField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The label to show next to the is null check box.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $isNullLabel;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new nullable field
|
|
|
|
* @param $valueField
|
|
|
|
* @return NullableField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct(FormField $valueField, $isNullLabel = null) {
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->valueField = $valueField;
|
|
|
|
$this->isNullLabel = $isNullLabel;
|
|
|
|
if ( is_null($this->isNullLabel) ) {
|
|
|
|
// Set a default label if one is not provided.
|
2012-04-14 00:16:22 +02:00
|
|
|
$this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null');
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
parent::__construct($valueField->getName(), $valueField->Title(), $valueField->Value(),
|
|
|
|
$valueField->getForm(), $valueField->RightTitle());
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->readonly = $valueField->isReadonly();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the label used for the Is Null checkbox.
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getIsNullLabel() {
|
2009-10-23 03:29:55 +02:00
|
|
|
return $this->isNullLabel;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set the label used for the Is Null checkbox.
|
|
|
|
* @param $isNulLabel string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setIsNullLabel(string $isNulLabel){
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->isNullLabel = $isNulLabel;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id used for the Is Null check box.
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getIsNullId() {
|
2011-10-29 06:01:52 +02:00
|
|
|
return $this->getName() . "_IsNull";
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
2012-03-24 04:38:57 +01:00
|
|
|
* @see framework/forms/FormField#Field()
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2009-10-23 03:29:55 +02:00
|
|
|
if ( $this->isReadonly()) {
|
|
|
|
$nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId());
|
|
|
|
} else {
|
|
|
|
$nullableCheckbox = new CheckboxField($this->getIsNullId());
|
|
|
|
}
|
|
|
|
$nullableCheckbox->setValue(is_null($this->dataValue()));
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
return $this->valueField->Field() . ' ' . $nullableCheckbox->Field()
|
|
|
|
. ' <span>' . $this->getIsNullLabel().'</span>';
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value is sometimes an array, and sometimes a single value, so we need to handle both cases
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value, $data = null) {
|
2009-10-23 03:29:55 +02:00
|
|
|
if ( is_array($data) && array_key_exists($this->getIsNullId(), $data) && $data[$this->getIsNullId()] ) {
|
|
|
|
$value = null;
|
|
|
|
}
|
|
|
|
$this->valueField->setValue($value);
|
|
|
|
parent::setValue($value);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
|
|
|
* @see forms/FormField#setName($name)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2009-10-23 03:29:55 +02:00
|
|
|
// We need to pass through the name change to the underlying value field.
|
|
|
|
$this->valueField->setName($name);
|
|
|
|
parent::setName($name);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
2012-03-24 04:38:57 +01:00
|
|
|
* @see framework/forms/FormField#debug()
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$result = "$this->class ($this->name: $this->title : <font style='color:red;'>$this->message</font>) = ";
|
|
|
|
$result .= (is_null($this->value)) ? "<<null>>" : $this->value;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|