/*
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;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
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.ExtensionMethod;
using Everest.Library.Versioning;
using Everest.Library.Data.Rule;
using Microsoft.Data.Extensions;
using Everest.CmsServices.Services;
using Everest.CmsServices.Models;
using Everest.CmsServices.Providers;
namespace Everest.CmsServices.Controllers{
public class TextSchemaController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region IStandardActions Members
/// <summary>
/// Gets the list.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.TextSchema)]
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
var folderUUID = new Guid(Request.Form["folderUUID"]);
var queryable = dataContext.QuerySchemas(application, SchemaType.Text);
string name = Request.Form["SchemaName"];
if (!StringExtensions.IsNullOrEmptyTrim(name))
{
queryable = queryable.Where(s => s.SchemaName.Contains(name));
}
queryable = queryable.OrderByDescending(p => p.SchemaId);
queryable = OrderByRequest(queryable);
var queryResult = queryable.Select<Cms_Schema, object>(c => new
{
UUID = c.UUID,
SchemaId = c.SchemaId,
SchemaName = c.SchemaName,
ModifiedDate = c.ModifiedDate,
UserName = c.UserName,
Application = c.aspnet_Applications.ApplicationName
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
/// <summary>
/// Gets the details.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.TextSchema)]
public ActionResult GetDetail()
{
var schemaUUID = new Guid(Request.Form["UUID"]);
object jsonData = GetFormSchemaDetail(schemaUUID);
return Json(new JsonResultData() { success = true, data = jsonData });
}
/// <summary>
/// Gets the form schema details.
/// </summary>
/// <param name="Id">The id.</param>
/// <returns></returns>
private object GetFormSchemaDetail(Guid schemaUUID)
{
var query = dataContext.QuerySchema(schemaUUID).Select(s => new
{
Schema = s,
s.aspnet_Applications.ApplicationName
});
var data = query.First();
var columns = (from c in dataContext.Cms_Column
where c.Cms_Schema.UUID == schemaUUID
orderby c.Order
select new DetailColumn()
{
UUID = c.UUID,
ColumnId = c.ColumnId,
ColumnName = c.ColumnName,
Label = c.Label,
DataType = c.DataType,
AllowNull = c.AllowNull,
Length = c.Length,
Order = c.Order,
Queryable = c.Queryable,
Modifiable = c.Modifiable,
ControlType = c.ControlType,
Items = c.Items,
DefaultValue = c.DefaultValue,
ValidatorGroupUniqueName = new UniqueName() { ApplicationName = c.Cms_ValidatorGroup.aspnet_Applications.ApplicationName, ItemName = c.Cms_ValidatorGroup.ValidateGroupName },
IntSortOrder = c.SortOrder,
Indexable = c.Indexable,
VisibleInList = c.VisibleInList,
OriginalUUID = c.OriginalUUID,
VType = c.VType,
Tpl = c.Tpl,
Tooltip = c.Tooltip
}).ToArray();
object jsonData = new
{
UUID = data.Schema.UUID,
Application = data.ApplicationName,
SchemaId = data.Schema.SchemaId,
SchemaName = data.Schema.SchemaName,
FormTitle = data.Schema.SchemaName,
data.Schema.IncludeUserKey,
data.Schema.FileUploadable,
data.Schema.Extensions,
data.Schema.BinarySchemas,
ChildSchemasId = data.Schema.ChildSchemas,
ReferencingSchemasId = data.Schema.ReferencingSchemas,
Columns = columns,
};
return jsonData;
}
/// <summary>
/// Submits the specified add.
/// </summary>
/// <param name="add">if set to <c>true</c> [add].</param>
/// <param name="closeForm"></param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.TextSchema)]
[System.Web.Mvc.ValidateInput(false)]
public ActionResult Submit(bool add, bool? closeForm)
{
var resultData = new JsonResultData() { success = true };
try
{
Cms_Schema schema;
List<Cms_Column> olderColumns = null;
if (add)
{
schema = new Cms_Schema();
string application = Request.Form["application"];
schema.aspnet_Applications = dataContext.QueryApplication(application).First();
schema.SchemaType = (int)SchemaType.Text;
schema.UUID = Guid.NewGuid();
}
else
{
int Id = int.Parse(Request.Form["oldData.SchemaId"]);
schema = dataContext.QuerySchema(Id).First();
//check out the version.
//schema.Checkout(User.Identity.Name, "");
olderColumns = schema.ClearColumns(dataContext);
}
#region Set Properties
schema.SchemaName = Request.Form["SchemaName"];
if (StringExtensions.IsTrue(Request["FileUploadable"]))
{
schema.FileUploadable = true;
}
else
{
schema.FileUploadable = false;
}
if (StringExtensions.IsTrue(Request["IncludeUserKey"]))
{
schema.IncludeUserKey = true;
}
else
{
schema.IncludeUserKey = false;
}
schema.Extensions = Request.Form["Extensions"];
schema.BinarySchemas = Request.Form["BinarySchemasId"];
schema.ChildSchemas = Request.Form["ChildSchemasId"];
schema.ReferencingSchemas = Request.Form["ReferencingSchemasId"];
schema.ModifiedDate = DateTime.Now;
schema.UserName = User.Identity.Name;
#endregion
#region Columns
Dictionary<string, string>[] columns = Request.Form["Columns"].DeserializeJSON<Dictionary<string, string>[]>();
int columnCount = 0;
for (int i = 0; i < columns.Length; i++)
{
var column = Cms_Column.CreateCms_Column(dataContext, columns[i].ToNameValueCollection());
if (column != null)
{
columnCount++;
column.Order = columnCount;
schema.Cms_Column.Add(column);
}
}
#endregion
var schemaService = UnityManager.Resolve<SchemaService>();
if (add)
{
schemaService.AddSchema(dataContext, schema, User.Identity.Name);
}
else
{
schemaService.UpdateSchema(dataContext, schema, User.Identity.Name, Request.Form["oldData.SchemaName"], olderColumns);
}
if (closeForm == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetFormSchemaDetail(schema.UUID);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
return Json(resultData);
}
/// <summary>
/// Deletes this instance.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.TextSchema)]
public ActionResult Delete(Guid[] UUID)
{
var resultData = new JsonResultData();
var schemaService = UnityManager.Resolve<SchemaService>();
foreach (var guid in UUID)
{
schemaService.DeleteSchema(guid);
}
return Json(resultData);
}
#endregion
#region Combobox
/// <summary>
/// Gets the data type for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetDataTypeForCombobox()
{
string[] names = Enum.GetNames(typeof(ColumnDataType));
var items = names.ToComboboxItems(s => s, s => s, false);
return Json(new ExtJsonReaderObject(items, items.Count));
}
/// <summary>
/// Gets the text schemas for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetTextSchemasForCombobox(string application)
{
var queryable = dataContext.QuerySchemas(application, SchemaType.Text);
var items = queryable.ToComboboxItems<Cms_Schema>(s => s.SchemaName, s => s.UUID.ToString());
return Json(new ExtJsonReaderObject(items, items.Count));
}
/// <summary>
/// Gets the controls from combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetControlsForCombobox()
{
string[] names = Enum.GetNames(typeof(ControlType));
if (!CmsGlobal.EnableRadEditor)
{
names = names.Where(n => n != "Radeditor" && n != "Radeditorsimple").ToArray();
}
if (!CmsGlobal.EnableTinymce)
{
names = names.Where(n => n != "Tinymce").ToArray();
}
if (!CmsGlobal.EnableCKEditor)
{
names = names.Where(n => n != "CKEditor").ToArray();
}
if (!CmsGlobal.EnableFCKEditor)
{
names = names.Where(n => n != "FCKEditor").ToArray();
}
names = names.Where(n => !n.Contains("MultiSelect") && !n.Contains("MultiFiles")).ToArray();
var items = names.ToComboboxItems(s => s, s => s, false);
return Json(new ExtJsonReaderObject(items, items.Count));
}
/// <summary>
/// Gets the schemas for content folder.
/// </summary>
/// <returns></returns>
public ActionResult GetSchemaForCreateFolder(string application)
{
var query = dataContext.QuerySchemas(application);
var items = query.ToComboboxItems<Cms_Schema>(s => s.SchemaName, s => s.UUID.ToString(), false);
return Json(new ExtJsonReaderObject(items, items.Count));
}
#endregion
#region Relations
public ActionResult UsedBy(Guid uuid)
{
//Cms_Schema schema = dataContext.QuerySchema(uuid).First();
string strSchemaId = uuid.ToString();
IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
#region Use in TextFolders
var foldersNode = new TreeNode();
treeNodes.Add(foldersNode.Attributes);
foldersNode.Text = Resources.UsedByFolders;
foldersNode.IconCls = FolderType.Content.ToString();
foldersNode.Leaf = false;
foldersNode.Expanded = true;
var folders = dataContext.QueryFoldersBySchema(uuid).
Select(f => new
{
f.aspnet_Applications.ApplicationName,
Folder = f
});
foldersNode.children = new List<IDictionary<string, object>>();
foreach (var folder in folders)
{
var folderNode = new TreeNode();
foldersNode.children.Add(folderNode.Attributes);
folderNode.Text = new UniqueName(CmsGlobal.GetApplicationName(folder.ApplicationName), folder.Folder.FolderName).ToString();
folderNode.IconCls = FolderType.TextContent.ToString();
folderNode.Attributes.Add("UUID", folder.Folder.UUID.ToString());
folderNode.Attributes.Add("dataUrl", "folder/UsedBy");
}
#endregion
#region Included by others
var includedByNode = new TreeNode();
includedByNode.Text = Resources.IncludedBy;
includedByNode.IconCls = FolderType.TextSchema.ToString();
includedByNode.Expanded = true;
treeNodes.Add(includedByNode.Attributes);
var includingSchemas = dataContext.Cms_Schema.Where(s => s.ChildSchemas.Contains(strSchemaId))
.Select(s => new
{
s.aspnet_Applications.ApplicationName,
Schema = s
});
includedByNode.children = new List<IDictionary<string, object>>();
foreach (var including in includingSchemas)
{
var schemaNode = new TreeNode();
includedByNode.children.Add(schemaNode.Attributes);
schemaNode.Text = new UniqueName(CmsGlobal.GetApplicationName(including.ApplicationName), including.Schema.SchemaName).ToString();
schemaNode.IconCls = FolderType.TextSchema.ToString();
schemaNode.Attributes.Add("uuid", including.Schema.UUID);
schemaNode.Attributes.Add("dataUrl", "textSchema/UsedBy");
}
#endregion
#region Referenced by others
var referencedByNode = new TreeNode();
referencedByNode.Text = Resources.ReferencedBy;
referencedByNode.IconCls = FolderType.TextSchema.ToString();
referencedByNode.Expanded = true;
treeNodes.Add(referencedByNode.Attributes);
var referencedBySchemas = dataContext.Cms_Schema.Where(s => s.ReferencingSchemas.Contains(strSchemaId))
.Select(s => new
{
s.aspnet_Applications.ApplicationName,
Schema = s
});
referencedByNode.children = new List<IDictionary<string, object>>();
foreach (var referencedBy in referencedBySchemas)
{
var schemaNode = new TreeNode();
referencedByNode.children.Add(schemaNode.Attributes);
schemaNode.Text = new UniqueName(CmsGlobal.GetApplicationName(referencedBy.ApplicationName), referencedBy.Schema.SchemaName).ToString();
schemaNode.IconCls = FolderType.TextSchema.ToString();
schemaNode.Attributes.Add("uuid", referencedBy.Schema.UUID);
schemaNode.Attributes.Add("dataUrl", "textSchema/UsedBy");
}
#endregion
return Json(treeNodes);
}
#endregion
}
}
|