PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsWeb/Models/HomeModel.cs

https://bitbucket.org/rbhakser/bvcms
C# | 303 lines | 285 code | 18 blank | 0 comment | 57 complexity | 550fbe4f844854bbb4bd5bac6573df2c MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Collections;
  4using System.Linq;
  5using System.Data.Linq;
  6using System.Web;
  7using CmsData;
  8using UtilityExtensions;
  9using System.Web.Mvc;
 10using System.Text;
 11using System.Net.Mail;
 12using System.Web.UI.WebControls;
 13using System.Web.UI;
 14using System.Data.Linq.SqlClient;
 15using System.Net;
 16using System.IO;
 17using System.Xml.Linq;
 18using System.ServiceModel.Syndication;
 19using System.Xml;
 20using CmsData.Codes;
 21
 22namespace CmsWeb.Models
 23{
 24    public class HomeModel
 25    {
 26        public class BirthdayInfo
 27        {
 28            public DateTime Birthday { get; set; }
 29            public string Name { get; set; }
 30            public int PeopleId { get; set; }
 31        }
 32        public IEnumerable<BirthdayInfo> Birthdays()
 33        {
 34            var up = DbUtil.Db.CurrentUserPerson;
 35            if (up == null)
 36                return new List<BirthdayInfo>();
 37
 38            var n = UtilityExtensions.Util.Now;
 39            var tag = DbUtil.Db.FetchOrCreateTag("TrackBirthdays", up.PeopleId, DbUtil.TagTypeId_Personal);
 40            var q = tag.People(DbUtil.Db);
 41            if (q.Count() == 0)
 42                q = from p in DbUtil.Db.People
 43                    where p.OrganizationMembers.Any(om => om.OrganizationId == up.BibleFellowshipClassId)
 44                    select p;
 45
 46            var q2 = from p in q
 47                     let nextbd = DbUtil.Db.NextBirthday(p.PeopleId)
 48                     where SqlMethods.DateDiffDay(UtilityExtensions.Util.Now, nextbd) <= 15
 49                     where p.DeceasedDate == null
 50                     orderby nextbd
 51                     select new BirthdayInfo { Birthday = nextbd.Value, Name = p.Name, PeopleId = p.PeopleId };
 52            return q2;
 53        }
 54        public class MyInvolvementInfo
 55        {
 56            public string Name { get; set; }
 57            public string MemberType { get; set; }
 58            public int OrgId { get; set; }
 59        	public string OrgType { get; set; }
 60        }
 61        public IEnumerable<MyInvolvementInfo> MyInvolvements()
 62        {
 63            var u = DbUtil.Db.CurrentUser;
 64            if (u == null)
 65                return new List<MyInvolvementInfo>();
 66
 67            var pid = u.PeopleId;
 68
 69            var limitvisibility = Util2.OrgMembersOnly || Util2.OrgLeadersOnly;
 70            var oids = new int[0];
 71            if (Util2.OrgLeadersOnly)
 72                oids = DbUtil.Db.GetLeaderOrgIds(pid);
 73
 74        	var roles = DbUtil.Db.CurrentUser.UserRoles.Select(uu => uu.Role.RoleName).ToArray();
 75        	var orgmembers = from om in DbUtil.Db.OrganizationMembers
 76        	                where om.Organization.LimitToRole == null || roles.Contains(om.Organization.LimitToRole)
 77        	                select om;
 78
 79            var q = from om in orgmembers
 80                    where om.PeopleId == pid
 81                    where (om.Pending ?? false) == false
 82                    where oids.Contains(om.OrganizationId) || !(limitvisibility && om.Organization.SecurityTypeId == 3)
 83                    orderby om.Organization.OrganizationType.Code ?? "z", om.Organization.OrganizationName
 84                    select new MyInvolvementInfo
 85                    {
 86                        Name = om.Organization.OrganizationName,
 87                        MemberType = om.MemberType.Description,
 88                        OrgId = om.OrganizationId,
 89						OrgType = om.Organization.OrganizationType.Description ?? "Other",
 90                    };
 91
 92            return q;
 93        }
 94        public class NewsInfo
 95        {
 96            public string Title { get; set; }
 97            public DateTime Published { get; set; }
 98            public string Url { get; set; }
 99        }
100        public IEnumerable<NewsInfo> BVCMSNews()
101        {
102            var feedurl = "http://feeds.feedburner.com/BvcmsBlog";
103
104            var wr = new WebClient();
105            var feed = DbUtil.Db.RssFeeds.FirstOrDefault(r => r.Url == feedurl);
106
107            HttpWebRequest req = null;
108            try
109            {
110                req = WebRequest.Create(feedurl) as HttpWebRequest;
111            }
112            catch
113            {
114            }
115
116            if (feed != null)
117            {
118                if (feed.LastModified.HasValue)
119                {
120                    req.IfModifiedSince = feed.LastModified.Value;
121                    req.Headers.Add("If-None-Match", feed.ETag);
122                }
123            }
124            else
125            {
126                feed = new RssFeed();
127                DbUtil.Db.RssFeeds.InsertOnSubmit(feed);
128                feed.Url = feedurl;
129            }
130
131            if (req != null)
132            {
133                try
134                {
135                    var resp = req.GetResponse() as HttpWebResponse;
136                    feed.LastModified = resp.LastModified;
137                    feed.ETag = resp.Headers["ETag"];
138                    var sr = new StreamReader(resp.GetResponseStream());
139                    feed.Data = sr.ReadToEnd();
140                    sr.Close();
141                    DbUtil.Db.SubmitChanges();
142                }
143                catch (WebException)
144                {
145                }
146                if (feed.Data != null)
147                {
148                    try
149                    {
150                        var reader = XmlReader.Create(new StringReader(feed.Data));
151                        var f = SyndicationFeed.Load(reader);
152                        var posts = from item in f.Items
153                                    select new NewsInfo
154                                    {
155                                        Title = item.Title.Text,
156                                        Published = item.PublishDate.DateTime,
157                                        Url = item.Links.Single(i => i.RelationshipType == "alternate").GetAbsoluteUri().AbsoluteUri
158                                    };
159                        return posts;
160                    }
161                    catch
162                    {
163                        return new List<NewsInfo>();
164                    }
165                }
166            }
167            return null;
168        }
169        public IEnumerable<NewsInfo> ChurchNews()
170        {
171			var feedurl = DbUtil.Db.Setting("ChurchFeedUrl", "");
172
173            var feed = DbUtil.Db.RssFeeds.FirstOrDefault(r => r.Url == feedurl);
174
175
176            HttpWebRequest req = null;
177            try
178            {
179                req = WebRequest.Create(feedurl) as HttpWebRequest;
180            }
181            catch
182            {
183            }
184
185            if (feed != null)
186            {
187                if (feed.LastModified.HasValue)
188                {
189                    req.IfModifiedSince = feed.LastModified.Value;
190                    req.Headers.Add("If-None-Match", feed.ETag);
191                }
192            }
193            else
194            {
195                feed = new RssFeed();
196                DbUtil.Db.RssFeeds.InsertOnSubmit(feed);
197                feed.Url = feedurl;
198            }
199
200            if (req != null)
201            {
202                try
203                {
204                    var resp = req.GetResponse() as HttpWebResponse;
205                    feed.LastModified = resp.LastModified;
206                    feed.ETag = resp.Headers["ETag"];
207                    var sr = new StreamReader(resp.GetResponseStream());
208                    feed.Data = sr.ReadToEnd();
209                    sr.Close();
210                    DbUtil.Db.SubmitChanges();
211                }
212                catch (WebException)
213                {
214                }
215                if (feed.Data != null)
216                {
217                    try
218                    {
219                        var reader = XmlReader.Create(new StringReader(feed.Data));
220                        var f = SyndicationFeed.Load(reader);
221                        var posts = from item in f.Items
222									let a = item.Authors.FirstOrDefault()
223									let au = a == null ? "" : a.Name
224                                    select new NewsInfo
225                                    {
226                                        Title = item.Title.Text,
227                                        Published = item.PublishDate.DateTime,
228                                        Url = item.Links.Single(i => i.RelationshipType == "alternate").GetAbsoluteUri().AbsoluteUri
229                                    };
230                        return posts;
231                    }
232                    catch
233                    {
234                        return new NewsInfo[] { };
235                    }
236                }
237            }
238            return new NewsInfo[] { };
239        }
240
241        public class MySavedQueryInfo
242        {
243            public string Name { get; set; }
244            public int QueryId { get; set; }
245        }
246        public IEnumerable<MySavedQueryInfo> MyQueries()
247        {
248            var up = DbUtil.Db.CurrentUserPerson;
249            if (up == null)
250                return new List<MySavedQueryInfo>();
251            var q = from c in DbUtil.Db.QueryBuilderClauses
252                    where c.SavedBy == Util.UserName
253                    where c.GroupId == null && c.Field == "Group" && c.Clauses.Any()
254                    where !c.Description.Contains("scratchpad")
255                    orderby c.Description
256                    select new MySavedQueryInfo
257                    {
258                        Name = c.Description,
259                        QueryId = c.QueryId
260                    };
261            return q;
262        }
263        public class TaskInfo
264        {
265            public int TaskId { get; set; }
266            public int PeopleId { get; set; }
267            public string Who { get; set; }
268            public string Description { get; set; }
269        }
270        public IEnumerable<TaskInfo> Tasks()
271        {
272            var up = DbUtil.Db.CurrentUserPerson;
273            if (up == null)
274                return new List<TaskInfo>();
275            var completedcode = TaskStatusCode.Complete;
276            var pid = DbUtil.Db.CurrentUser.PeopleId;
277            var q = from t in DbUtil.Db.Tasks
278                    where t.Archive == false // not archived
279                    where t.OwnerId == pid || t.CoOwnerId == pid
280                    where t.WhoId != null && t.StatusId != completedcode
281                    where !(t.OwnerId == pid && t.CoOwnerId != null)
282                    orderby t.CreatedOn
283                    select new TaskInfo
284                    {
285                        TaskId = t.Id,
286                        PeopleId = t.AboutWho.PeopleId,
287                        Who = t.AboutWho.Name,
288                        Description = t.Description,
289                    };
290            return q;
291        }
292        public IEnumerable<CodeValueItem> Tags()
293        {
294            var up = DbUtil.Db.CurrentUserPerson;
295            if (up == null)
296                return new List<CodeValueItem>();
297            var ctl = new CodeValueModel();
298            var pid = DbUtil.Db.CurrentUser.PeopleId;
299            var list = ctl.UserTags(pid);
300            return list;
301        }
302    }
303}