2014-12-01 14:14:44 +11:00
|
|
|
using System;
|
2014-12-01 12:59:36 +11:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
2014-12-01 16:22:18 +11:00
|
|
|
namespace Umbraco.Core.IO
|
2014-12-01 12:59:36 +11:00
|
|
|
{
|
|
|
|
|
internal class ViewHelper
|
|
|
|
|
{
|
2014-12-01 14:14:44 +11:00
|
|
|
private readonly IFileSystem _viewFileSystem;
|
|
|
|
|
|
|
|
|
|
public ViewHelper(IFileSystem viewFileSystem)
|
|
|
|
|
{
|
|
|
|
|
if (viewFileSystem == null) throw new ArgumentNullException("viewFileSystem");
|
|
|
|
|
_viewFileSystem = viewFileSystem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool ViewExists(ITemplate t)
|
2014-12-01 12:59:36 +11:00
|
|
|
{
|
2014-12-01 14:14:44 +11:00
|
|
|
return _viewFileSystem.FileExists(ViewPath(t.Alias));
|
2014-12-01 12:59:36 +11:00
|
|
|
}
|
|
|
|
|
|
2014-12-01 14:14:44 +11:00
|
|
|
[Obsolete("This is only used for legacy purposes and will be removed in future versions")]
|
|
|
|
|
internal string GetPhysicalFilePath(ITemplate t)
|
2014-12-01 12:59:36 +11:00
|
|
|
{
|
2014-12-01 14:14:44 +11:00
|
|
|
return _viewFileSystem.GetFullPath(ViewPath(t.Alias));
|
2014-12-01 12:59:36 +11:00
|
|
|
}
|
|
|
|
|
|
2014-12-01 17:34:22 +11:00
|
|
|
//internal string GetFileContents(ITemplate t)
|
|
|
|
|
//{
|
|
|
|
|
// string viewContent = "";
|
|
|
|
|
// string path = ViewPath(t.Alias);
|
|
|
|
|
|
|
|
|
|
// if (_viewFileSystem.FileExists(path))
|
|
|
|
|
// {
|
|
|
|
|
// using (var tr = new StreamReader(_viewFileSystem.OpenFile(path)))
|
|
|
|
|
// {
|
|
|
|
|
// viewContent = tr.ReadToEnd();
|
|
|
|
|
// tr.Close();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return viewContent;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//internal string CreateViewFile(ITemplate t, bool overWrite = false)
|
|
|
|
|
//{
|
|
|
|
|
// string viewContent;
|
|
|
|
|
// string path = ViewPath(t.Alias);
|
|
|
|
|
|
|
|
|
|
// if (_viewFileSystem.FileExists(path) == false || overWrite)
|
|
|
|
|
// {
|
|
|
|
|
// viewContent = SaveTemplateToFile(t, t.Alias);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// using (var tr = new StreamReader(_viewFileSystem.OpenFile(path)))
|
|
|
|
|
// {
|
|
|
|
|
// viewContent = tr.ReadToEnd();
|
|
|
|
|
// tr.Close();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return viewContent;
|
|
|
|
|
//}
|
2014-12-01 12:59:36 +11:00
|
|
|
|
2014-12-01 14:14:44 +11:00
|
|
|
internal static string GetDefaultFileContent(string layoutPageAlias = null)
|
|
|
|
|
{
|
|
|
|
|
var design = @"@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
|
|
|
|
|
@{
|
|
|
|
|
Layout = null;
|
|
|
|
|
}";
|
|
|
|
|
|
|
|
|
|
if (layoutPageAlias.IsNullOrWhiteSpace() == false)
|
|
|
|
|
design = design.Replace("null", string.Format("\"{0}.cshtml\"", layoutPageAlias));
|
|
|
|
|
|
|
|
|
|
return design;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 17:34:22 +11:00
|
|
|
//internal string SaveTemplateToFile(ITemplate template, string currentAlias)
|
|
|
|
|
//{
|
|
|
|
|
// var design = EnsureInheritedLayout(template);
|
|
|
|
|
// var path = ViewPath(template.Alias);
|
|
|
|
|
|
|
|
|
|
// using (var ms = new MemoryStream())
|
|
|
|
|
// using (var writer = new StreamWriter(ms, Encoding.UTF8))
|
|
|
|
|
// {
|
|
|
|
|
// writer.Write(design);
|
|
|
|
|
// _viewFileSystem.AddFile(path, ms, true);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return template.Content;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//internal string UpdateViewFile(ITemplate t, string currentAlias = null)
|
|
|
|
|
//{
|
|
|
|
|
// var path = ViewPath(t.Alias);
|
|
|
|
|
|
|
|
|
|
// if (string.IsNullOrEmpty(currentAlias) == false && currentAlias != t.Alias)
|
|
|
|
|
// {
|
|
|
|
|
// //NOTE: I don't think this is needed for MVC, this was ported over from the
|
|
|
|
|
// // masterpages helper but I think only relates to when templates are stored in the db.
|
|
|
|
|
// ////Ensure that child templates have the right master masterpage file name
|
|
|
|
|
// //if (t.HasChildren)
|
|
|
|
|
// //{
|
|
|
|
|
// // var c = t.Children;
|
|
|
|
|
// // foreach (CMSNode cmn in c)
|
|
|
|
|
// // UpdateViewFile(new Template(cmn.Id), null);
|
|
|
|
|
// //}
|
|
|
|
|
|
|
|
|
|
// //then kill the old file..
|
|
|
|
|
// var oldFile = ViewPath(currentAlias);
|
|
|
|
|
// if (_viewFileSystem.FileExists(oldFile))
|
|
|
|
|
// _viewFileSystem.DeleteFile(oldFile);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// using (var ms = new MemoryStream())
|
|
|
|
|
// using (var writer = new StreamWriter(ms, Encoding.UTF8))
|
|
|
|
|
// {
|
|
|
|
|
// writer.Write(t.Content);
|
|
|
|
|
// _viewFileSystem.AddFile(path, ms, true);
|
|
|
|
|
// }
|
|
|
|
|
// return t.Content;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//internal void RemoveViewFile(string alias)
|
|
|
|
|
//{
|
|
|
|
|
// if (string.IsNullOrWhiteSpace(alias) == false)
|
|
|
|
|
// {
|
|
|
|
|
// var file = ViewPath(alias);
|
|
|
|
|
// if (_viewFileSystem.FileExists(file))
|
|
|
|
|
// _viewFileSystem.DeleteFile(file);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2014-12-01 12:59:36 +11:00
|
|
|
|
2014-12-01 14:14:44 +11:00
|
|
|
public string ViewPath(string alias)
|
2014-12-01 12:59:36 +11:00
|
|
|
{
|
2014-12-01 14:14:44 +11:00
|
|
|
return _viewFileSystem.GetRelativePath(alias.Replace(" ", "") + ".cshtml");
|
|
|
|
|
|
|
|
|
|
//return SystemDirectories.MvcViews + "/" + alias.Replace(" ", "") + ".cshtml";
|
2014-12-01 12:59:36 +11:00
|
|
|
}
|
|
|
|
|
|
2014-12-01 14:14:44 +11:00
|
|
|
private string EnsureInheritedLayout(ITemplate template)
|
2014-12-01 12:59:36 +11:00
|
|
|
{
|
|
|
|
|
string design = template.Content;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(design))
|
|
|
|
|
{
|
2014-12-01 14:14:44 +11:00
|
|
|
design = GetDefaultFileContent(template.MasterTemplateAlias);
|
2014-12-01 12:59:36 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return design;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|