(merged from branches/roa. use "svn log -c <changeset> -g <module-svn-path>" for detailed commit message)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60214 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-08-09 05:04:15 +00:00
parent 9515ef3fc1
commit 410fa9540f
2 changed files with 215 additions and 179 deletions

View File

@ -1166,12 +1166,16 @@ class DataObject extends ViewableData implements DataObjectInterface {
* searchable, and map them to their default {@link FormField} * searchable, and map them to their default {@link FormField}
* representations. Used for scaffolding a searchform for {@link ModelAdmin}. * representations. Used for scaffolding a searchform for {@link ModelAdmin}.
* *
* Some additional logic is included for switching field labels, based on
* how generic or specific the field type is.
*
* @usedby {@link SearchContext} * @usedby {@link SearchContext}
* @return FieldSet * @return FieldSet
*/ */
public function scaffoldSearchFields() { public function scaffoldSearchFields() {
$fields = new FieldSet(); $fields = new FieldSet();
foreach($this->searchable_fields() as $fieldName => $fieldType) { foreach($this->searchable_fields() as $fieldName => $fieldType) {
if (is_int($fieldName)) $fieldName = $fieldType;
$field = $this->relObject($fieldName)->scaffoldSearchField(); $field = $this->relObject($fieldName)->scaffoldSearchField();
if (strstr($fieldName, '.')) { if (strstr($fieldName, '.')) {
$field->setName(str_replace('.', '__', $fieldName)); $field->setName(str_replace('.', '__', $fieldName));
@ -1207,6 +1211,8 @@ class DataObject extends ViewableData implements DataObjectInterface {
$fields->push(new HeaderField($this->singular_name())); $fields->push(new HeaderField($this->singular_name()));
foreach($this->db() as $fieldName => $fieldType) { foreach($this->db() as $fieldName => $fieldType) {
// @todo Pass localized title // @todo Pass localized title
// commented out, to be less of a pain in the ass
//$fields->addFieldToTab('Root.Main', $this->dbObject($fieldName)->scaffoldFormField());
$fields->push($this->dbObject($fieldName)->scaffoldFormField()); $fields->push($this->dbObject($fieldName)->scaffoldFormField());
} }
foreach($this->has_one() as $relationship => $component) { foreach($this->has_one() as $relationship => $component) {
@ -1225,6 +1231,13 @@ class DataObject extends ViewableData implements DataObjectInterface {
protected function addScaffoldRelationFields($fieldSet) { protected function addScaffoldRelationFields($fieldSet) {
if($this->has_many()) { if($this->has_many()) {
// Refactor the fields that we have been given into a tab, "Main", in a tabset
$oldFields = $fieldSet;
$fieldSet = new FieldSet(
new TabSet("Root", new Tab("Main"))
);
foreach($oldFields as $field) $fieldSet->addFieldToTab("Root.Main", $field);
// Add each relation as a separate tab // Add each relation as a separate tab
foreach($this->has_many() as $relationship => $component) { foreach($this->has_many() as $relationship => $component) {
$relationshipFields = singleton($component)->summary_fields(); $relationshipFields = singleton($component)->summary_fields();
@ -1258,17 +1271,12 @@ class DataObject extends ViewableData implements DataObjectInterface {
* @return FieldSet * @return FieldSet
*/ */
public function getCMSFields() { public function getCMSFields() {
$fieldSet = new FieldSet(new TabSet("Root", new Tab("Main"))); $fields = $this->scaffoldFormFields();
$baseFields = $this->scaffoldFormFields();
foreach($baseFields as $field) $fieldSet->addFieldToTab("Root.Main", $field);
// If we don't have an ID, then relation fields don't work // If we don't have an ID, then relation fields don't work
if($this->ID) { if($this->ID) {
$fieldSet = $this->addScaffoldRelationFields($fieldSet); $fields = $this->addScaffoldRelationFields($fields);
} }
return $fields;
return $fieldSet;
} }
/** /**
@ -1381,7 +1389,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
// Only existing fields // Only existing fields
$this->fieldExists($fieldName) $this->fieldExists($fieldName)
// Catches "0"==NULL // Catches "0"==NULL
&& (isset($this->record[$fieldName]) && is_numeric($val) && (intval($val) != intval($this->record[$fieldName]))) && (isset($this->record[$fieldName]) && (intval($val) != intval($this->record[$fieldName])))
// Main non type-based check // Main non type-based check
&& (isset($this->record[$fieldName]) && $this->record[$fieldName] != $val) && (isset($this->record[$fieldName]) && $this->record[$fieldName] != $val)
) { ) {
@ -1580,12 +1588,16 @@ class DataObject extends ViewableData implements DataObjectInterface {
} }
/** /**
* Traverses to a DBField referenced by a relationship. * Traverses to a DBField referenced by relationships between data objects.
* The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName)
* *
* @param $fieldPath string
* @return DBField * @return DBField
*/ */
public function relObject($fieldPath) { public function relObject($fieldPath) {
//Debug::message("relObject:$fieldPath");
$parts = explode('.', $fieldPath); $parts = explode('.', $fieldPath);
//Debug::dump($parts);
$fieldName = array_pop($parts); $fieldName = array_pop($parts);
$component = $this; $component = $this;
foreach($parts as $relation) { foreach($parts as $relation) {
@ -1593,9 +1605,23 @@ class DataObject extends ViewableData implements DataObjectInterface {
$component = singleton($rel); $component = singleton($rel);
} elseif ($rel = $component->has_many($relation)) { } elseif ($rel = $component->has_many($relation)) {
$component = singleton($rel); $component = singleton($rel);
//Debug::dump($component);
} elseif ($rel = $component->many_many($relation)) {
//Debug::dump($rel);
$component = singleton($rel[1]);
//Debug::dump($component);
} }
} }
return $component->dbObject($fieldName); $object = $component->dbObject($fieldName);
//Debug::message("$component->class.$fieldName");
//Debug::show($component);
//Debug::message("$component->class.$fieldName == $object->class");
if (!($object instanceof DBField)) {
user_error("Unable to traverse to related object field [$fieldPath]", E_USER_ERROR);
//Debug::dump($object);
}
return $object;
} }
/** /**

View File

@ -13,7 +13,7 @@
class Year extends DBField { class Year extends DBField {
function requireField() { function requireField() {
DB::requireField($this->tableName, $this->name, "year"); DB::requireField($this->tableName, $this->name, "year(4)");
} }
public function scaffoldFormField($title = null) { public function scaffoldFormField($title = null) {
@ -22,9 +22,19 @@ class Year extends DBField {
return $selectBox; return $selectBox;
} }
private function getDefaultOptions() { /**
$start = (int)date('Y'); * Returns a list of default options that can
$end = 1900; * be used to populate a select box, or compare against
* input values. Starts by default at the current year,
* and counts back to 1900.
*
* @param int $start starting date to count down from
* @param int $end end date to count down to
* @return array
*/
private function getDefaultOptions($start=false, $end=false) {
if (!$start) $start = (int)date('Y');
if (!$end) $end = 1900;
$years = array(); $years = array();
for($i=$start;$i>=$end;$i--) { for($i=$start;$i>=$end;$i--) {
$years[] = $i; $years[] = $i;