Beispiel #1
0
        /// <summary>
        /// Convert ContentPickerAlias data types with the given name
        ///
        /// Converts the content from ids to UDIs
        /// Swaps the data type alias itself over
        /// </summary>
        /// <param name="name"></param>
        public void Convert(string name)
        {
            var oldDataTypeDefinition = _dataTypeService.GetDataTypeDefinitionByName(name);

            oldDataTypeDefinition.Name = oldDataTypeDefinition.Name + " (Obsolete)";
            _dataTypeService.Save(oldDataTypeDefinition);

            var oldPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(oldDataTypeDefinition.Id);

            var newDataTypeDefinition = new DataTypeDefinition(_newDataTypeAlias);

            newDataTypeDefinition.Name = name;
            _dataTypeService.SaveDataTypeAndPreValues(newDataTypeDefinition, _preValueConversion == null ? new Dictionary <string, PreValue>() : _preValueConversion(oldPreValues.PreValuesAsDictionary));

            var allContentTypes       = _contentTypeService.GetAllContentTypes();
            var contentTypesToConvert = allContentTypes
                                        .Where(c =>
                                               c.PropertyTypes.Any(a => a.DataTypeDefinitionId == oldDataTypeDefinition.Id) ||
                                               c.CompositionPropertyTypes.Any(a => a.DataTypeDefinitionId == oldDataTypeDefinition.Id))
                                        .ToArray();

            AddReplacementDataType(contentTypesToConvert, oldDataTypeDefinition, newDataTypeDefinition);
            ConvertContent(contentTypesToConvert, oldDataTypeDefinition);
            DeleteOldDataType(oldDataTypeDefinition);
        }
        private void AddToPanelPickerAllowedEditors()
        {
            var panelPickerDataType = _dataTypeService.GetDataTypeDefinitionByName(PanelsInstallationConstants.DataTypeNames.PanelPicker);
            var preValuesDictionary = _dataTypeService.GetPreValuesCollectionByDataTypeId(panelPickerDataType.Id).PreValuesAsDictionary;

            AddAllowedEditor(preValuesDictionary, TableEditor);
            _dataTypeService.SavePreValues(panelPickerDataType, preValuesDictionary);
        }
Beispiel #3
0
        private void AddToDefaultGridAllowedEditors()
        {
            var defaultGridDataType = _dataTypeService.GetDataTypeDefinitionByName(CoreInstallationConstants.DataTypeNames.ContentGrid);
            var preValuesDictionary = _dataTypeService.GetPreValuesCollectionByDataTypeId(defaultGridDataType.Id).PreValuesAsDictionary;

            GridInstallationHelper.AddAllowedEditorForOneColumnRow(preValuesDictionary, UserListAlias);

            _dataTypeService.SaveDataTypeAndPreValues(defaultGridDataType, preValuesDictionary);
        }
        public PreValueCollection GetDataTypeOptions(string dataTypeName)
        {
            var dataType = _dataTypeService.GetDataTypeDefinitionByName(dataTypeName);

            if (dataType == null)
            {
                return(null);
            }
            return(_dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id));
        }
Beispiel #5
0
        internal DataTypeInfo ResolveDataType(FluidityEditorFieldConfig fieldConfig)
        {
            var dtdKey = !fieldConfig.DataTypeName.IsNullOrWhiteSpace()
                ? fieldConfig.DataTypeName
                : fieldConfig.GetOrCalculateDefinititionId().ToString();

            return(_cacheProvider.GetCacheItem <DataTypeInfo>($"fluidity_datatypeinfo_{dtdKey}", () =>
            {
                IDataTypeDefinition dataTypeDefinition = null;

                if (!fieldConfig.DataTypeName.IsNullOrWhiteSpace())
                {
                    dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionByName(fieldConfig.DataTypeName);
                }

                if (dataTypeDefinition == null)
                {
                    var dataTypeId = fieldConfig.GetOrCalculateDefinititionId();
                    dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionById(dataTypeId);
                }

                var preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinition.Id);
                var propEditor = _propertyEditorResolver.GetByAlias(dataTypeDefinition.PropertyEditorAlias);

                return new DataTypeInfo(dataTypeDefinition, propEditor, preValues);
            }));
        }
        public static int?GetPrevalueIdForIMember(IMember im, string propertyAlias, string propertyValue)
        {
            if (propertyValue == "")
            {
                //Return nothing
                return(null);
            }
            else
            {
                //find the property on the content node by its alias
                Property prop = im.Properties.FirstOrDefault(a => a.Alias == propertyAlias);

                if (prop != null)
                {
                    //get data type from the property
                    IDataTypeService    dtService    = ApplicationContext.Current.Services.DataTypeService;
                    IDataTypeDefinition dtDefinition = dtService.GetDataTypeDefinitionById(prop.PropertyType.DataTypeDefinitionId);

                    //Return property value Id
                    return(dtService.GetPreValuesCollectionByDataTypeId(dtDefinition.Id).PreValuesAsDictionary.Where(d => d.Value.Value == propertyValue).Select(f => f.Value.Id).First());
                }

                //Return nothing
                return(null);
            }
        }
        internal DataTypeInfo ResolveDataType(FluidityEditorFieldConfig fieldConfig, bool isReadOnly = false)
        {
            var dtdKey = !fieldConfig.DataTypeName.IsNullOrWhiteSpace()
                ? fieldConfig.DataTypeName
                : fieldConfig.GetOrCalculateDefinititionId().ToString();

            dtdKey += $"_{isReadOnly}";

            return(_cacheProvider.GetCacheItem <DataTypeInfo>($"fluidity_datatypeinfo_{dtdKey}", () =>
            {
                IDataTypeDefinition dataTypeDefinition = null;

                if (!fieldConfig.DataTypeName.IsNullOrWhiteSpace())
                {
                    dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionByName(fieldConfig.DataTypeName);
                }

                if (dataTypeDefinition == null)
                {
                    var dataTypeId = fieldConfig.DataTypeId == 0 && isReadOnly
                        ? -92 // If readonly and no explicit datatype defined, default to label
                        : fieldConfig.GetOrCalculateDefinititionId();
                    dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionById(dataTypeId);
                }

                var preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinition.Id);
                var propEditor = _propertyEditorResolver.GetByAlias(dataTypeDefinition.PropertyEditorAlias);

                return new DataTypeInfo(dataTypeDefinition, propEditor, preValues);
            }));
        }
        private string GetMailChimpApiKeyValue()
        {
            var dataType  = _dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias("SiteOcean.MailChimp.ListPicker").First();
            var prevalues = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id);

            return(prevalues.PreValuesAsDictionary["apiKey"].Value);
        }
        /// <summary>
        /// Exports an <see cref="IDataTypeDefinition"/> item to xml as an <see cref="XElement"/>
        /// </summary>
        /// <param name="dataTypeService"></param>
        /// <param name="dataTypeDefinition">IDataTypeDefinition type to export</param>
        /// <returns><see cref="XElement"/> containing the xml representation of the IDataTypeDefinition object</returns>
        public XElement Serialize(IDataTypeService dataTypeService, IDataTypeDefinition dataTypeDefinition)
        {
            var prevalues    = new XElement("PreValues");
            var prevalueList = dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinition.Id)
                               .FormatAsDictionary();

            var sort = 0;

            foreach (var pv in prevalueList)
            {
                var prevalue = new XElement("PreValue");
                prevalue.Add(new XAttribute("Id", pv.Value.Id));
                prevalue.Add(new XAttribute("Value", pv.Value.Value ?? ""));
                prevalue.Add(new XAttribute("Alias", pv.Key));
                prevalue.Add(new XAttribute("SortOrder", sort));
                prevalues.Add(prevalue);
                sort++;
            }

            var xml = new XElement("DataType", prevalues);

            xml.Add(new XAttribute("Name", dataTypeDefinition.Name));
            //The 'ID' when exporting is actually the property editor alias (in pre v7 it was the IDataType GUID id)
            xml.Add(new XAttribute("Id", dataTypeDefinition.PropertyEditorAlias));
            xml.Add(new XAttribute("Definition", dataTypeDefinition.Key));
            xml.Add(new XAttribute("DatabaseType", dataTypeDefinition.DatabaseType.ToString()));

            return(xml);
        }
        private void Synchronize(IDataTypeDefinition[] dataTypes, DataType model)
        {
            var dataType = _dataTypeFinder.Find(model, dataTypes).SingleOrDefault();

            if (dataType == null)
            {
                // Create new data type.
                dataType = CreateDataType(model);

                _dataTypeService.SaveDataTypeAndPreValues(dataType, GetPreValues(model));
            }
            else
            {
                // Update the data type and its pre-values.
                _dataTypeService.Save(UpdateDataTypeDefinition(dataType, model));

                var existingPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id).FormatAsDictionary();

                var preValuesToSave = existingPreValues.Any()
                    ? GetUpdatedPreValues(existingPreValues, model.PreValues)
                    : GetPreValues(model);

                _dataTypeService.SavePreValues(dataType.Id, preValuesToSave);
            }

            // We get the data type once more to refresh it after saving it.
            dataType = _dataTypeService.GetDataTypeDefinitionByName(dataType.Name);

            // Set/update tracking.
            SetDataTypeId(model, dataType);
        }
        private bool IsMultipleDataType(int dataTypeId, string propertyEditorAlias)
        {
            // GetPreValuesCollectionByDataTypeId is cached at repository level;
            // still, the collection is deep-cloned so this is kinda expensive,
            // better to cache here + trigger refresh in DataTypeCacheRefresher

            return(Storages.GetOrAdd(dataTypeId, id =>
            {
                var preVals = _dataTypeService.GetPreValuesCollectionByDataTypeId(id).PreValuesAsDictionary;

                if (preVals.ContainsKey("multiPicker"))
                {
                    var preValue = preVals
                                   .FirstOrDefault(x => string.Equals(x.Key, "multiPicker", StringComparison.InvariantCultureIgnoreCase))
                                   .Value;

                    return preValue != null && preValue.Value.TryConvertTo <bool>().Result;
                }

                //in some odd cases, the pre-values in the db won't exist but their default pre-values contain this key so check there
                var propertyEditor = PropertyEditorResolver.Current.GetByAlias(propertyEditorAlias);
                if (propertyEditor != null)
                {
                    var preValue = propertyEditor.DefaultPreValues
                                   .FirstOrDefault(x => string.Equals(x.Key, "multiPicker", StringComparison.InvariantCultureIgnoreCase))
                                   .Value;

                    return preValue != null && preValue.TryConvertTo <bool>().Result;
                }

                return false;
            }));
        }
        private IDictionary <string, PreValue> MergePreValuesWithExisting(DataTypeRegistration dataTypeRegistration, IDictionary <string, PreValue> codeFirstPreValues, ref bool modified)
        {
            bool preValuesChanged = false;
            IDictionary <string, PreValue> preValues = new Dictionary <string, PreValue>();

            //Ensure that pre-value IDs stay the same (correlate by alias)
            if (dataTypeRegistration.Definition.Id != -1)
            {
                var existingValues = _service.GetPreValuesCollectionByDataTypeId(dataTypeRegistration.Definition.Id);
                if (existingValues.IsDictionaryBased)
                {
                    var dict = existingValues.PreValuesAsDictionary;
                    foreach (var value in codeFirstPreValues)
                    {
                        var match = dict.Where(x => x.Key == value.Key);
                        if (match.Count() == 0)
                        {
                            preValues.Add(value);
                            preValuesChanged = true;
                        }
                        else
                        {
                            var existing = match.First();
                            preValuesChanged = preValuesChanged || value.Value.Value != existing.Value.Value || (value.Value.SortOrder != 0 && value.Value.SortOrder != existing.Value.SortOrder);
                            preValues.Add(existing.Key, new PreValue(existing.Value.Id, value.Value.Value, value.Value.SortOrder));
                        }
                    }
                }
            }
            modified = modified || preValuesChanged;
            return(preValues);
        }
Beispiel #13
0
        private static bool IsMultipleDataType(int dataTypeId)
        {
            IDataTypeService dataTypeService         = ApplicationContext.Current.Services.DataTypeService;
            IDictionary <string, PreValue> preValues = dataTypeService
                                                       .GetPreValuesCollectionByDataTypeId(dataTypeId)
                                                       .PreValuesAsDictionary;

            PreValue maxNumberOfItems;

            if (preValues.TryGetValue("maxNumberOfItems", out maxNumberOfItems) &&
                int.TryParse(maxNumberOfItems.Value, out _maxNumberOfItems))
            {
                PreValue versionPreValue;
                Version  version;
                // for backwards compatibility, always return true if version
                // is less than 2.0.0
                if (preValues.TryGetValue("version", out versionPreValue) &&
                    Version.TryParse(versionPreValue.Value, out version) &&
                    version >= new Version(2, 0, 0))
                {
                    return(_maxNumberOfItems != 1);
                }
            }
            return(true);
        }
Beispiel #14
0
 private EmbeddedContentConfig GetConfig(int dataTypeId)
 {
     using (_profilingLogger.DebugDuration <EmbeddedContentValueConverter>($"GetConfig({dataTypeId})"))
     {
         PreValueCollection preValues      = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId);
         PreValue           configPreValue = preValues.PreValuesAsDictionary["embeddedContentConfig"];
         return(JsonConvert.DeserializeObject <EmbeddedContentConfig>(configPreValue.Value));
     }
 }
Beispiel #15
0
        /// <summary>
        /// Updates the prevalues for the property editor
        /// </summary>
        /// <param name="prevalue">The new value</param>
        /// <param name="alias">The prevalue to update</param>
        public void UpdatePrevalueForEditor(string prevalue, string alias)
        {
            IDataTypeDefinition            datatype  = _dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(Alias).First();
            IDictionary <string, PreValue> prevalues = _dataTypeService.GetPreValuesCollectionByDataTypeId(datatype.Id).PreValuesAsDictionary;

            prevalues[alias] = new PreValue(prevalue);

            _dataTypeService.SaveDataTypeAndPreValues(datatype, prevalues);
        }
Beispiel #16
0
        public void Convert(string name)
        {
            var nestedContentDataType = _dataTypeService.GetDataTypeDefinitionByName(name);
            var preValues             = _dataTypeService.GetPreValuesCollectionByDataTypeId(nestedContentDataType.Id).FormatAsDictionary();

            nestedContentDataType.PropertyEditorAlias = "Umbraco.NestedContent";

            _dataTypeService.SaveDataTypeAndPreValues(nestedContentDataType, preValues);
        }
Beispiel #17
0
        protected void ConvertInnerContentEditorToDb(JObject item, IDataTypeService dataTypeService)
        {
            if (item == null)
            {
                return;
            }

            var contentType = InnerContentHelper.GetContentTypeFromItem(item);

            if (contentType == null)
            {
                return;
            }

            var propValueKeys = item.Properties().Select(x => x.Name).ToArray();

            foreach (var propKey in propValueKeys)
            {
                var propType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == propKey);
                if (propType == null)
                {
                    if (IsSystemPropertyKey(propKey) == false)
                    {
                        // Property missing so just delete the value
                        item[propKey] = null;
                    }
                }
                else
                {
                    // Fetch the property types prevalue
                    var propPreValues = dataTypeService.GetPreValuesCollectionByDataTypeId(propType.DataTypeDefinitionId);

                    // Lookup the property editor
                    var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);

                    // Create a fake content property data object
                    var contentPropData = new ContentPropertyData(
                        item[propKey], propPreValues,
                        new Dictionary <string, object>());

                    // Get the property editor to do it's conversion
                    var newValue = propEditor.ValueEditor.ConvertEditorToDb(contentPropData, item[propKey]);

                    // Store the value back
                    item[propKey] = (newValue == null) ? null : JToken.FromObject(newValue);
                }
            }

            // Process children
            var childrenProp = item.Properties().FirstOrDefault(x => x.Name == "children");

            if (childrenProp != null)
            {
                ConvertInnerContentEditorToDb(childrenProp.Value.Value <JArray>(), dataTypeService);
            }
        }
        internal static void MergePreValues(JObject currentValue, IDataTypeService dataTypeService, int dataTypeId)
        {
            //need to lookup the pre-values for this data type
            //TODO: Change all singleton access to use ctor injection in v8!!!
            var dt = dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId);

            if (dt != null && dt.IsDictionaryBased && dt.PreValuesAsDictionary.ContainsKey("crops"))
            {
                var    cropsString = dt.PreValuesAsDictionary["crops"].Value;
                JArray preValueCrops;
                try
                {
                    preValueCrops = JsonConvert.DeserializeObject <JArray>(cropsString);
                }
                catch (Exception ex)
                {
                    LogHelper.Error <ImageCropperValueConverter>("Could not parse the string " + cropsString + " to a json object", ex);
                    return;
                }

                //now we need to merge the crop values - the alias + width + height comes from pre-configured pre-values,
                // however, each crop can store it's own coordinates

                JArray existingCropsArray;
                if (currentValue["crops"] != null)
                {
                    existingCropsArray = (JArray)currentValue["crops"];
                }
                else
                {
                    currentValue["crops"] = existingCropsArray = new JArray();
                }

                foreach (var preValueCrop in preValueCrops.Where(x => x.HasValues))
                {
                    var found = existingCropsArray.FirstOrDefault(x =>
                    {
                        if (x.HasValues && x["alias"] != null)
                        {
                            return(x["alias"].Value <string>() == preValueCrop["alias"].Value <string>());
                        }
                        return(false);
                    });
                    if (found != null)
                    {
                        found["width"]  = preValueCrop["width"];
                        found["height"] = preValueCrop["height"];
                    }
                    else
                    {
                        existingCropsArray.Add(preValueCrop);
                    }
                }
            }
        }
        /// <summary>
        /// Validates the data for each property
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// All property data validation goes into the modelstate with a prefix of "Properties"
        /// </remarks>
        protected bool ValidatePropertyData(IDictionary <string, object> postedProperties, TPersisted content)
        {
            var hasError = false;

            if (postedProperties != null)
            {
                foreach (var p in postedProperties)
                {
                    var propertyType = content.PropertyTypes.Single(x => x.Alias == p.Key);

                    var editor = PropertyEditorResolver.Current.GetByAlias(propertyType.PropertyEditorAlias);

                    if (editor == null)
                    {
                        var message = string.Format("The property editor with alias: {0} was not found for property with id {1}", propertyType.PropertyEditorAlias, content.Properties[p.Key].Id);
                        LogHelper.Warn <ContentPropertyValidator <TPersisted> >(message);
                        continue;
                    }

                    //get the posted value for this property
                    var postedValue = p.Value;

                    //get the pre-values for this property
                    var preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId);

                    foreach (var result in editor.ValueEditor.Validators.SelectMany(v => v.Validate(postedValue, preValues, editor)))
                    {
                        _modelState.AddPropertyError(result, p.Key);
                    }

                    //Now we need to validate the property based on the PropertyType validation (i.e. regex and required)
                    // NOTE: These will become legacy once we have pre-value overrides.
                    if (propertyType.Mandatory)
                    {
                        foreach (var result in editor.ValueEditor.RequiredValidator.Validate(postedValue, "", preValues, editor))
                        {
                            hasError = true;
                            _modelState.AddPropertyError(result, p.Key);
                        }
                    }

                    if (propertyType.ValidationRegExp.IsNullOrWhiteSpace() == false)
                    {
                        foreach (var result in editor.ValueEditor.RegexValidator.Validate(postedValue, propertyType.ValidationRegExp, preValues, editor))
                        {
                            hasError = true;
                            _modelState.AddPropertyError(result, p.Key);
                        }
                    }
                }
            }

            return(!hasError);
        }
        protected IDictionary <string, PreValue> GetPreValues(Property property)
        {
            var preVals = _dataTypeService.GetPreValuesCollectionByDataTypeId(property.PropertyType.DataTypeDefinitionId);

            if (preVals != null)
            {
                var dictionary = preVals.FormatAsDictionary();
                return(dictionary);
            }
            return(null);
        }
Beispiel #21
0
 public static PreValueCollection GetPreValue(IDataTypeService service, int dataTypeDefinitionId)
 {
     try
     {
         return(service.GetPreValuesCollectionByDataTypeId(dataTypeDefinitionId));
     }
     catch
     {
         return(null);
     }
 }
        public static int?getPrevalueId(string dataTypeName, string dataTypeValue)
        {
            // Instantiate datatype service
            IDataTypeService dtService = ApplicationContext.Current.Services.DataTypeService;

            // Obtain prevalue collection from datatypes
            IDataTypeDefinition dtDefinition = dtService.GetDataTypeDefinitionByName(dataTypeName);
            PreValueCollection  pvCollection = dtService.GetPreValuesCollectionByDataTypeId(dtDefinition.Id);

            return(pvCollection.PreValuesAsDictionary.FirstOrDefault(preValue => string.Equals(preValue.Value.Value, dataTypeValue)).Value.Id);
        }
Beispiel #23
0
        public string GetExportValue(int dataTypeDefinitionId, string value)
        {
            // We need to retrieve the datatype associated with the property so that we can parse the fieldset
            // then we will go through each item in the fieldset and if there is a mapper associated with that item's datatype
            // we should map the property value

            string archetypeConfig = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinitionId).PreValuesAsDictionary["archetypeConfig"].Value;

            var config = JsonConvert.DeserializeObject <ArchetypePreValue>(archetypeConfig);

            var typedContent = JsonConvert.DeserializeObject <ArchetypeModel>(value);

            foreach (ArchetypePreValueFieldset fieldSet in config.Fieldsets)
            {
                foreach (ArchetypePreValueProperty property in fieldSet.Properties)
                {
                    IDataTypeDefinition dataType = _dataTypeService.GetDataTypeDefinitionById(property.DataTypeGuid);

                    uSyncContentMapping mapping =
                        uSyncCoreContext.Instance.Configuration.Settings.ContentMappings.SingleOrDefault(x => x.EditorAlias == dataType.PropertyEditorAlias);

                    if (mapping != null)
                    {
                        IContentMapper mapper = ContentMapperFactory.GetMapper(mapping);

                        if (mapper != null)
                        {
                            typedContent.Fieldsets.AsQueryable()
                            .SelectMany(fs => fs.Properties)
                            .Where(p => p.Alias == property.Alias)
                            .ForEach(pm => pm.Value = mapper.GetExportValue(dataType.Id, pm.Value.ToString()));
                        }
                    }
                }
            }

            return(typedContent.SerializeForPersistence());
        }
Beispiel #24
0
        private IEnumerable <int> NestedContentAliasWithContentType(string alias)
        {
            var nestedContents = _dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(NestedContentAlias);

            foreach (var dataType in nestedContents)
            {
                var preValues    = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id).FormatAsDictionary();
                var contentTypes = JsonConvert.DeserializeObject <NestedContentPreValue[]>(preValues["contentTypes"].Value, _serializerSettings);
                if (contentTypes.Any(n => n.NcAlias.Equals(alias, StringComparison.CurrentCultureIgnoreCase)))
                {
                    yield return(dataType.Id);
                }
            }
        }
Beispiel #25
0
        private void UpdateDatatypePreValues(IDataTypeService dataTypeService, string dataTypeName,
                                             string assemblyFullNamePreValue, string enumFullNamePreValue)
        {
            var dataType = dataTypeService.GetDataTypeDefinitionByName(dataTypeName);

            if (dataType != null)
            {
                var preValues  = dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id);
                var dictionary = preValues.FormatAsDictionary();
                dictionary[FolderConstants.PreValueAssemblyAlias].Value = assemblyFullNamePreValue;
                dictionary[FolderConstants.PreValueEnumAlias].Value     = enumFullNamePreValue;

                dataTypeService.SaveDataTypeAndPreValues(dataType, dictionary);
            }
        }
        /// <summary>
        /// Discovers if the slider is set to range mode.
        /// </summary>
        /// <param name="dataTypeId">
        /// The data type id.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private bool IsRangeDataType(int dataTypeId)
        {
            // GetPreValuesCollectionByDataTypeId is cached at repository level;
            // still, the collection is deep-cloned so this is kinda expensive,
            // better to cache here + trigger refresh in DataTypeCacheRefresher

            return(Storages.GetOrAdd(dataTypeId, id =>
            {
                var preValue = _dataTypeService.GetPreValuesCollectionByDataTypeId(id)
                               .PreValuesAsDictionary
                               .FirstOrDefault(x => string.Equals(x.Key, "enableRange", StringComparison.InvariantCultureIgnoreCase))
                               .Value;

                return preValue != null && preValue.Value.TryConvertTo <bool>().Result;
            }));
        }
        private List <PreValue> GetPreValues(int dataTypeId)
        {
            List <Umbraco.Core.Models.PreValue> result = new List <Umbraco.Core.Models.PreValue>();
            var preValues = _service.GetPreValuesCollectionByDataTypeId(dataTypeId);

            if (preValues.IsDictionaryBased)
            {
                result.AddRange(preValues.PreValuesAsDictionary.Values.OrderBy(x => x.SortOrder));
            }
            else
            {
                result.AddRange(preValues.PreValuesAsArray.OrderBy(x => x.SortOrder));
            }

            return(result);
        }
Beispiel #28
0
        private bool IsMultipleDataType(int dataTypeId, out int maxNumber)
        {
            // GetPreValuesCollectionByDataTypeId is cached at repository level;
            // still, the collection is deep-cloned so this is kinda expensive,
            // better to cache here + trigger refresh in DataTypeCacheRefresher

            maxNumber = Storages.GetOrAdd(dataTypeId, id =>
            {
                var preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(id).PreValuesAsDictionary;

                return(preValues.TryGetValue("maxNumber", out var maxNumberPreValue)
                    ? maxNumberPreValue.Value.TryConvertTo <int>().Result
                    : 0);
            });

            return(maxNumber != 1);
        }
        /// <summary>
        /// Exports an <see cref="IDataTypeDefinition"/> item to xml as an <see cref="XElement"/>
        /// </summary>
        /// <param name="dataTypeService"></param>
        /// <param name="dataTypeDefinition">IDataTypeDefinition type to export</param>
        /// <returns><see cref="XElement"/> containing the xml representation of the IDataTypeDefinition object</returns>
        public XElement Serialize(IDataTypeService dataTypeService, IDataTypeDefinition dataTypeDefinition)
        {
            var prevalues    = new XElement("PreValues");
            var prevalueList = dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinition.Id)
                               .FormatAsDictionary();

            var sort = 0;

            foreach (var pv in prevalueList)
            {
                var prevalue = new XElement("PreValue");
                prevalue.Add(new XAttribute("Id", pv.Value.Id));
                prevalue.Add(new XAttribute("Value", pv.Value.Value ?? ""));
                prevalue.Add(new XAttribute("Alias", pv.Key));
                prevalue.Add(new XAttribute("SortOrder", sort));
                prevalues.Add(prevalue);
                sort++;
            }

            var xml = new XElement("DataType", prevalues);

            xml.Add(new XAttribute("Name", dataTypeDefinition.Name));
            //The 'ID' when exporting is actually the property editor alias (in pre v7 it was the IDataType GUID id)
            xml.Add(new XAttribute("Id", dataTypeDefinition.PropertyEditorAlias));
            xml.Add(new XAttribute("Definition", dataTypeDefinition.Key));
            xml.Add(new XAttribute("DatabaseType", dataTypeDefinition.DatabaseType.ToString()));

            var folderNames = string.Empty;

            if (dataTypeDefinition.Level != 1)
            {
                //get url encoded folder names
                var folders = dataTypeService.GetContainers(dataTypeDefinition)
                              .OrderBy(x => x.Level)
                              .Select(x => HttpUtility.UrlEncode(x.Name));

                folderNames = string.Join("/", folders.ToArray());
            }

            if (string.IsNullOrWhiteSpace(folderNames) == false)
            {
                xml.Add(new XAttribute("Folders", folderNames));
            }

            return(xml);
        }
        protected virtual bool IsMultiPicker(int dataTypeId)
        {
            var preValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId);

            if (preValues.PreValuesAsDictionary.TryGetValue("multiPicker", out var preValue))
            {
                return(preValue.Value.TryConvertTo <bool>().Result);
            }
            if (preValues.PreValuesAsDictionary.TryGetValue("maxNumber", out preValue) ||
                preValues.PreValuesAsDictionary.TryGetValue("maxItems", out preValue))
            {
                var attempt = preValue.Value.TryConvertTo <int>();
                return(false == attempt.Success || attempt.Result != 1);
            }

            return(false);
        }