forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableStyleManager.cs
More file actions
484 lines (430 loc) · 18.9 KB
/
TableStyleManager.cs
File metadata and controls
484 lines (430 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SiteServer.CMS.Core;
using SiteServer.CMS.DataCache.Core;
using SiteServer.CMS.Model;
using SiteServer.CMS.Model.Attributes;
using SiteServer.CMS.Plugin.Impl;
using SiteServer.Plugin;
using SiteServer.Utils;
namespace SiteServer.CMS.DataCache
{
public static class TableStyleManager
{
private static class TableStyleManagerCache
{
private static readonly object LockObject = new object();
private static readonly string CacheKey = DataCacheManager.GetCacheKey(nameof(TableStyleManager));
public static List<KeyValuePair<string, TableStyleInfo>> GetAllTableStyles()
{
var retval = DataCacheManager.Get<List<KeyValuePair<string, TableStyleInfo>>>(CacheKey);
if (retval != null) return retval;
lock (LockObject)
{
retval = DataCacheManager.Get<List<KeyValuePair<string, TableStyleInfo>>>(CacheKey);
if (retval == null)
{
retval = DataProvider.TableStyleDao.GetAllTableStyles();
DataCacheManager.Insert(CacheKey, retval);
}
}
return retval;
}
public static void Clear()
{
DataCacheManager.Remove(CacheKey);
}
}
public static string GetKey(int relatedIdentity, string tableName, string attributeName)
{
return $"{GetKeyPrefix(relatedIdentity, tableName)}{attributeName}".ToLower();
}
private static string GetKeyPrefix(int relatedIdentity, string tableName)
{
return $"{relatedIdentity}${tableName}$".ToLower();
}
public static List<TableStyleInfo> GetStyleInfoList(string tableName, List<int> relatedIdentities)
{
var allAttributeNames = new List<string>();
var styleInfoList = new List<TableStyleInfo>();
var entries = TableStyleManagerCache.GetAllTableStyles();
relatedIdentities = ParseRelatedIdentities(relatedIdentities);
foreach (var relatedIdentity in relatedIdentities)
{
var startKey = GetKeyPrefix(relatedIdentity, tableName);
var list = entries.Where(tuple => tuple.Key.StartsWith(startKey)).ToList();
foreach (var pair in list)
{
if (pair.IsDefault()) continue;
if (!allAttributeNames.Contains(pair.Value.AttributeName))
{
allAttributeNames.Add(pair.Value.AttributeName);
styleInfoList.Add(pair.Value);
}
}
}
if (tableName == DataProvider.UserDao.TableName || tableName == DataProvider.ChannelDao.TableName || tableName == DataProvider.SiteDao.TableName)
{
if (tableName == DataProvider.UserDao.TableName)
{
foreach (var columnName in UserAttribute.TableStyleAttributes.Value)
{
if (!StringUtils.ContainsIgnoreCase(allAttributeNames, columnName))
{
allAttributeNames.Add(columnName);
styleInfoList.Add(GetDefaultUserTableStyleInfo(tableName, columnName));
}
}
}
}
else
{
var columnNames = TableColumnManager.GetTableColumnNameList(tableName, ContentAttribute.MetadataAttributes.Value);
foreach (var columnName in columnNames)
{
if (!StringUtils.ContainsIgnoreCase(allAttributeNames, columnName))
{
allAttributeNames.Add(columnName);
styleInfoList.Add(GetDefaultContentTableStyleInfo(tableName, columnName));
}
}
}
return styleInfoList.OrderBy(styleInfo => styleInfo.Taxis == 0 ? int.MaxValue : styleInfo.Taxis).ToList();
}
public static List<TableStyleInfo> GetSiteStyleInfoList(int siteId)
{
var relatedIdentities = GetRelatedIdentities(siteId);
return GetStyleInfoList(DataProvider.SiteDao.TableName, relatedIdentities);
}
public static List<TableStyleInfo> GetChannelStyleInfoList(ChannelInfo channelInfo)
{
var relatedIdentities = GetRelatedIdentities(channelInfo);
return GetStyleInfoList(DataProvider.ChannelDao.TableName, relatedIdentities);
}
public static List<TableStyleInfo> GetContentStyleInfoList(SiteInfo siteInfo, ChannelInfo channelInfo)
{
var relatedIdentities = GetRelatedIdentities(channelInfo);
var tableName = ChannelManager.GetTableName(siteInfo, channelInfo);
return GetStyleInfoList(tableName, relatedIdentities);
}
public static List<TableStyleInfo> GetUserStyleInfoList()
{
var relatedIdentities = EmptyRelatedIdentities;
return GetStyleInfoList(DataProvider.UserDao.TableName, relatedIdentities);
}
public static IAttributes GetDefaultAttributes(List<TableStyleInfo> styleInfoList)
{
var attributes = new AttributesImpl();
foreach (var styleInfo in styleInfoList)
{
var defaultValue = string.Empty;
if (!string.IsNullOrEmpty(styleInfo.DefaultValue))
{
defaultValue = styleInfo.DefaultValue;
}
else if (styleInfo.StyleItems != null)
{
var defaultValues = new List<string>();
foreach (var styleItem in styleInfo.StyleItems)
{
if (styleItem.IsSelected)
{
defaultValues.Add(styleItem.ItemValue);
}
}
if (defaultValues.Count > 0)
{
defaultValue = TranslateUtils.ObjectCollectionToString(defaultValues);
}
}
if (!string.IsNullOrEmpty(defaultValue))
{
attributes.Set(styleInfo.AttributeName, defaultValue);
}
}
return attributes;
}
public static void ClearCache()
{
TableStyleManagerCache.Clear();
}
//relatedIdentities从大到小,最后是0
public static TableStyleInfo GetTableStyleInfo(string tableName, string attributeName, List<int> relatedIdentities)
{
if (attributeName == null) attributeName = string.Empty;
relatedIdentities = ParseRelatedIdentities(relatedIdentities);
var entries = TableStyleManagerCache.GetAllTableStyles();
foreach (var relatedIdentity in relatedIdentities)
{
var key = GetKey(relatedIdentity, tableName, attributeName);
var pair = entries.FirstOrDefault(x => x.Key == key && x.Value != null);
if (pair.IsDefault()) continue;
if (pair.Value != null)
{
return pair.Value;
}
}
if (tableName == DataProvider.UserDao.TableName || tableName == DataProvider.ChannelDao.TableName || tableName == DataProvider.SiteDao.TableName)
{
if (tableName == DataProvider.UserDao.TableName)
{
return GetDefaultUserTableStyleInfo(tableName, attributeName);
}
}
else
{
return GetDefaultContentTableStyleInfo(tableName, attributeName);
}
return null;
}
public static TableStyleInfo GetTableStyleInfo(int id)
{
var entries = TableStyleManagerCache.GetAllTableStyles();
var entry = entries.FirstOrDefault(x => x.Value != null && x.Value.Id == id);
return entry.IsDefault() ? null : entry.Value;
}
private static List<int> ParseRelatedIdentities(IReadOnlyCollection<int> list)
{
var relatedIdentities = new List<int>();
if (list != null && list.Count > 0)
{
foreach (var i in list)
{
if (!relatedIdentities.Contains(i))
{
relatedIdentities.Add(i);
}
}
}
relatedIdentities.Sort();
relatedIdentities.Reverse();
if (!relatedIdentities.Contains(0))
{
relatedIdentities.Add(0);
}
return relatedIdentities;
}
public static bool IsExists(int relatedIdentity, string tableName, string attributeName)
{
var key = GetKey(relatedIdentity, tableName, attributeName);
var entries = TableStyleManagerCache.GetAllTableStyles();
return entries.Any(x => x.Key == key);
}
public static Dictionary<string, List<TableStyleInfo>> GetTableStyleInfoWithItemsDictinary(string tableName, List<int> allRelatedIdentities)
{
var dict = new Dictionary<string, List<TableStyleInfo>>();
var entries = TableStyleManagerCache.GetAllTableStyles();
foreach (var pair in entries)
{
var arr = pair.Key.Split('$');
var identityFromKey = TranslateUtils.ToInt(arr[0]);
var tableNameFromKey = arr[1];
if (!StringUtils.EqualsIgnoreCase(tableNameFromKey, tableName) ||
!allRelatedIdentities.Contains(identityFromKey)) continue;
var styleInfo = pair.Value;
var tableStyleInfoWithItemList = dict.ContainsKey(styleInfo.AttributeName) ? dict[styleInfo.AttributeName] : new List<TableStyleInfo>();
tableStyleInfoWithItemList.Add(styleInfo);
dict[styleInfo.AttributeName] = tableStyleInfoWithItemList;
}
return dict;
}
public static string GetValidateInfo(TableStyleInfo styleInfo)
{
var builder = new StringBuilder();
if (styleInfo.Additional.IsRequired)
{
builder.Append("必填项;");
}
if (styleInfo.Additional.MinNum > 0)
{
builder.Append($"最少{styleInfo.Additional.MinNum}个字符;");
}
if (styleInfo.Additional.MaxNum > 0)
{
builder.Append($"最多{styleInfo.Additional.MaxNum}个字符;");
}
if (styleInfo.Additional.ValidateType != ValidateType.None)
{
builder.Append($"验证:{ValidateTypeUtils.GetText(styleInfo.Additional.ValidateType)};");
}
if (builder.Length > 0)
{
builder.Length = builder.Length - 1;
}
else
{
builder.Append("无验证");
}
return builder.ToString();
}
public static List<int> GetRelatedIdentities(int siteId)
{
return new List<int> {siteId, 0};
}
public static List<int> GetRelatedIdentities(ChannelInfo channelInfo)
{
var list = new List<int>();
if (channelInfo != null)
{
var channelIdCollection = "0," + channelInfo.Id;
if (channelInfo.ParentsCount > 0)
{
channelIdCollection = "0," + channelInfo.ParentsPath + "," + channelInfo.Id;
}
list = TranslateUtils.StringCollectionToIntList(channelIdCollection);
list.Reverse();
}
else
{
list.Add(0);
}
return list;
}
public static List<int> EmptyRelatedIdentities => new List<int> {0};
private static TableStyleInfo GetDefaultContentTableStyleInfo(string tableName, string attributeName)
{
var styleInfo = new TableStyleInfo(0, 0, tableName, attributeName, 0, attributeName, string.Empty, false, InputType.Text, string.Empty, true, string.Empty);
if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.Title)))
{
styleInfo.AttributeName = nameof(ContentInfo.Title);
styleInfo.DisplayName = "标题";
styleInfo.Additional.VeeValidate = "required";
styleInfo.Taxis = 1;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.SubTitle)))
{
styleInfo.AttributeName = nameof(ContentInfo.SubTitle);
styleInfo.DisplayName = "副标题";
styleInfo.Taxis = 2;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.ImageUrl)))
{
styleInfo.AttributeName = nameof(ContentInfo.ImageUrl);
styleInfo.DisplayName = "图片";
styleInfo.InputType = InputType.Image;
styleInfo.Taxis = 3;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.VideoUrl)))
{
styleInfo.AttributeName = nameof(ContentInfo.VideoUrl);
styleInfo.DisplayName = "视频";
styleInfo.InputType = InputType.Video;
styleInfo.Taxis = 4;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.FileUrl)))
{
styleInfo.AttributeName = nameof(ContentInfo.FileUrl);
styleInfo.DisplayName = "附件";
styleInfo.InputType = InputType.File;
styleInfo.Taxis = 5;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.Content)))
{
styleInfo.AttributeName = nameof(ContentInfo.Content);
styleInfo.DisplayName = "内容";
styleInfo.Additional.VeeValidate = "required";
styleInfo.InputType = InputType.TextEditor;
styleInfo.Taxis = 6;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.Summary)))
{
styleInfo.AttributeName = nameof(ContentInfo.Summary);
styleInfo.DisplayName = "内容摘要";
styleInfo.InputType = InputType.TextArea;
styleInfo.Taxis = 7;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.Author)))
{
styleInfo.AttributeName = nameof(ContentInfo.Author);
styleInfo.DisplayName = "作者";
styleInfo.Taxis = 8;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(ContentInfo.Source)))
{
styleInfo.AttributeName = nameof(ContentInfo.Source);
styleInfo.DisplayName = "来源";
styleInfo.Taxis = 9;
}
return styleInfo;
}
private static TableStyleInfo GetDefaultUserTableStyleInfo(string tableName, string attributeName)
{
var styleInfo = new TableStyleInfo(0, 0, tableName, attributeName, 0, attributeName, string.Empty, false, InputType.Text, string.Empty, true, string.Empty);
if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.DisplayName)))
{
styleInfo.AttributeName = nameof(UserInfo.DisplayName);
styleInfo.DisplayName = "姓名";
styleInfo.Additional.VeeValidate = "required";
styleInfo.Taxis = 1;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.Mobile)))
{
styleInfo.AttributeName = nameof(UserInfo.Mobile);
styleInfo.DisplayName = "手机号";
styleInfo.Additional.VeeValidate = "mobile";
styleInfo.Taxis = 2;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.Email)))
{
styleInfo.AttributeName = nameof(UserInfo.Email);
styleInfo.DisplayName = "邮箱";
styleInfo.Additional.VeeValidate = "email";
styleInfo.Taxis = 3;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.Gender)))
{
styleInfo.AttributeName = nameof(UserInfo.Gender);
styleInfo.DisplayName = "性别";
styleInfo.InputType = InputType.Radio;
styleInfo.StyleItems = new List<TableStyleItemInfo>
{
new TableStyleItemInfo
{
ItemTitle = "男",
ItemValue = "男"
},
new TableStyleItemInfo
{
ItemTitle = "女",
ItemValue = "女"
}
};
styleInfo.Taxis = 4;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.Birthday)))
{
styleInfo.AttributeName = nameof(UserInfo.Birthday);
styleInfo.DisplayName = "出生日期";
styleInfo.InputType = InputType.Date;
styleInfo.Taxis = 5;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.WeiXin)))
{
styleInfo.AttributeName = nameof(UserInfo.WeiXin);
styleInfo.DisplayName = "微博";
styleInfo.Taxis = 6;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.Qq)))
{
styleInfo.AttributeName = nameof(UserInfo.Qq);
styleInfo.DisplayName = "QQ";
styleInfo.Taxis = 7;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.WeiBo)))
{
styleInfo.AttributeName = nameof(UserInfo.WeiBo);
styleInfo.DisplayName = "微信";
styleInfo.Taxis = 8;
}
else if (StringUtils.EqualsIgnoreCase(attributeName, nameof(UserInfo.Bio)))
{
styleInfo.AttributeName = nameof(UserInfo.Bio);
styleInfo.InputType = InputType.TextArea;
styleInfo.DisplayName = "个人简介";
styleInfo.Taxis = 9;
}
return styleInfo;
}
}
}