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-11-29 17:21:14 -08:00
|
|
|
|
using Orchard.Core.Contents.Controllers;
|
|
|
|
|
using Orchard.Core.Contents.Settings;
|
2010-07-24 01:03:56 -07:00
|
|
|
|
using Orchard.Core.Routable.Models;
|
2010-09-03 16:04:42 -07:00
|
|
|
|
using Orchard.DisplayManagement;
|
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-23 00:59:12 -07:00
|
|
|
|
using Orchard.UI.Notify;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
2010-11-18 16:01:09 -08:00
|
|
|
|
namespace Orchard.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-09-03 16:04:42 -07:00
|
|
|
|
public AdminController(
|
|
|
|
|
IOrchardServices orchardServices,
|
|
|
|
|
IContentManager contentManager,
|
|
|
|
|
ICultureManager cultureManager,
|
|
|
|
|
ILocalizationService localizationService,
|
2010-10-15 17:24:30 -07:00
|
|
|
|
IShapeFactory shapeFactory) {
|
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-10-15 17:24:30 -07:00
|
|
|
|
Shape = shapeFactory;
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-03 16:04:42 -07:00
|
|
|
|
dynamic Shape { get; set; }
|
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)
|
2010-10-14 11:30:58 -07:00
|
|
|
|
return HttpNotFound();
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
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-11-29 17:21:14 -08:00
|
|
|
|
|
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>()) {
|
2010-11-29 17:21:14 -08:00
|
|
|
|
RoutePart 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-27 10:19:52 -07:00
|
|
|
|
if (contentItem.As<LocalizationPart>().Culture != null)
|
|
|
|
|
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-10-11 02:06:01 -07:00
|
|
|
|
Content = _contentManager.BuildEditor(contentItem)
|
2010-07-09 14:14:17 -07:00
|
|
|
|
};
|
2010-11-29 17:21:14 -08:00
|
|
|
|
|
|
|
|
|
// Cancel transaction so that the routepart is not modified.
|
2010-07-24 01:03:56 -07:00
|
|
|
|
Services.TransactionManager.Cancel();
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
2010-09-13 15:30:00 -07:00
|
|
|
|
return View(model);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Translate")]
|
2010-11-29 17:21:14 -08:00
|
|
|
|
[FormValueRequired("submit.Save")]
|
2010-07-13 10:14:46 -07:00
|
|
|
|
public ActionResult TranslatePOST(int id) {
|
2010-11-29 17:21:14 -08:00
|
|
|
|
return TranslatePOST(id, contentItem => {
|
|
|
|
|
if (!contentItem.Has<IPublishingControlAspect>() && !contentItem.TypeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable)
|
|
|
|
|
Services.ContentManager.Publish(contentItem);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Translate")]
|
|
|
|
|
[FormValueRequired("submit.Publish")]
|
|
|
|
|
public ActionResult TranslateAndPublishPOST(int id) {
|
|
|
|
|
return TranslatePOST(id, contentItem => Services.ContentManager.Publish(contentItem));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult TranslatePOST(int id, Action<ContentItem> conditionallyPublish) {
|
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)
|
2010-10-14 11:30:58 -07:00
|
|
|
|
return HttpNotFound();
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
2010-09-03 16:04:42 -07:00
|
|
|
|
var model = new AddLocalizationViewModel();
|
|
|
|
|
TryUpdateModel(model);
|
2010-07-12 03:03:18 -07:00
|
|
|
|
|
2010-07-26 04:44:54 -07:00
|
|
|
|
ContentItem contentItemTranslation = null;
|
2010-09-03 16:04:42 -07:00
|
|
|
|
var existingTranslation = _localizationService.GetLocalizedContentItem(contentItem, model.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-11-29 17:21:14 -08:00
|
|
|
|
} else {
|
2010-07-26 04:44:54 -07:00
|
|
|
|
// create
|
2010-07-12 03:03:18 -07:00
|
|
|
|
contentItemTranslation = _contentManager.New(contentItem.ContentType);
|
2010-11-30 11:13:42 -08:00
|
|
|
|
if (contentItemTranslation.Has<ICommonPart>() && contentItem.Has<ICommonPart>()) {
|
|
|
|
|
contentItemTranslation.As<ICommonPart>().Container = contentItem.As<ICommonPart>().Container;
|
|
|
|
|
}
|
2010-11-29 17:21:14 -08:00
|
|
|
|
|
2010-07-12 03:03:18 -07:00
|
|
|
|
_contentManager.Create(contentItemTranslation, VersionOptions.Draft);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 02:06:01 -07:00
|
|
|
|
model.Content = _contentManager.UpdateEditor(contentItemTranslation, this);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
Services.TransactionManager.Cancel();
|
2010-09-03 16:04:42 -07:00
|
|
|
|
model.SiteCultures = _cultureManager.ListCultures().Where(s => s != _localizationService.GetContentCulture(contentItem) && s != _cultureManager.GetSiteCulture());
|
2010-12-06 12:26:03 -08:00
|
|
|
|
var culture = contentItem.As<LocalizationPart>().Culture;
|
|
|
|
|
if (culture != null) {
|
|
|
|
|
culture.Culture = null;
|
|
|
|
|
}
|
2010-10-11 02:06:01 -07:00
|
|
|
|
model.Content = _contentManager.BuildEditor(contentItem);
|
2010-09-13 15:30:00 -07:00
|
|
|
|
return View(model);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
}
|
2010-07-12 03:03:18 -07:00
|
|
|
|
|
2010-11-29 17:37:05 -08:00
|
|
|
|
if (existingTranslation != null) {
|
|
|
|
|
Services.Notifier.Information(T("Edited content item translation."));
|
|
|
|
|
}
|
|
|
|
|
else {
|
2010-11-30 15:00:21 -08:00
|
|
|
|
LocalizationPart localized = contentItemTranslation.As<LocalizationPart>();
|
|
|
|
|
localized.MasterContentItem = contentItem;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.SelectedCulture)) {
|
|
|
|
|
localized.Culture = _cultureManager.GetCultureByName(model.SelectedCulture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conditionallyPublish(contentItemTranslation);
|
|
|
|
|
|
2010-11-29 17:37:05 -08:00
|
|
|
|
Services.Notifier.Information(T("Created content item translation."));
|
|
|
|
|
}
|
2010-07-23 00:59:12 -07:00
|
|
|
|
|
2010-09-03 16:04:42 -07:00
|
|
|
|
var metadata = _contentManager.GetItemMetadata(model.Content);
|
2010-07-09 14:14:17 -07:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|