📄 modelmanager.cs
字号:
namespace PowerEasy.CommonModel
{
using PowerEasy.Common;
using PowerEasy.Components;
using PowerEasy.Enumerations;
using PowerEasy.IDal.CommonModel;
using PowerEasy.Model.CommonModel;
using PowerEasy.Model.Contents;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
public sealed class ModelManager
{
private static readonly IModel dal = DataAccess.CreateCotentModel();
private static Serialize<FieldInfo> ser = new Serialize<FieldInfo>();
private ModelManager()
{
}
public static bool Add(ModelInfo modelInfo, int modelTemplateId)
{
CreateCommonListPageTemplate(modelInfo);
return Add(modelInfo, modelTemplateId, ModelType.Content);
}
public static bool Add(ModelInfo modelInfo, int modelTemplateId, ModelType type)
{
bool flag = false;
if ((modelTemplateId == 0) && (type == ModelType.Content))
{
modelInfo.Field = AddDefaultField();
}
else
{
modelInfo.Field = ModelTemplate.GetField(modelTemplateId);
}
EncodeModelInfo(modelInfo);
if (dal.Add(modelInfo))
{
flag = true;
if (modelTemplateId != 0)
{
List<FieldInfo> list = ser.DeserializeFieldList(modelInfo.Field);
if (list != null)
{
list.Sort(new FieldInfoComparer());
}
foreach (FieldInfo info in list)
{
if (info.FieldLevel != 0)
{
dal.AddFieldToTable(info, modelInfo.TableName);
}
if (info.FieldType == FieldType.ContentType)
{
FieldInfo fieldInfo = new FieldInfo();
fieldInfo.FieldName = info.Settings[2];
fieldInfo.FieldType = FieldType.ContentType;
fieldInfo.FieldLevel = 1;
AddTableField(fieldInfo, modelInfo.ModelId);
FieldInfo info3 = new FieldInfo();
info3.FieldName = info.Settings[5];
info3.FieldType = FieldType.ContentType;
info3.FieldLevel = 1;
AddTableField(info3, modelInfo.ModelId);
}
}
}
}
RemoveCache();
return flag;
}
public static string AddDefaultField()
{
IList<FieldInfo> fieldList = new List<FieldInfo>();
string[] strArray = new string[] { "Title", "Status", "EliteLevel", "Priority", "Hits", "DayHits", "WeekHits", "MonthHits", "UpdateTime", "NodeId", "TemplateFile", "InfoId", "SpecialId", "DefaultPicUrl" };
string[] strArray2 = new string[] { "标题", "状态", "推荐级别", "优先级", "点击数", "日点击数", "周点击数", "月点击数", "更新时间", "所属节点", "内容页模板", "所属其它节点", "专题", "首页图片" };
string[] strArray3 = new string[] { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
string[] strArray4 = new string[] { "", "审核状态", " <span style='color:Blue;'>注:数字越大推荐级别越高</span>", " <span style='color:Blue;'>注:数字越大优先级别越高</span>", "", "", "", "", "更新时间", "", "内容页模板地址", "如一篇文章同时属于多个栏目,在此选择即可!", "", "" };
string[] strArray5 = new string[] { "", "0", "0", "0", "0", "0", "0", "0", "Now", "", "", "", "", "" };
FieldType[] typeArray = new FieldType[] { FieldType.TitleType, FieldType.StatusType, FieldType.NumberType, FieldType.NumberType, FieldType.NumberType, FieldType.NumberType, FieldType.NumberType, FieldType.NumberType, FieldType.DateTimeType, FieldType.NodeType, FieldType.TemplateType, FieldType.InfoType, FieldType.SpecialType, FieldType.PictureType };
Collection<string> item = new Collection<string>();
item.Add("255");
item.Add("50");
item.Add("0");
Collection<string> collection2 = new Collection<string>();
collection2.Add("0");
collection2.Add("100");
collection2.Add("-1");
collection2.Add("0");
Collection<string> collection3 = new Collection<string>();
collection3.Add("0");
collection3.Add("100");
collection3.Add("-1");
collection3.Add("0");
Collection<string> collection4 = new Collection<string>();
collection4.Add("0");
collection4.Add("100");
collection4.Add("-1");
collection4.Add("0");
Collection<string> collection5 = new Collection<string>();
collection5.Add("0");
collection5.Add("");
collection5.Add("-1");
collection5.Add("0");
Collection<string> collection6 = new Collection<string>();
collection6.Add("0");
collection6.Add("");
collection6.Add("-1");
collection6.Add("0");
Collection<string> collection7 = new Collection<string>();
collection7.Add("0");
collection7.Add("");
collection7.Add("-1");
collection7.Add("0");
Collection<string> collection8 = new Collection<string>();
collection8.Add("0");
collection8.Add("");
collection8.Add("-1");
collection8.Add("0");
Collection<string> collection9 = new Collection<string>();
collection9.Add("yyyy-MM-dd HH:mm:ss");
collection9.Add("1");
collection9.Add("1");
collection9.Add("");
Collection<string> collection10 = new Collection<string>();
collection10.Add("");
collection10.Add("");
collection10.Add("");
collection10.Add("");
Collection<string> collection11 = new Collection<string>();
collection11.Add("");
collection11.Add("");
collection11.Add("");
collection11.Add("");
Collection<string> collection12 = new Collection<string>();
collection12.Add("");
collection12.Add("");
collection12.Add("");
collection12.Add("");
Collection<string> collection13 = new Collection<string>();
collection13.Add("");
collection13.Add("");
collection13.Add("");
collection13.Add("");
Collection<string> collection14 = new Collection<string>();
collection14.Add("30");
collection14.Add("1024");
collection14.Add("jpg|gif|bmp");
collection14.Add("0");
collection14.Add("0");
collection14.Add("0");
collection14.Add("False");
collection14.Add("False");
Collection<Collection<string>> collection15 = new Collection<Collection<string>>();
collection15.Add(item);
collection15.Add(collection2);
collection15.Add(collection3);
collection15.Add(collection4);
collection15.Add(collection5);
collection15.Add(collection6);
collection15.Add(collection7);
collection15.Add(collection8);
collection15.Add(collection9);
collection15.Add(collection10);
collection15.Add(collection11);
collection15.Add(collection12);
collection15.Add(collection13);
collection15.Add(collection14);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 14; i++)
{
FieldInfo info = new FieldInfo();
info.Id = strArray[i];
info.FieldName = strArray[i];
info.FieldAlias = strArray2[i];
info.Tips = strArray3[i];
info.OrderId = i + 1;
info.FieldType = typeArray[i];
info.Disabled = false;
info.Description = strArray4[i];
info.DefaultValue = strArray5[i];
if (i == 10)
{
info.EnableNull = false;
}
else
{
info.EnableNull = true;
}
info.FieldLevel = 0;
info.CopyToSettings(collection15[i]);
info.EnableShowOnSearchForm = true;
fieldList.Add(info);
}
builder.Append(Field.SerializeFieldList(fieldList));
return builder.ToString();
}
public static bool AddFieldToTable(FieldInfo fieldInfo, int modelId)
{
IList<FieldInfo> list = new List<FieldInfo>();
string xmlFieldByModelId = GetXmlFieldByModelId(modelId);
if (!string.IsNullOrEmpty(xmlFieldByModelId))
{
list = ser.DeserializeFieldList(xmlFieldByModelId);
}
list.Add(fieldInfo);
xmlFieldByModelId = ser.SerializeFieldList(list);
bool flag = false;
flag = UpdateField(modelId, xmlFieldByModelId);
if (flag && (fieldInfo.FieldType != FieldType.Property))
{
flag = AddTableField(fieldInfo, modelId);
}
return flag;
}
public static bool AddModelForNodes(int nodeId, DataTable contentData)
{
bool flag = true;
int count = contentData.Rows.Count;
if (count == 0)
{
flag = false;
}
for (int i = 0; i < count; i++)
{
int modelId = DataConverter.CLng(contentData.Rows[i]["ModelId"]);
string templateId = DataSecurity.HtmlEncode(contentData.Rows[i]["TemplateFile"].ToString());
if (!ModelIdExists(nodeId, modelId) && !dal.AddModelForNodes(nodeId, modelId, templateId))
{
return false;
}
}
return flag;
}
public static bool AddNodesModelTemplateRelationShip(NodesModelTemplateRelationShipInfo info)
{
bool flag = true;
if ((!string.IsNullOrEmpty(info.DefaultTemplateFile) && (info.NodeId > 0)) && (info.ModelId > 0))
{
info.DefaultTemplateFile = DataSecurity.HtmlEncode(info.DefaultTemplateFile);
flag = dal.AddNodesModelTemplateRelationShip(info);
}
return flag;
}
public static bool AddTableField(FieldInfo fieldInfo, int modelId)
{
ModelInfo modelInfoById = GetModelInfoById(modelId);
return dal.AddFieldToTable(fieldInfo, modelInfoById.TableName);
}
public static bool AddTemplateForNodes(int nodeId, IList<int> templateIdList, int defaultTemplateId)
{
bool flag = true;
if (templateIdList.Count == 0)
{
flag = false;
}
foreach (int num2 in templateIdList)
{
bool isDefault = false;
if (num2 == defaultTemplateId)
{
isDefault = true;
}
if (!TemplateIdExists(nodeId, num2) && !dal.AddTemplateForNodes(nodeId, num2, isDefault))
{
return false;
}
}
return flag;
}
public static IList<ModelInfo> ContentModelList(ModelShowType showType)
{
return GetModelList(ModelType.Content, showType);
}
private static void CreateCommonLable(string moduleName)
{
string virtualPath = HttpContext.Current.Server.MapPath("~/");
FileInfo[] files = new DirectoryInfo(VirtualPathUtility.AppendTrailingSlash(virtualPath) + "Template/CommonTemplate/").GetFiles();
string str3 = VirtualPathUtility.AppendTrailingSlash(virtualPath + SiteConfig.SiteOption.LabelDir);
foreach (FileInfo info2 in files)
{
if ((string.Compare(info2.Extension, ".config", true) == 0) && (string.Compare(info2.Name.Replace(".config", ""), "内容页", true) != 0))
{
string str4 = info2.Name.ToLower().Replace(".config", "_" + moduleName + ".config");
using (StreamReader reader = info2.OpenText())
{
byte[] buffer = new byte[reader.BaseStream.Length];
reader.BaseStream.Read(buffer, 0, buffer.Length);
using (StreamWriter writer = new StreamWriter(str3 + str4))
{
for (int i = 0; i < buffer.Length; i++)
{
writer.BaseStream.WriteByte(buffer[i]);
}
}
}
}
}
}
private static void CreateCommonListPageTemplate(ModelInfo modelInfo)
{
string virtualPath = HttpContext.Current.Server.MapPath("~/");
string fileContent = FileSystemObject.ReadFile(VirtualPathUtility.AppendTrailingSlash(virtualPath) + "Template/CommonTemplate/栏目列表页模板.htm").Replace("{$$$TableName$$$}", modelInfo.TableName);
string str4 = VirtualPathUtility.AppendTrailingSlash(virtualPath) + VirtualPathUtility.AppendTrailingSlash(SiteConfig.SiteOption.TemplateDir) + modelInfo.ModelName;
FileSystemObject.WriteFile(str4 + "/栏目列表页模板.html", fileContent);
fileContent = FileSystemObject.ReadFile(VirtualPathUtility.AppendTrailingSlash(virtualPath) + "Template/CommonTemplate/内容页模板.htm").Replace("{$$$ContentLabelName$$$}", "内容页_" + modelInfo.ModelName);
FileSystemObject.WriteFile(str4 + "/内容页模板.html", fileContent);
CreateCommonLable(modelInfo.ModelName);
CreateContentLabel(virtualPath, modelInfo);
}
private static void CreateContentLabel(string path, ModelInfo modelInfo)
{
string str = "内容页.config";
string str2 = path + "Template/CommonTemplate/" + str;
string str3 = VirtualPathUtility.AppendTrailingSlash(path + SiteConfig.SiteOption.LabelDir);
string fileContent = string.Empty;
using (StreamReader reader = File.OpenText(str2))
{
fileContent = reader.ReadToEnd();
}
fileContent = fileContent.Replace("{$$$TableName$$$}", modelInfo.TableName).Replace("{$$$Custom$$$}", "");
FileSystemObject.WriteFile(str3 + "内容页_" + modelInfo.ModelName + ".config", fileContent);
}
private static void DecodeModelInfo(ModelInfo modelInfo)
{
modelInfo.ModelName = DataSecurity.HtmlDecode(modelInfo.ModelName);
modelInfo.Description = DataSecurity.HtmlDecode(modelInfo.Description);
modelInfo.ItemName = DataSecurity.HtmlDecode(modelInfo.ItemName);
modelInfo.ItemUnit = DataSecurity.HtmlDecode(modelInfo.ItemUnit);
modelInfo.DefaultTemplateFile = DataSecurity.HtmlDecode(modelInfo.DefaultTemplateFile);
modelInfo.AddInfoFilePath = DataSecurity.HtmlDecode(modelInfo.AddInfoFilePath);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -