From 20850632314e1c8dca0a2d5a3a9eed9731436fed Mon Sep 17 00:00:00 2001 From: Per Ploug Date: Thu, 12 Jan 2017 10:07:44 +0100 Subject: [PATCH] Fixes issue with creating template below a parent template --- .../src/common/resources/template.resource.js | 5 +- .../src/views/templates/edit.controller.js | 2 +- src/Umbraco.Web/Editors/TemplateController.cs | 305 +++++++++--------- 3 files changed, 162 insertions(+), 150 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/template.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/template.resource.js index c90bb1766d..3052d532bb 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/template.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/template.resource.js @@ -107,13 +107,14 @@ function templateResource($q, $http, umbDataFormatter, umbRequestHelper) { * @returns {Promise} resourcePromise object containing the template scaffold. * */ - getScaffold: function () { + getScaffold: function (id) { return umbRequestHelper.resourcePromise( $http.get( umbRequestHelper.getApiUrl( "templateApiBaseUrl", - "GetEmpty")), + "GetScaffold", + [{ id: id }] )), "Failed to retrieve data for empty template"); }, diff --git a/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js index e67273850f..8e2b8337ad 100644 --- a/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/templates/edit.controller.js @@ -95,7 +95,7 @@ if($routeParams.create){ - templateResource.getScaffold().then(function(template){ + templateResource.getScaffold(($routeParams.id)).then(function (template) { vm.ready(template); }); diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 83a6bb5fe1..40effc10fa 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -1,161 +1,172 @@ -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Web.Http; -using AutoMapper; -using Umbraco.Core.IO; -using Umbraco.Core.Models; -using Umbraco.Core.Services; -using Umbraco.Web.Models.ContentEditing; -using Umbraco.Web.Mvc; -using Umbraco.Web.WebApi; -using Umbraco.Web.WebApi.Filters; -using Constants = Umbraco.Core.Constants; - -namespace Umbraco.Web.Editors -{ - [PluginController("UmbracoApi")] - [UmbracoTreeAuthorize(Constants.Trees.Templates)] - public class TemplateController : BackOfficeNotificationsController - { - /// - /// Gets data type by alias - /// - /// - /// - public TemplateDisplay GetByAlias(string alias) - { - var template = Services.FileService.GetTemplate(alias); - return template == null ? null : Mapper.Map(template); - } - - /// - /// Get all templates - /// - /// - public IEnumerable GetAll() - { - return Services.FileService.GetTemplates().Select(Mapper.Map); - } - - /// - /// Gets the content json for the content id - /// - /// - /// - public TemplateDisplay GetById(int id) - { - var template = Services.FileService.GetTemplate(id); - if (template == null) - throw new HttpResponseException(HttpStatusCode.NotFound); - - return Mapper.Map(template); - } - - /// - /// Deletes a template wth a given ID - /// - /// - /// - [HttpDelete] - [HttpPost] - public HttpResponseMessage DeleteById(int id) - { - var template = Services.FileService.GetTemplate(id); - if (template == null) - throw new HttpResponseException(HttpStatusCode.NotFound); - - Services.FileService.DeleteTemplate(template.Alias); - return Request.CreateResponse(HttpStatusCode.OK); - } - - public TemplateDisplay GetEmpty() - { - var dt = new Template("", ""); - var content = ViewHelper.GetDefaultFileContent(); - - var scaffold = Mapper.Map(dt); - scaffold.Path = "-1"; - scaffold.Content = content + "\r\n\r\n@* the fun starts here *@\r\n\r\n"; - return scaffold; - } - - /// - /// Saves the data type - /// - /// - /// - public TemplateDisplay PostSave(TemplateDisplay display) - { - - //Checking the submitted is valid with the Required attributes decorated on the ViewModel - if (ModelState.IsValid == false) - { - throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); - } - - - if (display.Id > 0) - { - // update - var template = Services.FileService.GetTemplate(display.Id); - if (template == null) - throw new HttpResponseException(HttpStatusCode.NotFound); - +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; +using AutoMapper; +using Umbraco.Core.IO; +using Umbraco.Core.Models; +using Umbraco.Core.Services; +using Umbraco.Web.Models.ContentEditing; +using Umbraco.Web.Mvc; +using Umbraco.Web.WebApi; +using Umbraco.Web.WebApi.Filters; +using Constants = Umbraco.Core.Constants; + +namespace Umbraco.Web.Editors +{ + [PluginController("UmbracoApi")] + [UmbracoTreeAuthorize(Constants.Trees.Templates)] + public class TemplateController : BackOfficeNotificationsController + { + /// + /// Gets data type by alias + /// + /// + /// + public TemplateDisplay GetByAlias(string alias) + { + var template = Services.FileService.GetTemplate(alias); + return template == null ? null : Mapper.Map(template); + } + + /// + /// Get all templates + /// + /// + public IEnumerable GetAll() + { + return Services.FileService.GetTemplates().Select(Mapper.Map); + } + + /// + /// Gets the content json for the content id + /// + /// + /// + public TemplateDisplay GetById(int id) + { + var template = Services.FileService.GetTemplate(id); + if (template == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + return Mapper.Map(template); + } + + /// + /// Deletes a template wth a given ID + /// + /// + /// + [HttpDelete] + [HttpPost] + public HttpResponseMessage DeleteById(int id) + { + var template = Services.FileService.GetTemplate(id); + if (template == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + Services.FileService.DeleteTemplate(template.Alias); + return Request.CreateResponse(HttpStatusCode.OK); + } + + public TemplateDisplay GetScaffold(int id) + { + //empty default + var dt = new Template("", ""); + dt.Path = "-1"; + + if (id > 0) + { + var master = Services.FileService.GetTemplate(id); + if(master != null) + { + dt.SetMasterTemplate(master); + } + } + + var content = ViewHelper.GetDefaultFileContent( layoutPageAlias: dt.MasterTemplateAlias ); + var scaffold = Mapper.Map(dt); + + scaffold.Content = content + "\r\n\r\n@* the fun starts here *@\r\n\r\n"; + return scaffold; + } + + /// + /// Saves the data type + /// + /// + /// + public TemplateDisplay PostSave(TemplateDisplay display) + { + + //Checking the submitted is valid with the Required attributes decorated on the ViewModel + if (ModelState.IsValid == false) + { + throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); + } + + + if (display.Id > 0) + { + // update + var template = Services.FileService.GetTemplate(display.Id); + if (template == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + var changeMaster = template.MasterTemplateAlias != display.MasterTemplateAlias; var changeAlias = template.Alias != display.Alias; - Mapper.Map(display, template); - - if (changeMaster) - { - if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) - { - - var master = Services.FileService.GetTemplate(display.MasterTemplateAlias); + Mapper.Map(display, template); + + if (changeMaster) + { + if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) + { + + var master = Services.FileService.GetTemplate(display.MasterTemplateAlias); if(master == null || master.Id == display.Id) { template.SetMasterTemplate(null); }else { template.SetMasterTemplate(master); - } - - } - else - { - //remove the master - template.SetMasterTemplate(null); - } - } - + } + + } + else + { + //remove the master + template.SetMasterTemplate(null); + } + } + Services.FileService.SaveTemplate(template); if (changeAlias) { template = Services.FileService.GetTemplate(template.Id); - } - - Mapper.Map(template, display); - } - else - { - //create - ITemplate master = null; - if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) - { - master = Services.FileService.GetTemplate(display.MasterTemplateAlias); - if (master == null) - throw new HttpResponseException(HttpStatusCode.NotFound); - } - - var template = Services.FileService.CreateTemplateWithIdentity(display.Name, display.Content, master); - //template = Services.FileService.GetTemplate(template.Id); - Mapper.Map(template, display); - } - - return display; - } - } -} + } + + Mapper.Map(template, display); + } + else + { + //create + ITemplate master = null; + if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) + { + master = Services.FileService.GetTemplate(display.MasterTemplateAlias); + if (master == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + } + + var template = Services.FileService.CreateTemplateWithIdentity(display.Name, display.Content, master); + //template = Services.FileService.GetTemplate(template.Id); + Mapper.Map(template, display); + } + + return display; + } + } +}