/*
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.Data.Objects.DataClasses;
using System.Linq.Dynamic;
using System.Web.Compilation;
using Everest.Library;
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.CmsServices.Extension.Module;
using System.Reflection;
using Everest.CmsServices.MvcHelper;
namespace Everest.CmsServices.Controllers{
[ValidateInput(false)]
public class ContentTemplateController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region Content Template
[PermissionFilter(Permission = FolderType.ContentTemplate)]
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
var folderUUID = new Guid(Request.Form["folderUUID"]);
string strNamespace = Request.Form["fullNamespace"];
var queryable = dataContext.QueryContentTemplates(application, strNamespace);
string name = Request.Form["Name"];
if (!StringExtensions.IsNullOrEmptyTrim(name))
{
queryable = queryable.Where(ct => ct.Name.Contains(name));
}
queryable = queryable.OrderByDescending(ct => ct.ContentTemplateId);
queryable = OrderByRequest(queryable);
var queryResult = queryable.Select<Cms_ContentTemplate, object>(c => new
{
c.UUID,
c.ContentTemplateId,
Name = c.Name,
ModifiedDate = c.ModifiedDate,
Application = c.aspnet_Applications.ApplicationName,
c.Description,
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.ContentTemplate)]
public ActionResult GetDetail()
{
int contentTemplateId = int.Parse(Request.Form["ContentTemplateId"]);
object jsonData = GetContentTemplateDetail(contentTemplateId);
return Json(new JsonResultData() { success = true, data = jsonData });
}
private object GetContentTemplateDetail(int contentTemplateId)
{
var query = dataContext.QueryContentTemplate(contentTemplateId)
.Select(c => new
{
c,
c.aspnet_Applications.ApplicationName,
c.Cms_ContentTemplateParameters,
c.Cms_Plugin,
c.Cms_DataRule
});
var data = query.First();
string body = data.c.GetBody();
object jsonData = new
{
data.c.UUID,
data.c.ContentTemplateId,
Application = data.ApplicationName,
Name = data.c.Name,
FormTitle = data.c.Name,
Body = body,
data.c.Description,
Parameters = data.Cms_ContentTemplateParameters.Select(p => new
{
p.ParameterId,
p.Name,
p.DataType
}).ToArray(),
DataRules = data.Cms_DataRule.ToDataRuleQuery(),
Plugins = data.Cms_Plugin.Select(p => new
{
p.PluginId,
p.PluginName,
p.PluginType
}).ToArray(),
};
return jsonData;
}
[PermissionFilter(Permission = FolderType.ContentTemplate)]
[ValidateInput(false)]
public ActionResult Submit(bool add, bool? closeForm)
{
JsonResultData resultData = new JsonResultData() { success = true };
try
{
Cms_ContentTemplate template;
var contentTemplateService = UnityManager.Resolve<ContentTemplateService>();
if (add)
{
template = contentTemplateService.AddContentTemplate(Request.Form, User.Identity.Name);
}
else
{
Guid uuid = new Guid(Request.Form["oldData.UUID"]);
template = contentTemplateService.UpdateContentTemplate(uuid, Request.Form, User.Identity.Name);
}
if (closeForm == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetContentTemplateDetail(template.ContentTemplateId);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
return Json(resultData);
}
/// <summary>
/// Deletes the content template.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.ContentTemplate)]
public ActionResult Delete(Guid[] uuid)
{
var resultData = new JsonResultData() { success = true };
ContentTemplateService contentTemplateService = UnityManager.Resolve<ContentTemplateService>();
foreach (var guid in uuid)
{
contentTemplateService.Delete(guid);
}
return Json(resultData);
}
#endregion
#region combobox
/// <summary>
/// Gets the content template for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetContentTemplateForCombobox(string application)
{
ContentTemplateService contentTemplateService = UnityManager.Resolve<ContentTemplateService>();
var webComponents = contentTemplateService.GetComponentsForPage(application);
var items = webComponents.ToComboboxItems(wc => string.Format(wc.ComponentName),
wc => wc.ToValue(), wc => Url.Content(wc.Image), wc => wc.ComponentType.ToString(), false);
return Json(new ExtJsonReaderObject(items, items.Count()));
}
public ActionResult GetPropertyEnum(string componentValue, string parameterName)
{
ComponentType componentType;
var pureComponentValue = GetComponentValue(componentValue, out componentType);
if (componentType != ComponentType.ContentTemplate)
{
return null;
}
int contentTemplateId = int.Parse(pureComponentValue);
var contentTemplate = dataContext.QueryContentTemplate(contentTemplateId).First();
var contentTemplateType = BuildManager.GetCompiledType(contentTemplate.GetVirtualPath());
if (contentTemplateType == null)
{
return null;
}
var property = contentTemplateType.GetProperty(parameterName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
if (property == null)
{
return null;
}
var enumAttribute = (EnumAttribute)Attribute.GetCustomAttribute(property, typeof(EnumAttribute));
if (enumAttribute == null || StringExtensions.IsNullOrEmptyTrim(enumAttribute.Values))
{
return null;
}
string[] values = enumAttribute.SplitedValues;
var items = values.ToComboboxItems(s => s, s => s, false);
return Json(new ExtJsonReaderObject(items, items.Count()));
}
#endregion
#region Localize
/// <summary>
/// Localizes the content template.
/// </summary>
/// <param name="ContentTemplateId">The content template id.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.ContentTemplate)]
public ActionResult Localize(Guid uuid, string application)
{
JsonResultData resultData = new JsonResultData();
ContentTemplateService contentTemplateService = UnityManager.Resolve<ContentTemplateService>();
resultData.data = contentTemplateService.Localize(uuid, application, User.Identity.Name);
return Json(resultData);
}
#endregion
#region Relations
public ActionResult UsedBy(Guid uuid, string dataUrl)
{
ContentTemplateService contentTemplateService = UnityManager.Resolve<ContentTemplateService>();
IList<Dictionary<string, object>> treeNodes = contentTemplateService.UsedBy(uuid, dataUrl);
return Json(treeNodes);
}
#endregion
#region Get Tree Nodes
/// <summary>
/// Gets the namespace nodes.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="fullNamespace">The full namespace.</param>
/// <param name="id">The id.</param>
/// <param name="schema">The schema.</param>
/// <param name="dataUrl">The data URL.</param>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public ActionResult GetNamespaceNodes(string application, string fullNamespace, string id, string schema,
string dataUrl, string folderUUID)
{
var namespaces = UnityManager.Resolve<ContentTemplateService>().GetChildNamespaces(application, fullNamespace);
List<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
foreach (var item in namespaces)
{
TreeNode treeNode = new TreeNode();
treeNode.Text = item.Name;
treeNode.ParentNode = id;
treeNode.AddAttribute("schema", schema);
//treeNode.AddAttribute("formType", item.FormType);
treeNode.AddAttribute("folderType", FolderType.TextResourceNamespace.ToString());
treeNode.AddAttribute("application", application);
treeNode.AddAttribute("dataUrl", dataUrl);
treeNode.AddAttribute("fullNamespace", item.FullName);
treeNode.AddAttribute("folderUUID", folderUUID);
treeNode.IconCls = "Namespace";
treeNodes.Add(treeNode.Attributes);
}
return Json(treeNodes);
}
#endregion
#region Component
/// <summary>
/// Gets the content template parameters.
/// </summary>
/// <param name="ComponentValue">The component value.[such like: 0:123]</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.ContentTemplate)]
public ActionResult GetContentTemplateParameters(string componentValue)
{
ComponentType componentType;
var pureComponentValue = GetComponentValue(componentValue, out componentType);
IEnumerable<object> parameters = new object[0];
if (componentType == ComponentType.ContentTemplate)
{
var contentTemplateId = int.Parse(pureComponentValue);
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
parameters = dataContext.Cms_ContentTemplateParameters.Where(p => p.Cms_ContentTemplate.ContentTemplateId == contentTemplateId)
.Select(p => new
{
ParameterName = p.Name,
DataType = p.DataType,
Value = ""
}).ToArray();
}
else if (componentType == ComponentType.Module)
{
var moduleName = pureComponentValue;
var moduleInfo = ModuleManager.GetModule(moduleName, CmsGlobal.RootApplicationName);
parameters = moduleInfo.Configuration.Settings.Select(s => new
{
ParameterName = s.Key,
DataType = "String",
Value = s.Value,
//Editor = s.Editor.ToString(),
Items = s.Items
}).ToArray();
}
return Json(new ExtJsonReaderObject(parameters.ToArray(), parameters.Count()));
}
private static string GetComponentValue(string componentValue, out ComponentType componentType)
{
string pureComponentValue;
Component.ParseValue(componentValue, out pureComponentValue, out componentType);
return pureComponentValue;
}
#endregion
}
}
|