private function _applyFieldTranslationSetting($owner, $field, $blocks)
{
// Does it look like any work is needed here?
$applyNewTranslationSetting = false;
foreach ($blocks as $block) {
if ($block->id && ($field->translatable && !$block->ownerLocale || !$field->translatable && $block->ownerLocale)) {
$applyNewTranslationSetting = true;
break;
}
}
if ($applyNewTranslationSetting) {
// Get all of the blocks for this field/owner that use the other locales, whose ownerLocale attribute is set
// incorrectly
$blocksInOtherLocales = [];
$criteria = craft()->elements->getCriteria(Neo_ElementType::NeoBlock);
$criteria->fieldId = $field->id;
$criteria->ownerId = $owner->id;
$criteria->status = null;
$criteria->localeEnabled = null;
$criteria->limit = null;
if ($field->translatable) {
$criteria->ownerLocale = ':empty:';
}
foreach (craft()->i18n->getSiteLocaleIds() as $localeId) {
if ($localeId == $owner->locale) {
continue;
}
$criteria->locale = $localeId;
if (!$field->translatable) {
$criteria->ownerLocale = $localeId;
}
$blocksInOtherLocale = $criteria->find();
if ($blocksInOtherLocale) {
$blocksInOtherLocales[$localeId] = $blocksInOtherLocale;
}
}
if ($blocksInOtherLocales) {
if ($field->translatable) {
$newBlockIds = [];
// Duplicate the other-locale blocks so each locale has their own unique set of blocks
foreach ($blocksInOtherLocales as $localeId => $blocksInOtherLocale) {
foreach ($blocksInOtherLocale as $blockInOtherLocale) {
$originalBlockId = $blockInOtherLocale->id;
$blockInOtherLocale->id = null;
$blockInOtherLocale->getContent()->id = null;
$blockInOtherLocale->ownerLocale = $localeId;
$this->saveBlock($blockInOtherLocale, false);
$newBlockIds[$originalBlockId][$localeId] = $blockInOtherLocale->id;
}
}
// Duplicate the relations, too. First by getting all of the existing relations for the original
// blocks
$relations = craft()->db->createCommand()->select('fieldId, sourceId, sourceLocale, targetId, sortOrder')->from('relations')->where(['in', 'sourceId', array_keys($newBlockIds)])->queryAll();
if ($relations) {
// Now duplicate each one for the other locales' new blocks
$rows = [];
foreach ($relations as $relation) {
$originalBlockId = $relation['sourceId'];
// Just to be safe...
if (isset($newBlockIds[$originalBlockId])) {
foreach ($newBlockIds[$originalBlockId] as $localeId => $newBlockId) {
$rows[] = [$relation['fieldId'], $newBlockId, $relation['sourceLocale'], $relation['targetId'], $relation['sortOrder']];
}
}
}
craft()->db->createCommand()->insertAll('relations', ['fieldId', 'sourceId', 'sourceLocale', 'targetId', 'sortOrder'], $rows);
}
} else {
// Delete all of these blocks
$blockIdsToDelete = [];
foreach ($blocksInOtherLocales as $localeId => $blocksInOtherLocale) {
foreach ($blocksInOtherLocale as $blockInOtherLocale) {
$blockIdsToDelete[] = $blockInOtherLocale->id;
}
}
$this->deleteBlockById($blockIdsToDelete);
}
}
}
}