public function saveBlockType(Neo_BlockTypeModel $blockType, $validate = true)
{
if (!$validate || $this->validateBlockType($blockType)) {
// Save this for use by plugins (eg. Reasons)
$this->currentSavingBlockType = $blockType;
$transaction = $this->beginTransaction();
try {
// Get the block type record
$blockTypeRecord = $this->_getBlockTypeRecord($blockType);
$isNewBlockType = $blockType->isNew();
$oldBlockType = $isNewBlockType ? null : Neo_BlockTypeModel::populateModel($blockTypeRecord);
// Is there a new field layout?
$fieldLayout = $blockType->getFieldLayout();
if (!$fieldLayout->id) {
// Delete the old one
if (!$isNewBlockType && $oldBlockType->fieldLayoutId) {
craft()->fields->deleteLayoutById($oldBlockType->fieldLayoutId);
}
// Save the new one
craft()->fields->saveLayout($fieldLayout);
// Update the entry type record/model with the new layout ID
$blockType->fieldLayoutId = $fieldLayout->id;
$blockTypeRecord->fieldLayoutId = $fieldLayout->id;
}
// Set the basic info on the new block type record
$blockTypeRecord->fieldId = $blockType->fieldId;
$blockTypeRecord->name = $blockType->name;
$blockTypeRecord->handle = $blockType->handle;
$blockTypeRecord->sortOrder = $blockType->sortOrder;
$blockTypeRecord->maxBlocks = $blockType->maxBlocks;
$blockTypeRecord->childBlocks = $blockType->childBlocks;
$blockTypeRecord->topLevel = $blockType->topLevel;
// Save it, minus the field layout for now
$blockTypeRecord->save(false);
if ($isNewBlockType) {
// Set the new ID on the model
$blockType->id = $blockTypeRecord->id;
}
// Update the block type with the field layout ID
$blockTypeRecord->save(false);
$this->commitTransaction($transaction);
} catch (\Exception $e) {
$this->rollbackTransaction($transaction);
throw $e;
}
// Dereference the block type just for good measure
$this->currentSavingBlockType = null;
return true;
}
return false;
}