public function saveSettings(Neo_SettingsModel $settings, $validate = true)
{
if (!$validate || $this->validateFieldSettings($settings)) {
$transaction = $this->beginTransaction();
try {
$neoField = $settings->getField();
// Delete the old block types and groups
// Delete the old block types first, in case there's a handle conflict with one of the new ones
$oldBlockTypes = $this->getBlockTypesByFieldId($neoField->id);
$oldBlockTypesById = [];
foreach ($oldBlockTypes as $blockType) {
$oldBlockTypesById[$blockType->id] = $blockType;
}
foreach ($settings->getBlockTypes() as $blockType) {
if (!$blockType->isNew()) {
unset($oldBlockTypesById[$blockType->id]);
}
}
foreach ($oldBlockTypesById as $blockType) {
$this->deleteBlockType($blockType);
}
$this->deleteGroupsByFieldId($neoField->id);
// Save the new block types and groups
foreach ($settings->getBlockTypes() as $blockType) {
$blockType->fieldId = $neoField->id;
$this->saveBlockType($blockType, false);
}
foreach ($settings->getGroups() as $group) {
$group->fieldId = $neoField->id;
$this->saveGroup($group);
}
$this->commitTransaction($transaction);
// Update our cache of this field's block types and groups
$fieldId = $settings->getField()->id;
$this->_blockTypesByFieldId[$fieldId] = $settings->getBlockTypes();
$this->_groupsByFieldId[$fieldId] = $settings->getGroups();
return true;
} catch (\Exception $e) {
$this->rollbackTransaction($transaction);
throw $e;
}
}
return false;
}