2010-07-14 11:34:19 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using JetBrains.Annotations;
|
2010-07-14 11:34:19 -07:00
|
|
|
|
using Orchard.ContentManagement;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
2010-11-18 16:01:09 -08:00
|
|
|
|
using Orchard.Localization.Models;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using Orchard.Localization.Services;
|
2010-11-18 16:01:09 -08:00
|
|
|
|
using Orchard.Localization.ViewModels;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
2010-11-18 16:01:09 -08:00
|
|
|
|
namespace Orchard.Localization.Drivers {
|
2010-07-09 14:14:17 -07:00
|
|
|
|
[UsedImplicitly]
|
2010-07-23 00:22:13 -07:00
|
|
|
|
public class LocalizationPartDriver : ContentPartDriver<LocalizationPart> {
|
2010-07-20 11:45:44 -07:00
|
|
|
|
private const string TemplatePrefix = "Localization";
|
2010-07-12 03:03:18 -07:00
|
|
|
|
private readonly ICultureManager _cultureManager;
|
|
|
|
|
private readonly ILocalizationService _localizationService;
|
2011-03-14 17:36:33 -07:00
|
|
|
|
private readonly IContentManager _contentManager;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
|
2011-03-14 17:36:33 -07:00
|
|
|
|
public LocalizationPartDriver(ICultureManager cultureManager, ILocalizationService localizationService, IContentManager contentManager) {
|
2010-07-12 03:03:18 -07:00
|
|
|
|
_cultureManager = cultureManager;
|
|
|
|
|
_localizationService = localizationService;
|
2011-03-14 17:36:33 -07:00
|
|
|
|
_contentManager = contentManager;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Display(LocalizationPart part, string displayType, dynamic shapeHelper) {
|
2010-10-14 11:15:22 -07:00
|
|
|
|
var masterId = part.MasterContentItem != null
|
|
|
|
|
? part.MasterContentItem.Id
|
|
|
|
|
: part.Id;
|
2010-10-13 04:51:15 -07:00
|
|
|
|
return Combined(
|
|
|
|
|
ContentShape("Parts_Localization_ContentTranslations",
|
2010-12-01 11:01:08 -08:00
|
|
|
|
() => shapeHelper.Parts_Localization_ContentTranslations(ContentPart: part, MasterId: masterId, Localizations: GetDisplayLocalizations(part, VersionOptions.Published))),
|
2010-10-13 04:51:15 -07:00
|
|
|
|
ContentShape("Parts_Localization_ContentTranslations_Summary",
|
2010-12-01 11:01:08 -08:00
|
|
|
|
() => shapeHelper.Parts_Localization_ContentTranslations_Summary(ContentPart: part, MasterId: masterId, Localizations: GetDisplayLocalizations(part, VersionOptions.Published))),
|
2010-10-19 15:44:25 -07:00
|
|
|
|
ContentShape("Parts_Localization_ContentTranslations_SummaryAdmin",
|
2010-12-01 11:01:08 -08:00
|
|
|
|
() => shapeHelper.Parts_Localization_ContentTranslations_SummaryAdmin(ContentPart: part, MasterId: masterId, Localizations: GetDisplayLocalizations(part, VersionOptions.Latest)))
|
2010-10-13 04:51:15 -07:00
|
|
|
|
);
|
2010-07-20 11:45:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Editor(LocalizationPart part, dynamic shapeHelper) {
|
2010-07-20 11:45:44 -07:00
|
|
|
|
var localizations = GetEditorLocalizations(part).ToList();
|
|
|
|
|
var model = new EditLocalizationViewModel {
|
|
|
|
|
SelectedCulture = part.Culture != null ? part.Culture.Culture : null,
|
|
|
|
|
SiteCultures = _cultureManager.ListCultures().Where(s => s != _cultureManager.GetSiteCulture() && !localizations.Select(l => l.Culture.Culture).Contains(s)),
|
2010-07-26 04:44:54 -07:00
|
|
|
|
ContentItem = part,
|
2010-07-20 11:45:44 -07:00
|
|
|
|
MasterContentItem = part.MasterContentItem,
|
|
|
|
|
ContentLocalizations = new ContentLocalizationsViewModel(part) { Localizations = localizations }
|
|
|
|
|
};
|
|
|
|
|
|
2010-10-19 00:06:47 -07:00
|
|
|
|
return ContentShape("Parts_Localization_ContentTranslations_Edit",
|
|
|
|
|
() => shapeHelper.EditorTemplate(TemplateName: "Parts/Localization.ContentTranslations.Edit", Model: model, Prefix: TemplatePrefix));
|
2010-07-20 11:45:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
protected override DriverResult Editor(LocalizationPart part, IUpdateModel updater, dynamic shapeHelper) {
|
2010-07-20 11:45:44 -07:00
|
|
|
|
var model = new EditLocalizationViewModel();
|
|
|
|
|
if (updater != null && updater.TryUpdateModel(model, TemplatePrefix, null, null)) {
|
|
|
|
|
_localizationService.SetContentCulture(part, model.SelectedCulture);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
return Editor(part, shapeHelper);
|
2010-07-20 11:45:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 11:01:08 -08:00
|
|
|
|
private IEnumerable<LocalizationPart> GetDisplayLocalizations(LocalizationPart part, VersionOptions versionOptions) {
|
|
|
|
|
return _localizationService.GetLocalizations(part.ContentItem, versionOptions)
|
2010-07-20 11:45:44 -07:00
|
|
|
|
.Select(c => {
|
2010-07-23 00:22:13 -07:00
|
|
|
|
var localized = c.ContentItem.As<LocalizationPart>();
|
2010-07-26 04:44:54 -07:00
|
|
|
|
if (localized.Culture == null)
|
|
|
|
|
localized.Culture = _cultureManager.GetCultureByName(_cultureManager.GetSiteCulture());
|
2010-07-20 11:45:44 -07:00
|
|
|
|
return c;
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 00:22:13 -07:00
|
|
|
|
private IEnumerable<LocalizationPart> GetEditorLocalizations(LocalizationPart part) {
|
2010-12-01 11:01:08 -08:00
|
|
|
|
return _localizationService.GetLocalizations(part.ContentItem, VersionOptions.Latest)
|
2010-07-26 04:44:54 -07:00
|
|
|
|
.Select(c => {
|
|
|
|
|
var localized = c.ContentItem.As<LocalizationPart>();
|
|
|
|
|
if (localized.Culture == null)
|
|
|
|
|
localized.Culture = _cultureManager.GetCultureByName(_cultureManager.GetSiteCulture());
|
|
|
|
|
return c;
|
|
|
|
|
}).ToList();
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
2011-03-14 17:36:33 -07:00
|
|
|
|
|
|
|
|
|
protected override void Exporting(LocalizationPart part, ContentManagement.Handlers.ExportContentContext context) {
|
|
|
|
|
if (part.MasterContentItem != null) {
|
|
|
|
|
var masterContentItemIdentity = _contentManager.GetItemMetadata(part.MasterContentItem).Identity;
|
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("MasterContentItem", masterContentItemIdentity.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (part.Culture != null) {
|
|
|
|
|
context.Element(part.PartDefinition.Name).SetAttributeValue("Culture", part.Culture.Culture);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
|
|
|
|
}
|