/*
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.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Microsoft.Data.Extensions;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Json;
using Everest.Library.Extjs.Tree;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.CmsServices.Extension.Module;
namespace Everest.CmsServices.Controllers{
/// <summary>
///
/// </summary>
public class ModuleController : CmsUserController
{
#region IStandardActions Members
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult GetList(string application)
{
var runtimeApplication = application;
var modules = ModuleManager.GetModules(runtimeApplication);
ExtJsonReaderObject jsonReader = new ExtJsonReaderObject(modules.Select(m => new
{
m.ModuleName,
ThemePath = Everest.Library.Web.UrlConvertor.AbsolutePathToRelativeUrl(m.ThemePath),
Application = application,
m.Configuration.Installed,
m.Configuration.Version,
m.Configuration.FrameworkVersion,
m.Configuration.AdminUrl
}), modules.Count());
return Json(jsonReader);
}
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult GetDetail(string application, string moduleName)
{
JsonResultData resultData = new JsonResultData();
resultData.data = GetModuleDetail(application, moduleName);
return Json(resultData);
}
private object GetModuleDetail(string application, string moduleName)
{
string moduleApplication = application;
var moduleInfo = ModuleManager.GetModule(moduleName, application);
return new
{
Application = application,
Applications = string.Join(",", moduleInfo.GetAllowedApplications()),
moduleInfo.ModuleName,
moduleInfo.Configuration.Installed,
moduleInfo.Configuration.Version,
moduleInfo.Configuration.FrameworkVersion,
Permission = moduleInfo.Permissions
};
}
/// <summary>
/// Submits the specified application.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="closeForm">The close form.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult Submit(string application, bool? closeForm, string applications)
{
JsonResultData resultData = new JsonResultData();
var moduleName = Request.Form["oldData.ModuleName"];
var moduleInfo = ModuleManager.GetModule(moduleName, application);
var modulePermissions = Request.Form["Permission"].DeserializeJSON<ModulePermission[]>();
var dataContext = EverestCmsEntities.GetDataContext();
moduleInfo.SavePermission(dataContext, application, modulePermissions);
if (application == CmsGlobal.RootApplicationName && !string.IsNullOrEmpty(applications))
{
moduleInfo.SaveAllowedApplications(dataContext, applications);
}
if (closeForm.HasValue && closeForm.Value == false)
{
resultData.closeForm = false;
resultData.data = GetModuleDetail(application, moduleName);
}
return Json(resultData);
}
//private void SaveModuleSetting(IEverestCmsDataContext dataContext, string application, string moduleName, IEnumerable<ModuleSetting> moduleSettings)
//{
// var oldSettings = dataContext.QueryModuleSettings(application, moduleName);
// foreach (var item in oldSettings)
// {
// dataContext.DeleteObject(item);
// }
// dataContext.SaveChanges();
// var app = dataContext.QueryApplication(application).First();
// foreach (var setting in moduleSettings)
// {
// var moduleSetting = new Cms_ModuleSetting();
// moduleSetting.aspnet_Applications = app;
// moduleSetting.ModuleName = moduleName;
// moduleSetting.Key = setting.Key;
// moduleSetting.Value = setting.Value;
// dataContext.AddToCms_ModuleSetting(moduleSetting);
// }
// dataContext.SaveChanges();
//}
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult Delete(string application, string moduleName)
{
JsonResultData resultData = new JsonResultData();
if (BeReferenced(moduleName))
{
resultData.message = string.Format(Resources.ModuleCannotDeleteOrUnstall);
}
else
{
//Unstall(application, moduleName);
ModuleManager.DeleteModule(moduleName);
}
return Json(resultData);
}
#endregion
#region Upload & Install & Unstall
/// <summary>
/// Uploads the module.
/// </summary>
/// <param name="application">The application.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult Upload(string application, string moduleName, string applications)
{
JsonResultData resultData = new JsonResultData();
try
{
if (StringExtensions.IsNullOrEmptyTrim(moduleName))
{
resultData.AddError("ModuleName", Resources.FieldIsRequired);
}
if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
{
bool upgrade = StringExtensions.IsTrue(Request.Form["Upgrade"]);
var resultText = ModuleManager.UploadModule(moduleName, Request.Files[0].InputStream, upgrade);
if (!string.IsNullOrEmpty(resultText))
{
resultData.AddError("ModuleName", resultText);
}
else
{
if (!string.IsNullOrEmpty(applications))
{
var dataContext = EverestCmsEntities.GetDataContext();
var moduleInfo = ModuleManager.GetModule(moduleName, application);
moduleInfo.SaveAllowedApplications(dataContext, applications);
}
}
}
else
{
resultData.AddError("ModuleFile", Resources.FieldIsRequired);
}
}
catch (Exception e)
{
resultData.message = e.Message;
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(e);
}
return Json(resultData);
}
/// <summary>
/// Installs the module.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="moduleName">Name of the module.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult Install(string application, string moduleName)
{
JsonResultData resultData = new JsonResultData();
resultData.warning = ModuleManager.Install(moduleName, User.Identity.Name);
return Json(resultData);
}
/// <summary>
/// Unstalls the module
/// </summary>
/// <param name="application">The application.</param>
/// <param name="moduleName">Name of the module.</param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Module)]
public ActionResult Unstall(string application, string moduleName)
{
JsonResultData resultData = new JsonResultData();
if (BeReferenced(moduleName))
{
resultData.message = string.Format(Resources.ModuleCannotDeleteOrUnstall);
}
else
{
resultData.warning = ModuleManager.Uninstall(moduleName);
}
return Json(resultData);
}
#endregion
#region Relations
private bool BeReferenced(string moduleName)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
return dataContext.QueryPagesByModule(moduleName).Count() > 0;
}
public ActionResult UsedBy(string moduleName, string application)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
#region Used in Pages
var pageNodes = new TreeNode();
treeNodes.Add(pageNodes.Attributes);
pageNodes.Text = Resources.TreeNode_Page;
pageNodes.IconCls = FolderType.Page.ToString();
pageNodes.Expanded = true;
var pages = dataContext.QueryPagesByModule(moduleName)
.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.Attributes.Add("uuid", page.UUID);
pageNode.IconCls = "PageNode";
pageNode.Leaf = true;
}
#endregion
return Json(treeNodes);
}
#endregion
#region Combobox
///// <summary>
///// Gets the themes.
///// </summary>
///// <param name="moduleName">Name of the module.</param>
///// <returns></returns>
//public ActionResult GetThemes(string moduleName)
//{
// var moduleInfo = ModuleManager.GetModule(moduleName, CmsGlobal.RootApplicationName);
// var themes = moduleInfo.GetThemes();
// var items = themes.ToComboboxItems(s => s, s => s);
// return Json(new ExtJsonReaderObject(items, items.Count));
//}
///// <summary>
///// Gets the entry urls.
///// </summary>
///// <param name="moduleName">Name of the module.</param>
///// <returns></returns>
//public ActionResult GetEntryUrls(string moduleName)
//{
// var moduleInfo = ModuleManager.GetModule(moduleName, CmsGlobal.RootApplicationName);
// var entryUrls = moduleInfo.Configuration.EntryUrls;
// IList<ComboboxItem> items = new List<ComboboxItem>();
// if (entryUrls != null)
// {
// items = entryUrls.ToComboboxItems(s => s.Url, s => s.Url);
// }
// return Json(new ExtJsonReaderObject(items, items.Count));
//}
#endregion
}
}
|