2012-12-30 03:11:21 +03:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
2013-04-19 10:18:46 -02:00
|
|
|
|
using System.Text;
|
2012-12-30 03:11:21 +03:00
|
|
|
|
using System.Web.Mvc;
|
2014-08-14 17:20:25 +02:00
|
|
|
|
using Umbraco.Core.Events;
|
2012-12-30 03:11:21 +03:00
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
2014-08-19 12:51:07 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
2014-08-19 10:39:52 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
2013-01-01 00:42:20 +03:00
|
|
|
|
using Umbraco.Web.Macros;
|
2012-12-30 03:11:21 +03:00
|
|
|
|
using Umbraco.Web.Mvc;
|
|
|
|
|
|
using umbraco;
|
2013-01-01 00:42:20 +03:00
|
|
|
|
using umbraco.cms.businesslogic.macro;
|
2013-08-30 14:15:51 +10:00
|
|
|
|
using System.Collections.Generic;
|
2012-12-31 02:51:30 +03:00
|
|
|
|
using Umbraco.Core;
|
2013-08-30 14:15:51 +10:00
|
|
|
|
|
2013-02-11 03:55:58 +06:00
|
|
|
|
using Template = umbraco.cms.businesslogic.template.Template;
|
2012-12-30 03:11:21 +03:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.WebServices
|
|
|
|
|
|
{
|
2013-02-09 04:05:01 +06:00
|
|
|
|
/// <summary>
|
2014-08-14 17:20:25 +02:00
|
|
|
|
/// A REST controller used to save files such as templates, partial views, macro files, etc...
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This isn't fully implemented yet but we should migrate all of the logic in the umbraco.presentation.webservices.codeEditorSave
|
|
|
|
|
|
/// over to this controller.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public class SaveFileController : UmbracoAuthorizedController
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-08-19 10:39:52 +02:00
|
|
|
|
/// Saves a partial view for a partial view macro
|
2014-08-14 17:20:25 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename"></param>
|
|
|
|
|
|
/// <param name="oldName"></param>
|
|
|
|
|
|
/// <param name="contents"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public JsonResult SavePartialView(string filename, string oldName, string contents)
|
|
|
|
|
|
{
|
|
|
|
|
|
var folderPath = SystemDirectories.MvcViews.EnsureEndsWith('/');// +"/Partials/";
|
|
|
|
|
|
var savePath = filename.StartsWith("~/") ? IOHelper.MapPath(filename) : IOHelper.MapPath(folderPath + filename);
|
|
|
|
|
|
|
2014-08-19 12:51:07 +02:00
|
|
|
|
var partialView = new PartialView(savePath)
|
2014-08-14 17:20:25 +02:00
|
|
|
|
{
|
2014-08-19 12:51:07 +02:00
|
|
|
|
BasePath = folderPath,
|
|
|
|
|
|
OldFileName = oldName,
|
|
|
|
|
|
FileName = filename,
|
|
|
|
|
|
Content = contents,
|
|
|
|
|
|
};
|
2014-08-14 17:20:25 +02:00
|
|
|
|
|
2014-08-20 14:08:45 +02:00
|
|
|
|
var fileService = (FileService)ApplicationContext.Current.Services.FileService;
|
2014-08-19 12:51:07 +02:00
|
|
|
|
var attempt = fileService.SavePartialView(partialView);
|
2014-08-14 17:20:25 +02:00
|
|
|
|
|
2014-08-19 12:51:07 +02:00
|
|
|
|
if (attempt.Success == false)
|
2014-08-14 17:20:25 +02:00
|
|
|
|
{
|
|
|
|
|
|
return Failed(
|
|
|
|
|
|
ui.Text("speechBubbles", "partialViewErrorText"), ui.Text("speechBubbles", "partialViewErrorHeader"),
|
|
|
|
|
|
//pass in a new exception ... this will also append the the message
|
2014-08-19 12:51:07 +02:00
|
|
|
|
attempt.Exception);
|
2014-08-14 17:20:25 +02:00
|
|
|
|
}
|
2013-04-18 23:00:57 +06:00
|
|
|
|
|
2014-08-14 17:20:25 +02:00
|
|
|
|
return Success(ui.Text("speechBubbles", "partialViewSavedText"), ui.Text("speechBubbles", "partialViewSavedHeader"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saves a template
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="templateName"></param>
|
|
|
|
|
|
/// <param name="templateAlias"></param>
|
|
|
|
|
|
/// <param name="templateContents"></param>
|
|
|
|
|
|
/// <param name="templateId"></param>
|
|
|
|
|
|
/// <param name="masterTemplateId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public JsonResult SaveTemplate(string templateName, string templateAlias, string templateContents, int templateId, int masterTemplateId)
|
|
|
|
|
|
{
|
|
|
|
|
|
Template t;
|
|
|
|
|
|
bool pathChanged = false;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
t = new Template(templateId)
|
|
|
|
|
|
{
|
|
|
|
|
|
Text = templateName,
|
|
|
|
|
|
Alias = templateAlias,
|
|
|
|
|
|
Design = templateContents
|
|
|
|
|
|
};
|
2013-08-30 14:15:51 +10:00
|
|
|
|
|
|
|
|
|
|
//check if the master page has changed
|
|
|
|
|
|
if (t.MasterTemplate != masterTemplateId)
|
|
|
|
|
|
{
|
|
|
|
|
|
pathChanged = true;
|
|
|
|
|
|
t.MasterTemplate = masterTemplateId;
|
|
|
|
|
|
}
|
2014-08-14 17:20:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (ArgumentException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
//the template does not exist
|
|
|
|
|
|
return Failed("Template does not exist", ui.Text("speechBubbles", "templateErrorHeader"), ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
t.Save();
|
2012-12-30 03:11:21 +03:00
|
|
|
|
|
2013-08-30 14:15:51 +10:00
|
|
|
|
//ensure the correct path is synced as the parent might have been changed
|
|
|
|
|
|
// http://issues.umbraco.org/issue/U4-2300
|
|
|
|
|
|
if (pathChanged)
|
|
|
|
|
|
{
|
|
|
|
|
|
//need to re-look it up
|
|
|
|
|
|
t = new Template(templateId);
|
|
|
|
|
|
}
|
|
|
|
|
|
var syncPath = "-1,init," + t.Path.Replace("-1,", "");
|
|
|
|
|
|
|
2014-08-14 17:20:25 +02:00
|
|
|
|
return Success(ui.Text("speechBubbles", "templateSavedText"), ui.Text("speechBubbles", "templateSavedHeader"),
|
|
|
|
|
|
new { path = syncPath });
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Failed(ui.Text("speechBubbles", "templateErrorText"), ui.Text("speechBubbles", "templateErrorHeader"), ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-12-30 03:11:21 +03:00
|
|
|
|
|
2013-08-30 14:15:51 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a successful message
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">The message to display in the speach bubble</param>
|
|
|
|
|
|
/// <param name="header">The header to display in the speach bubble</param>
|
|
|
|
|
|
/// <param name="additionalVals"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private JsonResult Success(string message, string header, object additionalVals = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var d = additionalVals == null ? new Dictionary<string, object>() : additionalVals.ToDictionary<object>();
|
|
|
|
|
|
d["success"] = true;
|
|
|
|
|
|
d["message"] = message;
|
|
|
|
|
|
d["header"] = header;
|
|
|
|
|
|
|
2014-08-14 17:20:25 +02:00
|
|
|
|
return Json(d);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a failed message
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">The message to display in the speach bubble</param>
|
|
|
|
|
|
/// <param name="header">The header to display in the speach bubble</param>
|
|
|
|
|
|
/// <param name="exception">The exception if there was one</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private JsonResult Failed(string message, string header, Exception exception = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (exception != null)
|
|
|
|
|
|
LogHelper.Error<SaveFileController>("An error occurred saving a file. " + message, exception);
|
|
|
|
|
|
return Json(new
|
|
|
|
|
|
{
|
|
|
|
|
|
success = false,
|
|
|
|
|
|
header = header,
|
|
|
|
|
|
message = message + (exception == null ? "" : (exception.Message + ". Check log for details."))
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-12-30 03:11:21 +03:00
|
|
|
|
}
|