diff --git a/src/Umbraco.Core/Models/ContentTypeBase.cs b/src/Umbraco.Core/Models/ContentTypeBase.cs index ae0e587eb9..03fca722ea 100644 --- a/src/Umbraco.Core/Models/ContentTypeBase.cs +++ b/src/Umbraco.Core/Models/ContentTypeBase.cs @@ -130,7 +130,7 @@ namespace Umbraco.Core.Models get { return _alias; } set { - _alias = value; + _alias = value.ToUmbracoAlias(StringAliasCaseType.CamelCase, true); OnPropertyChanged(AliasSelector); } } diff --git a/src/umbraco.cms/businesslogic/ContentType.cs b/src/umbraco.cms/businesslogic/ContentType.cs index 533baf6407..0633c050fb 100644 --- a/src/umbraco.cms/businesslogic/ContentType.cs +++ b/src/umbraco.cms/businesslogic/ContentType.cs @@ -1,16 +1,15 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics; using System.Threading; using System.Runtime.CompilerServices; using System.Linq; +using Umbraco.Core; using Umbraco.Core.Models; using umbraco.cms.businesslogic.cache; using umbraco.cms.businesslogic.propertytype; using umbraco.cms.businesslogic.web; using umbraco.DataLayer; -using umbraco.BusinessLogic; using DataTypeDefinition = umbraco.cms.businesslogic.datatype.DataTypeDefinition; using Language = umbraco.cms.businesslogic.language.Language; using PropertyType = umbraco.cms.businesslogic.propertytype.PropertyType; @@ -76,6 +75,7 @@ namespace umbraco.cms.businesslogic internal ContentType(IContentTypeComposition contentType) : base(contentType) { + _contentType = contentType; } #endregion @@ -279,10 +279,74 @@ namespace umbraco.cms.businesslogic private static readonly object propertyTypesCacheSyncLock = new object(); + private IContentTypeComposition _contentType; + #endregion #region Public Properties + /// + /// The Alias of the ContentType, is used for import/export and more human readable initialization see: GetByAlias + /// method. + /// + public string Alias + { + get { return _alias; } + set + { + _alias = helpers.Casing.SafeAliasWithForcingCheck(value); + + // validate if alias is empty + if (String.IsNullOrEmpty(_alias)) + { + throw new ArgumentOutOfRangeException("An Alias cannot be empty"); + } + + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) + { + SqlHelper.ExecuteNonQuery("update cmsContentType set alias = @alias where nodeId = @id", + SqlHelper.CreateParameter("@alias", _alias), + SqlHelper.CreateParameter("@id", Id)); + } + else + { + _contentType.Alias = _alias; + } + + // Remove from cache + FlushFromCache(Id); + } + } + + /// + /// A Content object is often (always) represented in the treeview in the Umbraco console, the ContentType defines + /// which Icon the Content of this type is representated with. + /// + public string IconUrl + { + get { return _iconurl; } + set + { + _iconurl = value; + + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) + { + SqlHelper.ExecuteNonQuery("update cmsContentType set icon='" + value + "' where nodeid = " + Id); + } + else + { + _contentType.Icon = _iconurl; + } + + // Remove from cache + FlushFromCache(Id); + } + } + /// /// Get or Sets the Container status of the Content Type. A Container Content Type doesn't show its children in the tree, /// but instead adds a tab when edited showing its children in a grid @@ -293,10 +357,20 @@ namespace umbraco.cms.businesslogic set { _isContainerContentType = value; - SqlHelper.ExecuteNonQuery( + + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) + { + SqlHelper.ExecuteNonQuery( "update cmsContentType set isContainer = @isContainer where nodeId = @id", SqlHelper.CreateParameter("@isContainer", value), SqlHelper.CreateParameter("@id", Id)); + } + else + { + _contentType.IsContainer = _isContainerContentType; + } } } @@ -309,10 +383,20 @@ namespace umbraco.cms.businesslogic set { _allowAtRoot = value; - SqlHelper.ExecuteNonQuery( + + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) + { + SqlHelper.ExecuteNonQuery( "update cmsContentType set allowAtRoot = @allowAtRoot where nodeId = @id", SqlHelper.CreateParameter("@allowAtRoot", value), SqlHelper.CreateParameter("@id", Id)); + } + else + { + _contentType.AllowedAsRoot = _allowAtRoot; + } } } @@ -350,10 +434,20 @@ namespace umbraco.cms.businesslogic set { _description = value; - SqlHelper.ExecuteNonQuery( + + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) + { + SqlHelper.ExecuteNonQuery( "update cmsContentType set description = @description where nodeId = @id", SqlHelper.CreateParameter("@description", value), SqlHelper.CreateParameter("@id", Id)); + } + else + { + _contentType.Description = _description; + } FlushFromCache(Id); } @@ -369,10 +463,20 @@ namespace umbraco.cms.businesslogic set { _thumbnail = value; - SqlHelper.ExecuteNonQuery( + + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) + { + SqlHelper.ExecuteNonQuery( "update cmsContentType set thumbnail = @thumbnail where nodeId = @id", SqlHelper.CreateParameter("@thumbnail", value), SqlHelper.CreateParameter("@id", Id)); + } + else + { + _contentType.Thumbnail = _thumbnail; + } FlushFromCache(Id); } @@ -410,6 +514,11 @@ namespace umbraco.cms.businesslogic { base.Text = value; + if (_contentType != null) + { + _contentType.Name = value; + } + // Remove from cache FlushFromCache(Id); } @@ -466,50 +575,6 @@ namespace umbraco.cms.businesslogic } } - /// - /// The Alias of the ContentType, is used for import/export and more human readable initialization see: GetByAlias - /// method. - /// - public string Alias - { - get { return _alias; } - set - { - _alias = helpers.Casing.SafeAliasWithForcingCheck(value); - - // validate if alias is empty - if (String.IsNullOrEmpty(_alias)) - { - throw new ArgumentOutOfRangeException("An Alias cannot be empty"); - } - - SqlHelper.ExecuteNonQuery( - "update cmsContentType set alias = @alias where nodeId = @id", - SqlHelper.CreateParameter("@alias", _alias), - SqlHelper.CreateParameter("@id", Id)); - - // Remove from cache - FlushFromCache(Id); - } - } - - /// - /// A Content object is often (always) represented in the treeview in the Umbraco console, the ContentType defines - /// which Icon the Content of this type is representated with. - /// - public string IconUrl - { - get { return _iconurl; } - set - { - _iconurl = value; - SqlHelper.ExecuteNonQuery("update cmsContentType set icon='" + value + "' where nodeid = " + Id); - // Remove from cache - FlushFromCache(Id); - } - } - - public List MasterContentTypes { get @@ -703,13 +768,31 @@ namespace umbraco.cms.businesslogic { m_AllowedChildContentTypeIDs = value.ToList(); - SqlHelper.ExecuteNonQuery( - "delete from cmsContentTypeAllowedContentType where id=" + Id); - foreach (int i in value) + //This switches between using new vs. legacy api. + //Note that this is currently only done to support both DocumentType and MediaType, which use the new api and MemberType that doesn't. + if (_contentType == null) { SqlHelper.ExecuteNonQuery( - "insert into cmsContentTypeAllowedContentType (id,AllowedId) values (" + - Id + "," + i + ")"); + "delete from cmsContentTypeAllowedContentType where id=" + Id); + foreach (int i in value) + { + SqlHelper.ExecuteNonQuery( + "insert into cmsContentTypeAllowedContentType (id,AllowedId) values (" + + Id + "," + i + ")"); + } + } + else + { + var list = new List(); + int sort = 0; + foreach (var i in value) + { + int id = i; + list.Add(new ContentTypeSort{Id = new Lazy(() => id), SortOrder = sort}); + sort++; + } + + _contentType.AllowedContentTypes = list; } } } @@ -894,11 +977,6 @@ namespace umbraco.cms.businesslogic // NH 22-08-08, Get from the property type stack to ensure support of master document types object o = this.PropertyTypes.Find(pt => pt.Alias == alias); - //object o = SqlHelper.ExecuteScalar( - // "Select id from cmsPropertyType where contentTypeId=@contentTypeId And Alias=@alias", - // SqlHelper.CreateParameter("@contentTypeId", this.Id), - // SqlHelper.CreateParameter("@alias", alias)); - if (o == null) { return null; @@ -907,12 +985,6 @@ namespace umbraco.cms.businesslogic { return (PropertyType)o; } - - //int propertyTypeId; - //if (!int.TryParse(o.ToString(), out propertyTypeId)) - // return null; - - //return PropertyType.GetPropertyType(propertyTypeId); } /// @@ -957,7 +1029,7 @@ namespace umbraco.cms.businesslogic #region Protected Methods - internal protected void PopulateContentTypeFromContentTypeBase(IContentTypeBase contentType) + internal protected void PopulateContentTypeFromContentTypeBase(IContentTypeComposition contentType) { _alias = contentType.Alias; _iconurl = contentType.Icon; @@ -965,6 +1037,9 @@ namespace umbraco.cms.businesslogic _allowAtRoot = contentType.AllowedAsRoot; _thumbnail = contentType.Thumbnail; _description = contentType.Description; + + if (_contentType == null) + _contentType = contentType; } protected void PopulateContentTypeNodeFromReader(IRecordsReader dr) @@ -987,6 +1062,26 @@ namespace umbraco.cms.businesslogic { base.setupNode(); + //Try to load the ContentType/MediaType through the new public api + if (nodeObjectType == new Guid("A2CB7800-F571-4787-9638-BC48539A0EFB")) + { + var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(Id); + if (contentType != null) + { + PopulateContentTypeFromContentTypeBase(contentType); + return; + } + } + else if (nodeObjectType == new Guid("4EA4382B-2F5A-4C2B-9587-AE9B3CF3602E")) + { + var mediaType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType(Id); + if (mediaType != null) + { + PopulateContentTypeFromContentTypeBase(mediaType); + return; + } + } + // TODO: Load master content types using (IRecordsReader dr = SqlHelper.ExecuteReader("Select allowAtRoot, isContainer, Alias,icon,thumbnail,description from cmsContentType where nodeid=" + Id) diff --git a/src/umbraco.cms/businesslogic/media/MediaType.cs b/src/umbraco.cms/businesslogic/media/MediaType.cs index fe229009f9..a25834698c 100644 --- a/src/umbraco.cms/businesslogic/media/MediaType.cs +++ b/src/umbraco.cms/businesslogic/media/MediaType.cs @@ -3,10 +3,8 @@ using System.Collections.Generic; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Persistence.Caching; -using umbraco.DataLayer; using System.Linq; - namespace umbraco.cms.businesslogic.media { /// @@ -94,7 +92,7 @@ namespace umbraco.cms.businesslogic.media [Obsolete("Deprecated, Use Umbraco.Core.Models.MediaType and Umbraco.Core.Services.ContentTypeService.Save()", false)] public static MediaType MakeNew(BusinessLogic.User u, string Text) { - var mediaType = new Umbraco.Core.Models.MediaType(-1) { Name = Text, Alias = Text, CreatorId = u.Id}; + var mediaType = new Umbraco.Core.Models.MediaType(-1) { Name = Text, Alias = Text, CreatorId = u.Id, Thumbnail = "folder.png", Icon = "folder.gif" }; ApplicationContext.Current.Services.ContentTypeService.Save(mediaType, u.Id); var mt = new MediaType(mediaType.Id); @@ -119,6 +117,8 @@ namespace umbraco.cms.businesslogic.media //Ensure that MediaTypes are reloaded from db by clearing cache InMemoryCacheProvider.Current.Clear(); + ApplicationContext.Current.Services.ContentTypeService.Save(_mediaType); + base.Save(); FireBeforeSave(e); diff --git a/src/umbraco.cms/businesslogic/member/MemberType.cs b/src/umbraco.cms/businesslogic/member/MemberType.cs index 30b04b5b23..c6ba370986 100644 --- a/src/umbraco.cms/businesslogic/member/MemberType.cs +++ b/src/umbraco.cms/businesslogic/member/MemberType.cs @@ -1,5 +1,4 @@ using System; -using System.Data; using System.Xml; using umbraco.cms.businesslogic.propertytype; using System.Linq; @@ -12,10 +11,16 @@ namespace umbraco.cms.businesslogic.member /// Due to the inheritance of the ContentType class, it enables definition of generic datafields on a Members. /// public class MemberType : ContentType - { - private static Guid _objectType = new Guid("9b5416fb-e72f-45a9-a07b-5a9a2709ce43"); + { + #region Private Members + + private static Guid _objectType = new Guid("9b5416fb-e72f-45a9-a07b-5a9a2709ce43"); + + #endregion - /// + #region Constructors + + /// /// Initializes a new instance of the MemberType class. /// /// MemberType id @@ -27,56 +32,11 @@ namespace umbraco.cms.businesslogic.member /// MemberType id public MemberType(Guid id) : base(id) { } - /// - /// Used to persist object changes to the database. In Version3.0 it's just a stub for future compatibility - /// - public override void Save() - { - SaveEventArgs e = new SaveEventArgs(); - FireBeforeSave(e); + #endregion - if(!e.Cancel){ - FireAfterSave(e); - } - } + #region Public Methods - /// - /// Create a new MemberType - /// - /// The name of the MemberType - /// Creator of the MemberType - public static MemberType MakeNew(User u,string Text) - { - int ParentId= -1; - int level = 1; - Guid uniqueId = Guid.NewGuid(); - CMSNode n = CMSNode.MakeNew(ParentId, _objectType, u.Id, level,Text, uniqueId); - - ContentType.Create(n.Id, Text,""); - MemberType mt = new MemberType(n.Id); - mt.IconUrl = "member.gif"; - NewEventArgs e = new NewEventArgs(); - mt.OnNew(e); - - return mt; - } - - /// - /// Retrieve a list of all MemberTypes - /// - public new static MemberType[] GetAll - { - get - { - Guid[] Ids = CMSNode.getAllUniquesFromObjectType(_objectType); - - MemberType[] retVal = new MemberType[Ids.Length]; - for (int i = 0; i < Ids.Length; i++) retVal[i] = new MemberType(Ids[i]); - return retVal; - } - } - - /// + /// /// Get an true/false if the Member can edit the given data defined in the propertytype /// /// Propertytype to edit @@ -90,29 +50,7 @@ namespace umbraco.cms.businesslogic.member } return false; } - - /// - /// Get a MemberType by it's alias - /// - /// The alias of the MemberType - /// The MemberType with the given Alias - public new static MemberType GetByAlias(string Alias) - { - try - { - return - new MemberType( - SqlHelper.ExecuteScalar(@"SELECT nodeid from cmsContentType INNER JOIN umbracoNode on cmsContentType.nodeId = umbracoNode.id WHERE nodeObjectType=@nodeObjectType AND alias=@alias", - SqlHelper.CreateParameter("@nodeObjectType", MemberType._objectType), - SqlHelper.CreateParameter("@alias", Alias))); - } - catch - { - return null; - } - } - - + /// /// Get an true/false if the given data defined in the propertytype, should be visible on the members profile page /// @@ -142,13 +80,7 @@ namespace umbraco.cms.businesslogic.member SqlHelper.ExecuteNonQuery("insert into cmsMemberType (NodeId, propertytypeid, memberCanEdit,viewOnProfile) values (" + this.Id + "," + pt.Id + ", " + tmpval + ",0)"); } - - private bool propertyTypeRegistered(PropertyType pt) - { - return (SqlHelper.ExecuteScalar("Select count(pk) as tmp from cmsMemberType where NodeId = " + this.Id + " And propertytypeId = " + pt.Id) > 0); - } - - + /// /// Set if the data should be displayed on members of this type's profilepage /// @@ -190,6 +122,20 @@ namespace umbraco.cms.businesslogic.member FireAfterDelete(e); } } + + /// + /// Used to persist object changes to the database. In Version3.0 it's just a stub for future compatibility + /// + public override void Save() + { + SaveEventArgs e = new SaveEventArgs(); + FireBeforeSave(e); + + if (!e.Cancel) + { + FireAfterSave(e); + } + } public XmlElement ToXml(XmlDocument xd) { @@ -235,6 +181,76 @@ namespace umbraco.cms.businesslogic.member return root; } + #endregion + + #region Public Static Methods + /// + /// Get a MemberType by it's alias + /// + /// The alias of the MemberType + /// The MemberType with the given Alias + public new static MemberType GetByAlias(string Alias) + { + try + { + return + new MemberType( + SqlHelper.ExecuteScalar(@"SELECT nodeid from cmsContentType INNER JOIN umbracoNode on cmsContentType.nodeId = umbracoNode.id WHERE nodeObjectType=@nodeObjectType AND alias=@alias", + SqlHelper.CreateParameter("@nodeObjectType", MemberType._objectType), + SqlHelper.CreateParameter("@alias", Alias))); + } + catch + { + return null; + } + } + + /// + /// Create a new MemberType + /// + /// The name of the MemberType + /// Creator of the MemberType + public static MemberType MakeNew(User u, string Text) + { + int ParentId = -1; + int level = 1; + Guid uniqueId = Guid.NewGuid(); + CMSNode n = CMSNode.MakeNew(ParentId, _objectType, u.Id, level, Text, uniqueId); + + ContentType.Create(n.Id, Text, ""); + MemberType mt = new MemberType(n.Id); + mt.IconUrl = "member.gif"; + NewEventArgs e = new NewEventArgs(); + mt.OnNew(e); + + return mt; + } + + /// + /// Retrieve a list of all MemberTypes + /// + public new static MemberType[] GetAll + { + get + { + Guid[] Ids = CMSNode.getAllUniquesFromObjectType(_objectType); + + MemberType[] retVal = new MemberType[Ids.Length]; + for (int i = 0; i < Ids.Length; i++) retVal[i] = new MemberType(Ids[i]); + return retVal; + } + } + #endregion + + #region Private Methods + + private bool propertyTypeRegistered(PropertyType pt) + { + return (SqlHelper.ExecuteScalar("Select count(pk) as tmp from cmsMemberType where NodeId = " + this.Id + " And propertytypeId = " + pt.Id) > 0); + } + + #endregion + #region Events /// diff --git a/src/umbraco.cms/businesslogic/web/DocumentType.cs b/src/umbraco.cms/businesslogic/web/DocumentType.cs index 109834f055..581cf50110 100644 --- a/src/umbraco.cms/businesslogic/web/DocumentType.cs +++ b/src/umbraco.cms/businesslogic/web/DocumentType.cs @@ -141,7 +141,7 @@ namespace umbraco.cms.businesslogic.web [Obsolete("Deprecated, Use Umbraco.Core.Models.ContentType and Umbraco.Core.Services.ContentTypeService.Save()", false)] public static DocumentType MakeNew(User u, string Text) { - var contentType = new Umbraco.Core.Models.ContentType(-1) { Name = Text, Alias = Text, CreatorId = u.Id}; + var contentType = new Umbraco.Core.Models.ContentType(-1) { Name = Text, Alias = Text, CreatorId = u.Id, Thumbnail = "folder.png", Icon = "folder.gif" }; ApplicationContext.Current.Services.ContentTypeService.Save(contentType, u.Id); var newDt = new DocumentType(contentType.Id); @@ -463,6 +463,8 @@ namespace umbraco.cms.businesslogic.web //Ensure that MediaTypes are reloaded from db by clearing cache InMemoryCacheProvider.Current.Clear(); + ApplicationContext.Current.Services.ContentTypeService.Save(_contentType); + base.Save(); FireAfterSave(e); }