Esempio n. 1
0
        public ContentData ToApiModel(Schema schema, IReadOnlyCollection <Language> languages, Language masterLanguage, bool excludeHidden = true)
        {
            Guard.NotNull(schema, nameof(schema));
            Guard.NotNull(languages, nameof(languages));
            Guard.NotNull(masterLanguage, nameof(masterLanguage));

            var invariantCode = Language.Invariant.Iso2Code;

            var result = new ContentData();

            foreach (var fieldValue in this)
            {
                if (!schema.FieldsByName.TryGetValue(fieldValue.Key, out Field field) || (excludeHidden && field.IsHidden))
                {
                    continue;
                }

                var fieldResult = new ContentFieldData();
                var fieldValues = fieldValue.Value;

                if (field.RawProperties.IsLocalizable)
                {
                    foreach (var language in languages)
                    {
                        var languageCode = language.Iso2Code;

                        if (fieldValues.TryGetValue(languageCode, out JToken value))
                        {
                            fieldResult.Add(languageCode, value);
                        }
                        else if (language.Equals(masterLanguage) && fieldValues.TryGetValue(invariantCode, out value))
                        {
                            fieldResult.Add(languageCode, value);
                        }
                    }
                }
                else
                {
                    if (fieldValues.TryGetValue(invariantCode, out JToken value))
                    {
                        fieldResult.Add(invariantCode, value);
                    }
                    else if (fieldValues.TryGetValue(masterLanguage.Iso2Code, out value))
                    {
                        fieldResult.Add(invariantCode, value);
                    }
                    else if (fieldValues.Count > 0)
                    {
                        fieldResult.Add(invariantCode, fieldValues.Values.First());
                    }
                }

                result.Add(field.Name, fieldResult);
            }

            return(result);
        }
Esempio n. 2
0
        public ContentData ToApiModel(Schema schema, LanguagesConfig languagesConfig, IReadOnlyCollection <Language> languagePreferences = null, bool excludeHidden = true)
        {
            Guard.NotNull(schema, nameof(schema));
            Guard.NotNull(languagesConfig, nameof(languagesConfig));

            var codeForInvariant      = InvariantPartitioning.Instance.Master.Key;
            var codeForMasterLanguage = languagesConfig.Master.Language.Iso2Code;

            var result = new ContentData();

            foreach (var fieldValue in this)
            {
                if (!schema.FieldsByName.TryGetValue(fieldValue.Key, out Field field) || (excludeHidden && field.IsHidden))
                {
                    continue;
                }

                var fieldResult = new ContentFieldData();
                var fieldValues = fieldValue.Value;

                if (field.Paritioning.Equals(Partitioning.Language))
                {
                    foreach (var languageConfig in languagesConfig)
                    {
                        var languageCode = languageConfig.Key;

                        if (fieldValues.TryGetValue(languageCode, out JToken value))
                        {
                            fieldResult.Add(languageCode, value);
                        }
                        else if (languageConfig == languagesConfig.Master && fieldValues.TryGetValue(codeForInvariant, out value))
                        {
                            fieldResult.Add(languageCode, value);
                        }
                    }
                }
                else
                {
                    if (fieldValues.TryGetValue(codeForInvariant, out JToken value))
                    {
                        fieldResult.Add(codeForInvariant, value);
                    }
                    else if (fieldValues.TryGetValue(codeForMasterLanguage, out value))
                    {
                        fieldResult.Add(codeForInvariant, value);
                    }
                    else if (fieldValues.Count > 0)
                    {
                        fieldResult.Add(codeForInvariant, fieldValues.Values.First());
                    }
                }

                result.Add(field.Name, fieldResult);
            }

            return(result);
        }