diff --git a/core/model/DataObject.php b/core/model/DataObject.php index ef23abc76..1fae0f516 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -1124,6 +1124,80 @@ class DataObject extends ViewableData implements DataObjectInterface { } return isset($items) ? $items : null; } + + /** + * Generates a SearchContext to be used for building and processing + * a generic search form for properties on this object. + * + * @usedby {@link ModelAdmin} + * @return SearchContext + */ + public function getDefaultSearchContext() { + $c = new SearchContext($this->class); + + return $c; + } + + /** + * Determine which properties on the DataObject are + * searchable, and map them to their default {@link FormField} + * representations. Useful for scaffolding a searchform for {@link ModelAdmin}. + * + * @usedby {@link SearchContext} + * @return FieldSet + */ + public function scaffoldSearchFields() { + $fields = new FieldSet(); + foreach($this->databaseFields() as $fieldName => $fieldType) { + // @todo Pass localized title + $fields->push($this->dbObject($fieldName)->scaffoldSearchField()); + } + + return $fields; + } + + /** + * Scaffold a simple edit form for all properties on this dataobject, + * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()} + * + * @uses {@link DBField::scaffoldFormField()} + * @return FieldSet + */ + public function scaffoldFormFields() { + $fields = new FieldSet(); + foreach($this->databaseFields() as $fieldName => $fieldType) { + // @todo Pass localized title + $fields->push($this->dbObject($fieldName)->scaffoldFormField()); + } + + return $fields; + } + + /** + * Centerpiece of every data administration interface in Silverstripe, + * which returns a {@link FieldSet} suitable for a {@link Form} object. + * If not overloaded, we're using {@link scaffoldFormFields()} to automatically + * generate this set. + * + * + * class MyCustomClass extends DataObject { + * static $db = array('CustomProperty'=>'Boolean'); + * + * public function getCMSFields() { + * $fields = parent::getCMSFields(); + * $fields->push(new CheckboxField('CustomProperty')); + * return $fields; + * } + * } + * + * + * @see Good example of complex FormField building: {@link SiteTree::getCMSFields()} + * + * @return FieldSet + */ + public function getCMSFields() { + return $this->scaffoldFormFields(); + } /** * Checks if the given fields have been filled out. diff --git a/core/model/fieldtypes/Boolean.php b/core/model/fieldtypes/Boolean.php index bf2ff7a13..f7d8c3a85 100644 --- a/core/model/fieldtypes/Boolean.php +++ b/core/model/fieldtypes/Boolean.php @@ -35,6 +35,10 @@ class Boolean extends DBField { user_error("DBField::saveInto() Called on a nameless '$this->class' object", E_USER_ERROR); } } + + public function scaffoldFormField($title = null) { + return new CheckboxField($this->name, $title); + } } ?> \ No newline at end of file diff --git a/core/model/fieldtypes/DBField.php b/core/model/fieldtypes/DBField.php index 4cb28fb6d..4de444f03 100644 --- a/core/model/fieldtypes/DBField.php +++ b/core/model/fieldtypes/DBField.php @@ -3,6 +3,7 @@ * Single field in the database. * Every field from the database is represented as a sub-class of DBField. In addition to supporting * the creation of the field in the database, + * * @package sapphire * @subpackage model */ @@ -102,6 +103,38 @@ abstract class DBField extends ViewableData { } } + /** + * Returns a FormField instance used as a default + * for form scaffolding. + * + * @usedby {@link SearchContext} + * @usedby {@link ModelAdmin} + * @usedby {@link DataObject::scaffoldFormFields()} + * + * @param string $title Optional. Localized title of the generated instance + * @return FormField + */ + public function scaffoldFormField($title = null) { + $field = new TextField($this->name, $title); + + return $field; + } + + /** + * Returns a FormField instance used as a default + * for searchform scaffolding. + * + * @usedby {@link SearchContext} + * @usedby {@link ModelAdmin} + * @usedby {@link DataObject::scaffoldFormFields()} + * + * @param string $title Optional. Localized title of the generated instance + * @return FormField + */ + public function scaffoldSearchField($title = null) { + return $this->scaffoldFormField($title); + } + /** * Add the field to the underlying database. */ diff --git a/core/model/fieldtypes/Date.php b/core/model/fieldtypes/Date.php index 104c46dde..2225a1823 100644 --- a/core/model/fieldtypes/Date.php +++ b/core/model/fieldtypes/Date.php @@ -235,6 +235,10 @@ class Date extends DBField { return date("Y-m-d", mktime(0,0,0,$fmonth,$fday,$fyear-1)); } } + + public function scaffoldFormField($title = null) { + return new DateField($this->name, $title); + } } ?> \ No newline at end of file diff --git a/core/model/fieldtypes/Datetime.php b/core/model/fieldtypes/Datetime.php index a89b1a75b..37c44c120 100644 --- a/core/model/fieldtypes/Datetime.php +++ b/core/model/fieldtypes/Datetime.php @@ -39,6 +39,10 @@ if(!class_exists('Datetime')) { function requireField() { DB::requireField($this->tableName, $this->name, "datetime"); } + + public function scaffoldFormField($title = null) { + return new PopupDateTimeField($this->name, $title); + } } } ?> diff --git a/core/model/fieldtypes/Decimal.php b/core/model/fieldtypes/Decimal.php index c5019e0c9..3d4e220af 100644 --- a/core/model/fieldtypes/Decimal.php +++ b/core/model/fieldtypes/Decimal.php @@ -35,7 +35,11 @@ class Decimal extends DBField { } else { user_error("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object", E_USER_ERROR); } - } + } + + public function scaffoldFormField($title = null) { + return new NumericField($this->name, $title); + } } ?> \ No newline at end of file diff --git a/core/model/fieldtypes/Float.php b/core/model/fieldtypes/Float.php index 74a71aa63..25a6d7e67 100644 --- a/core/model/fieldtypes/Float.php +++ b/core/model/fieldtypes/Float.php @@ -12,6 +12,10 @@ class Float extends DBField { function Nice() { return number_format($this->value, 2); - } + } + + public function scaffoldFormField($title = null) { + return new NumericField($this->name, $title); + } } ?> \ No newline at end of file diff --git a/core/model/fieldtypes/HTMLText.php b/core/model/fieldtypes/HTMLText.php index a3f23c5d5..6d600d997 100755 --- a/core/model/fieldtypes/HTMLText.php +++ b/core/model/fieldtypes/HTMLText.php @@ -64,6 +64,14 @@ class HTMLText extends Text { return $summary; } + + public function scaffoldFormField($title = null) { + return new HTMLEditorField($this->name, $title); + } + + public function scaffoldSearchField($title = null) { + return new TextField($this->name, $title); + } } diff --git a/core/model/fieldtypes/HTMLVarchar.php b/core/model/fieldtypes/HTMLVarchar.php index 905fbee66..df95ee064 100755 --- a/core/model/fieldtypes/HTMLVarchar.php +++ b/core/model/fieldtypes/HTMLVarchar.php @@ -7,7 +7,14 @@ * @subpackage model */ class HTMLVarchar extends Varchar { - + + public function scaffoldFormField($title = null) { + return new HTMLOneLineField($this->name, $title); + } + + public function scaffoldSearchField($title = null) { + return new TextField($this->name, $title); + } } diff --git a/core/model/fieldtypes/Int.php b/core/model/fieldtypes/Int.php index 2df34f083..a821d47a4 100644 --- a/core/model/fieldtypes/Int.php +++ b/core/model/fieldtypes/Int.php @@ -38,7 +38,9 @@ class Int extends DBField { return sprintf( '%d', $this->value ); } - + public function scaffoldFormField($title = null) { + return new NumericField($this->name, $title); + } } diff --git a/core/model/fieldtypes/SSDatetime.php b/core/model/fieldtypes/SSDatetime.php index 85a983e42..727ee8558 100644 --- a/core/model/fieldtypes/SSDatetime.php +++ b/core/model/fieldtypes/SSDatetime.php @@ -34,10 +34,9 @@ class SSDatetime extends Date { return date('Y-m-d%20H:i:s', strtotime($this->value)); } - function __construct( $name ) { - // Debug::show( 'Created SSDatetime: ' . $name ); - parent::__construct( $name ); + public function scaffoldFormField($title = null) { + return new PopupDateTimeField($this->name, $title); } } -?> +?> \ No newline at end of file diff --git a/core/model/fieldtypes/Time.php b/core/model/fieldtypes/Time.php index aaa1f8fda..0b311e9ea 100755 --- a/core/model/fieldtypes/Time.php +++ b/core/model/fieldtypes/Time.php @@ -38,5 +38,9 @@ class Time extends DBField { function requireField() { DB::requireField($this->tableName, $this->name, "time"); } + + public function scaffoldFormField($title = null) { + return new TimeField($this->name, $title); + } } ?> \ No newline at end of file diff --git a/forms/TextField.php b/forms/TextField.php index bd23ba47d..618b83305 100755 --- a/forms/TextField.php +++ b/forms/TextField.php @@ -34,5 +34,7 @@ class TextField extends FormField { if(!$this->value) $this->value = $this->Title(); return $this->Field(); } + + } -?> +?> \ No newline at end of file diff --git a/search/SearchForm.php b/search/SearchForm.php index a2bee5fef..d9af22644 100755 --- a/search/SearchForm.php +++ b/search/SearchForm.php @@ -1,6 +1,9 @@ +?> \ No newline at end of file