예제 #1
0
 public static HtmlString IsAncestorOrSelf(this Content content, Content child, string valueIfTrue, string valueIfFalse)
 {
     if (child.AllAncestorIdsOrSelf().Contains(content.Id))
         return new HtmlString(valueIfTrue);
     else
         return new HtmlString(valueIfFalse);
 }
        public static dynamic Bend(this Content content, IHiveManager hiveManager, IMembershipService <Member> membershipService,
                                   IPublicAccessService publicAccessService, IEnumerable <Assembly> dynamicExtensionAssemblies = null)
        {
            if (content == null)
            {
                return(null);
            }

            var     bendy        = new BendyObject(content);
            dynamic dynamicBendy = bendy;

            //get all fields that have a value
            var attributesWithValue = content.Attributes.Where(typedAttribute => typedAttribute.Values.Any()).ToArray();

            foreach (var attrib in attributesWithValue)
            {
                //if the default value doesn't exist, then add to sub bendy... normally this occurs if the
                // property editor is saving custom values
                var defaultval = attrib.Values.GetDefaultValue();
                if (attrib.Values.Count > 1 || defaultval == null)
                {
                    //03/05/12 (SD). we only need to create one sub bendy, then assign it to each property alias
                    // this used to create 2 of them not sure why as this will just have more overhead.
                    // This also used to create a new bendy object, then iterate over each of the attrib.Values and
                    //manually assign each value using AddToBendy. This has changed so that we can better support
                    //multi-value properties and because the BendyObject ctor now has support to check for IDictionary
                    //which automatically adds each dictionary key/value as a property.
                    var subBendy = new BendyObject(attrib.Values);
                    AddToBendy(bendy, attrib.AttributeDefinition.Alias, subBendy);

                    subBendy["__OriginalItem"] = attrib;
                    subBendy.AddLazy("__Parent", () => bendy);
                    subBendy["__ParentKey"] = attrib.AttributeDefinition.Alias;

                    BendyObjectExtensionsHelper.ApplyDynamicFieldExtensions(content,
                                                                            attrib.AttributeDefinition.AttributeType.RenderTypeProvider,
                                                                            subBendy,
                                                                            dynamicExtensionAssemblies);

                    BendyObjectExtensionsHelper.ApplyDynamicExtensions <TypedAttribute>(subBendy, dynamicExtensionAssemblies);

                    AddToBendy(bendy, attrib.AttributeDefinition.Alias, subBendy);
                }
                else
                {
                    AddToBendy(bendy, attrib.AttributeDefinition.Alias, defaultval);
                }
            }

            // Now put in rudimentary default values for any that don't have a value
            // TODO: Get the default values from the backoffice/AttributeDefinition/AttributeType involved in this
            foreach (var attrib in content.Attributes.Except(attributesWithValue))
            {
                object defaultValue = null;
                switch (attrib.AttributeDefinition.AttributeType.SerializationType.DataSerializationType)
                {
                case DataSerializationTypes.Date:
                    defaultValue = DateTime.MinValue;
                    break;

                case DataSerializationTypes.Guid:
                    defaultValue = Guid.Empty;
                    break;

                case DataSerializationTypes.SmallInt:
                case DataSerializationTypes.LargeInt:
                    defaultValue = 0;
                    break;

                case DataSerializationTypes.Decimal:
                    defaultValue = 0d;
                    break;

                case DataSerializationTypes.ByteArray:
                    defaultValue = new byte[] { };
                    break;

                case DataSerializationTypes.Boolean:
                    defaultValue = false;
                    break;

                case DataSerializationTypes.String:
                case DataSerializationTypes.LongString:
                    defaultValue = string.Empty;
                    break;
                }
                AddToBendy(bendy, attrib.AttributeDefinition.Alias, defaultValue);
            }

            bendy["__OriginalItem"] = content;

            bendy.WhenItemNotFound = (bent, membername) =>
            {
                if (membername.IsNullOrWhiteSpace())
                {
                    return(new BendyObject());
                }
                // Support recursively looking up a property similarly to v4
                if (membername.StartsWith("_") && membername.ElementAt(1).IsLowerCase())
                {
                    return(content.Field(membername.TrimStart('_'), true));
                }
                // Support pluralised document-type aliases inferring a query
                if (membername.EndsWith("s", StringComparison.InvariantCultureIgnoreCase))
                {
                    var alias = membername.TrimEnd("s");
                    var up    = alias.StartsWith("Ancestor", StringComparison.InvariantCultureIgnoreCase);
                    if (up)
                    {
                        alias = alias.TrimStart("Ancestor");
                    }
                    dynamic theBendy = bent;

                    return(up ? theBendy.AncestorsOrSelf.Where("NodeTypeAlias == @0", alias) : theBendy.DescendantsOrSelf.Where("NodeTypeAlias == @0", alias));
                }
                return(new BendyObject());
            };

            bendy.AddLazy("AllAncestorIds", () => content.AllAncestorIds());
            bendy.AddLazy("Ancestors", () =>
            {
                var ancestorIds = content.AllAncestorIds();
                return(new DynamicContentList(content.Id, hiveManager, membershipService, publicAccessService, new BendyObject(), ancestorIds));
            });
            bendy.AddLazy("AllAncestorIdsOrSelf", () => content.AllAncestorIdsOrSelf());
            bendy.AddLazy("AncestorsOrSelf", () =>
            {
                var ancestorIds = content.AllAncestorIdsOrSelf();
                return(new DynamicContentList(content.Id, hiveManager, membershipService, publicAccessService, new BendyObject(), ancestorIds));
            });
            bendy.AddLazy("AllDescendantIds", () => content.AllDescendantIds());
            bendy.AddLazy("Descendants", () =>
            {
                var descendantContentIds = content.AllDescendantIds();
                return(new DynamicContentList(content.Id, hiveManager, membershipService, publicAccessService, new BendyObject(), descendantContentIds));
            });
            bendy.AddLazy("AllDescendantIdsOrSelf", () => content.AllDescendantIdsOrSelf());
            bendy.AddLazy("DescendantsOrSelf", () =>
            {
                var descendantContentIds = content.AllDescendantIdsOrSelf();
                return(new DynamicContentList(content.Id, hiveManager, membershipService, publicAccessService, new BendyObject(), descendantContentIds));
            });
            bendy.AddLazy("AllChildIds", () => content.AllChildIds(hiveManager));
            bendy.AddLazy("Children", () =>
            {
                var childIds = content.AllChildIds(hiveManager);
                return(new DynamicContentList(content.Id, hiveManager, membershipService, publicAccessService, new BendyObject(), childIds));
            });
            bendy.AddLazy("Parent", () => content.ParentContent(hiveManager).Bend(hiveManager, membershipService, publicAccessService));
            bendy.AddLazy("Path", () => content.GetPath(hiveManager));
            bendy.AddLazy("PathById", () => content.GetPath(hiveManager));
            bendy.AddLazy("Level", () => content.GetPath(hiveManager).Level);

            //Add lazy url property
            bendy.AddLazy("Url", content.NiceUrl);
            bendy.AddLazy("NiceUrl", content.NiceUrl);
            bendy.AddLazy("NodeTypeAlias", () => content.ContentType.IfNotNull(x => x.Alias, string.Empty));
            bendy.AddLazy("Template", () => content.CurrentTemplate.IfNotNull(x => x.Alias, string.Empty));
            bendy.AddLazy("TemplateId", () => content.CurrentTemplate.IfNotNull(x => x.Id, HiveId.Empty));
            bendy.AddLazy("TemplateFileName", () => content.CurrentTemplate.IfNotNull(x => (string)x.Id.Value, string.Empty));
            bendy.AddLazy("CreateDate", () => content.UtcCreated);
            bendy.AddLazy("UpdateDate", () => content.UtcModified);
            bendy.AddLazy("StatusChangedDate", () => content.UtcStatusChanged);

            dynamicBendy.ContentType = content.ContentType;
            var nodeNameAlias = NodeNameAttributeDefinition.AliasValue.ToRebelAlias(StringAliasCaseType.PascalCase);

            try
            {
                dynamicBendy.Name    = bendy[nodeNameAlias].Name;
                dynamicBendy.UrlName = bendy[nodeNameAlias].UrlName;
            }
            catch
            {
                /* Nothing */
            }

            dynamicBendy.Id = content.Id;

            // Add any dynamic registered methods
            BendyObjectExtensionsHelper.ApplyDynamicExtensions <Content>(bendy, dynamicExtensionAssemblies);

            return(dynamicBendy);
        }
예제 #3
0
        public static dynamic Bend(this Content content, IHiveManager hiveManager)
        {
            if (content == null)
            {
                return(null);
            }

            var     bendy        = new BendyObject(content);
            dynamic dynamicBendy = bendy;

            //get all fields that have a value
            foreach (var typedAttribute in
                     content.Attributes.Where(typedAttribute => typedAttribute.Values.Any()))
            {
                //if there are more than 1 value, put them into a sub bendy
                if (typedAttribute.Values.Count() > 1)
                {
                    var subBendy1 = new BendyObject();
                    var subBendy2 = new BendyObject();

                    foreach (var v in typedAttribute.Values)
                    {
                        subBendy1[v.Key.ToUmbracoAlias(StringAliasCaseType.PascalCase)] = v.Value;
                        subBendy2[v.Key.ToUmbracoAlias(StringAliasCaseType.Unchanged)]  = v.Value;
                    }

                    bendy[typedAttribute.AttributeDefinition.Alias.ToUmbracoAlias(StringAliasCaseType.PascalCase)] = subBendy1;
                    bendy[typedAttribute.AttributeDefinition.Alias.ToUmbracoAlias(StringAliasCaseType.Unchanged)]  = subBendy2;
                }
                else
                {
                    bendy[typedAttribute.AttributeDefinition.Alias.ToUmbracoAlias(StringAliasCaseType.PascalCase)] = typedAttribute.Values.First().Value;
                    bendy[typedAttribute.AttributeDefinition.Alias.ToUmbracoAlias(StringAliasCaseType.Unchanged)]  = typedAttribute.Values.First().Value;
                }
            }

            bendy["__OriginalItem"] = content;

            bendy.WhenItemNotFound = (bent, membername) =>
            {
                if (membername.IsNullOrWhiteSpace())
                {
                    return(new BendyObject());
                }
                // Support recursively looking up a property similarly to v4
                if (membername.StartsWith("_") && membername.ElementAt(1).IsLowerCase())
                {
                    return(content.Field(membername.TrimStart('_'), true));
                }
                // Support pluralised document-type aliases inferring a query
                if (membername.EndsWith("s", StringComparison.InvariantCultureIgnoreCase))
                {
                    var alias = membername.TrimEnd("s");
                    var up    = alias.StartsWith("Ancestor", StringComparison.InvariantCultureIgnoreCase);
                    if (up)
                    {
                        alias = alias.TrimStart("Ancestor");
                    }
                    dynamic theBendy = bent;

                    return(up ? theBendy.AncestorsOrSelf.Where("NodeTypeAlias == @0", alias) : theBendy.DescendantsOrSelf.Where("NodeTypeAlias == @0", alias));
                }
                return(new BendyObject());
            };

            bendy.AddLazy("AllAncestorIds", () => content.AllAncestorIds());
            bendy.AddLazy("Ancestors", () =>
            {
                var ancestorIds = content.AllAncestorIds();
                return(new DynamicContentList(content.Id, hiveManager, new BendyObject(), ancestorIds));
            });
            bendy.AddLazy("AllAncestorIdsOrSelf", () => content.AllAncestorIdsOrSelf());
            bendy.AddLazy("AncestorsOrSelf", () =>
            {
                var ancestorIds = content.AllAncestorIdsOrSelf();
                return(new DynamicContentList(content.Id, hiveManager, new BendyObject(), ancestorIds));
            });
            bendy.AddLazy("AllDescendantIds", () => content.AllDescendantIds());
            bendy.AddLazy("Descendants", () =>
            {
                var descendantContentIds = content.AllDescendantIds();
                return(new DynamicContentList(content.Id, hiveManager, new BendyObject(), descendantContentIds));
            });
            bendy.AddLazy("AllDescendantIdsOrSelf", () => content.AllDescendantIdsOrSelf());
            bendy.AddLazy("DescendantsOrSelf", () =>
            {
                var descendantContentIds = content.AllDescendantIdsOrSelf();
                return(new DynamicContentList(content.Id, hiveManager, new BendyObject(), descendantContentIds));
            });
            bendy.AddLazy("AllChildIds", () => content.AllChildIds());
            bendy.AddLazy("Children", () =>
            {
                var childIds = content.AllChildIds();
                return(new DynamicContentList(content.Id, hiveManager, new BendyObject(), childIds));
            });
            bendy.AddLazy("Parent", () => content.ParentContent().Bend());
            bendy.AddLazy("Path", () => content.GetPath(hiveManager));
            bendy.AddLazy("PathById", () => content.GetPath(hiveManager));
            bendy.AddLazy("Level", () => content.GetPath(hiveManager).Level);

            //Add lazy url property
            bendy.AddLazy("Url", content.NiceUrl);
            bendy.AddLazy("NiceUrl", content.NiceUrl);
            bendy.AddLazy("NodeTypeAlias", () => content.ContentType.IfNotNull(x => x.Alias, string.Empty));
            bendy.AddLazy("Template", () => content.CurrentTemplate.IfNotNull(x => x.Alias, string.Empty));
            bendy.AddLazy("TemplateId", () => content.CurrentTemplate.IfNotNull(x => x.Id, HiveId.Empty));
            bendy.AddLazy("TemplateFileName", () => content.CurrentTemplate.IfNotNull(x => (string)x.Id.Value, string.Empty));
            bendy.AddLazy("CreateDate", () => content.UtcCreated);
            bendy.AddLazy("UpdateDate", () => content.UtcModified);
            bendy.AddLazy("StatusChangedDate", () => content.UtcStatusChanged);

            dynamicBendy.ContentType = content.ContentType;
            var nodeNameAlias = NodeNameAttributeDefinition.AliasValue.ToUmbracoAlias(StringAliasCaseType.PascalCase);

            try
            {
                dynamicBendy.Name    = bendy[nodeNameAlias].Name;
                dynamicBendy.UrlName = bendy[nodeNameAlias].UrlName;
            }
            catch
            {
                /* Nothing */
            }

            dynamicBendy.Id = content.Id;

            return(dynamicBendy);
        }