Repository navigation
Expand file tree
/
Copy pathuQuery-Document.cs
More file actions
149 lines (132 loc) · 3.53 KB
/
Copy pathuQuery-Document.cs
File metadata and controls
149 lines (132 loc) · 3.53 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
using System;
using System.Collections.Generic;
using umbraco.cms.businesslogic.web;
namespace uComponents.Core
{
/// <summary>
/// uQuery sub-class for Documents
/// </summary>
public static partial class uQuery
{
/// <summary>
/// Get collection of Document objects from the supplied CSV of IDs
/// </summary>
/// <param name="csv">string csv of IDs</param>
/// <returns>collection or emtpy collection</returns>
public static List<Document> GetDocumentsByCsv(string csv)
{
var documents = new List<Document>();
var ids = uQuery.GetCsvIds(csv);
if (ids != null)
{
foreach (string id in ids)
{
var document = uQuery.GetDocument(id);
if (document != null)
{
documents.Add(document);
}
}
}
return documents;
}
/// <summary>
/// Gets the documents by XML.
/// </summary>
/// <param name="xml">The XML.</param>
/// <returns></returns>
public static List<Document> GetDocumentsByXml(string xml)
{
var documents = new List<Document>();
var ids = uQuery.GetXmlIds(xml);
if (ids != null)
{
foreach (int id in ids)
{
var document = uQuery.GetDocument(id);
if (document != null)
{
documents.Add(document);
}
}
}
return documents;
}
/// <summary>
/// checks to see if the current node can be got via the nodeFactory, and then constructed as a Document, if not then
/// checks to see if there's an id parameter on the QueryString to construct a Document
/// </summary>
/// <returns>the current Document or null</returns>
public static Document GetCurrentDocument()
{
Document document = null;
var currentNode = uQuery.GetCurrentNode();
if (currentNode != null)
{
document = uQuery.GetDocument(currentNode.Id);
}
else
{
document = uQuery.GetDocument(uQuery.GetIdFromQueryString());
}
return document;
}
/// <summary>
/// Get document from an ID
/// </summary>
/// <param name="documentId">string ID of document to get</param>
/// <returns>Document or null</returns>
public static Document GetDocument(string documentId)
{
int id;
Document document = null;
if (!string.IsNullOrWhiteSpace(documentId) && int.TryParse(documentId, out id))
{
document = uQuery.GetDocument(id);
}
return document;
}
/// <summary>
/// Get media item from an ID
/// </summary>
/// <param name="id">ID of media item to get</param>
/// <returns>Media or null</returns>
public static Document GetDocument(int id)
{
// suppress error if Document with supplied id doesn't exist
try
{
return new Document(id);
}
catch
{
return null;
}
}
/// <summary>
/// Determines whether the specified Document is published.
/// </summary>
/// <param name="id">The Document's id.</param>
/// <returns>
/// <c>true</c> if the specified id is published; otherwise, <c>false</c>.
/// </returns>
public static bool IsDocumentPublished(string id)
{
return umbraco.content.Instance.XmlContent.GetElementById(id) != null;
}
/// <summary>
/// Extension method on Document collection to return key value pairs of: Id / Text
/// </summary>
/// <param name="documents">generic list of Document objects</param>
/// <returns>a collection of document IDs and their text fields</returns>
public static Dictionary<int, string> ToNameIds(this List<Document> documents)
{
var dictionary = new Dictionary<int, string>();
foreach (var document in documents)
{
dictionary.Add(document.Id, document.Text);
}
return dictionary;
}
}
}