Exemple #1
0
        protected override bool EvaluateCondition(ExecutionContext context)
        {
            context.AssertRepositoryStarted();

            if (string.IsNullOrEmpty(ContentType) || string.IsNullOrEmpty(Field))
            {
                throw new PackagingException("ContentType or Field is empty.");
            }

            var ct = ContentRepository.Schema.ContentType.GetByName(ContentType);

            if (ct == null)
            {
                throw new PackagingException("ContentType not found: " + ContentType);
            }

            // We have to check only this particular CTD xml, it does not
            // matter if the field is already defined in a parent type.
            if (LocalOnly)
            {
                var xDoc  = EditContentType.LoadContentTypeXmlDocument(ct);
                var field = EditContentType.LoadFieldElement(xDoc, Field, false);

                return(field != null);
            }

            return(ct.FieldSettings.Any(fs => string.Compare(fs.Name, Field, StringComparison.Ordinal) == 0));
        }
Exemple #2
0
        private void RemoveAllowedTypes(ContentTypeDependencies dependencies)
        {
            Logger.LogMessage("Remove from allowed types:");

            var names = dependencies.InheritedTypeNames.ToList();

            names.AddRange(dependencies.ContentTypeNames);
            var ns = EditContentType.NamespacePrefix;

            if (dependencies.PermittingContentTypes.Length + dependencies.PermittingFieldSettings.Length > 0)
            {
                foreach (var contentType in ContentType.GetContentTypes())
                {
                    var changed = false;
                    var xml     = new XmlDocument();
                    xml.Load(contentType.Binary.GetStream());
                    var nsmgr = EditContentType.GetNamespaceManager(xml);

                    var allowedChildTypesElement =
                        (XmlElement)xml.SelectSingleNode($"/{ns}:ContentType/{ns}:AllowedChildTypes", nsmgr);
                    if (allowedChildTypesElement != null)
                    {
                        var oldList = allowedChildTypesElement.InnerText.Split(new[] { ',' },
                                                                               StringSplitOptions.RemoveEmptyEntries);
                        var newList = oldList.Except(names).ToArray();
                        if (oldList.Length != newList.Length)
                        {
                            changed = true;
                            Logger.LogMessage($"    {contentType.Name}");
                            allowedChildTypesElement.InnerText = string.Join(",", newList);
                        }
                    }

                    foreach (XmlElement refFieldElement in xml.SelectNodes($"//{ns}:Field[@type='Reference']", nsmgr))
                    {
                        var elementsToDelete = new List <XmlElement>();
                        var oldAllowedTypes  =
                            refFieldElement.SelectNodes($"{ns}:Configuration/{ns}:AllowedTypes/{ns}:Type", nsmgr);
                        foreach (XmlElement typeElement in oldAllowedTypes)
                        {
                            if (names.Contains(typeElement.InnerText))
                            {
                                elementsToDelete.Add(typeElement);
                            }
                        }
                        if (elementsToDelete.Any())
                        {
                            var fieldName = refFieldElement.Attributes["name"].Value;
                            Logger.LogMessage($"    {contentType.Name}.{fieldName}");
                            changed = true;
                        }
                        foreach (var element in elementsToDelete)
                        {
                            element.ParentNode.RemoveChild(element);
                        }
                    }

                    if (changed)
                    {
                        ContentTypeInstaller.InstallContentType(xml.OuterXml);
                    }
                }
            }

            if (dependencies.PermittingContentCollection.Count > 0)
            {
                foreach (var item in dependencies.PermittingContentCollection)
                {
                    Logger.LogMessage($"    {item.Key}");

                    var content = Content.Load(item.Key);
                    if (content != null)
                    {
                        if (content.ContentHandler is GenericContent gc)
                        {
                            var newList = new List <ContentType>();
                            var oldList = gc.AllowedChildTypes.ToList();
                            foreach (var ct in oldList)
                            {
                                if (!names.Contains(ct.Name))
                                {
                                    newList.Add(ct);
                                }
                            }

                            gc.AllowedChildTypes = newList;
                            gc.Save();
                        }
                    }
                }
            }
        }