/*
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.Text;
using System.Collections.Specialized;
using Everest.Library.Data;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Everest.Forms;
using Everest.Forms.SchemaProvider;
namespace Everest.CmsServices.Providers{
public class ContentSchemaProvider : ISchemaProvider
{
IControlTypeFactory controlTypeFactory;
public ContentSchemaProvider(IControlTypeFactory controlTypeFactory)
{
this.controlTypeFactory = controlTypeFactory;
}
#region ISchemaProvider Members
/// <summary>
/// Gets the schema.
/// </summary>
/// <param name="name">The name. The content schema id</param>
/// <returns></returns>
public ISchema GetSchema(string name, NameValueCollection nameValues)
{
Guid schemaUUID = new Guid(name);
Guid? folderUUID = null;
if (!StringExtensions.IsNullOrEmptyTrim(nameValues["folderUUID"]))
{
folderUUID = new Guid(nameValues["folderUUID"]);
}
bool childSchema = nameValues.AllKeys.Contains("isChildForm");
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
dataContext.EnableCaching = true;
Cms_Schema cms_Schema = dataContext.QuerySchema(schemaUUID).First();
ISchema schema = new Schema();
schema.Name = cms_Schema.SchemaName;
schema.Title = string.Format("{0}>{1}", Resources.TreeNode_Content, cms_Schema.SchemaName);
schema.FormType = "Everest.Cms.TextContentFormPanel";
schema.GridType = "Everest.Cms.TextContentGridForm";
schema.SubmitUrl = "Kooboo_Content/submit";
schema.QueryUrl = "Kooboo_Content/GetList";
schema.DeleteUrl = "Kooboo_Content/delete";
schema.DetailsUrl = "Kooboo_Content/GetDetail";
//the url for localize
schema.GridAttributes.Add("localizeUrl", "Kooboo_Content/localize");
schema.GridAttributes.Add("unlocalizeUrl", "Kooboo_Content/delete");
schema.GridAttributes.Add("usedByUrl", "Kooboo_Content/UsedBy");
cms_Schema.Cms_Column.Load();
//Backward compatible
var titleColumn = cms_Schema.TitleColumn;
if (titleColumn == null)
{
Column title = new Column();
title.Name = "Title";
title.Label = Resources.ContentForm_Title;
title.Length = 200;
title.Control = controlTypeFactory.ResolveControl("Text");
title.DataIndexInDetail = title.Name;
title.DataIndexInList = title.Name;
title.Queryable = true;
title.AllowNull = false;
title.Modifiable = true;
schema.AddColumn(title);
}
else
{
Column column = NewSechmaColumn(titleColumn);
schema.AddColumn(column);
}
DefaultColumns(dataContext, schema, cms_Schema, folderUUID, childSchema);
foreach (var item in cms_Schema.DynamicTableColumns)
{
Column column = NewSechmaColumn(item);
schema.AddColumn(column);
}
if (!StringExtensions.IsNullOrEmptyTrim(cms_Schema.ReferencingSchemas))
{
string[] arrSchemaUUID = cms_Schema.ReferencingSchemas.Split(',');
foreach (var uuid in arrSchemaUUID)
{
if (!StringExtensions.IsNullOrEmptyTrim(uuid))
{
Guid guid = new Guid(uuid);
var refSchema = CachedData.GetSchema(guid);
Column referencingColumn = new Column();
referencingColumn.Label = refSchema.SchemaName;// Resources.ContentForm_Reference;
referencingColumn.Control = controlTypeFactory.ResolveControl(ControlType.MultiSelect.ToString());
referencingColumn.Name = "Ref_" + refSchema.SchemaName;
referencingColumn.DataIndexInDetail = referencingColumn.Name;
referencingColumn.DetailAttributes.Add("schemaUUID", uuid);
referencingColumn.DetailAttributes.Add("schemaName", uuid);
referencingColumn.DetailAttributes.Add("keyColumnName", "UUID");
referencingColumn.DetailAttributes.Add("titleColumnName", "Title");
referencingColumn.DetailAttributes.Add("getFormUrl", "Kooboo_MultiSelectContent/GetForm");
schema.AddColumn(referencingColumn);
}
}
}
if (!StringExtensions.IsNullOrEmptyTrim(cms_Schema.BinarySchemas))
{
string[] arrSchemaUUID = cms_Schema.BinarySchemas.Split(',');
foreach (var uuid in arrSchemaUUID)
{
if (!StringExtensions.IsNullOrEmptyTrim(uuid))
{
Guid guid = new Guid(uuid);
var refSchema = CachedData.GetSchema(guid);
Column binaryColumn = new Column();
binaryColumn.Label = refSchema.SchemaName; // Resources.ContentForm_BinaryContents;
binaryColumn.Control = controlTypeFactory.ResolveControl(ControlType.MultiSelect.ToString());
binaryColumn.Name = "Bin_" + refSchema.SchemaName;
binaryColumn.DataIndexInDetail = binaryColumn.Name;
binaryColumn.DetailAttributes.Add("schemaUUID", uuid);
binaryColumn.DetailAttributes.Add("schemaName", uuid);
binaryColumn.DetailAttributes.Add("keyColumnName", "UUID");
binaryColumn.DetailAttributes.Add("titleColumnName", "Title");
binaryColumn.DetailAttributes.Add("getFormUrl", "Kooboo_MultiSelectContent/GetForm");
schema.AddColumn(binaryColumn);
}
}
}
if (cms_Schema.FileUploadable)
{
Column multiFiles = new Column();
multiFiles.Label = Resources.ContentForm_Attachment;
multiFiles.Name = "deletedFiles";
multiFiles.DetailAttributes.Add("allowedExtensions", cms_Schema.Extensions);
multiFiles.Control = controlTypeFactory.ResolveControl(ControlType.MultiFiles.ToString());
multiFiles.DataIndexInDetail = "Attachment";
schema.AddColumn(multiFiles);
}
if (!StringExtensions.IsNullOrEmptyTrim(cms_Schema.ChildSchemas))
{
string[] schemaIdGroup = cms_Schema.ChildSchemas.Split(',');
if (schemaIdGroup.Length > 0)
{
var list = new List<IReferenceForm>();
schema.ReferenceForms = list;
foreach (var refrenceId in schemaIdGroup)
{
list.Add(new SchemaReference()
{
DataIndex = "DataIndex" + list.Count.ToString(),
FormType = "grid",
SchemaName = refrenceId
});
}
}
}
var gridButtons = GetCustomButtions(schemaUUID, FunctionType.Grid);
if (!string.IsNullOrEmpty(gridButtons))
{
schema.GridAttributes.Add("customButtons", string.Format("[{0}]", gridButtons));
}
var detailButtons = GetCustomButtions(schemaUUID, FunctionType.Form);
if (!string.IsNullOrEmpty(detailButtons))
{
schema.FormAttributes.Add("customButtons", string.Format("[{0}]", detailButtons));
}
return schema;
}
private Column NewSechmaColumn(Cms_Column item)
{
Column column = new Column();
column.Name = item.ColumnName;
column.Label = item.Label;
column.Control = controlTypeFactory.ResolveControl(item.ControlType);
column.DataIndexInDetail = column.Name;
if (item.Length != null)
{
column.Length = item.Length.Value;
}
if (item.Queryable != null)
{
column.Queryable = item.Queryable.Value;
}
if (item.AllowNull != null)
{
column.AllowNull = item.AllowNull.Value;
}
if (item.Modifiable != null)
{
column.Modifiable = item.Modifiable.Value;
}
if (item.VisibleInList != null && item.VisibleInList.Value == true)
{
column.DataIndexInList = item.ColumnName;
}
if (item.VType != null && !StringExtensions.IsNullOrEmptyTrim(item.VType.Trim()))
{
column.DetailAttributes.Add("vtype", item.VType.Trim());
}
if (!StringExtensions.IsNullOrEmptyTrim(item.Tpl))
{
column.ListAttributes.Add("tpl", item.Tpl.Trim());
column.ListAttributes.Add("xtype", "templatecolumn");
}
if (item.DataType.Equals("datetime", StringComparison.InvariantCultureIgnoreCase))
{
column.ListAttributes.Add("renderer", "Ext.util.Format.dateTime");
}
if (item.Length.HasValue)
{
column.DetailAttributes.Add("maxLength", item.Length.Value.ToString());
}
if (!string.IsNullOrEmpty(item.Tooltip))
{
column.DetailAttributes.Add("tip", item.Tooltip);
}
column.Items = item.Items;
column.DefaultValue = item.DefaultValue;
item.Cms_ValidatorGroupReference.Load();
if (item.Cms_ValidatorGroup != null)
{
item.Cms_ValidatorGroup.Cms_Validator.Load();
StringBuilder validator = new StringBuilder();
foreach (var v in item.Cms_ValidatorGroup.Cms_Validator)
{
validator.AppendFormat(@" r = eval('f={0}')(value);if(r != true) return r;", v.Function.Replace("\n", " ").Replace("\r", " "));
}
column.DetailAttributes.Add("validator", string.Format("function (value){{var r=''; {0} ;return true}}", validator.ToString()));
//enable validator must be set disallow null.
column.AllowNull = false;
}
return column;
}
private string GetCustomButtions(Guid schemaUUID, FunctionType type)
{
StringBuilder buttonsBuilder = new StringBuilder();
var functions = CachedData.GetFunctionBySchema(schemaUUID, type);
foreach (var function in functions)
{
string buttonConfig = string.Format("{{ text: \"{0}\",functionId:{1}, scope: this}},",
function.Name, function.FunctionId);
buttonsBuilder.Append(buttonConfig);
}
return buttonsBuilder.RemoveLastSpecifiedChar(',').ToString();
}
private void DefaultColumns(IEverestCmsDataContext dataContext, ISchema schema, Cms_Schema cms_Schema, Guid? folderUUID, bool childSchema)
{
bool hasWorkflow = false;
if (folderUUID.HasValue && dataContext.QueryWorkflowByFolder(folderUUID.Value).Exists())
{
hasWorkflow = true;
}
Column contentId = new Column();
contentId.Label = "ContentId";
contentId.Name = "ContentId";
contentId.DataIndexInList = "ContentId";
contentId.ListAttributes.Add("hidden", "true");
schema.AddColumn(contentId);
Column isLocalized = new Column();
isLocalized.Label = "IsLocalized";
isLocalized.Name = "IsLocalized";
isLocalized.DataIndexInList = "IsLocalized";
isLocalized.ListAttributes.Add("hidden", "true");
schema.AddColumn(isLocalized);
Column originalContentId = new Column();
originalContentId.Label = "OriginalUUID";
originalContentId.Name = "OriginalUUID";
originalContentId.DataIndexInList = "OriginalUUID";
originalContentId.ListAttributes.Add("hidden", "true");
schema.AddColumn(originalContentId);
Column baseUUID = new Column();
baseUUID.Label = "BaseUUID";
baseUUID.Name = "BaseUUID";
baseUUID.DataIndexInList = "BaseUUID";
baseUUID.ListAttributes.Add("hidden", "true");
schema.AddColumn(baseUUID);
Column uuid = new Column();
uuid.Label = "UUID";
uuid.Name = "UUID";
uuid.DataIndexInList = "UUID";
uuid.ListAttributes.Add("hidden", "true");
schema.AddColumn(uuid);
if (cms_Schema.IncludeUserKey.HasValue && cms_Schema.IncludeUserKey.Value == true)
{
Column userKey = new Column();
userKey.Name = "UserKey";
userKey.Label = Resources.UserKeyColumnLabel;
userKey.Length = 200;
userKey.Control = controlTypeFactory.ResolveControl("Text");
userKey.DataIndexInDetail = userKey.Name;
userKey.DataIndexInList = userKey.Name;
//userKey.Queryable = true;
userKey.AllowNull = true;
userKey.Modifiable = true;
userKey.DetailAttributes.Add("rightLabel", Resources.UserKeyTips);
schema.AddColumn(userKey);
}
Column application = new Column();
application.Name = "Application";
application.Label = Resources.ContentForm_Application;
application.DataIndexInList = application.Name;
application.Queryable = false;
schema.AddColumn(application);
Column userName = new Column();
userName.Name = "UserName";
userName.Label = Resources.ContentForm_UserName;
userName.DataIndexInList = userName.Name;
schema.AddColumn(userName);
Column postDate = new Column();
postDate.Name = "PostDate";
postDate.Label = Resources.ContentForm_PostDate;
postDate.DataIndexInList = postDate.Name;
postDate.ListAttributes.Add("renderer", "Ext.util.Format.dateTime");
schema.AddColumn(postDate);
Column modifiedDate = new Column();
modifiedDate.Name = "ModifiedDate";
modifiedDate.Label = Resources.ContentForm_ModifiedDate;
modifiedDate.DataIndexInList = modifiedDate.Name;
modifiedDate.ListAttributes.Add("hidden", "true");
modifiedDate.ListAttributes.Add("renderer", "Ext.util.Format.dateTime");
schema.AddColumn(modifiedDate);
Column published = new Column();
published.Name = "Published";
published.Label = Resources.ContentForm_Published;
published.Control = controlTypeFactory.ResolveControl("Checkbox");
published.DataIndexInList = "ContentStatus";
published.DataIndexInDetail = "Published";
if (hasWorkflow)
{
published.DetailAttributes.Add("disabled", "true");
}
else
{
published.DetailAttributes.Add("checked", "true");
}
published.DetailAttributes.Add("resizable", "true");
//published.ListAttributes.Add("width", "100");
published.ListAttributes.Add("renderer", "Everest.Cms.formatter.formatContentStatus");
schema.AddColumn(published);
if (!childSchema && folderUUID.HasValue)
{
if (dataContext.QueryFolder(folderUUID.Value).Select(f => f.Base != null).First())
{
Column including = new Column();
including.Name = "Including";
including.Label = Resources.ContentForm_Inclusion;
including.DataIndexInList = "Including";
including.ListAttributes.Add("renderer", "Ext.util.Format.formatBoolean");
including.ListAttributes.Add("sortable", "false");
schema.AddColumn(including);
}
if (hasWorkflow == true)
{
Column stepName = new Column();
stepName.Name = "StepName";
stepName.Label = Resources.ContentForm_StepName;
stepName.DataIndexInList = "StepName";
schema.AddColumn(stepName);
Column preResult = new Column();
preResult.Name = "PreResult";
preResult.Label = Resources.ContentForm_PreResult;
preResult.DataIndexInList = preResult.Name;
preResult.ListAttributes.Add("hidden", "true");
preResult.ListAttributes.Add("renderer", "Everest.Cms.formatter.formatWorkflowResult");
schema.AddColumn(preResult);
Column processResult = new Column();
processResult.Name = "ProcessResult";
processResult.Label = Resources.ContentForm_ProcessResult;
processResult.DataIndexInList = processResult.Name;
processResult.ListAttributes.Add("hidden", "true");
processResult.ListAttributes.Add("renderer", "Everest.Cms.formatter.formatWorkflowResult");
schema.AddColumn(processResult);
Column processed = new Column();
processed.Name = "Processed";
processed.Label = Resources.ContentForm_Processed;
processed.Control = controlTypeFactory.ResolveControl("Combobox");
processed.Items = Resources.ContentForm_ProcessedValue;
processed.Queryable = true;
processed.DefaultValue = " ";
//preResult.ListAttributes.Add("renderer", "Ext.util.Format.date");
schema.AddColumn(processed);
Column workflowHistoryUUID = new Column();
workflowHistoryUUID.Label = Resources.ContentForm_WorkflowHistoryUUID;
workflowHistoryUUID.Name = "WorkflowHistoryUUID";
workflowHistoryUUID.DataIndexInList = "WorkflowHistoryUUID";
workflowHistoryUUID.ListAttributes.Add("hidden", "true");
schema.AddColumn(workflowHistoryUUID);
Column sequenceOrder = new Column();
sequenceOrder.Label = "SequenceOrder";
sequenceOrder.Name = "SequenceOrder";
sequenceOrder.DataIndexInList = "SequenceOrder";
sequenceOrder.ListAttributes.Add("hidden", "true");
schema.AddColumn(sequenceOrder);
Column startSequenceOrder = new Column();
startSequenceOrder.Label = "StartSequenceOrder";
startSequenceOrder.Name = "StartSequenceOrder";
startSequenceOrder.DataIndexInList = "StartSequenceOrder";
startSequenceOrder.ListAttributes.Add("hidden", "true");
schema.AddColumn(startSequenceOrder);
}
}
Column folderUUIDColumn = new Column();
folderUUIDColumn.Label = Resources.ContentForm_FolderUUID;
folderUUIDColumn.Name = "FolderUUID";
folderUUIDColumn.DataIndexInList = "FolderUUID";
folderUUIDColumn.ListAttributes.Add("hidden", "true");
schema.AddColumn(folderUUIDColumn);
}
#endregion
}
}
|