mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Allow "Yes" and "No" english text to be translated
BUGFIX Fix DropdownField to select the correct option when using a map with "0" as an array key - useful for boolean searching using DropdownField MINOR Added tests to DropdownFieldTest for asserting the correct OPTION element is selected git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@75119 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
496d159333
commit
67e5126ce8
@ -21,11 +21,11 @@ class Boolean extends DBField {
|
||||
}
|
||||
|
||||
function Nice() {
|
||||
return ($this->value) ? "yes" : "no";
|
||||
return ($this->value) ? _t('Boolean.YES', 'Yes') : _t('Boolean.NO', 'No');
|
||||
}
|
||||
|
||||
function NiceAsBoolean() {
|
||||
return ($this->value) ? "true" : "false";
|
||||
return ($this->value) ? 'true' : 'false';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,7 +34,7 @@ class Boolean extends DBField {
|
||||
function saveInto($dataObject) {
|
||||
$fieldName = $this->name;
|
||||
if($fieldName) {
|
||||
$dataObject->$fieldName = $this->value ? 1 : 0;
|
||||
$dataObject->$fieldName = ($this->value) ? 1 : 0;
|
||||
} else {
|
||||
user_error("DBField::saveInto() Called on a nameless '$this->class' object", E_USER_ERROR);
|
||||
}
|
||||
@ -43,6 +43,16 @@ class Boolean extends DBField {
|
||||
public function scaffoldFormField($title = null, $params = null) {
|
||||
return new CheckboxField($this->name, $title);
|
||||
}
|
||||
|
||||
public function scaffoldSearchField($title = null) {
|
||||
$anyText = _t('Boolean.ANY', 'Any');
|
||||
$source = array(
|
||||
1 => _t('Boolean.YES', 'Yes'),
|
||||
0 => _t('Boolean.NO', 'No')
|
||||
);
|
||||
|
||||
return new DropdownField($this->name, $title, $source, '', null, "($anyText)");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an encoding of the given value suitable for inclusion in a SQL statement.
|
||||
|
@ -73,7 +73,7 @@ class Date extends DBField {
|
||||
/**
|
||||
* Return the date formatted using the given strftime formatting string.
|
||||
*
|
||||
* strftime obeyes the current LC_TIME/LC_ALL when printing lexical values
|
||||
* strftime obeys the current LC_TIME/LC_ALL when printing lexical values
|
||||
* like day- and month-names
|
||||
*/
|
||||
function FormatI18N($formattingString) {
|
||||
|
@ -45,27 +45,25 @@ class Enum extends DBField {
|
||||
DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set utf8 collate utf8_general_ci default '{$this->default}'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a dropdown field suitable for editing this field
|
||||
*/
|
||||
function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null, $emptyString = null) {
|
||||
if(!$title) $title = $this->name;
|
||||
if(!$name) $name = $this->name;
|
||||
|
||||
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form, $emptyString);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function scaffoldFormField($title = null, $params = null) {
|
||||
return $this->formField($title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a dropdown field suitable for editing this field
|
||||
*/
|
||||
function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null) {
|
||||
if(!$title) $title = $this->name;
|
||||
if(!$name) $name = $this->name;
|
||||
|
||||
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
function scaffoldSearchField($title = null) {
|
||||
$field = $this->formField($title);
|
||||
$field->Source = array_merge(array("" => "(Any)"), $this->enumValues());
|
||||
return $field;
|
||||
$anyText = _t('Enum.ANY', 'Any');
|
||||
return $this->formField($title, null, false, '', null, "($anyText)");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,7 +79,7 @@ HTML;
|
||||
*/
|
||||
|
||||
function performReadonlyTransformation() {
|
||||
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? 'Yes' : 'No');
|
||||
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
|
||||
$field->setForm($this->form);
|
||||
return $field;
|
||||
}
|
||||
@ -102,7 +102,7 @@ class CheckboxField_Readonly extends ReadonlyField {
|
||||
}
|
||||
|
||||
function setValue($val) {
|
||||
$this->value = ($val) ? 'Yes' : 'No';
|
||||
$this->value = ($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,9 +77,20 @@ class DropdownField extends FormField {
|
||||
}
|
||||
|
||||
foreach($source as $value => $title) {
|
||||
$selected = ($value == $this->value) ? 'selected' : null;
|
||||
if($selected && $this->value != 0) {
|
||||
$this->isSelected = true;
|
||||
|
||||
// Blank value of field and source (e.g. "" => "(Any)")
|
||||
if($value === '' && ($this->value === '' || $this->value === null)) {
|
||||
$selected = 'selected';
|
||||
} else {
|
||||
// Normal value from the source
|
||||
if($value) {
|
||||
$selected = ($value == $this->value) ? 'selected' : null;
|
||||
} else {
|
||||
// Do a type check comparison, we might have an array key of 0
|
||||
$selected = ($value === $this->value) ? 'selected' : null;
|
||||
}
|
||||
|
||||
$this->isSelected = ($selected) ? true : false;
|
||||
}
|
||||
|
||||
$options .= $this->createTag(
|
||||
@ -92,7 +103,7 @@ class DropdownField extends FormField {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$attributes = array(
|
||||
'class' => ($this->extraClass() ? $this->extraClass() : ''),
|
||||
'id' => $this->id(),
|
||||
|
@ -10,7 +10,7 @@ class DropdownFieldTest extends SapphireTest {
|
||||
$dropdownField = new DropdownField('FeelingOk', 'Are you feeling ok?', array(0 => 'No', 1 => 'Yes'), '', null, '(Select one)');
|
||||
$dropdownField->addExtraClass('thisIsMyExtraClassForDropdownField');
|
||||
preg_match('/thisIsMyExtraClassForDropdownField/', $dropdownField->Field(), $matches);
|
||||
$this->assertTrue($matches[0] == 'thisIsMyExtraClassForDropdownField');
|
||||
$this->assertEquals($matches[0], 'thisIsMyExtraClassForDropdownField');
|
||||
}
|
||||
|
||||
function testGetSource() {
|
||||
@ -89,5 +89,127 @@ class DropdownFieldTest extends SapphireTest {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function testNumberOfSelectOptionsAvailable() {
|
||||
/* Create a field with a blank value */
|
||||
$field = $this->testDropdownField('(Any)');
|
||||
|
||||
/* 3 options are available */
|
||||
$this->assertEquals(count($this->findOptionElements($field->Field())), 3, '3 options are available');
|
||||
|
||||
/* There is one selected option, since a dropdown can only possibly have one! */
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
$this->assertEquals(count($selectedOptions), 1, 'We only have 1 selected option, since a dropdown can only possibly have one!');
|
||||
|
||||
/* Create a field without a blank value */
|
||||
$field = $this->testDropdownField();
|
||||
|
||||
/* 2 options are available */
|
||||
$this->assertEquals(count($this->findOptionElements($field->Field())), 2, '2 options are available');
|
||||
|
||||
/* There are no selected options */
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
$this->assertEquals(count($selectedOptions), 0, 'There are no selected options');
|
||||
}
|
||||
|
||||
function testIntegerZeroValueSeelctedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)', 0);
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "No" */
|
||||
$this->assertEquals((string) $selectedOptions[0], 'No', 'The selected option is "No"');
|
||||
}
|
||||
|
||||
function testBlankStringValueSelectedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)');
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "(Any)" */
|
||||
$this->assertEquals((string) $selectedOptions[0], '(Any)', 'The selected option is "(Any)"');
|
||||
}
|
||||
|
||||
function testNullValueSelectedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)', null);
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "(Any)" */
|
||||
$this->assertEquals((string) $selectedOptions[0], '(Any)', 'The selected option is "(Any)"');
|
||||
}
|
||||
|
||||
function testStringValueSelectedOptionBehaviour() {
|
||||
$field = $this->testDropdownField('(Any)', '1');
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "Yes" */
|
||||
$this->assertEquals((string) $selectedOptions[0], 'Yes', 'The selected option is "Yes"');
|
||||
|
||||
$field->setSource(array(
|
||||
'Cats' => 'Cats and Kittens',
|
||||
'Dogs' => 'Dogs and Puppies'
|
||||
));
|
||||
$field->setValue('Cats');
|
||||
|
||||
$selectedOptions = $this->findSelectedOptionElements($field->Field());
|
||||
|
||||
/* The selected option is "Cats and Kittens" */
|
||||
$this->assertEquals((string) $selectedOptions[0], 'Cats and Kittens', 'The selected option is "Cats and Kittens"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a test dropdown field, with the option to
|
||||
* set what source and blank value it should contain
|
||||
* as optional parameters.
|
||||
*
|
||||
* @param string|null $emptyString The text to display for the empty value
|
||||
* @param string|integer $value The default value of the field
|
||||
* @return DropdownField object
|
||||
*/
|
||||
function testDropdownField($emptyString = null, $value = '') {
|
||||
/* Set up source, with 0 and 1 integers as the values */
|
||||
$source = array(
|
||||
0 => 'No',
|
||||
1 => 'Yes'
|
||||
);
|
||||
|
||||
return new DropdownField('Field', null, $source, $value, null, $emptyString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the <OPTION> elements from a
|
||||
* string of HTML.
|
||||
*
|
||||
* @param string $html HTML to scan for elements
|
||||
* @return SimpleXMLElement
|
||||
*/
|
||||
function findOptionElements($html) {
|
||||
$parser = new CSSContentParser($html);
|
||||
return $parser->getBySelector('option');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the <OPTION> elements from a
|
||||
* string of HTML that have the "selected"
|
||||
* attribute.
|
||||
*
|
||||
* @param string $html HTML to parse for elements
|
||||
* @return array of SimpleXMLElement objects
|
||||
*/
|
||||
function findSelectedOptionElements($html) {
|
||||
$options = $this->findOptionElements($html);
|
||||
|
||||
/* Find any elements that have the "selected" attribute and put them into a list */
|
||||
$foundSelected = array();
|
||||
foreach($options as $option) {
|
||||
$attributes = $option->attributes();
|
||||
if($attributes) foreach($attributes as $attribute => $value) {
|
||||
if($attribute == 'selected') {
|
||||
$foundSelected[] = $option;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $foundSelected;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user