bvcms /CmsWeb/Models/HomeModel.cs

Language C# Lines 304
MD5 Hash 550fbe4f844854bbb4bd5bac6573df2c Estimated Cost $7,232 (why?)
Repository https://bitbucket.org/mahalowe/bvcms View Raw File View Project SPDX
  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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Data.Linq;
using System.Web;
using CmsData;
using UtilityExtensions;
using System.Web.Mvc;
using System.Text;
using System.Net.Mail;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data.Linq.SqlClient;
using System.Net;
using System.IO;
using System.Xml.Linq;
using System.ServiceModel.Syndication;
using System.Xml;
using CmsData.Codes;

namespace CmsWeb.Models
{
    public class HomeModel
    {
        public class BirthdayInfo
        {
            public DateTime Birthday { get; set; }
            public string Name { get; set; }
            public int PeopleId { get; set; }
        }
        public IEnumerable<BirthdayInfo> Birthdays()
        {
            var up = DbUtil.Db.CurrentUserPerson;
            if (up == null)
                return new List<BirthdayInfo>();

            var n = UtilityExtensions.Util.Now;
            var tag = DbUtil.Db.FetchOrCreateTag("TrackBirthdays", up.PeopleId, DbUtil.TagTypeId_Personal);
            var q = tag.People(DbUtil.Db);
            if (q.Count() == 0)
                q = from p in DbUtil.Db.People
                    where p.OrganizationMembers.Any(om => om.OrganizationId == up.BibleFellowshipClassId)
                    select p;

            var q2 = from p in q
                     let nextbd = DbUtil.Db.NextBirthday(p.PeopleId)
                     where SqlMethods.DateDiffDay(UtilityExtensions.Util.Now, nextbd) <= 15
                     where p.DeceasedDate == null
                     orderby nextbd
                     select new BirthdayInfo { Birthday = nextbd.Value, Name = p.Name, PeopleId = p.PeopleId };
            return q2;
        }
        public class MyInvolvementInfo
        {
            public string Name { get; set; }
            public string MemberType { get; set; }
            public int OrgId { get; set; }
        	public string OrgType { get; set; }
        }
        public IEnumerable<MyInvolvementInfo> MyInvolvements()
        {
            var u = DbUtil.Db.CurrentUser;
            if (u == null)
                return new List<MyInvolvementInfo>();

            var pid = u.PeopleId;

            var limitvisibility = Util2.OrgMembersOnly || Util2.OrgLeadersOnly;
            var oids = new int[0];
            if (Util2.OrgLeadersOnly)
                oids = DbUtil.Db.GetLeaderOrgIds(pid);

        	var roles = DbUtil.Db.CurrentUser.UserRoles.Select(uu => uu.Role.RoleName).ToArray();
        	var orgmembers = from om in DbUtil.Db.OrganizationMembers
        	                where om.Organization.LimitToRole == null || roles.Contains(om.Organization.LimitToRole)
        	                select om;

            var q = from om in orgmembers
                    where om.PeopleId == pid
                    where (om.Pending ?? false) == false
                    where oids.Contains(om.OrganizationId) || !(limitvisibility && om.Organization.SecurityTypeId == 3)
                    orderby om.Organization.OrganizationType.Code ?? "z", om.Organization.OrganizationName
                    select new MyInvolvementInfo
                    {
                        Name = om.Organization.OrganizationName,
                        MemberType = om.MemberType.Description,
                        OrgId = om.OrganizationId,
						OrgType = om.Organization.OrganizationType.Description ?? "Other",
                    };

            return q;
        }
        public class NewsInfo
        {
            public string Title { get; set; }
            public DateTime Published { get; set; }
            public string Url { get; set; }
        }
        public IEnumerable<NewsInfo> BVCMSNews()
        {
            var feedurl = "http://feeds.feedburner.com/BvcmsBlog";

            var wr = new WebClient();
            var feed = DbUtil.Db.RssFeeds.FirstOrDefault(r => r.Url == feedurl);

            HttpWebRequest req = null;
            try
            {
                req = WebRequest.Create(feedurl) as HttpWebRequest;
            }
            catch
            {
            }

            if (feed != null)
            {
                if (feed.LastModified.HasValue)
                {
                    req.IfModifiedSince = feed.LastModified.Value;
                    req.Headers.Add("If-None-Match", feed.ETag);
                }
            }
            else
            {
                feed = new RssFeed();
                DbUtil.Db.RssFeeds.InsertOnSubmit(feed);
                feed.Url = feedurl;
            }

            if (req != null)
            {
                try
                {
                    var resp = req.GetResponse() as HttpWebResponse;
                    feed.LastModified = resp.LastModified;
                    feed.ETag = resp.Headers["ETag"];
                    var sr = new StreamReader(resp.GetResponseStream());
                    feed.Data = sr.ReadToEnd();
                    sr.Close();
                    DbUtil.Db.SubmitChanges();
                }
                catch (WebException)
                {
                }
                if (feed.Data != null)
                {
                    try
                    {
                        var reader = XmlReader.Create(new StringReader(feed.Data));
                        var f = SyndicationFeed.Load(reader);
                        var posts = from item in f.Items
                                    select new NewsInfo
                                    {
                                        Title = item.Title.Text,
                                        Published = item.PublishDate.DateTime,
                                        Url = item.Links.Single(i => i.RelationshipType == "alternate").GetAbsoluteUri().AbsoluteUri
                                    };
                        return posts;
                    }
                    catch
                    {
                        return new List<NewsInfo>();
                    }
                }
            }
            return null;
        }
        public IEnumerable<NewsInfo> ChurchNews()
        {
			var feedurl = DbUtil.Db.Setting("ChurchFeedUrl", "");

            var feed = DbUtil.Db.RssFeeds.FirstOrDefault(r => r.Url == feedurl);


            HttpWebRequest req = null;
            try
            {
                req = WebRequest.Create(feedurl) as HttpWebRequest;
            }
            catch
            {
            }

            if (feed != null)
            {
                if (feed.LastModified.HasValue)
                {
                    req.IfModifiedSince = feed.LastModified.Value;
                    req.Headers.Add("If-None-Match", feed.ETag);
                }
            }
            else
            {
                feed = new RssFeed();
                DbUtil.Db.RssFeeds.InsertOnSubmit(feed);
                feed.Url = feedurl;
            }

            if (req != null)
            {
                try
                {
                    var resp = req.GetResponse() as HttpWebResponse;
                    feed.LastModified = resp.LastModified;
                    feed.ETag = resp.Headers["ETag"];
                    var sr = new StreamReader(resp.GetResponseStream());
                    feed.Data = sr.ReadToEnd();
                    sr.Close();
                    DbUtil.Db.SubmitChanges();
                }
                catch (WebException)
                {
                }
                if (feed.Data != null)
                {
                    try
                    {
                        var reader = XmlReader.Create(new StringReader(feed.Data));
                        var f = SyndicationFeed.Load(reader);
                        var posts = from item in f.Items
									let a = item.Authors.FirstOrDefault()
									let au = a == null ? "" : a.Name
                                    select new NewsInfo
                                    {
                                        Title = item.Title.Text,
                                        Published = item.PublishDate.DateTime,
                                        Url = item.Links.Single(i => i.RelationshipType == "alternate").GetAbsoluteUri().AbsoluteUri
                                    };
                        return posts;
                    }
                    catch
                    {
                        return new NewsInfo[] { };
                    }
                }
            }
            return new NewsInfo[] { };
        }

        public class MySavedQueryInfo
        {
            public string Name { get; set; }
            public int QueryId { get; set; }
        }
        public IEnumerable<MySavedQueryInfo> MyQueries()
        {
            var up = DbUtil.Db.CurrentUserPerson;
            if (up == null)
                return new List<MySavedQueryInfo>();
            var q = from c in DbUtil.Db.QueryBuilderClauses
                    where c.SavedBy == Util.UserName
                    where c.GroupId == null && c.Field == "Group" && c.Clauses.Any()
                    where !c.Description.Contains("scratchpad")
                    orderby c.Description
                    select new MySavedQueryInfo
                    {
                        Name = c.Description,
                        QueryId = c.QueryId
                    };
            return q;
        }
        public class TaskInfo
        {
            public int TaskId { get; set; }
            public int PeopleId { get; set; }
            public string Who { get; set; }
            public string Description { get; set; }
        }
        public IEnumerable<TaskInfo> Tasks()
        {
            var up = DbUtil.Db.CurrentUserPerson;
            if (up == null)
                return new List<TaskInfo>();
            var completedcode = TaskStatusCode.Complete;
            var pid = DbUtil.Db.CurrentUser.PeopleId;
            var q = from t in DbUtil.Db.Tasks
                    where t.Archive == false // not archived
                    where t.OwnerId == pid || t.CoOwnerId == pid
                    where t.WhoId != null && t.StatusId != completedcode
                    where !(t.OwnerId == pid && t.CoOwnerId != null)
                    orderby t.CreatedOn
                    select new TaskInfo
                    {
                        TaskId = t.Id,
                        PeopleId = t.AboutWho.PeopleId,
                        Who = t.AboutWho.Name,
                        Description = t.Description,
                    };
            return q;
        }
        public IEnumerable<CodeValueItem> Tags()
        {
            var up = DbUtil.Db.CurrentUserPerson;
            if (up == null)
                return new List<CodeValueItem>();
            var ctl = new CodeValueModel();
            var pid = DbUtil.Db.CurrentUser.PeopleId;
            var list = ctl.UserTags(pid);
            return list;
        }
    }
}
Back to Top