public void IsVortoPropertyWorks() { var vortoVal = new VortoValue(); var propHelper = new PrivateType(typeof(PropertyHelper)); var prop = new Mock <IPublishedProperty>(); prop.Setup(x => x.Value).Returns(vortoVal); prop.Setup(x => x.HasValue).Returns(true); var content = new Mock <IPublishedContent>(); content.Setup( x => x.GetProperty( It.Is <string>(y => y == "propAlias"), It.IsAny <bool>())) .Returns(prop.Object); content.Setup( x => x.GetProperty( It.Is <string>(y => y == "propAlias"))) .Returns(prop.Object); var result = (bool)propHelper.InvokeStatic( "IsVortoProperty", new object[] { content.Object, "propAlias" } ); Assert.IsTrue(result); }
/// <summary> /// The resolve value. /// </summary> /// <returns> /// The <see cref="string"/>. /// </returns> public override string ResolveValue() { IEnumerable <Language> languages = LocalizationHelper.GetInstalledLanguages(); StringBuilder stringBuilder = new StringBuilder(); VortoValue vortoValue = JsonConvert.DeserializeObject <VortoValue>(this.RawValue); string name = this.Property.Name; foreach (Language language in languages) { string iso = language.IsoCode; if (this.Content.HasVortoValue(name, iso)) { object value; // Umbraco method Parse internal links fails since we are operating on a background thread. try { value = this.Content.GetVortoValue(name, iso); } catch { value = vortoValue.Values[iso]; } stringBuilder.Append(string.Format(SearchConstants.CultureTemplate, iso, value)); } } return(stringBuilder.ToString()); }
public static string FindBestMatchCulture(this VortoValue value, string cultureName) { // Check for actual values if (value.Values == null) { return(string.Empty); } // Check for exact match if (value.Values.ContainsKey(cultureName)) { return(cultureName); } // Close match return(cultureName.Length == 2 ? value.Values.Keys.FirstOrDefault(x => x.StartsWith(cultureName + "-")) : string.Empty); }
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { var propertyValue = property?.Value?.ToString(); if (propertyValue.IsNullOrWhiteSpace()) { return(string.Empty); } // Something weird is happening in core whereby ConvertDbToEditor is getting // called loads of times on publish, forcing the property value to get converted // again, which in tern screws up the values. To get round it, we create a // dummy property copying the original properties value, this way not overwriting // the original property value allowing it to be re-converted again later var prop2 = new Property(propertyType, property.Value); try { VortoValue value = null; // Does the value look like JSON and does it look like a vorto value? if (propertyValue.DetectIsJson() && propertyValue.IndexOf("dtdGuid") != -1) { value = JsonConvert.DeserializeObject <VortoValue>(propertyValue); } else { // Doesn't look like a vorto value so we are going to assume it got converted // from a normal prop editor to a vorto editor, so lets construct a VortoValue var dataTypeDef = dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId); string primaryLanguage = null; // Look for primary language in prevalues var preValues = dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDef.Id)?.PreValuesAsDictionary; if (preValues != null) { // We need to store the current value inder a language key so try and find the best key to store it under primaryLanguage = preValues.ContainsKey("primaryLanguage") && !preValues["primaryLanguage"].Value.IsNullOrWhiteSpace() ? preValues["primaryLanguage"].Value : null; } // No explicit primary language set, so try and work out the best match if (primaryLanguage.IsNullOrWhiteSpace()) { var currentCulture = Thread.CurrentThread.CurrentUICulture.Name; var languages = umbraco.cms.businesslogic.language.Language.GetAllAsList() .Select(x => x.CultureAlias) .ToList(); // Check for an exact culture match primaryLanguage = languages.FirstOrDefault(x => x == currentCulture); // Check for a close match if (primaryLanguage.IsNullOrWhiteSpace()) { primaryLanguage = languages.FirstOrDefault(x => x.Contains(currentCulture)); } // Check for a close match if (primaryLanguage.IsNullOrWhiteSpace()) { primaryLanguage = languages.FirstOrDefault(x => currentCulture.Contains(x)); } // Couldn't find a good enough match, just select the first language if (primaryLanguage.IsNullOrWhiteSpace()) { primaryLanguage = languages.FirstOrDefault(); } } if (!primaryLanguage.IsNullOrWhiteSpace()) { value = new VortoValue { DtdGuid = dataTypeDef.Key, Values = new Dictionary <string, object> { { primaryLanguage, property.Value } } }; } } if (value?.Values != null) { var dtd = VortoHelper.GetTargetDataTypeDefinition(value.DtdGuid); if (dtd != null) { var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias); var propType = new PropertyType(dtd); var keys = value.Values.Keys.ToArray(); foreach (var key in keys) { var prop = new Property(propType, value.Values[key] == null ? null : value.Values[key].ToString()); var newValue = propEditor.ValueEditor.ConvertDbToEditor(prop, propType, dataTypeService); value.Values[key] = (newValue == null) ? null : JToken.FromObject(newValue); } prop2.Value = JsonConvert.SerializeObject(value); } else { LogHelper.Error <VortoPropertyValueEditor>($"Unabled to locate target DTD for source DTD ${value.DtdGuid}", null); } } } catch (Exception ex) { LogHelper.Error <VortoPropertyValueEditor>("Error converting DB value to Editor", ex); } return(base.ConvertDbToEditor(prop2, propertyType, dataTypeService)); }