/*
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.Web.Mvc;
using System.Runtime.Serialization;
using System.Linq.Dynamic;
using Everest.Library.Data;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Extjs.Tree;
using Everest.Library.Data.Entity;
using Everest.Library.Json;
using Everest.Library.Versioning;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Library.Web;
namespace Everest.CmsServices.Controllers{
/// <summary>
///
/// </summary>
[ValidateInput(false)]
public class PageTemplateController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region Localize
/// <summary>
/// Localizes the page template.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.LayoutTemplate)]
public ActionResult LocalizePageTemplate(int pageTemplateId, string application)
{
JsonResultData resultData = new JsonResultData();
var asp_Application = dataContext.QueryApplication(application).First();
var originalPageTemplate = dataContext.QueryPageTemplate(pageTemplateId).FirstOrDefault();
if (originalPageTemplate != null)
{
var localizedPageTemplate = originalPageTemplate.CopyNew();
localizedPageTemplate.aspnet_Applications = asp_Application;
localizedPageTemplate.ApplicationLevel = asp_Application.ApplicationLevel;
localizedPageTemplate.UserName = User.Identity.Name;
localizedPageTemplate.Base = originalPageTemplate;
localizedPageTemplate.OriginalUUID = originalPageTemplate.OriginalUUID;
dataContext.AddToCms_PageTemplate(localizedPageTemplate);
dataContext.SaveChanges();
localizedPageTemplate.SaveBody();
localizedPageTemplate.Checkin(User.Identity.Name, string.Format(Resources.CheckinByLocalization, localizedPageTemplate.PageTemplateId));
//
resultData.data = localizedPageTemplate;
}
return Json(resultData);
}
#endregion
#region Combobox
/// <summary>
/// Gets the page template for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetPageTemplateForCombobox(string application)
{
//the application value will be null when show the snapshot..
if (StringExtensions.IsNullOrEmptyTrim(application))
{
return null;
}
var query = dataContext.QueryPageTemplates(application);
var items = query.ToArray().ToComboboxItems(v => v.Name, v => v.Name, false);
return Json(new ExtJsonReaderObject(items, items.Count));
}
/// <summary>
/// Gets the page template for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetPageTemplateKeyValueForCombobox(string application)
{
//the application value will be null when show the snapshot..
if (StringExtensions.IsNullOrEmptyTrim(application))
{
return null;
}
var query = dataContext.QueryPageTemplates(application);
var items = query.ToArray().ToComboboxItems(v => v.Name, v => v.UUID.ToString(), false);
return Json(new ExtJsonReaderObject(items, items.Count));
}
/// <summary>
/// Gets the page holders for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetPageHoldersForCombobox(string pageTemplateUUID)
{
if (!StringExtensions.IsNullOrEmptyTrim(pageTemplateUUID))
{
Guid uuid = new Guid(pageTemplateUUID);
var query = from h in dataContext.Cms_PageTemplateHolders
where h.Cms_PageTemplate.UUID == uuid
select h;
var items = query.ToArray().ToComboboxItems(h => h.Name, h => h.Name.ToString());
return Json(new ExtJsonReaderObject(items, items.Count));
}
return null;
}
#endregion
#region IStandardActions Members
[PermissionFilter(Permission = FolderType.LayoutTemplate)]
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
var queryable = dataContext.QueryPageTemplates(application);
string name = Request.Form["Name"];
if (!StringExtensions.IsNullOrEmptyTrim(name))
{
queryable = queryable.Where(pt => pt.Name.Contains(name));
}
queryable = queryable.OrderByDescending(pt => pt.PageTemplateId);
queryable = OrderByRequest(queryable);
var queryResult = queryable.Select<Cms_PageTemplate, object>(c => new
{
c.UUID,
c.PageTemplateId,
Name = c.Name,
ModifiedDate = c.ModifiedDate,
c.Thumbnail,
Application = c.aspnet_Applications.ApplicationName,
IsLocalized = c.aspnet_Applications.ApplicationName == application,
BaseUUID = c.Base == null ? null : (Guid?)c.Base.UUID
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
[PermissionFilter(Permission = FolderType.LayoutTemplate)]
public ActionResult GetDetail()
{
int pageTemplateId = int.Parse(Request.Form["PageTemplateId"]);
object data = GetPageTemplateDetails(pageTemplateId);
return Json(new JsonResultData() { success = true, data = data });
}
/// <summary>
/// Gets the page template details.
/// </summary>
/// <param name="pageTemplateId">The page template id.</param>
/// <returns></returns>
private object GetPageTemplateDetails(int pageTemplateId)
{
var query = dataContext.QueryPageTemplate(pageTemplateId)
.Select(c => new
{
c,
c.aspnet_Applications.ApplicationName
});
var data = query.First();
string body = data.c.GetBody();
object jsonData = new
{
Application = data.ApplicationName,
data.c.UUID,
data.c.PageTemplateId,
Name = data.c.Name,
FormTitle = data.c.Name,
Body = data.c.Body,
Thumbnail = data.c.Thumbnail,
ModifiedDate = data.c.ModifiedDate
};
return jsonData;
}
[PermissionFilter(Permission = FolderType.LayoutTemplate)]
public ActionResult Submit(bool add, bool? closeForm)
{
var resultData = new JsonResultData() { success = true };
try
{
Cms_PageTemplate template;
string oldFileName = string.Empty;
string application = Request.Form["Application"];
if (add)
{
template = new Cms_PageTemplate();
template.aspnet_Applications = dataContext.QueryApplication(application).First();
template.ApplicationLevel = template.aspnet_Applications.ApplicationLevel;
dataContext.AddToCms_PageTemplate(template);
}
else
{
int pageTemplateId = int.Parse(Request.Form["oldData.PageTemplateId"]);
template = dataContext.QueryPageTemplate(pageTemplateId).First();
oldFileName = template.FileName;
}
template.Name = Request.Form["Name"];
template.Body = Request.Form["Body"];
template.ModifiedDate = DateTime.Now;
template.UserName = User.Identity.Name;
template.AnalyzeHolders(dataContext);
template.SaveBody();
if (!string.IsNullOrEmpty(Request.Form["Thumbnail"]))
{
template.Thumbnail = Request.Form["Thumbnail"];
}
if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
{
string extension = System.IO.Path.GetFileName(Request.Files[0].FileName);
string thumbnailFile = System.IO.Path.Combine(TemplateFileManager.GetPageTemplateThumbnailPath(application, template.Name), extension);
string savedFilePath = Request.Files[0].InputStream.SaveAs(thumbnailFile, false);
template.Thumbnail = UrlConvertor.AbsolutePathToRelativeUrl(savedFilePath);
}
dataContext.SaveChanges();
if (!template.FileName.Equals(oldFileName, StringComparison.InvariantCultureIgnoreCase))
{
template.DeleteBody(oldFileName);
}
//save the history
template.Checkin(User.Identity.Name, "");
if (closeForm == false && resultData.success == true)
{
resultData.closeForm = false;
resultData.data = GetPageTemplateDetails(template.PageTemplateId);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
}
return Json(resultData);
}
[PermissionFilter(Permission = FolderType.LayoutTemplate)]
public ActionResult Delete(Guid[] uuid)
{
var resultData = new JsonResultData() { success = true };
foreach (var guid in uuid)
{
var template = dataContext.QueryPageTemplate(guid).First();
if (BeReferenced(guid))
{
resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, template.Name);
}
else
{
template.aspnet_ApplicationsReference.Load(template.aspnet_Applications, template.EntityState);
var application = template.aspnet_Applications.ApplicationName;
template.Delete(dataContext);
dataContext.DeleteObject(template);
template.ClearVersions();
dataContext.SaveChanges();
var thumbnailPath = TemplateFileManager.GetPageTemplateThumbnailPath(application, template.Name);
if (System.IO.Directory.Exists(thumbnailPath))
{
System.IO.Directory.Delete(thumbnailPath, true);
}
}
}
return Json(resultData);
}
private bool BeReferenced(Guid uuid)
{
if (dataContext.QueryPagesByPageTemplate(uuid).Exists())
{
return true;
}
if (dataContext.QueryPageTemplateLocalizations(uuid).Exists())
{
return true;
}
return false;
}
#endregion
#region Relations
public ActionResult UsedBy(Guid uuid)
{
IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
#region Use in Pages
var pageNodes = new TreeNode();
treeNodes.Add(pageNodes.Attributes);
pageNodes.Text = Resources.TreeNode_Page;
pageNodes.Expanded = true;
pageNodes.IconCls = FolderType.Page.ToString();
var pages = dataContext.QueryPagesByPageTemplate(uuid)
.Select(p => new { p.UUID, p.aspnet_Applications.ApplicationName, p.PageName });
pageNodes.children = new List<IDictionary<string, object>>();
foreach (var page in pages)
{
var pageNode = new TreeNode();
pageNodes.children.Add(pageNode.Attributes);
pageNode.Text = new UniqueName(page.ApplicationName, page.PageName).ToString();
pageNode.IconCls = FolderType.PageNode.ToString();
pageNode.Attributes.Add("uuid", page.UUID);
pageNode.Attributes.Add("dataUrl", "Kooboo_Page/UsedBy");
}
#endregion
#region Localizations
var localizationNodes = new TreeNode();
treeNodes.Add(localizationNodes.Attributes);
localizationNodes.Text = Resources.LocalizedBy;
localizationNodes.Expanded = true;
localizationNodes.IconCls = FolderType.LayoutTemplate.ToString();
var localizations = dataContext.QueryPageTemplateLocalizations(uuid)
.Select(pt => new
{
pt.UUID,
pt.aspnet_Applications.ApplicationName,
pt.Name
});
localizationNodes.children = new List<IDictionary<string, object>>();
foreach (var pageTemplate in localizations)
{
var pageTemplateNode = new TreeNode();
localizationNodes.AddChild(pageTemplateNode);
pageTemplateNode.Text = new UniqueName(pageTemplate.ApplicationName, pageTemplate.Name).ToString();
pageTemplateNode.IconCls = FolderType.LayoutTemplate.ToString();
pageTemplateNode.Attributes.Add("uuid", pageTemplate.UUID);
}
#endregion
#region Localize From
var localizeFromNodes = new TreeNode();
treeNodes.Add(localizeFromNodes.Attributes);
localizeFromNodes.Text = Resources.LocalizeFrom;
localizeFromNodes.Expanded = true;
localizeFromNodes.IconCls = FolderType.LayoutTemplate.ToString();
var localizeFrom = dataContext.QueryPageTemplate(uuid).Where(pt => pt.Base != null)
.Select(pt => new
{
pt.Base.UUID,
pt.Base.aspnet_Applications.ApplicationName,
pt.Base.Name
});
localizeFromNodes.children = new List<IDictionary<string, object>>();
foreach (var pageTemplate in localizeFrom)
{
var pageTemplateNode = new TreeNode();
localizeFromNodes.AddChild(pageTemplateNode);
pageTemplateNode.Text = new UniqueName(pageTemplate.ApplicationName, pageTemplate.Name).ToString();
pageTemplateNode.IconCls = FolderType.LayoutTemplate.ToString();
pageTemplateNode.Attributes.Add("uuid", pageTemplate.UUID);
}
#endregion
return Json(treeNodes);
}
#endregion
}
}
|