function saveInto(DataObject $record) { $name = $this->name; $fieldSet = $record->{$name}(); $record->EmailTo = $_REQUEST[$name]['EmailTo']; $record->EmailOnSubmit = isset($_REQUEST[$name]['EmailOnSubmit']) ? "1" : "0"; $record->SubmitButtonText = $_REQUEST[$name]['SubmitButtonText']; // store the field IDs and delete the missing fields // alternatively, we could delete all the fields and re add them $missingFields = array(); foreach ($fieldSet as $existingField) { $missingFields[$existingField->ID] = $existingField; } // write the new fields to the database if ($_REQUEST[$name]) { foreach (array_keys($_REQUEST[$name]) as $newEditableID) { $newEditableData = $_REQUEST[$name][$newEditableID]; // `ParentID`=0 is for the new page $editable = DataObject::get_one('EditableFormField', "(`ParentID`='{$record->ID}' OR `ParentID`=0) AND `EditableFormField`.`ID`='{$newEditableID}'"); // check if we are updating an existing field if ($editable && isset($missingFields[$editable->ID])) { unset($missingFields[$editable->ID]); } // create a new object // this should now be obsolete if (!$editable && !empty($newEditableData['Type']) && class_exists($newEditableData['Type'])) { $editable = new $newEditableData['Type'](); $editable->ID = 0; $editable->ParentID = $record->ID; if (!is_subclass_of($editable, 'EditableFormField')) { $editable = null; } } if ($editable) { if ($editable->ParentID == 0) { $editable->ParentID = $record->ID; } $editable->populateFromPostData($newEditableData); //$editable->write(); } } } // remove the fields not saved foreach ($missingFields as $removedField) { if (is_numeric($removedField->ID)) { $removedField->delete(); } } if ($record->hasMethod('customFormSave')) { $record->customFormSave($_REQUEST[$name], $record); } //$record->writeWithoutVersion(); if ($record->hasMethod('processNewFormFields')) { $record->processNewFormFields(); } }
/** * Handles saving the page. Needs to keep an eye on fields * and options which have been removed / added * * @param DataObject Record to Save it In */ function saveInto(DataObject $record) { $name = $this->name; $fieldSet = $record->$name(); // @todo shouldn't we deal with customFormActions on that object? $record->EmailOnSubmit = isset($_REQUEST[$name]['EmailOnSubmit']) ? "1" : "0"; $record->SubmitButtonText = isset($_REQUEST[$name]['SubmitButtonText']) ? $_REQUEST[$name]['SubmitButtonText'] : ""; $record->ShowClearButton = isset($_REQUEST[$name]['ShowClearButton']) ? "1" : "0"; // store the field IDs and delete the missing fields // alternatively, we could delete all the fields and re add them $missingFields = array(); foreach($fieldSet as $existingField) { $missingFields[$existingField->ID] = $existingField; } if(isset($_REQUEST[$name]) && is_array($_REQUEST[$name])) { foreach($_REQUEST[$name] as $newEditableID => $newEditableData) { if(!is_numeric($newEditableID)) continue; // get it from the db $editable = DataObject::get_by_id('EditableFormField', $newEditableID); // if it exists in the db update it if($editable) { // remove it from the removed fields list if(isset($missingFields[$editable->ID]) && isset($newEditableData) && is_array($newEditableData)) { unset($missingFields[$editable->ID]); } // set form id if($editable->ParentID == 0) { $editable->ParentID = $record->ID; } // save data $editable->populateFromPostData($newEditableData); } } } // remove the fields not saved if($this->canEdit()) { foreach($missingFields as $removedField) { if(is_numeric($removedField->ID)) { // check we can edit this $removedField->delete(); } } } if($record->hasMethod('customFormSave')) { $record->customFormSave($_REQUEST[$name], $record); } if($record->hasMethod('processNewFormFields')) { $record->processNewFormFields(); } }