Clean up NullableField

This commit is contained in:
Christopher Pitt 2015-04-27 14:50:40 +12:00
parent 0653ba9630
commit efcd64a314

View File

@ -1,75 +1,98 @@
<?php <?php
/** /**
* NullableField is a field that wraps other fields when you want to allow the user to specify whether the value of * NullableField is a field that wraps other fields when you want to allow the user to specify
* the field is null or not. * whether the value of the field is null or not.
* *
* The classic case is to wrap a TextField so that the user can distinguish between an empty string and a null string. * 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")); * $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 displays the field that is wrapped followed by a checkbox that is used to specify if the
* It uses the Title of the wrapped field for its title. * 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: * 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() * $field->Value() or $field->dataValue()
* *
* You can specify the label to use for the "is null" checkbox. If you want to use I8N for this label then specify it * You can specify the label to use for the "is null" checkbox. If you want to use i18n for this
* like this: * label then specify it like this:
* *
* $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null"); * $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null"));
* *
* @author Pete Bacon Darwin * @author Pete Bacon Darwin
*
* @package forms * @package forms
* @subpackage fields-basic * @subpackage fields-basic
*/ */
class NullableField extends FormField { class NullableField extends FormField {
/** /**
* The field that holds the value of this field * The field that holds the value of this field
*
* @var FormField * @var FormField
*/ */
protected $valueField; protected $valueField;
/** /**
* The label to show next to the is null check box. * The label to show next to the is null check box.
*
* @var string * @var string
*/ */
protected $isNullLabel; protected $isNullLabel;
/** /**
* Create a new nullable field * Create a new nullable field
* @param $valueField *
* @return NullableField * @param FormField $valueField
* @param null|string $isNullLabel
*/ */
public function __construct(FormField $valueField, $isNullLabel = null) { public function __construct(FormField $valueField, $isNullLabel = null) {
$this->valueField = $valueField; $this->valueField = $valueField;
$this->isNullLabel = $isNullLabel;
if ( is_null($this->isNullLabel) ) { if(isset($isNullLabel)) {
// Set a default label if one is not provided. $this->setIsNullLabel($isNullLabel);
} else {
$this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null'); $this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null');
} }
parent::__construct($valueField->getName(), $valueField->Title(), $valueField->Value(),
$valueField->getForm(), $valueField->RightTitle()); parent::__construct(
$this->readonly = $valueField->isReadonly(); $valueField->getName(),
$valueField->Title(),
$valueField->Value()
);
$this->setForm($valueField->getForm());
$this->setRightTitle($valueField->RightTitle());
$this->setReadonly($valueField->isReadonly());
} }
/** /**
* Get the label used for the Is Null checkbox. * Get the label used for the Is Null checkbox.
*
* @return string * @return string
*/ */
public function getIsNullLabel() { public function getIsNullLabel() {
return $this->isNullLabel; return $this->isNullLabel;
} }
/** /**
* Set the label used for the Is Null checkbox. * Set the label used for the Is Null checkbox.
*
* @param $isNulLabel string * @param $isNulLabel string
*
* @return $this
*/ */
public function setIsNullLabel(string $isNulLabel){ public function setIsNullLabel($isNulLabel) {
$this->isNullLabel = $isNulLabel; $this->isNullLabel = $isNulLabel;
return $this; return $this;
} }
/** /**
* Get the id used for the Is Null check box. * Get the id used for the Is Null check box.
*
* @return string * @return string
*/ */
public function getIsNullId() { public function getIsNullId() {
@ -77,53 +100,80 @@ class NullableField extends FormField {
} }
/** /**
* (non-PHPdoc) * @param array $properties
* @see framework/forms/FormField#Field() *
* @return string
*/ */
public function Field($properties = array()) { public function Field($properties = array()) {
if ( $this->isReadonly()) { if($this->isReadonly()) {
$nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId()); $nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId());
} else { } else {
$nullableCheckbox = new CheckboxField($this->getIsNullId()); $nullableCheckbox = new CheckboxField($this->getIsNullId());
} }
$nullableCheckbox->setValue(is_null($this->dataValue())); $nullableCheckbox->setValue(is_null($this->dataValue()));
return $this->valueField->Field() . ' ' . $nullableCheckbox->Field() return sprintf(
. '&nbsp;<span>' . $this->getIsNullLabel().'</span>'; '%s %s&nbsp;<span>%s</span>',
$this->valueField->Field(),
$nullableCheckbox->Field(),
$this->getIsNullLabel()
);
} }
/** /**
* Value is sometimes an array, and sometimes a single value, so we need to handle both cases * Value is sometimes an array, and sometimes a single value, so we need to handle both cases
*
* @param mixed $value
* @param null|array $data
*
* @return $this
*/ */
public function setValue($value, $data = null) { public function setValue($value, $data = null) {
if ( is_array($data) && array_key_exists($this->getIsNullId(), $data) && $data[$this->getIsNullId()] ) { $id = $this->getIsNullId();
if(is_array($data) && array_key_exists($id, $data) && $data[$id]) {
$value = null; $value = null;
} }
$this->valueField->setValue($value); $this->valueField->setValue($value);
parent::setValue($value); parent::setValue($value);
return $this; return $this;
} }
/** /**
* (non-PHPdoc) * @param string $name
* @see forms/FormField#setName($name) *
* @return $this
*/ */
public function setName($name) { public function setName($name) {
// We need to pass through the name change to the underlying value field.
$this->valueField->setName($name); $this->valueField->setName($name);
parent::setName($name); parent::setName($name);
return $this; return $this;
} }
/** /**
* (non-PHPdoc) * @return string
* @see framework/forms/FormField#debug()
*/ */
public function debug() { public function debug() {
$result = "$this->class ($this->name: $this->title : <font style='color:red;'>$this->message</font>) = "; $result = sprintf(
$result .= (is_null($this->value)) ? "<<null>>" : $this->value; '%s (%s: $s : <span style="color: red">%s</span>) = ',
return result; $this->class,
$this->name,
$this->title,
$this->message
);
if($this->value === null) {
$result .= "<<null>>";
} else {
$result .= (string) $this->value;
}
return $result;
} }
} }