diff --git a/src/Orchard.Web/Core/Localization/Models/Localized.cs b/src/Orchard.Web/Core/Localization/Models/Localized.cs index 8c6165b2e..897b5bc99 100644 --- a/src/Orchard.Web/Core/Localization/Models/Localized.cs +++ b/src/Orchard.Web/Core/Localization/Models/Localized.cs @@ -5,5 +5,16 @@ namespace Orchard.Core.Localization.Models { public sealed class Localized : ContentPart { [HiddenInput(DisplayValue = false)] public int Id { get { return ContentItem.Id; } } + + public int CultureId { + get { return Record.CultureId; } + set { Record.CultureId = value; } + } + + public int MasterContentItemId { + get { return Record.MasterContentItemId; } + set { Record.MasterContentItemId = value; } + } + } } diff --git a/src/Orchard.Web/Core/Localization/Models/LocalizedRecord.cs b/src/Orchard.Web/Core/Localization/Models/LocalizedRecord.cs index 0e45ddf94..cab717d5f 100644 --- a/src/Orchard.Web/Core/Localization/Models/LocalizedRecord.cs +++ b/src/Orchard.Web/Core/Localization/Models/LocalizedRecord.cs @@ -2,5 +2,7 @@ namespace Orchard.Core.Localization.Models { public class LocalizedRecord : ContentPartRecord { + public virtual int CultureId { get; set; } + public virtual int MasterContentItemId { get; set; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Localization/Services/ContentItemLocalizationService.cs b/src/Orchard.Web/Core/Localization/Services/ContentItemLocalizationService.cs new file mode 100644 index 000000000..e89f0820e --- /dev/null +++ b/src/Orchard.Web/Core/Localization/Services/ContentItemLocalizationService.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; +using Orchard.ContentManagement; +using Orchard.Core.Localization.Models; +using Orchard.Localization.Services; +using Localized = Orchard.Core.Localization.Models.Localized; + +namespace Orchard.Core.Localization.Services { + [UsedImplicitly] + public class ContentItemLocalizationService : IContentItemLocalizationService { + private readonly IContentManager _contentManager; + private readonly ICultureManager _cultureManager; + + public ContentItemLocalizationService(IContentManager contentManager, ICultureManager cultureManager) { + _contentManager = contentManager; + _cultureManager = cultureManager; + } + + public IEnumerable Get() { + return _contentManager.Query().List(); + } + + public Localized Get(int localizedId) { + return _contentManager.Get(localizedId); + } + + public Localized GetLocalizationForCulture(int masterId, string cultureName) { + var cultures = _cultureManager.ListCultures(); + if (cultures.Contains(cultureName)) { + int cultureId = _cultureManager.GetCultureIdByName(cultureName); + if (cultureId != 0) { + return _contentManager.Query() + .Where(x => x.MasterContentItemId == masterId && x.CultureId == cultureId).List().FirstOrDefault(); + } + } + return null; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Localization/Services/IContentItemLocalizationService.cs b/src/Orchard.Web/Core/Localization/Services/IContentItemLocalizationService.cs index 974034aa4..87a5eb993 100644 --- a/src/Orchard.Web/Core/Localization/Services/IContentItemLocalizationService.cs +++ b/src/Orchard.Web/Core/Localization/Services/IContentItemLocalizationService.cs @@ -1,4 +1,10 @@ -namespace Orchard.Core.Localization.Services { +using System.Collections.Generic; +using Orchard.Core.Localization.Models; + +namespace Orchard.Core.Localization.Services { public interface IContentItemLocalizationService : IDependency { + IEnumerable Get(); + Localized Get(int localizedId); + Localized GetLocalizationForCulture(int masterId, string cultureName); } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index 69d4363bf..340d874ec 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -121,6 +121,7 @@ + diff --git a/src/Orchard/Localization/Services/DefaultCultureManager.cs b/src/Orchard/Localization/Services/DefaultCultureManager.cs index 36a48f445..ae77a121c 100644 --- a/src/Orchard/Localization/Services/DefaultCultureManager.cs +++ b/src/Orchard/Localization/Services/DefaultCultureManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text.RegularExpressions; using System.Web; @@ -46,6 +47,13 @@ namespace Orchard.Localization.Services { return String.Empty; } + public int GetCultureIdByName(string cultureName) { + if (!IsValidCulture(cultureName)) { + throw new ArgumentException("cultureName"); + } + return _cultureRepository.Get(x => x.Culture == cultureName).Id; + } + // "" or // "-" or // "--" diff --git a/src/Orchard/Localization/Services/ICultureManager.cs b/src/Orchard/Localization/Services/ICultureManager.cs index 2a55558f1..53c7d13af 100644 --- a/src/Orchard/Localization/Services/ICultureManager.cs +++ b/src/Orchard/Localization/Services/ICultureManager.cs @@ -6,5 +6,6 @@ namespace Orchard.Localization.Services { IEnumerable ListCultures(); void AddCulture(string cultureName); string GetCurrentCulture(HttpContext requestContext); + int GetCultureIdByName(string cultureName); } }