Repository navigation
Expand file tree
/
Copy pathuQuery-PreValues.cs
More file actions
113 lines (99 loc) · 3.37 KB
/
Copy pathuQuery-PreValues.cs
File metadata and controls
113 lines (99 loc) · 3.37 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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using uComponents.Core.uQueryExtensions;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.propertytype;
using umbraco.NodeFactory;
using System;
namespace uComponents.Core
{
/// <summary>
/// Static helper methods, previously this class was UmbracoHelper
/// </summary>
public static partial class uQuery
{
/// <summary>
/// Makes a new PreValue.
/// </summary>
/// <param name="dataTypeDefinitionId">The datatype definition id.</param>
/// <param name="value">The value.</param>
/// <param name="alias">The alias.</param>
/// <param name="sortOrder">The sort order.</param>
/// <returns>
/// The inserted prevalue or null if the operation failed.
/// </returns>
public static PreValue MakeNewPreValue(int dataTypeDefinitionId, string value, string alias = "", int sortOrder = 0)
{
var result =
SqlHelper.ExecuteNonQuery(
"INSERT INTO cmsDataTypePreValues (datatypeNodeId, value, alias, sortorder) VALUES (@dtdefid, @value, @alias, @sortorder)",
SqlHelper.CreateParameter("@dtdefid", dataTypeDefinitionId),
SqlHelper.CreateParameter("@value", value),
SqlHelper.CreateParameter("@alias", alias),
SqlHelper.CreateParameter("@sortorder", sortOrder));
if (result > -1)
{
return uQuery.GetPreValues(dataTypeDefinitionId).Single(x => x.Value.Equals(value) && x.GetAlias().Equals(alias) && x.SortOrder == sortOrder);
}
return null;
}
/// <summary>
/// Gets the prevalues for a specified datatype as a strongly typed list.
/// </summary>
/// <param name="dataTypeDefinitionId">The datatype definition id.</param>
/// <returns>The list of PreValues.</returns>
public static PreValue[] GetPreValues(int dataTypeDefinitionId)
{
var values = new List<PreValue>();
foreach (var entry in PreValues.GetPreValues(dataTypeDefinitionId))
{
var prevalue = ((DictionaryEntry)entry).Value as PreValue;
values.Add(prevalue);
}
return values.ToArray();
}
/// <summary>
/// Gets the pre values.
/// TODO: [OA] Document on `ucomponents.github.io`
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <param name="propertyAlias">The property alias.</param>
/// <returns></returns>
public static PreValue[] GetPreValues(int nodeId, string propertyAlias)
{
var node = new Node(nodeId);
var xml = node.ToXml();
if (xml != null && xml.Attributes != null)
{
int nodeType;
int.TryParse(xml.Attributes["nodeType"].Value, out nodeType);
foreach (var property in PropertyType.GetAll().Where(x => x.ContentTypeId.Equals(nodeType) && x.Alias.Equals(propertyAlias)))
{
var dtd = property.DataTypeDefinition;
return GetPreValues(dtd.Id);
}
}
return new List<PreValue>().ToArray();
}
/// <summary>
/// Reorders the prevalue.
/// </summary>
/// <param name="preValueId">The prevalue id.</param>
/// <param name="sortOrder">The sort order.</param>
/// <returns></returns>
public static bool ReorderPreValue(int preValueId, int sortOrder)
{
var result =
SqlHelper.ExecuteNonQuery(
"UPDATE cmsDataTypePreValues SET sortorder = @sortorder WHERE id = @prevalueid",
SqlHelper.CreateParameter("@prevalueid", preValueId),
SqlHelper.CreateParameter("@sortorder", sortOrder));
if (result > 0)
{
return true;
}
return false;
}
}
}