MINOR Simplified TableField->saveData() code structure (no logic changes) (merged from branches/2.3-nzct)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@82078 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-07-17 00:17:21 +00:00
parent 98cc45291b
commit bc07a4644e

View File

@ -391,8 +391,10 @@ class TableField extends TableListField {
* If set to FALSE, it will always create new object (default: TRUE) * If set to FALSE, it will always create new object (default: TRUE)
* @return array Array of saved object IDs in the key, and the status ("Updated") in the value * @return array Array of saved object IDs in the key, and the status ("Updated") in the value
*/ */
function saveData($dataObjects,$ExistingValues = true) { function saveData($dataObjects, $existingValues = true) {
$savedObj = array(); if(!$dataObjects) return false;
$savedObjIds = array();
$fieldset = $this->FieldSetForRow(); $fieldset = $this->FieldSetForRow();
// add hiddenfields // add hiddenfields
@ -404,10 +406,8 @@ class TableField extends TableListField {
$form = new Form($this, null, $fieldset, new FieldSet()); $form = new Form($this, null, $fieldset, new FieldSet());
if($dataObjects) { foreach ($dataObjects as $objectid => $fieldValues) {
foreach ($dataObjects as $objectid => $fieldValues) { // 'new' counts as an empty column, don't save it
// we have to "sort" new data first,
// and process it in a seperate saveData-call (see setValue())
if($objectid === "new") continue; if($objectid === "new") continue;
// extra data was creating fields, but // extra data was creating fields, but
@ -415,56 +415,53 @@ class TableField extends TableListField {
$fieldValues = array_merge( $this->extraData, $fieldValues ); $fieldValues = array_merge( $this->extraData, $fieldValues );
} }
$hasData = false; // either look for an existing object, or create a new one
$obj = new $this->sourceClass(); if($existingValues) {
if($ExistingValues) {
$obj = DataObject::get_by_id($this->sourceClass, $objectid); $obj = DataObject::get_by_id($this->sourceClass, $objectid);
} else {
$sourceClass = $this->sourceClass;
$obj = new $sourceClass();
} }
// Legacy: Use the filter as a predefined relationship-ID // Legacy: Use the filter as a predefined relationship-ID
if($this->filterField && $this->filterValue) { if($this->filterField && $this->filterValue) {
$filterField = $this->filterField; $filterField = $this->filterField;
$obj->$filterField = $this->filterValue; $obj->$filterField = $this->filterValue;
} }
// Determine if there is changed data for saving // Determine if there is changed data for saving
$dataFields = array(); $dataFields = array();
foreach($fieldValues as $type => $value) {
foreach($fieldValues as $type => $value) { // if the field is an actual datafield (not a preset hiddenfield)
if(is_array($this->extraData)){ // if the field is an actual datafield (not a preset hiddenfield) if(is_array($this->extraData)) {
if(!in_array($type, array_keys($this->extraData))) { if(!in_array($type, array_keys($this->extraData))) {
$dataFields[$type] = $value;
}
} else { // all fields are real
$dataFields[$type] = $value; $dataFields[$type] = $value;
} }
// all fields are real
} else {
$dataFields[$type] = $value;
} }
$dataValues = ArrayLib::array_values_recursive($dataFields);
foreach($dataValues as $value) {
if(!empty($value)) {
$hasData = true;
}
}
// save
if($hasData) {
$form->loadDataFrom($fieldValues, true);
$form->saveInto($obj);
$objectid = $obj->write();
$savedObj[$objectid] = "Updated";
}
} }
return $savedObj; $dataValues = ArrayLib::array_values_recursive($dataFields);
} else { // determine if any of the fields have a value (loose checking with empty())
return false; $hasData = false;
foreach($dataValues as $value) {
if(!empty($value)) $hasData = true;
}
if($hasData) {
$form->loadDataFrom($fieldValues, true);
$form->saveInto($obj);
$objectid = $obj->write();
$savedObjIds[$objectid] = "Updated";
}
} }
}
return $savedObjIds;
}
/** /**
* Organises the data in the appropriate manner for saving * Organises the data in the appropriate manner for saving