From bca0886ed65a56528c231ce7528032e31efb19d2 Mon Sep 17 00:00:00 2001 From: hartvig Date: Tue, 20 Jul 2010 12:21:13 +0000 Subject: [PATCH] Fixing 28013, 28017 and 28111. Exceptions caused by FK conflicts when deleting templates and property types [TFS Changeset #73429] --- .../propertytype/propertytype.cs | 35 ++++++++----- .../cms/businesslogic/template/Template.cs | 49 ++++++++++--------- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/umbraco/cms/businesslogic/propertytype/propertytype.cs b/umbraco/cms/businesslogic/propertytype/propertytype.cs index 76e3b8ca0e..20118a4702 100644 --- a/umbraco/cms/businesslogic/propertytype/propertytype.cs +++ b/umbraco/cms/businesslogic/propertytype/propertytype.cs @@ -11,6 +11,7 @@ using umbraco.cms.businesslogic.language; using umbraco.interfaces; using umbraco.DataLayer; using umbraco.BusinessLogic; +using umbraco.cms.businesslogic.web; namespace umbraco.cms.businesslogic.propertytype { @@ -320,20 +321,12 @@ namespace umbraco.cms.businesslogic.propertytype // flush cache FlushCache(); - // Delete all properties of propertytype - var objs = Content.getContentOfContentType(new ContentType(_contenttypeid)); - foreach(Content c in objs.ToList()) - { - var prop = c.getProperty(this); - if (prop != null) - { - prop.delete(); - } - } + // clean all properties on inherited document types (if this propertytype is removed from a master) + DocumentType.GetAllAsList().FindAll(dt => dt.MasterContentType == _contenttypeid).ForEach(dt => cleanPropertiesOnDeletion(dt.Id)); + + // Delete all properties of propertytype + cleanPropertiesOnDeletion(_contenttypeid); - // invalidate content type cache - ContentType.GetContentType(this.ContentTypeId).FlushFromCache(this.ContentTypeId); - // Delete PropertyType .. SqlHelper.ExecuteNonQuery("Delete from cmsPropertyType where id = " + this.Id); @@ -342,6 +335,22 @@ namespace umbraco.cms.businesslogic.propertytype this.InvalidateCache(); } + private void cleanPropertiesOnDeletion(int contentTypeId) + { + var objs = Content.getContentOfContentType(new ContentType(contentTypeId)); + foreach (Content c in objs.ToList()) + { + var prop = c.getProperty(this); + if (prop != null) + { + prop.delete(); + } + } + + // invalidate content type cache + ContentType.GetContentType(contentTypeId).FlushFromCache(contentTypeId); + } + public IDataType GetEditControl(object Value, bool IsPostBack) { IDataType dt = this.DataTypeDefinition.DataType; diff --git a/umbraco/cms/businesslogic/template/Template.cs b/umbraco/cms/businesslogic/template/Template.cs index 8f3df97683..5c73e67e75 100644 --- a/umbraco/cms/businesslogic/template/Template.cs +++ b/umbraco/cms/businesslogic/template/Template.cs @@ -23,7 +23,7 @@ namespace umbraco.cms.businesslogic.template #region Private members - + private string _OutputContentType; private string _design; private string _alias; @@ -42,13 +42,13 @@ namespace umbraco.cms.businesslogic.template private static object templateCacheSyncLock = new object(); private static readonly string UmbracoTemplateCacheKey = "UmbracoTemplateCache"; private static object _templateLoaderLocker = new object(); - private static Guid _objectType = new Guid("6fbde604-4178-42ce-a10b-8a2600a2f07d"); + private static Guid _objectType = new Guid("6fbde604-4178-42ce-a10b-8a2600a2f07d"); #endregion public string MasterPageFile { - get { return IOHelper.MapPath( SystemDirectories.Masterpages + "/" + Alias.Replace(" ", "") + ".master") ; } + get { return IOHelper.MapPath(SystemDirectories.Masterpages + "/" + Alias.Replace(" ", "") + ".master"); } } public static Hashtable TemplateAliases @@ -60,7 +60,7 @@ namespace umbraco.cms.businesslogic.template #region Constructors public Template(int id) : base(id) { } - public Template(Guid id) : base(id) { } + public Template(Guid id) : base(id) { } #endregion /// @@ -182,7 +182,7 @@ namespace umbraco.cms.businesslogic.template get { return (_mastertemplate > 0); } } - + public override bool HasChildren { get @@ -315,7 +315,7 @@ namespace umbraco.cms.businesslogic.template return t; } - public static Template MakeNew(string Name, BusinessLogic.User u) + public static Template MakeNew(string Name, BusinessLogic.User u) { // CMSNode MakeNew(int parentId, Guid objectType, int userId, int level, string text, Guid uniqueID) CMSNode n = CMSNode.MakeNew(-1, _objectType, u.Id, 1, Name, Guid.NewGuid()); @@ -392,7 +392,7 @@ namespace umbraco.cms.businesslogic.template _templateAliasesInitialized = true; } - + } } } @@ -406,18 +406,9 @@ namespace umbraco.cms.businesslogic.template throw new InvalidOperationException("Can't delete a master template. Remove any bindings from child templates first."); } - // don't allow template deletion if it is in use - // (get all doc types and filter based on any that have the template id of this one) + // NH: Changed this; if you delete a template we'll remove all references instead of + // throwing an exception if (DocumentType.GetAllAsList().Where(x => x.allowedTemplates.Select(t => t.Id).Contains(this.Id)).Count() > 0) - { - Log.Add(LogTypes.Error, this.Id, "Can't delete a template that is assigned to existing content"); - throw new InvalidOperationException("Can't delete a template that is assigned to existing content"); - } - - DeleteEventArgs e = new DeleteEventArgs(); - FireBeforeDelete(e); - - if (!e.Cancel) { // the uncommented code below have been refactored into removeAllReferences method that clears template // from documenttypes, subtemplates and documents. @@ -430,6 +421,20 @@ namespace umbraco.cms.businesslogic.template // Need to update any other template that references this one as it's master to NULL SqlHelper.ExecuteNonQuery("update cmsTemplate set [master] = NULL where [master] = " + this.Id); */ + + // don't allow template deletion if it is in use + // (get all doc types and filter based on any that have the template id of this one) + /* + Log.Add(LogTypes.Error, this.Id, "Can't delete a template that is assigned to existing content"); + throw new InvalidOperationException("Can't delete a template that is assigned to existing content"); + */ + } + + DeleteEventArgs e = new DeleteEventArgs(); + FireBeforeDelete(e); + + if (!e.Cancel) + { //re-set the template aliases _templateAliasesInitialized = false; initTemplateAliases(); @@ -611,14 +616,14 @@ namespace umbraco.cms.businesslogic.template } //then kill the old file.. - string _oldFile = IOHelper.MapPath( SystemDirectories.Masterpages + "/" + _oldAlias.Replace(" ", "") + ".master"); - + string _oldFile = IOHelper.MapPath(SystemDirectories.Masterpages + "/" + _oldAlias.Replace(" ", "") + ".master"); + if (System.IO.File.Exists(_oldFile)) System.IO.File.Delete(_oldFile); } // save the file in UTF-8 - + File.WriteAllText(MasterPageFile, masterPageContent, System.Text.Encoding.UTF8); } @@ -831,7 +836,7 @@ namespace umbraco.cms.businesslogic.template { if (AfterDelete != null) AfterDelete(this, e); - } + } #endregion }