diff --git a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs index 14e8e7ece4..68df221c68 100644 --- a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs +++ b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs @@ -9,6 +9,7 @@ using System.Web.Mvc.Html; using System.Web.Routing; using Umbraco.Core; using Umbraco.Core.Dynamics; +using Umbraco.Core.IO; using Umbraco.Web.Mvc; using umbraco; using umbraco.cms.businesslogic.member; @@ -20,6 +21,30 @@ namespace Umbraco.Web /// public static class HtmlHelperRenderExtensions { + /// + /// Will render the preview badge when in preview mode which is not required ever unless the MVC page you are + /// using does not inherit from UmbracoTemplatePage + /// + /// + /// + /// + /// See: http://issues.umbraco.org/issue/U4-1614 + /// + public static MvcHtmlString PreviewBadge(this HtmlHelper helper) + { + if (UmbracoContext.Current.InPreviewMode) + { + var htmlBadge = + String.Format(UmbracoSettings.PreviewBadge, + IOHelper.ResolveUrl(SystemDirectories.Umbraco), + IOHelper.ResolveUrl(SystemDirectories.UmbracoClient), + UmbracoContext.Current.HttpContext.Server.UrlEncode(UmbracoContext.Current.HttpContext.Request.Path)); + return new MvcHtmlString(htmlBadge); + } + return new MvcHtmlString(""); + + } + public static IHtmlString CachedPartial( this HtmlHelper htmlHelper, string partialViewName, diff --git a/src/Umbraco.Web/Mvc/UmbracoTemplatePage.cs b/src/Umbraco.Web/Mvc/UmbracoTemplatePage.cs index 962fba50be..5f762b430e 100644 --- a/src/Umbraco.Web/Mvc/UmbracoTemplatePage.cs +++ b/src/Umbraco.Web/Mvc/UmbracoTemplatePage.cs @@ -1,5 +1,10 @@ +using System; +using Umbraco.Core; +using Umbraco.Core.Configuration; using Umbraco.Core.Dictionary; using Umbraco.Core.Dynamics; +using Umbraco.Core.IO; +using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Web.Models; @@ -60,5 +65,33 @@ namespace Umbraco.Web.Mvc get { return _helper ?? (_helper = new UmbracoHelper(UmbracoContext, Model.Content)); } } + /// + /// This will detect the end /body tag and insert the preview badge if in preview mode + /// + /// + public override void WriteLiteral(object value) + { + // filter / add preview banner + if (Response.ContentType.InvariantEquals("text/html") && UmbracoContext.Current.InPreviewMode) // ASP.NET default value + { + var text = value.ToString().ToLowerInvariant(); + int pos = text.IndexOf(""); + if (pos > -1) + { + var htmlBadge = + String.Format(UmbracoSettings.PreviewBadge, + IOHelper.ResolveUrl(SystemDirectories.Umbraco), + IOHelper.ResolveUrl(SystemDirectories.UmbracoClient), + Server.UrlEncode(UmbracoContext.Current.HttpContext.Request.Path)); + + text = text.Substring(0, pos) + htmlBadge + text.Substring(pos, text.Length - pos); + base.WriteLiteral(text); + return; + } + } + + base.WriteLiteral(value); + } + } } \ No newline at end of file diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs index 38c376591d..b12a4cd4f3 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs @@ -16,6 +16,7 @@ using System.IO; using umbraco.cms.businesslogic.web; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; +using Umbraco.Core.IO; namespace umbraco.presentation.preview { @@ -38,12 +39,12 @@ namespace umbraco.presentation.preview public PreviewContent(Guid previewSet) { - ValidPreviewSet = updatePreviewPaths(previewSet, true); + ValidPreviewSet = UpdatePreviewPaths(previewSet, true); } public PreviewContent(User user, Guid previewSet, bool validate) { _userId = user.Id; - ValidPreviewSet = updatePreviewPaths(previewSet, validate); + ValidPreviewSet = UpdatePreviewPaths(previewSet, validate); } @@ -85,7 +86,7 @@ namespace umbraco.presentation.preview } - private bool updatePreviewPaths(Guid previewSet, bool validate) + private bool UpdatePreviewPaths(Guid previewSet, bool validate) { if (_userId == -1) { @@ -112,8 +113,8 @@ namespace umbraco.presentation.preview private static string GetPreviewsetPath(int userId, Guid previewSet) { - return IO.IOHelper.MapPath( - Path.Combine(IO.SystemDirectories.Preview, userId + "_" + previewSet + ".config")); + return IOHelper.MapPath( + Path.Combine(SystemDirectories.Preview, userId + "_" + previewSet + ".config")); } /// @@ -139,7 +140,7 @@ namespace umbraco.presentation.preview public void SavePreviewSet() { //make sure the preview folder exists first - var dir = new DirectoryInfo(IO.IOHelper.MapPath(IO.SystemDirectories.Preview)); + var dir = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Preview)); if (!dir.Exists) { dir.Create(); @@ -155,17 +156,17 @@ namespace umbraco.presentation.preview { foreach (FileInfo file in dir.GetFiles(userId + "_*.config")) { - deletePreviewFile(userId, file); + DeletePreviewFile(userId, file); } // also delete any files accessed more than one hour ago foreach (FileInfo file in dir.GetFiles("*.config")) { if ((DateTime.Now - file.LastAccessTime).TotalMinutes > 1) - deletePreviewFile(userId, file); + DeletePreviewFile(userId, file); } } - private static void deletePreviewFile(int userId, FileInfo file) + private static void DeletePreviewFile(int userId, FileInfo file) { try { @@ -191,7 +192,7 @@ namespace umbraco.presentation.preview if (StateHelper.Cookies.Preview.HasValue) { - deletePreviewFile( + DeletePreviewFile( UmbracoContext.Current.UmbracoUser.Id, new FileInfo(GetPreviewsetPath( UmbracoContext.Current.UmbracoUser.Id,