/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using Everest.Library.Data;
using Everest.Library.Data.Entity;
using Everest.CmsServices.Services;
using Everest.Library.Versioning;
using Everest.Library;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Everest.Library.Web;
namespace Everest.CmsServices.Models{
public partial class Cms_PageTemplate : IVersionable, IRuleEntity
{
public Cms_PageTemplate()
{
this.UUID = Guid.NewGuid();
this.OriginalUUID = this.UUID;
}
#region file
/// <summary>
/// Gets or sets the body.
/// </summary>
/// <value>The body.</value>
public string Body { get; set; }
/// <summary>
/// Gets the name of the file.
/// </summary>
/// <value>The name of the file.</value>
public string FileName
{
get
{
//cached the pagetemplate and detach the entity object
var application = this.aspnet_Applications;
if (application == null)
application = CachedData.GetApplicationByPageTemplate(this.UUID);
return TemplateFileManager.GetAbsolutePageTemplateFilePath(application.ApplicationName, this.Name);
}
}
/// <summary>
/// Saves the body to disk file.
/// </summary>
public void SaveBody()
{
if (!Body.Trim().StartsWith("<%@"))
{
Body = @"<%@ Page Language=""C#"" Inherits=""Everest.CmsServices.MvcHelper.CmsViewPage""%>
" + Body;
}
FileExtensions.SaveFileBody(FileName, this.Body);
this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
UnityManager.Resolve<TextResourceService>().ParseText(this.aspnet_Applications, this.Body);
}
/// <summary>
/// Gets the body form disk file
/// </summary>
public string GetBody()
{
if (System.IO.File.Exists(FileName))
{
this.Body = FileExtensions.GetFileBody(this.FileName);
}
else
this.Body = string.Empty;
return this.Body;
}
/// <summary>
/// Gets the page template virtual path.
/// </summary>
/// <returns></returns>
public string GetVirtualPath()
{
return UrlConvertor.AbsolutePathToRelativeUrl(FileName);
}
#endregion
#region AnalyzeHolders
Regex tagRegex = new Regex(@"[<]\w+:(?:PositionHolder|Position)[^<|>]+Id=['|""]?(?<HolderId>[^'|""]+)['|""]?[^<|>]*[>]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// Analyzes the holders.
/// </summary>
public void AnalyzeHolders(IEverestCmsDataContext dataContext)
{
MatchCollection matches = tagRegex.Matches(this.Body);
List<string> holders = new List<string>();
foreach (Match item in matches)
{
if (!StringExtensions.IsNullOrEmptyTrim(item.Groups["HolderId"].Value))
{
holders.Add(item.Groups["HolderId"].Value.Trim());
}
}
if (this.EntityState == System.Data.EntityState.Detached || this.EntityState == System.Data.EntityState.Added)
{
foreach (var item in holders)
{
this.Cms_PageTemplateHolders.Add(new Cms_PageTemplateHolders() { Name = item });
}
}
else
{
if (!this.Cms_PageTemplateHolders.IsLoaded)
{
this.Cms_PageTemplateHolders.Load();
}
var deletedHolders = this.Cms_PageTemplateHolders.Where(p => !holders.Contains(p.Name)).Select(p => p).ToArray();
foreach (var item in deletedHolders)
{
dataContext.DeleteObject(item);
this.Cms_PageTemplateHolders.Remove(item);
}
foreach (var item in holders)
{
if (this.Cms_PageTemplateHolders.Count(p => p.Name == item) == 0)
{
this.Cms_PageTemplateHolders.Add(new Cms_PageTemplateHolders() { Name = item });
}
}
}
}
#endregion
#region delete
public void DeleteBody()
{
DeleteBody(FileName);
}
/// <summary>
/// Deletes the body.
/// </summary>
public void DeleteBody(string fileName)
{
string absoluteFile = CmsGlobal.ToAbsolutePath(fileName);
if (File.Exists(absoluteFile))
{
File.Delete(absoluteFile);
}
}
/// <summary>
/// Deletes the specified context.
/// </summary>
/// <param name="context">The context.</param>
public void Delete(IEverestCmsDataContext context)
{
this.Cms_PageTemplateHolders.Load();
while (Cms_PageTemplateHolders.Count > 0)
{
context.DeleteObject(Cms_PageTemplateHolders.First());
}
Cms_PageTemplateHolders.Clear();
this.DeleteBody();
}
#endregion
#region IVersionable Members
/// <summary>
/// Gets the name of the snapshot form.
/// </summary>
/// <value>The name of the snapshot form.</value>
public string FormSchemaName
{
get { return "LayoutTemplate"; }
}
public void OnRevert(IVersionItem workingItem, IVersionItem toBeRevertedItem)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var pageTemplate = dataContext.QueryPageTemplate(this.UUID).First();
pageTemplate.Name = this.Name;
pageTemplate.Body = this.Body;
pageTemplate.ModifiedDate = DateTime.Now;
pageTemplate.Thumbnail = this.Thumbnail;
pageTemplate.UserName = workingItem.CheckinUser;
dataContext.SaveChanges();
pageTemplate.SaveBody();
}
#endregion
#region copy
/// <summary>
/// Copies the new.
/// </summary>
/// <returns></returns>
public Cms_PageTemplate CopyNew()
{
Cms_PageTemplate pageTemplate = new Cms_PageTemplate();
pageTemplate.Name = this.Name;
pageTemplate.Body = this.GetBody();
pageTemplate.Thumbnail = this.Thumbnail;
pageTemplate.ModifiedDate = DateTime.Now;
pageTemplate.aspnet_Applications = this.aspnet_Applications;
return pageTemplate;
}
#endregion
#region IRuleEntity Members
public IEnumerable<RuleViolation> GetRuleViolations()
{
var dataContext = EverestCmsEntities.GetDataContext();
List<RuleViolation> list = new List<RuleViolation>();
this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
if (dataContext.IsPageTemplateExists(this.Name, this.aspnet_Applications.ApplicationName, this.PageTemplateId).Exists())
{
if (this.Base == null)
{
list.Add(new RuleViolation("Name", this.Name, Resources.PageTemplateAlreadyExists));
}
}
return list;
}
#endregion
#region Serialize
public static Cms_PageTemplate DeserializeFromNode(XmlNode parentNode)
{
var pageTemplate = new Cms_PageTemplate();
XmlNode pageTemplateNode = parentNode.SelectSingleNode("pageTemplate");
if (pageTemplateNode != null)
{
pageTemplate.UUID = new Guid(pageTemplateNode.Attributes["UUID"].Value);
pageTemplate.Name = pageTemplateNode.Attributes["Name"].Value;
if (pageTemplateNode.Attributes["Thumbnail"] != null)
{
pageTemplate.Thumbnail = pageTemplateNode.Attributes["Thumbnail"].Value;
}
return pageTemplate;
}
return null;
}
public void SerializeAsNode(XmlNode parentNode)
{
var xmlDom = parentNode.OwnerDocument;
//add page template node
XmlNode pageTemplateNode = parentNode.OwnerDocument.CreateElement("pageTemplate");
parentNode.AppendChild(pageTemplateNode);
XmlAttribute pageTemplateIdAtt = xmlDom.CreateAttribute("UUID");
pageTemplateIdAtt.Value = this.UUID.ToString();
pageTemplateNode.Attributes.Append(pageTemplateIdAtt);
XmlAttribute pageTemplateNameAtt = xmlDom.CreateAttribute("Name");
pageTemplateNameAtt.Value = this.Name;
pageTemplateNode.Attributes.Append(pageTemplateNameAtt);
XmlAttribute thumnailAtt = xmlDom.CreateAttribute("Thumbnail");
thumnailAtt.Value = this.Thumbnail;
pageTemplateNode.Attributes.Append(thumnailAtt);
}
#endregion
}
}
|