2009-10-23 03:29:55 +02:00
|
|
|
<?php
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
2015-04-27 04:50:40 +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.
|
|
|
|
*
|
|
|
|
* The classic case is to wrap a TextField so that the user can distinguish between an empty string
|
|
|
|
* and a null string.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-04-27 04:50:40 +02:00
|
|
|
* 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:
|
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* $field->Value() or $field->dataValue()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-04-27 04:50:40 +02:00
|
|
|
* You can specify the label to use for the "is null" checkbox. If you want to use i18n for this
|
|
|
|
* label then specify it like this:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-04-27 04:50:40 +02:00
|
|
|
* $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null"));
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @author Pete Bacon Darwin
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
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
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @var FormField
|
|
|
|
*/
|
|
|
|
protected $valueField;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* The label to show next to the is null check box.
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $isNullLabel;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Create a new nullable field
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
|
|
|
* @param FormField $valueField
|
|
|
|
* @param null|string $isNullLabel
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
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;
|
2015-04-27 04:50:40 +02:00
|
|
|
|
|
|
|
if(isset($isNullLabel)) {
|
|
|
|
$this->setIsNullLabel($isNullLabel);
|
|
|
|
} else {
|
2012-04-14 00:16:22 +02:00
|
|
|
$this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null');
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
2015-04-27 04:50:40 +02:00
|
|
|
|
|
|
|
parent::__construct(
|
|
|
|
$valueField->getName(),
|
|
|
|
$valueField->Title(),
|
|
|
|
$valueField->Value()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->setForm($valueField->getForm());
|
|
|
|
$this->setRightTitle($valueField->RightTitle());
|
|
|
|
$this->setReadonly($valueField->isReadonly());
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Get the label used for the Is Null checkbox.
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getIsNullLabel() {
|
2009-10-23 03:29:55 +02:00
|
|
|
return $this->isNullLabel;
|
|
|
|
}
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Set the label used for the Is Null checkbox.
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @param $isNulLabel string
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2015-04-27 04:50:40 +02:00
|
|
|
public function setIsNullLabel($isNulLabel) {
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->isNullLabel = $isNulLabel;
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Get the id used for the Is Null check box.
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
2009-10-23 03:29:55 +02:00
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 04:50:40 +02:00
|
|
|
* @param array $properties
|
|
|
|
*
|
2015-06-20 12:11:08 +02:00
|
|
|
* @return HTMLText
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2015-04-27 04:50:40 +02:00
|
|
|
if($this->isReadonly()) {
|
2009-10-23 03:29:55 +02:00
|
|
|
$nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId());
|
|
|
|
} else {
|
|
|
|
$nullableCheckbox = new CheckboxField($this->getIsNullId());
|
|
|
|
}
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$nullableCheckbox->setValue(is_null($this->dataValue()));
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2015-06-20 12:11:08 +02:00
|
|
|
return DBField::create_field('HTMLText', sprintf(
|
2015-04-27 04:50:40 +02:00
|
|
|
'%s %s <span>%s</span>',
|
|
|
|
$this->valueField->Field(),
|
|
|
|
$nullableCheckbox->Field(),
|
|
|
|
$this->getIsNullLabel()
|
2015-06-20 12:11:08 +02:00
|
|
|
));
|
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
|
2015-04-27 04:50:40 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param null|array $data
|
|
|
|
*
|
|
|
|
* @return $this
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value, $data = null) {
|
2015-04-27 04:50:40 +02:00
|
|
|
$id = $this->getIsNullId();
|
|
|
|
|
|
|
|
if(is_array($data) && array_key_exists($id, $data) && $data[$id]) {
|
2009-10-23 03:29:55 +02:00
|
|
|
$value = null;
|
|
|
|
}
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->valueField->setValue($value);
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
parent::setValue($value);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
2015-04-27 04:50:40 +02:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return $this
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->valueField->setName($name);
|
2015-04-27 04:50:40 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
parent::setName($name);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 04:50:40 +02:00
|
|
|
* @return string
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2015-04-27 04:50:40 +02:00
|
|
|
$result = sprintf(
|
|
|
|
'%s (%s: $s : <span style="color: red">%s</span>) = ',
|
|
|
|
$this->class,
|
|
|
|
$this->name,
|
|
|
|
$this->title,
|
|
|
|
$this->message
|
|
|
|
);
|
|
|
|
|
|
|
|
if($this->value === null) {
|
|
|
|
$result .= "<<null>>";
|
|
|
|
} else {
|
|
|
|
$result .= (string) $this->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|
2014-11-12 03:19:12 +01:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
}
|