Multilingual.ps1

function global:Expand-ValoSearchResults {
    Param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [PSCustomObject]$SearchResults,

        [ValidateSet("STS_ListItem_WebPageLibrary", "STS_Site")]
        [Parameter(Mandatory)]
        [string]$ContentClass
    )

    Write-Verbose "Expanding results for content class --> $ContentClass"
    return $SearchResults.ResultRows `
            | Where-Object { $_["ContentClass"] -eq $ContentClass }  `
            | ForEach-Object {
                @{
                    Title = $_["Title"]
                    Path = $_["Path"]; 
                    HubId = $_["DepartmentId"] | Format-SearchResultGuid;
                    SiteId = $_["SiteId"] | Format-SearchResultGuid;
                    WebId = $_["WebId"] | Format-SearchResultGuid;
                    UniqueId = $_["UniqueId"] | Format-SearchResultGuid;
                    LanguageTermId = $_["owstaxIdValoLanguage"] | Format-SearchResultTaxonomy;
                }
            }
}

function global:Import-ValoInformationArchitecture {
    Param (
        [Parameter(ValueFromPipeline)]
        [string]$GenesisFolderPath,

        [string]$Locale
    )
    return Join-Path $GenesisFolderPath "genesis-$Locale.jsonc" `
            | Get-ChildItem `
            | Get-Content -Encoding utf8 `
            | ConvertFrom-Json `
            | Select-Object -Property ValoSite
}

function global:Export-ValoMultilingualMapping {
    Param (
        [Parameter(ValueFromPipeline)]
        [Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext]$StorageContext,
        
        [PSCustomObject[]]$Mappings,

        [ValidateSet("Site", "Page")]
        [string]$MappingType,

        [string]$TableName
    )

    $tableStorage = Get-AzureStorageTable -Name $TableName -Context $StorageContext

    if($MappingType -eq "Site") {
        $Mappings | ForEach-Object {
            $_.Root `
            | New-AzureSiteTableRow -LinkedId $_.LinkedId `
            | Add-ValoMultilingualLink -TableStorage $tableStorage
        
            $_.Secondary `
            | New-AzureSiteTableRow -LinkedId $_.LinkedId `
            | Add-ValoMultilingualLink -TableStorage $tableStorage
        }
    }

    if($MappingType -eq "Page") {
        $Mappings | ForEach-Object {
            $_.Root `
            | New-AzurePageTableRow -LinkedId $_.LinkedId `
            | Add-ValoMultilingualLink -TableStorage $tableStorage
        
            $_.Secondary `
            | New-AzurePageTableRow -LinkedId $_.LinkedId `
            | Add-ValoMultilingualLink -TableStorage $tableStorage
        }
    }
}

function global:New-ValoLinkMapping {
    Param (
        [hashtable]$Root,
        [hashtable]$Secondary
    )
    return [PSCustomObject]@{
        LinkedId = New-Guid
        Root = $Root
        Secondary = $Secondary
    }
}

function script:Format-SearchResultGuid {
    Param (
        [Parameter(ValueFromPipeline)]
        [string]$Guid
    )
    return ([Guid]$guid).ToString("D").ToLower();
}

function script:Format-SearchResultTaxonomy {
    Param (
        [Parameter(ValueFromPipeline)]
        [string]$TermInfo
    )
    $lang = $TermInfo.Substring($TermInfo.IndexOf(";L0|#0") + 6)
    return $lang.Substring(0, $lang.IndexOf("|")) `
            | Format-SearchResultGuid
}


function script:New-AzureSiteTableRow {
    Param (
        [Parameter(ValueFromPipeline)]
        [hashtable]$RowInfo,
        [string]$LinkedId
    )
    return [PSCustomObject]@{
        PartitionKey = "$($RowInfo.SiteId)-$($RowInfo.WebId)"
        RowKey = $RowInfo.SiteId
        Properties = @{
            HubId = $RowInfo.HubId;
            SiteId = $RowInfo.SiteId;
            WebId = $RowInfo.WebId;
            IsHubSite = $RowInfo.SiteId -eq $RowInfo.HubId;
            Type = "site";
            LanguageTermId = $termId
            SiteUrl = $RowInfo.Path;
            LinkedId = $LinkedId;
        }
    }
}

function script:New-AzurePageTableRow {
    Param (
        [Parameter(ValueFromPipeline)]
        [hashtable]$RowInfo,
        [string]$LinkedId      
    )
    $uniqueId = $RowInfo.UniqueId
    return [PSCustomObject]@{
        PartitionKey = "$($RowInfo.SiteId)-$($RowInfo.WebId)-$($uniqueId)"
        RowKey = $uniqueId
        Properties = @{
            HubId = $RowInfo.HubId;
            SiteId = $RowInfo.SiteId;
            WebId = $RowInfo.WebId;
            LanguageTermId = $RowInfo.LanguageTermId;
            PageUrl = $RowInfo.Path;
            LinkedId = $LinkedId;
            UniqueId = $uniqueId;
        }
    }
}

function script:Add-ValoMultilingualLink {
    Param(
        [Parameter(ValueFromPipeline)]
        [PSCustomObject]$TableRow,
        [object]$TableStorage
    )

    try {
        $existingRow = Get-AzureStorageTableRowByPartitionKey -Table $TableStorage -PartitionKey $TableRow.PartitionKey
        if ($null -eq $existingRow) {
            Add-StorageTableRow -Table $TableStorage -PartitionKey $TableRow.PartitionKey -RowKey $TableRow.RowKey -Property $TableRow.Properties `
            | Out-Null
        } else {
            Write-Warning "Link already exists for: $($TableRow.RowKey)"
        }
    }
    catch {
        Write-Error $_.Exception.Message
    }
}