2010-07-14 11:34:19 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using System.Web;
|
|
|
|
|
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-07-21 17:17:13 -07:00
|
|
|
|
using Orchard.Core.ContentsLocation.Models;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
using Orchard.Core.Localization.Models;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using Orchard.Core.Localization.Services;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
using Orchard.Core.Localization.ViewModels;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using Orchard.Localization.Services;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Localization.Drivers {
|
|
|
|
|
[UsedImplicitly]
|
|
|
|
|
public class LocalizationDriver : ContentPartDriver<Localized> {
|
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;
|
|
|
|
|
|
2010-07-20 11:45:44 -07:00
|
|
|
|
public LocalizationDriver(ICultureManager cultureManager, ILocalizationService localizationService) {
|
2010-07-12 03:03:18 -07:00
|
|
|
|
_cultureManager = cultureManager;
|
|
|
|
|
_localizationService = localizationService;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Display(Localized part, string displayType) {
|
2010-07-12 03:03:18 -07:00
|
|
|
|
var model = new ContentLocalizationsViewModel(part) {
|
2010-07-20 11:45:44 -07:00
|
|
|
|
Localizations = GetDisplayLocalizations(part)
|
2010-07-12 03:03:18 -07:00
|
|
|
|
};
|
2010-07-20 11:45:44 -07:00
|
|
|
|
|
2010-07-21 21:28:47 -07:00
|
|
|
|
return ContentPartTemplate(model, "Parts/Localization.ContentTranslations", TemplatePrefix).LongestMatch(displayType, "Summary", "SummaryAdmin").Location(part.GetLocation(displayType));
|
2010-07-20 11:45:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(Localized part) {
|
|
|
|
|
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)),
|
|
|
|
|
MasterContentItem = part.MasterContentItem,
|
|
|
|
|
ContentLocalizations = new ContentLocalizationsViewModel(part) { Localizations = localizations }
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-21 21:28:47 -07:00
|
|
|
|
return ContentPartTemplate(model, "Parts/Localization.Translation", TemplatePrefix).Location(part.GetLocation("Editor"));
|
2010-07-20 11:45:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DriverResult Editor(Localized part, IUpdateModel updater) {
|
|
|
|
|
var model = new EditLocalizationViewModel();
|
|
|
|
|
if (updater != null && updater.TryUpdateModel(model, TemplatePrefix, null, null)) {
|
|
|
|
|
_localizationService.SetContentCulture(part, model.SelectedCulture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Editor(part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<Localized> GetDisplayLocalizations(Localized part) {
|
|
|
|
|
return _localizationService.GetLocalizations(part.ContentItem)
|
|
|
|
|
.Select(c => {
|
|
|
|
|
var localized = c.ContentItem.As<Localized>();
|
|
|
|
|
if (localized.Culture == null) {
|
|
|
|
|
localized.Culture = _cultureManager.GetCultureByName(_cultureManager.GetCurrentCulture(new HttpContextWrapper(HttpContext.Current)));
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<Localized> GetEditorLocalizations(Localized part) {
|
|
|
|
|
return _localizationService.GetLocalizations(part.ContentItem)
|
|
|
|
|
.Select(c => c.ContentItem.As<Localized>())
|
|
|
|
|
.Where(l => l.MasterContentItem != null).ToList();
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|