/src/EyeCatch.Umbraco.FeedGenerator/FeedModel.cs
C# | 236 lines | 175 code | 30 blank | 31 comment | 18 complexity | 3b9c9d2fcf61ca06baaca28b47605d4c MD5 | raw file
1namespace EyeCatch.Umbraco.FeedGenerator
2{
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Web;
7
8 using global::Umbraco.Core.Models;
9 using global::Umbraco.Web;
10 using global::Umbraco.Web.Models;
11
12 public class FeedModel
13 {
14 /// <summary>The source content.</summary>
15 private IPublishedContent source;
16
17 /// <summary>The site base url.</summary>
18 private string baseUrl;
19
20 /// <summary>The feed items.</summary>
21 private List<FeedItem> items;
22
23 public FeedModel(PartialViewMacroModel model)
24 {
25 var source = model.MacroParameters["Source"] ?? string.Empty;
26 var baseUrl = model.MacroParameters["BaseUrl"] ?? string.Empty;
27 var documentTypeAlias = model.MacroParameters["DocumentTypeAlias"] ?? string.Empty;
28 var keywordProperty = model.MacroParameters["KeywordProperty"] ?? string.Empty;
29 var summaryTextProperty = model.MacroParameters["SummaryTextProperty"] ?? string.Empty;
30 var fullTextProperty = model.MacroParameters["FullTextProperty"] ?? string.Empty;
31 int maxLevel = 0;
32 if (model.MacroParameters["MaxLevel"] != null)
33 {
34 int.TryParse(model.MacroParameters["MaxLevel"].ToString(), out maxLevel);
35 }
36
37 this.Initialize(
38 model.Content,
39 source.ToString(),
40 baseUrl.ToString(),
41 documentTypeAlias.ToString().Trim('[', ']').Split(',').Where(x => !string.IsNullOrEmpty(x)).ToArray(),
42 keywordProperty.ToString().Trim('[', ']'),
43 summaryTextProperty.ToString().Trim('[', ']'),
44 fullTextProperty.ToString().Trim('[', ']'),
45 maxLevel);
46 }
47
48 /// <summary>Gets the title.</summary>
49 /// <value>The title.</value>
50 public string Title
51 {
52 get
53 {
54 return this.source.GetPropertyValue<string>(
55 "rssTitle",
56 new Uri(this.source.UrlWithDomain()).Host + " RSS Feed");
57 }
58 }
59
60 /// <summary>Gets URL of the site.</summary>
61 /// <value>The site URL.</value>
62 public string SiteUrl
63 {
64 get
65 {
66 return this.baseUrl;
67 }
68 }
69
70 /// <summary>Gets URL of the feed.</summary>
71 /// <value>The feed URL.</value>
72 public string SelfUrl
73 {
74 get
75 {
76 try
77 {
78 return HttpContext.Current.Request.Url.AbsoluteUri;
79 }
80 catch (Exception)
81 {
82 return null;
83 }
84 }
85 }
86
87 /// <summary>Gets the publication date.</summary>
88 /// <value>The publication date.</value>
89 public DateTime Updated
90 {
91 get
92 {
93 if (!this.Items.Any())
94 {
95 return this.source.UpdateDate.ToUniversalTime();
96 }
97
98 return this.Items.Max(x => x.UpdateDate);
99 }
100 }
101
102 /// <summary>Gets the feed description.</summary>
103 /// <value>The feed description.</value>
104 public string Description
105 {
106 get
107 {
108 return this.source.GetPropertyValue<string>("rssDescription", true, string.Empty);
109 }
110 }
111
112 /// <summary>Gets the feed language.</summary>
113 /// <value>The feed language.</value>
114 public string Language
115 {
116 get
117 {
118 var l = this.source.GetPropertyValue<string>("rssLanguage", true, string.Empty);
119
120 if (string.IsNullOrEmpty(l))
121 {
122 return this.source.GetCulture().Name;
123 }
124
125 return l;
126 }
127 }
128
129 /// <summary>Gets the feed items.</summary>
130 /// <value>The feed items.</value>
131 public List<FeedItem> Items
132 {
133 get
134 {
135 return this.items ?? (this.items = new List<FeedItem>());
136 }
137 }
138
139 /// <summary>Gets the Umbraco helper for the current request.</summary>
140 private UmbracoHelper Umbraco
141 {
142 get
143 {
144 return new UmbracoHelper(UmbracoContext.Current);
145 }
146 }
147
148 /// <summary>Initializes this instance.</summary>
149 /// <param name="content">The current content.</param>
150 /// <param name="source">The source content.</param>
151 /// <param name="baseUrl">The base url.</param>
152 /// <param name="documentTypeAliases">The document type aliases.</param>
153 /// <param name="keywordProperty">The keyword property.</param>
154 /// <param name="summaryTextProperty">The summary text property.</param>
155 /// <param name="fullTextProperty">The full text property.</param>
156 /// <param name="maxDepth">The maximum number of levels below the source to traverse.</param>
157 internal void Initialize(IPublishedContent content, string source, string baseUrl, string[] documentTypeAliases, string keywordProperty, string summaryTextProperty, string fullTextProperty, int maxDepth)
158 {
159 // Populate Source
160 if (source != null && !string.IsNullOrEmpty(source))
161 {
162 // Check if this is a media item first
163 var media = this.Umbraco.TypedMedia(source);
164
165 this.source = media ?? this.Umbraco.TypedContent(source);
166 }
167 else
168 {
169 this.source = content;
170 }
171
172 // Populate Base Url
173 if (baseUrl != null && !string.IsNullOrEmpty(baseUrl))
174 {
175 this.baseUrl = baseUrl;
176 }
177 else
178 {
179 var u = new Uri(content.UrlWithDomain());
180 this.baseUrl = string.Format("{0}://{1}", u.Scheme, u.Host);
181 }
182
183 // Populate Items
184 var descendants = this.source.Descendants();
185
186 if (maxDepth > 0)
187 {
188 descendants = descendants.Where(x => x.Level <= (this.source.Level + maxDepth));
189 }
190
191 if (documentTypeAliases.Any())
192 {
193 descendants = descendants.Where(x => documentTypeAliases.Contains(x.DocumentTypeAlias));
194 }
195
196 foreach (var descendant in descendants)
197 {
198 var item = new FeedItem()
199 {
200 Title = descendant.Name,
201 PublishDate = descendant.CreateDate.ToUniversalTime(),
202 UpdateDate = descendant.UpdateDate.ToUniversalTime(),
203 Url = descendant.UrlWithDomain(),
204 Author = descendant.CreatorName
205 };
206
207 if (!string.IsNullOrEmpty(keywordProperty) && descendant.HasValue(keywordProperty))
208 {
209 item.Tags = descendant.GetPropertyValue<string>(keywordProperty).Split(',');
210 }
211 else
212 {
213 item.Tags = new string[0];
214 }
215
216 if (!string.IsNullOrEmpty(summaryTextProperty) && descendant.HasValue(summaryTextProperty))
217 {
218 var s = descendant.GetPropertyValue<string>(summaryTextProperty);
219 var processed = new HtmlDocumentProcessor().EncodeUris(descendant, s, baseUrl);
220
221 item.Summary = processed.DocumentNode.InnerHtml;
222 }
223
224 if (!string.IsNullOrEmpty(fullTextProperty) && descendant.HasValue(fullTextProperty))
225 {
226 var c = descendant.GetPropertyValue<string>(fullTextProperty);
227 var processed = new HtmlDocumentProcessor().EncodeUris(descendant, c, baseUrl);
228
229 item.Content = processed.DocumentNode.InnerHtml;
230 }
231
232 this.Items.Add(item);
233 }
234 }
235 }
236}