예제 #1
0
        /// <summary>
        /// public constructor - picker value is calculated from lookup, either the published value from cache, or the latest saved db value
        /// </summary>
        /// <param name="contextId">the id of the content, media or member</param>
        /// <param name="propertyAlias">the property alias</param>
        /// <param name="usePublishedValue">when true uses the published value, otherwise when false uses the lastest saved value (which may also be the published value)</param>
        public Picker(int contextId, string propertyAlias, bool usePublishedValue = true)
        {
            bool success = false;

            var publishedContent = new UmbracoHelper(UmbracoContext.Current).GetPublishedContent(contextId);

            if (usePublishedValue && publishedContent != null)
            {
                var property = publishedContent.GetProperty(propertyAlias);

                if (property != null)
                {
                    var propertyType = publishedContent.ContentType.PropertyTypes.Single(x => x.PropertyTypeAlias == property.PropertyTypeAlias);

                    this.ContextId           = publishedContent.Id;
                    this.ParentId            = (publishedContent.Parent != null) ? publishedContent.Parent.Id : -1;
                    this.PropertyAlias       = propertyAlias;
                    this.DataTypeId          = propertyType.DataTypeId;
                    this.PropertyEditorAlias = propertyType.PropertyEditorAlias;
                    this.SavedValue          = property.Value;

                    success = true;
                }
            }
            else // use unpublished data
            {
                var content = ApplicationContext.Current.Services.ContentService.GetById(contextId);

                if (content != null)
                {
                    var property = content.Properties.SingleOrDefault(x => x.Alias == propertyAlias);

                    if (property != null)
                    {
                        var propertyType = content.PropertyTypes.Single(x => x.Alias == property.Alias);

                        this.ContextId           = content.Id;
                        this.ParentId            = content.ParentId;
                        this.PropertyAlias       = propertyAlias;
                        this.DataTypeId          = propertyType.DataTypeDefinitionId;
                        this.PropertyEditorAlias = propertyType.PropertyEditorAlias;
                        this.SavedValue          = content.GetValue(PropertyAlias);

                        success = true;
                    }
                }
            }

            if (!success)
            {
                throw new Exception(string.Format("Unable to create Picker object for ContextId: {0}, PropertyAlias: {1}", contextId.ToString(), propertyAlias));
            }
        }