Repository navigation
Expand file tree
/
Copy pathWidgetsRepository.cs
More file actions
87 lines (77 loc) · 2.9 KB
/
Copy pathWidgetsRepository.cs
File metadata and controls
87 lines (77 loc) · 2.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
using BlogEngine.Core.Data.ViewModels;
using BlogEngine.Core.Data.Contracts;
using System.Collections.Generic;
using System.Xml;
using BlogEngine.Core.DataStore;
namespace BlogEngine.Core.Data
{
/// <summary>
/// Widgets repository
/// </summary>
public class WidgetsRepository : IWidgetsRepository
{
/// <summary>
/// Get theme widgets
/// </summary>
/// <returns>Widgets view model</returns>
public WidgetsVM Get()
{
if (!Security.IsAuthorizedTo(Rights.ManageWidgets))
throw new System.UnauthorizedAccessException();
return new WidgetsVM();
}
/// <summary>
/// Update widget zones
/// </summary>
/// <param name="items">List of zones</param>
/// <returns>True on success</returns>
public bool Update(List<WidgetZone> items)
{
if (!Security.IsAuthorizedTo(Rights.ManageWidgets))
throw new System.UnauthorizedAccessException();
try
{
foreach (var zone in items)
{
XmlDocument doc = new XmlDocument();
var widgets = doc.CreateElement("widgets");
doc.AppendChild(widgets);
foreach (var widget in zone.Widgets)
{
XmlNode node = doc.CreateElement("widget");
node.InnerText = widget.Name;
var id = doc.CreateAttribute("id");
// in javascript, new id initiated as random number
// replace with new GUID before saving to back-end
if(widget.Id.Length < 30)
{
widget.Id = System.Guid.NewGuid().ToString();
}
id.InnerText = widget.Id;
node.Attributes.Append(id);
var title = doc.CreateAttribute("title");
title.InnerText = widget.Title;
node.Attributes.Append(title);
var show = doc.CreateAttribute("showTitle");
show.InnerText = "True";
node.Attributes.Append(show);
widgets.AppendChild(node);
}
SaveXmlDocument(doc, zone.Id);
}
return true;
}
catch (System.Exception ex)
{
Utils.Log("WidgetsRepository.Update", ex);
return false;
}
}
private void SaveXmlDocument(XmlDocument doc, string zone)
{
var ws = new WidgetSettings(zone) { SettingsBehavior = new XmlDocumentBehavior() };
ws.SaveSettings(doc);
Blog.CurrentInstance.Cache[zone] = doc;
}
}
}