This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathEditProperty.cs
More file actions
167 lines (144 loc) · 3.89 KB
/
Copy pathEditProperty.cs
File metadata and controls
167 lines (144 loc) · 3.89 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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using uComponents.Core;
using umbraco;
using umbraco.interfaces;
namespace uComponents.Controls
{
/// <summary>
/// EditProperty server control.
/// </summary>
[DefaultProperty("Alias")]
[ToolboxBitmap(typeof(Constants), Constants.FaviconResourcePath)]
[ToolboxData("<{0}:EditProperty runat=server Alias= NodeId=></{0}:EditProperty>")]
public class EditProperty : WebControl
{
/// <summary>
/// Private field to store the data-type.
/// </summary>
private IDataType m_DataType;
/// <summary>
/// Gets or sets the alias.
/// </summary>
/// <value>The alias.</value>
[DefaultValue("")]
public string Alias { get; set; }
/// <summary>
/// Gets or sets the node id.
/// </summary>
/// <value>The node id.</value>
[DefaultValue("")]
public string NodeId { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="EditProperty"/> class.
/// </summary>
public EditProperty()
: base(HtmlTextWriterTag.Div)
{
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (string.IsNullOrWhiteSpace(this.NodeId))
{
this.NodeId = uQuery.GetIdFromQueryString();
}
// try getting the content from the supplied nodeId.
var content = this.GetContent(this.NodeId);
if (content == null)
{
// fall-back on the current page.
content = uQuery.GetCurrentDocument();
}
if (content != null)
{
var property = content.getProperty(this.Alias);
if (property != null)
{
var propertyType = property.PropertyType;
var definition = propertyType.DataTypeDefinition;
var dataType = definition.DataType;
// set the value and Id of the control
dataType.DataEditor.Editor.ID = this.Alias;
dataType.Data.Value = property.Value;
// store the data-type in a private field for re-use
this.m_DataType = dataType;
// add the control to the collection
this.Controls.Add(dataType.DataEditor.Editor);
}
}
}
/// <summary>
/// Saves the property value.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
/// <returns></returns>
public bool Save(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(this.NodeId))
{
this.NodeId = uQuery.GetIdFromQueryString();
}
// try getting the content from the supplied Id.
var content = this.GetContent(this.NodeId);
if (content == null)
{
// fall-back on the current page.
content = uQuery.GetCurrentDocument();
}
if (content != null)
{
// get the data-type from the private field
var dataType = this.m_DataType;
// get the property value (from the current node)
var property = content.getProperty(this.Alias);
if (property != null)
{
// save the value
dataType.Data.PropertyId = property.Id;
dataType.DataEditor.Save();
return true;
}
}
return false;
}
/// <summary>
/// Gets the content.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
private umbraco.cms.businesslogic.Content GetContent(int id)
{
try
{
return new umbraco.cms.businesslogic.Content(id);
}
catch
{
return null;
}
}
/// <summary>
/// Gets the content.
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <returns></returns>
private umbraco.cms.businesslogic.Content GetContent(string nodeId)
{
int id;
if (!string.IsNullOrWhiteSpace(nodeId) && int.TryParse(nodeId, out id))
{
return this.GetContent(id);
}
return null;
}
}
}