2010-07-09 14:14:17 -07:00
|
|
|
|
using System;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using System.Linq;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-07-23 00:59:12 -07:00
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using Orchard.Core.Localization.Models;
|
|
|
|
|
using Orchard.Core.Localization.Services;
|
|
|
|
|
using Orchard.Core.Localization.ViewModels;
|
2010-07-24 01:03:56 -07:00
|
|
|
|
using Orchard.Core.Routable.Models;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
using Orchard.Localization;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
using Orchard.Localization.Services;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
using Orchard.Mvc.Results;
|
|
|
|
|
using Orchard.Mvc.ViewModels;
|
2010-07-23 00:59:12 -07:00
|
|
|
|
using Orchard.UI.Notify;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Localization.Controllers {
|
2010-07-12 03:03:18 -07:00
|
|
|
|
[ValidateInput(false)]
|
2010-07-09 14:14:17 -07:00
|
|
|
|
public class AdminController : Controller, IUpdateModel {
|
|
|
|
|
private readonly IContentManager _contentManager;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
private readonly ICultureManager _cultureManager;
|
|
|
|
|
private readonly ILocalizationService _localizationService;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
2010-07-12 03:03:18 -07:00
|
|
|
|
public AdminController(IOrchardServices orchardServices, IContentManager contentManager, ICultureManager cultureManager, ILocalizationService localizationService) {
|
2010-07-09 14:14:17 -07:00
|
|
|
|
_contentManager = contentManager;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
_cultureManager = cultureManager;
|
|
|
|
|
_localizationService = localizationService;
|
2010-07-13 02:52:02 -07:00
|
|
|
|
T = NullLocalizer.Instance;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
Services = orchardServices;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 02:52:02 -07:00
|
|
|
|
public Localizer T { get; set; }
|
2010-07-09 14:14:17 -07:00
|
|
|
|
public IOrchardServices Services { get; set; }
|
|
|
|
|
|
2010-07-12 03:03:18 -07:00
|
|
|
|
public ActionResult Translate(int id, string to) {
|
2010-07-09 14:14:17 -07:00
|
|
|
|
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
|
|
|
|
|
2010-07-20 11:45:44 -07:00
|
|
|
|
// only support translations from the site culture, at the moment at least
|
2010-07-09 14:14:17 -07:00
|
|
|
|
if (contentItem == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
2010-07-23 00:22:13 -07:00
|
|
|
|
if (!contentItem.Is<LocalizationPart>() || contentItem.As<LocalizationPart>().MasterContentItem != null) {
|
2010-07-20 11:45:44 -07:00
|
|
|
|
var metadata = _contentManager.GetItemMetadata(contentItem);
|
|
|
|
|
return RedirectToAction(Convert.ToString(metadata.EditorRouteValues["action"]), metadata.EditorRouteValues);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 04:44:54 -07:00
|
|
|
|
var siteCultures = _cultureManager.ListCultures().Where(s => s != _localizationService.GetContentCulture(contentItem) && s != _cultureManager.GetSiteCulture());
|
2010-07-24 01:03:56 -07:00
|
|
|
|
var selectedCulture = siteCultures.SingleOrDefault(s => string.Equals(s, to, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
?? _cultureManager.GetCurrentCulture(HttpContext); // could be null but the person doing the translating might be translating into their current culture
|
|
|
|
|
|
|
|
|
|
//todo: need a better solution for modifying some parts when translating - or go with a completely different experience
|
|
|
|
|
if (contentItem.Has<RoutePart>()) {
|
|
|
|
|
var routePart = contentItem.As<RoutePart>();
|
2010-07-26 04:44:54 -07:00
|
|
|
|
routePart.Slug = string.Format("{0}{2}{1}", routePart.Slug, siteCultures.Any(s => string.Equals(s, selectedCulture, StringComparison.OrdinalIgnoreCase)) ? selectedCulture : "", !string.IsNullOrWhiteSpace(routePart.Slug) ? "-" : "");
|
2010-07-24 01:03:56 -07:00
|
|
|
|
routePart.Path = null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 04:44:54 -07:00
|
|
|
|
contentItem.As<LocalizationPart>().Culture.Culture = null;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
var model = new AddLocalizationViewModel {
|
2010-07-09 14:14:17 -07:00
|
|
|
|
Id = id,
|
2010-07-24 01:03:56 -07:00
|
|
|
|
SelectedCulture = selectedCulture,
|
2010-07-12 03:03:18 -07:00
|
|
|
|
SiteCultures = siteCultures,
|
2010-07-09 14:14:17 -07:00
|
|
|
|
Content = _contentManager.BuildEditorModel(contentItem)
|
|
|
|
|
};
|
2010-07-24 01:03:56 -07:00
|
|
|
|
Services.TransactionManager.Cancel();
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Translate")]
|
2010-07-13 10:14:46 -07:00
|
|
|
|
public ActionResult TranslatePOST(int id) {
|
2010-07-13 02:52:02 -07:00
|
|
|
|
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
if (contentItem == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
2010-07-12 03:03:18 -07:00
|
|
|
|
var viewModel = new AddLocalizationViewModel();
|
|
|
|
|
TryUpdateModel(viewModel);
|
|
|
|
|
|
2010-07-26 04:44:54 -07:00
|
|
|
|
ContentItem contentItemTranslation = null;
|
2010-07-12 03:03:18 -07:00
|
|
|
|
var existingTranslation = _localizationService.GetLocalizedContentItem(contentItem, viewModel.SelectedCulture);
|
2010-07-26 04:44:54 -07:00
|
|
|
|
if (existingTranslation != null) {
|
|
|
|
|
// edit existing
|
2010-07-13 02:52:02 -07:00
|
|
|
|
contentItemTranslation = _contentManager.Get(existingTranslation.ContentItem.Id, VersionOptions.DraftRequired);
|
2010-07-12 03:03:18 -07:00
|
|
|
|
}
|
2010-07-26 04:44:54 -07:00
|
|
|
|
else {
|
|
|
|
|
// create
|
2010-07-12 03:03:18 -07:00
|
|
|
|
contentItemTranslation = _contentManager.New(contentItem.ContentType);
|
2010-07-23 00:22:13 -07:00
|
|
|
|
var localized = contentItemTranslation.As<LocalizationPart>();
|
2010-07-12 03:03:18 -07:00
|
|
|
|
localized.MasterContentItem = contentItem;
|
2010-07-26 04:44:54 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(viewModel.SelectedCulture))
|
|
|
|
|
localized.Culture = _cultureManager.GetCultureByName(viewModel.SelectedCulture);
|
2010-07-12 03:03:18 -07:00
|
|
|
|
_contentManager.Create(contentItemTranslation, VersionOptions.Draft);
|
2010-07-23 00:59:12 -07:00
|
|
|
|
|
|
|
|
|
if (!contentItem.Has<IPublishingControlAspect>() && contentItem.VersionRecord != null && contentItem.VersionRecord.Published) {
|
|
|
|
|
_contentManager.Publish(contentItemTranslation);
|
|
|
|
|
}
|
2010-07-12 03:03:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 04:44:54 -07:00
|
|
|
|
viewModel.Content = _contentManager.UpdateEditorModel(contentItemTranslation, this);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
Services.TransactionManager.Cancel();
|
2010-07-26 04:44:54 -07:00
|
|
|
|
viewModel.SiteCultures = _cultureManager.ListCultures().Where(s => s != _localizationService.GetContentCulture(contentItem) && s != _cultureManager.GetSiteCulture());
|
|
|
|
|
contentItem.As<LocalizationPart>().Culture.Culture = null;
|
|
|
|
|
viewModel.Content = _contentManager.BuildEditorModel(contentItem);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
PrepareEditorViewModel(viewModel.Content);
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
2010-07-12 03:03:18 -07:00
|
|
|
|
|
2010-07-26 04:44:54 -07:00
|
|
|
|
Services.Notifier.Information(T("Created content item translation."));
|
2010-07-23 00:59:12 -07:00
|
|
|
|
|
2010-07-09 14:14:17 -07:00
|
|
|
|
var metadata = _contentManager.GetItemMetadata(viewModel.Content.Item);
|
|
|
|
|
if (metadata.EditorRouteValues == null)
|
2010-07-12 03:03:18 -07:00
|
|
|
|
return null; //todo: (heskew) redirect to somewhere better than nowhere
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
return RedirectToRoute(metadata.EditorRouteValues);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void PrepareEditorViewModel(ContentItemViewModel itemViewModel) {
|
|
|
|
|
if (string.IsNullOrEmpty(itemViewModel.TemplateName)) {
|
|
|
|
|
itemViewModel.TemplateName = "Items/Contents.Item";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
|
|
|
|
|
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
|
|
|
|
|
ModelState.AddModelError(key, errorMessage.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|