From df199ebc2bda4a497272a8c998ccfa2e601fcb07 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 3 Oct 2008 18:38:52 +0000 Subject: [PATCH] ENHANCEMENT Added DataObject->scaffoldCMSFields() to simplify overloading of getCMSFields(). scaffoldCMSFields() automatically converts scaffoldFormFields() to a tabset ENHANCEMENT Added DataObject->fieldLabel() and removed $fieldname parameter from DataObject->fieldLabels($fieldName) to simplify overloading of fieldLabels() for i18n git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63630 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 78 +++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index a596e6113..22d1c3852 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -1201,11 +1201,10 @@ class DataObject extends ViewableData implements DataObjectInterface { * Add the scaffold-generated relation fields to the given field set */ protected function addScaffoldRelationFields($fieldSet) { - if ($this->has_many() || $this->many_many()) { + // make sure we have a tabset + if($this->has_many() || $this->many_many()) { $oldFields = $fieldSet; - $fieldSet = new FieldSet( - new TabSet("Root", new Tab("Main")) - ); + $fieldSet = new FieldSet(new TabSet("Root", new Tab("Main"))); foreach($oldFields as $field) { $fieldSet->addFieldToTab("Root.Main", $field); } @@ -1508,7 +1507,7 @@ class DataObject extends ViewableData implements DataObjectInterface { } else { $fieldObject = $this->dbObject($fieldName)->scaffoldFormField(); } - $fieldObject->setTitle($this->fieldLabels($fieldName)); + $fieldObject->setTitle($this->fieldLabel($fieldName)); $fields->push($fieldObject); } foreach($this->has_one() as $relationship => $component) { @@ -1520,6 +1519,31 @@ class DataObject extends ViewableData implements DataObjectInterface { } return $fields; } + + /** + * Returns automatically generated fields useable within the CMS. + * Does not include relations, you can add them by using + * {@link addScaffoldRelationFields()}. Call {@link getCMSFields()} + * to get all fields including relational tables and decorated + * fields in a TabSet. + * + * @uses getCMSFields() + * + * @return FieldSet Tabbed fields + */ + public function scaffoldCMSFields() { + $untabbedFields = $this->scaffoldFormFields(); + + // make sure we have a tabset + if(!$untabbedFields->hasTabSet()) { + $tabbedFields = new FieldSet(new TabSet("Root", new Tab("Main"))); + foreach($untabbedFields as $field) { + $tabbedFields->addFieldToTab("Root.Main", $field); + } + } + + return $tabbedFields; + } /** * Centerpiece of every data administration interface in Silverstripe, @@ -1545,16 +1569,17 @@ class DataObject extends ViewableData implements DataObjectInterface { * @return FieldSet Returns a TabSet for usage within the CMS - don't use for frontend forms. */ public function getCMSFields() { - $fields = $this->scaffoldFormFields(); + // should be a plain FieldSet without tabs + $tabbedFields = $this->scaffoldCMSFields(); // If we don't have an ID, then relation fields don't work if($this->ID) { - $fields = $this->addScaffoldRelationFields($fields); + $this->addScaffoldRelationFields($tabbedFields); } - $this->extend('updateCMSFields', $fields); + $this->extend('updateCMSFields', $tabbedFields); - return $fields; + return $tabbedFields; } @@ -2579,26 +2604,39 @@ class DataObject extends ViewableData implements DataObjectInterface { * between data object being required in the search interface. * * Generates labels based on name of the field itself, if no static property - * {@link self::searchable_fields_labels} exists. + * {@link self::field_labels} exists. * - * @param $fieldName name of the field to retrieve + * @uses $field_labels + * @uses FormField::name_to_label() + * * @return array of all element labels if no argument given * @return string of label if field */ - public function fieldLabels($fieldName = false) { + public function fieldLabels() { $customLabels = $this->stat('field_labels'); $autoLabels = array(); if($this->inheritedDatabaseFields()){ - foreach($this->inheritedDatabaseFields() as $name => $type) { - $autoLabels[$name] = FormField::name_to_label($name); + foreach($this->inheritedDatabaseFields() as $name => $type) { + $autoLabels[$name] = FormField::name_to_label($name); + } } - $labels = array_merge((array)$autoLabels, (array)$customLabels); - if($fieldName) { - return (isset($labels[$fieldName])) ? $labels[$fieldName] : FormField::name_to_label($fieldName); - } else { - return $labels; - }} + return array_merge((array)$autoLabels, (array)$customLabels); + } + + /** + * Get a human-readable label for a single field, + * see {@link fieldLabels()} for more details. + * + * @uses fieldLabels() + * @uses FormField::name_to_label() + * + * @param string $name Name of the field + * @return string Label of the field + */ + public function fieldLabel($name) { + $labels = $this->fieldLabels(); + return (isset($labels[$name])) ? $labels[$name] : FormField::name_to_label($name); } /**