Exemplo n.º 1
0
        private IContentTypeBase SyncMemberType(ContentTypeRegistration registration, IContentTypeBase result)
        {
            result.ResetDirtyProperties(false);
            Save(result); //need to save to ensure no sync issues when we load from the legacy API
            bool modified = false;
            var  member   = new umbraco.cms.businesslogic.member.MemberType(result.Id);

            foreach (var prop in member.PropertyTypes)
            {
                var propReg = registration.Properties.SingleOrDefault(x => x.Alias == prop.Alias);
                if (propReg != null)
                {
                    var attr = propReg.Metadata.GetCodeFirstAttribute <MemberPropertyAttribute>();
                    if (attr != null)
                    {
                        if (attr.MemberCanEdit != member.MemberCanEdit(prop))
                        {
                            member.setMemberCanEdit(prop, attr.MemberCanEdit);
                            modified = true;
                        }
                        if (attr.ShowOnProfile != member.ViewOnProfile(prop))
                        {
                            member.setMemberViewOnProfile(prop, attr.ShowOnProfile);
                            modified = true;
                        }
                    }
                }
            }

            if (modified)
            {
                modified = false;
                member.Save();
            }

            return(_service.Get(member.Id));
        }
        private void Prune(IContentTypeBase contentType, Type documentClrType, PropertyInfo[] tabProperties, IEnumerable<PropertyInfo> propertiesOfRoot)
        {
            bool modified = false;

            var propertiesToKeep = propertiesOfRoot.Where(x =>
            {
                var attr = x.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false);
                return x.DeclaringType == documentClrType || attr != null;
            }).Select(x => x.GetCodeFirstAttribute<ContentPropertyAttribute>().Alias)
            .Union(tabProperties.Where(x =>
            {
                var attr = x.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false);
                return x.DeclaringType == documentClrType || attr != null;
            }).SelectMany(x =>
            {
                return x.PropertyType.GetProperties().Where(y =>
                {
                    return (y.DeclaringType == x.PropertyType || y.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false) != null) && y.GetCodeFirstAttribute<ContentPropertyAttribute>() != null;
                }).Select(y =>
                    {
                      var attr = y.GetCodeFirstAttribute<ContentPropertyAttribute>();
                      var tabAttr = x.GetCodeFirstAttribute<ContentTabAttribute>();
                      return attr.AddTabAliasToPropertyAlias ? attr.Alias + "_" + tabAttr.Name.Replace(" ", "_") : attr.Alias;
                    });
            })).ToList();

            propertiesToKeep.AddRange(documentClrType.GetCustomAttributes<DoNotRemovePropertyAttribute>(true).Select(x => x.PropertyAlias));

            //loop through all the properties on the ContentType to see if they should be removed.
            var existingUmbracoProperties = contentType.PropertyTypes.ToArray();
            int length = contentType.PropertyTypes.Count();
            for (int i = 0; i < length; i++)
            {
                if (!propertiesToKeep.Any(x => x.Equals(existingUmbracoProperties[i].Alias, StringComparison.InvariantCultureIgnoreCase)))
                {
                    if (contentType.PropertyTypeExists(existingUmbracoProperties[i].Alias))
                    {
                        modified = true;
                        //remove the property
                        contentType.RemovePropertyType(existingUmbracoProperties[i].Alias);
                        var alias = existingUmbracoProperties[i].Alias;
                        var children = GetChildren(contentType);

                        //TODO is this needed? I reckon it shouldn't be
                        RemovePropertyFromChildren(alias, children);
                    }
                }
            }

            if (modified)
            {
                contentType.ResetDirtyProperties(false);
                Save(contentType);
            }
        }