protected void LazyLoadHandlers(LocalizationPart localizationPart) { localizationPart.CultureField.Loader(ctx => _cultureManager.GetCultureById(localizationPart.Record.CultureId)); localizationPart.MasterContentItemField.Loader(ctx => _contentManager.Get(localizationPart.Record.MasterContentItemId, localizationPart.IsPublished() ? VersionOptions.Published : VersionOptions.Latest)); }
protected void LazyLoadHandlers(LocalizationPart localizationPart) { localizationPart.CultureField.Loader(() => _cultureManager.GetCultureById(localizationPart.Record.CultureId)); localizationPart.MasterContentItemField.Loader(() => _contentManager.Get(localizationPart.Record.MasterContentItemId, VersionOptions.AllVersions)); }
protected static void PropertySetHandlers(ActivatedContentContext context, LocalizationPart localizationPart) { localizationPart.CultureField.Setter(cultureRecord => { localizationPart.Record.CultureId = cultureRecord.Id; return cultureRecord; }); localizationPart.MasterContentItemField.Setter(masterContentItem => { localizationPart.Record.MasterContentItemId = masterContentItem.ContentItem.Id; return masterContentItem; }); }
public IEnumerable <LocalizationPart> GetEditorLocalizations(LocalizationPart part) { return(_localizationService.GetLocalizations(part.ContentItem, VersionOptions.Latest) .Select(c => { var localized = c.ContentItem.As <LocalizationPart>(); if (localized.Culture == null) { localized.Culture = _lazyCultureManager.Value.GetCultureByName(GetSiteCulture()); } return c; }).ToList()); }
public IEnumerable<LocalizationPart> GetLocalizations(LocalizationPart part, VersionOptions versionOptions) { CultureRecord siteCulture = null; return new[] { (part.MasterContentItem ?? part.ContentItem).As<LocalizationPart>() } .Union(part.Id > 0 ? _localizationService.GetLocalizations(part.MasterContentItem ?? part.ContentItem, versionOptions) : new LocalizationPart[0]) .Select(c => { var localized = c.ContentItem.As<LocalizationPart>(); if (localized.Culture == null) localized.Culture = siteCulture ?? (siteCulture = _cultureManager.GetCultureByName(GetSiteCulture())); return c; }); }
private List <string> RetrieveMissingCultures(LocalizationPart part, bool excludePartCulture) { var editorLocalizations = GetEditorLocalizations(part); var cultures = _cultureManager .ListCultures() .Where(s => editorLocalizations.All(l => l.Culture.Culture != s)) .ToList(); if (excludePartCulture) { cultures.Remove(part.Culture.Culture); } return(cultures); }
/// <summary> /// This method attempts to synchronize a part across the localization set /// </summary> /// <param name="part">The part that has just been published and that we wish to use to update all corresponding parts from /// the other elements of the localization set.</param> /// <param name="localizationPart">The localization part of the ContentItem that was just published.</param> /// <param name="lSet">The localization set for the synchronization</param> private void Synchronize(ContentPart part, LocalizationPart localizationPart, List <LocalizationPart> lSet) { if (lSet.Count > 0) { var partDrivers = _partDrivers.Where(cpd => cpd.GetPartInfo().FirstOrDefault().PartName == part.PartDefinition.Name); //use cloning foreach (var target in lSet.Select(lp => lp.ContentItem)) { var context = new CloneContentContext(localizationPart.ContentItem, target); partDrivers.Invoke(driver => driver.Cloning(context), context.Logger); partDrivers.Invoke(driver => driver.Cloned(context), context.Logger); } } }
protected override bool ConsiderProductValid(IContent prod, BundlePart part) { var settings = part.TypePartDefinition.Settings.GetModel <BundleProductLocalizationSettings>(); var bundleProductQuantities = part.ProductQuantities.ToDictionary(pq => pq.ProductId, pq => pq.Quantity); LocalizationPart locPart = part.ContentItem.As <LocalizationPart>(); return //Conditions under which we consider the product for the bundle: (!settings.HideProductsFromEditor || //if we should not hide away products, they are all fine !_bundleProductLocalizationServices.ValidLocalizationPart(locPart) || //if the bundle does not have a valid LocalizationPart, I have no criteria to hide products bundleProductQuantities.ContainsKey(prod.ContentItem.Id) || //the product is in the bundle, so don't hide it ( settings.HideProductsFromEditor && //should hide products in the "wrong" culture !_bundleProductLocalizationServices.HasDifferentCulture(prod, locPart) //keep the products whose culture is not "wrong" )); }
/// <summary> /// This method attempts to synchronize a field across the localization set /// </summary> /// <param name="field">The field that has just been published and that we wish to use to update all corresponding parts from /// the other elements of the localization set.</param> /// <param name="localizationPart">The localization part of the ContentItem that was just published.</param> /// <param name="lSet">The localization set for the synchronization</param> private void Synchronize(ContentField field, LocalizationPart localizationPart, List <LocalizationPart> lSet) { if (lSet.Count > 0) { var fieldDrivers = _fieldDrivers.Where(cfd => cfd.GetFieldInfo().FirstOrDefault().FieldTypeName == field.FieldDefinition.Name); //use cloning foreach (var target in lSet.Select(lp => lp.ContentItem)) { var context = new CloneContentContext(localizationPart.ContentItem, target); context.FieldName = field.Name; fieldDrivers.Invoke(driver => driver.Cloning(context), context.Logger); fieldDrivers.Invoke(driver => driver.Cloned(context), context.Logger); } } }
public IEnumerable <LocalizationPart> GetLocalizations(LocalizationPart part, VersionOptions versionOptions) { CultureRecord siteCulture = null; return(new[] { (part.MasterContentItem ?? part.ContentItem).As <LocalizationPart>() } .Union(part.Id > 0 ? _localizationService.GetLocalizations(part.MasterContentItem ?? part.ContentItem, versionOptions) : new LocalizationPart[0]) .Select(c => { var localized = c.ContentItem.As <LocalizationPart>(); if (localized.Culture == null) { localized.Culture = siteCulture ?? (siteCulture = _cultureManager.GetCultureByName(GetSiteCulture())); } return c; })); }
private Func <ProductAttributePart, AttributeIdPair> NewAttributeIdSelector(LocalizationPart locPart) { return(pap => { var ci = pap.ContentItem; if (_localizationService.GetContentCulture(ci) == locPart.Culture.Culture) { //this attribute is fine return new AttributeIdPair(ci.Id, ci.Id); } var localized = _localizationService.GetLocalizations(ci) .FirstOrDefault(lp => lp.Culture == locPart.Culture); return localized == null ? new AttributeIdPair(ci.Id, -ci.Id) : //negative id where we found no localization new AttributeIdPair(ci.Id, localized.Id); }); }
protected override DriverResult Editor(ProductAttributesPart part, dynamic shapeHelper) { //check the settings List <ProductAttributePart> toHide = new List <ProductAttributePart>(); //hide these attributes List <ProductAttributePart> toMark = new List <ProductAttributePart>(); //write the culture for these attributes LocalizationPart locPart = part.ContentItem.As <LocalizationPart>(); if (locPart != null && locPart.Culture != null && !string.IsNullOrWhiteSpace(locPart.Culture.Culture)) { Func <ProductAttributePart, bool> HasDifferentCulture = pap => { var lP = pap.ContentItem.As <LocalizationPart>(); return(lP != null && //has a LocalizationPart AND (lP.Culture == null || //culture undefined OR (string.IsNullOrWhiteSpace(lP.Culture.Culture) || //culture undefined OR (lP.Culture != locPart.Culture)))); //culture different than the product's }; //We need to have selected a product's culture var settings = part.TypePartDefinition.Settings.GetModel <ProductAttributeLocalizationSettings>(); if (settings.HideAttributesFromEditor) { //check the attributes and tell to the view which ones to hide away toHide.AddRange(_attributeService.Attributes .Where(pap => !part.AttributeIds.Contains(pap.Id)) //we don't hide the attributes that are selected .Where(HasDifferentCulture)); toMark.AddRange(_attributeService.GetAttributes(part.AttributeIds) .Where(HasDifferentCulture)); } else { toMark.AddRange(_attributeService.Attributes .Where(HasDifferentCulture)); } } return(ContentShape( "Parts_ProductAttributes_Edit", () => shapeHelper.EditorTemplate( TemplateName: "Parts/AttributeLocalizationProductAttributes", Prefix: Prefix, Model: new AttributeLocalizationProductAttributesPartEditViewModel { Prefix = Prefix, Part = part, AttributesToHide = toHide, AttributesToMark = toMark }))); }
public IEnumerable <Tuple <string, int> > GetLocalizationsIds(ContentItem item) { CultureRecord siteCulture = null; LocalizationPart part = item.As <LocalizationPart>(); if (part != null) { return(new[] { (part.MasterContentItem ?? item).As <LocalizationPart>() } .Union(part.Id > 0 ? _localizationService.GetLocalizations(part.MasterContentItem ?? item, VersionOptions.Published) : new LocalizationPart[0]) .Select(c => { var localized = c.ContentItem.As <LocalizationPart>(); if (localized.Culture == null) { localized.Culture = siteCulture ?? (siteCulture = _lazyCultureManager.Value.GetCultureByName(GetSiteCulture())); } return new Tuple <string, int>(localized.Culture.Culture, localized.Id); })); } return(new[] { new Tuple <string, int>(GetSiteCulture(), item.Id) }); // new Tuple<string, int>[0] }
private IEnumerable <LocalizationPart> GetEditorLocalizations(LocalizationPart part) { return(_localizationService.GetLocalizations(part.ContentItem, VersionOptions.Latest) .Where(c => c.Culture != null) .ToList()); }
public IEnumerable <IContent> GetProductsInTheWrongCulture(BundlePart bundlePart, LocalizationPart locPart) { return(bundlePart.ProductIds .Select(pid => _contentManager.Get(pid)) .Where(WrongCulturePredicate(locPart))); }
public IEnumerable <IContent> GetProductsInTheWrongCulture(IEnumerable <int> productIds, LocalizationPart locPart) { return(productIds .Select(id => _contentManager.Get(id)) .Where(WrongCulturePredicate(locPart))); }
private Func <ProductQuantity, ProductQuantityPair> TranslatedProductSelector(LocalizationPart locPart) { return(pq => { var ci = _contentManager.Get(pq.ProductId); if (_localizationService.GetContentCulture(ci) == locPart.Culture.Culture) { //this product is fine return new ProductQuantityPair(pq, pq.ProductId); } var localized = _localizationService.GetLocalizations(ci) .FirstOrDefault(lp => lp.Culture == locPart.Culture); if (localized == null) //found no localization { return new ProductQuantityPair(pq, -pq.ProductId); } return new ProductQuantityPair(pq, localized.Id); }); }
public ActionResult TranslatePOST(int id, Action <ContentItem> conditionallyPublish) { var contentItem = _contentManager.Get(id, VersionOptions.Latest); if (contentItem == null) { return(HttpNotFound()); } var model = new AddLocalizationViewModel(); TryUpdateModel(model); ContentItem contentItemTranslation; var existingTranslation = _localizationService.GetLocalizedContentItem(contentItem, model.SelectedCulture); if (existingTranslation != null) { // edit existing contentItemTranslation = _contentManager.Get(existingTranslation.ContentItem.Id, VersionOptions.DraftRequired); } else { // create contentItemTranslation = _contentManager.New(contentItem.ContentType); if (contentItemTranslation.Has <ICommonPart>() && contentItem.Has <ICommonPart>()) { contentItemTranslation.As <ICommonPart>().Container = contentItem.As <ICommonPart>().Container; } _contentManager.Create(contentItemTranslation, VersionOptions.Draft); } model.Content = _contentManager.UpdateEditor(contentItemTranslation, this); if (!ModelState.IsValid) { Services.TransactionManager.Cancel(); model.SiteCultures = _cultureManager.ListCultures().Where(s => s != _localizationService.GetContentCulture(contentItem) && s != _cultureManager.GetSiteCulture()); var culture = contentItem.As <LocalizationPart>().Culture; if (culture != null) { culture.Culture = null; } model.Content = _contentManager.BuildEditor(contentItem); return(View(model)); } if (existingTranslation != null) { Services.Notifier.Information(T("Edited content item translation.")); } else { LocalizationPart localized = contentItemTranslation.As <LocalizationPart>(); localized.MasterContentItem = contentItem; if (!string.IsNullOrWhiteSpace(model.SelectedCulture)) { localized.Culture = _cultureManager.GetCultureByName(model.SelectedCulture); } conditionallyPublish(contentItemTranslation); Services.Notifier.Information(T("Created content item translation.")); } var metadata = _contentManager.GetItemMetadata(model.Content.ContentItem); //todo: (heskew) if null, redirect to somewhere better than nowhere return(metadata.EditorRouteValues == null ? null : RedirectToRoute(metadata.EditorRouteValues)); }
public IEnumerable <ProductQuantityPair> GetLocalizationIdPairs(BundlePart bundlePart, LocalizationPart locPart) { return(bundlePart.ProductQuantities .Select(TranslatedProductSelector(locPart))); }
protected override void UpdateEditorShape(UpdateEditorContext context) { base.UpdateEditorShape(context); //Here we implement localization logic LocalizationPart locPart = context.Content.As <LocalizationPart>(); ProductAttributesPart attributesPart = context.ContentItem.As <ProductAttributesPart>(); if (locPart != null && attributesPart != null) { if (attributesPart.AttributeIds.Count() > 0) { var settings = attributesPart.TypePartDefinition.Settings.GetModel <ProductAttributeLocalizationSettings>(); if (settings.TryToLocalizeAttributes) { //try to replace attributes with their correct localization //newAttributesIds is IEnumerable<AttributeIdPair>. //newAttributesIds.OriginalId is the attribute id in the initial ProductAttributesPart //newAttributesIds.NewId is the attribute id after localization (<0 if no localization is found) var newAttributeIds = _productAttributeLocalizationServices.GetLocalizationIdPairs(attributesPart, locPart); if (newAttributeIds.Any(ni => ni.NewId < 0)) { if (settings.RemoveAttributesWithoutLocalization) { //remove the items for which we could not find a localization _orchardServices.Notifier.Warning(T( "We could not find a correct localization for the following attributes, so they were removed from this product: {0}", string.Join(", ", newAttributeIds.Where(ni => ni.NewId < 0) .Select(ni => DisplayTextFromId(ni.OriginalId) ) ) )); newAttributeIds = newAttributeIds.Where(tup => tup.NewId > 0); } else { //negative Ids are made positive again newAttributeIds = newAttributeIds.Select(ni => ni = new AttributeIdPair(ni.OriginalId, Math.Abs(ni.NewId))); } } //replace the ids attributesPart.AttributeIds = newAttributeIds.Select(ni => ni.NewId).Distinct(); if (newAttributeIds.Where(ni => ni.OriginalId != ni.NewId).Any()) { _orchardServices.Notifier.Warning(T( "The following attributes where replaced by their correct localization: {0}", string.Join(", ", newAttributeIds.Where(ni => ni.OriginalId != ni.NewId) .Select(ni => DisplayTextFromId(ni.OriginalId) ) ) )); } } if (settings.AssertAttributesHaveSameCulture) { //verify that all the attributes are in the same culture as the product var badAttributes = _productAttributeLocalizationServices.GetAttributesInTheWrongCulture(attributesPart, locPart); if (badAttributes.Any()) { context.Updater.AddModelError("", T("Some of the attributes have the wrong culture: {0}", string.Join(", ", badAttributes.Select(ba => _contentManager.GetItemMetadata(ba).DisplayText)) )); } } } } }
public ContentLocalizationsViewModel(LocalizationPart part) { MasterId = part.MasterContentItem != null ? part.MasterContentItem.ContentItem.Id : part.Id; }
public IEnumerable <ProductAttributePart> GetAttributesInTheWrongCulture(ProductAttributesPart attributesPart, LocalizationPart locPart) { return(_attributeService.GetAttributes(attributesPart.AttributeIds) .Where(WrongCulturePredicate(locPart))); }
public IEnumerable <AttributeIdPair> GetLocalizationIdPairs(ProductAttributesPart attributesPart, LocalizationPart locPart) { return(_attributeService.GetAttributes(attributesPart.AttributeIds) .Select(NewAttributeIdSelector(locPart))); }
public IEnumerable <ProductQuantityPair> GetLocalizationIdPairs(IEnumerable <ProductQuantity> originalProducts, LocalizationPart locPart) { return(originalProducts.Select(TranslatedProductSelector(locPart))); }
public bool ValidLocalizationPart(LocalizationPart part) { return(part != null && part.Culture != null && !string.IsNullOrWhiteSpace(part.Culture.Culture)); }