Esempio n. 1
0
        /// <summary>
        /// Creates a new property on the ContentType under the correct tab
        /// </summary>
        /// <param name="newContentType"></param>
        /// <param name="tabName"></param>
        /// <param name="dataTypeService"></param>
        /// <param name="atTabGeneric"></param>
        /// <param name="item"></param>
        private static void CreateProperty(IContentType newContentType, string tabName, IDataTypeService dataTypeService, bool atTabGeneric, PropertyInfo item)
        {
            UmbracoPropertyAttribute attribute = item.GetCustomAttribute <UmbracoPropertyAttribute>();

            IDataTypeDefinition dataTypeDef;

            if (string.IsNullOrEmpty(attribute.DataTypeInstanceName))
            {
                dataTypeDef = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(attribute.DataType).FirstOrDefault();
            }
            else
            {
                dataTypeDef = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(attribute.DataType).FirstOrDefault(x => x.Name == attribute.DataTypeInstanceName);
            }

            if (dataTypeDef != null)
            {
                PropertyType propertyType = new PropertyType(dataTypeDef);
                propertyType.Name        = attribute.Name;
                propertyType.Alias       = (atTabGeneric ? attribute.Alias : UmbracoCodeFirstExtensions.HyphenToUnderscore(UmbracoCodeFirstExtensions.ParseUrl(attribute.Alias + "_" + tabName, false)));
                propertyType.Description = attribute.Description;
                propertyType.Mandatory   = attribute.Mandatory;

                if (atTabGeneric)
                {
                    newContentType.AddPropertyType(propertyType);
                }
                else
                {
                    newContentType.AddPropertyType(propertyType, tabName);
                }
            }
        }
Esempio n. 2
0
        private static string VerifyExistingProperty(IContentType contentType, string tabName, IDataTypeService dataTypeService, PropertyInfo item, bool atGenericTab = false)
        {
            UmbracoPropertyAttribute attribute = item.GetCustomAttribute <UmbracoPropertyAttribute>();
            IDataTypeDefinition      dataTypeDef;

            if (string.IsNullOrEmpty(attribute.DataTypeInstanceName))
            {
                dataTypeDef = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(attribute.DataType).FirstOrDefault();
            }
            else
            {
                dataTypeDef = dataTypeService.GetDataTypeDefinitionByPropertyEditorAlias(attribute.DataType).FirstOrDefault(x => x.Name == attribute.DataTypeInstanceName);
            }

            if (dataTypeDef != null)
            {
                PropertyType property;
                bool         alreadyExisted = contentType.PropertyTypeExists(attribute.Alias);
                // TODO: Added attribute.Tab != null after Generic Properties add, is this bulletproof?
                if (alreadyExisted && attribute.Tab != null)
                {
                    property = contentType.PropertyTypes.FirstOrDefault(x => x.Alias == attribute.Alias);
                }
                else
                {
                    property = new PropertyType(dataTypeDef);
                }

                property.Name = attribute.Name;
                //TODO: correct name?
                property.Alias       = (atGenericTab ? attribute.Alias : UmbracoCodeFirstExtensions.HyphenToUnderscore(UmbracoCodeFirstExtensions.ParseUrl(attribute.Alias + "_" + tabName, false)));
                property.Description = attribute.Description;
                property.Mandatory   = attribute.Mandatory;

                if (!alreadyExisted)
                {
                    if (atGenericTab)
                    {
                        contentType.AddPropertyType(property);
                    }
                    else
                    {
                        contentType.AddPropertyType(property, tabName);
                    }
                }

                return(property.Alias);
            }
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// Abstract base class for any entity you want to be generated
        /// The persist method makes sure that you can save all the changes to you entity back into the database
        /// </summary>
        /// <param name="contentId">Id of the Umbraco Document</param>
        /// <param name="userId"></param>
        /// <param name="raiseEvents"></param>
        public virtual void Persist(int contentId, int userId = 0, bool raiseEvents = false)
        {
            IContentService contentSerivce = ApplicationContext.Current.Services.ContentService;
            IContent        content        = contentSerivce.GetById(contentId);

            //search for propertys with the UmbracoTab on
            Type currentType = this.GetType();
            var  propertiesWithTabAttribute = currentType.GetProperties().Where(x => x.GetCustomAttributes <UmbracoTabAttribute>() != null).ToArray();
            int  length = propertiesWithTabAttribute.Count();

            for (int i = 0; i < length; i++)
            {
                PropertyInfo        tabProperty   = propertiesWithTabAttribute[i];
                Type                tabType       = tabProperty.PropertyType;
                object              instanceOfTab = tabProperty.GetValue(this);
                UmbracoTabAttribute tabAttribute  = tabProperty.GetCustomAttribute <UmbracoTabAttribute>();

                //persist the fields foreach tab
                var propertiesInsideTab = tabType.GetProperties().Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null).ToArray();
                int propertyLength      = propertiesInsideTab.Length;
                for (int j = 0; j < propertyLength; j++)
                {
                    PropertyInfo             property = propertiesInsideTab[j];
                    UmbracoPropertyAttribute umbracoPropertyAttribute = property.GetCustomAttribute <UmbracoPropertyAttribute>();
                    object propertyValue = property.GetValue(instanceOfTab);
                    string alias         = UmbracoCodeFirstExtensions.HyphenToUnderscore(UmbracoCodeFirstExtensions.ParseUrl(umbracoPropertyAttribute.Alias + "_" + tabAttribute.Name, false));
                    SetPropertyOnIContent(content, umbracoPropertyAttribute, propertyValue, alias);
                }
            }

            //properties on generic tab
            var propertiesOnGenericTab = currentType.GetProperties().Where(x => x.GetCustomAttribute <UmbracoPropertyAttribute>() != null);

            foreach (var item in propertiesOnGenericTab)
            {
                UmbracoPropertyAttribute umbracoPropertyAttribute = item.GetCustomAttribute <UmbracoPropertyAttribute>();
                object propertyValue = item.GetValue(this);
                SetPropertyOnIContent(content, umbracoPropertyAttribute, propertyValue);
            }

            //persist object into umbraco database
            contentSerivce.SaveAndPublishWithStatus(content, userId, raiseEvents);
        }