PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsWeb/Areas/Org/Models/MissionTripEmailer.cs

https://github.com/vs06/bvcms
C# | 285 lines | 263 code | 21 blank | 1 comment | 59 complexity | 81ca61986b4c252f2e35d3cc10e3bc98 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Mail;
  5. using System.Web;
  6. using CmsData;
  7. using Newtonsoft.Json;
  8. using UtilityExtensions;
  9. using Settings = CmsData.Registration.Settings;
  10. namespace CmsWeb.Areas.Org.Models
  11. {
  12. public class MissionTripEmailer
  13. {
  14. public int PeopleId
  15. {
  16. get { return _peopleId; }
  17. set
  18. {
  19. _peopleId = value;
  20. }
  21. }
  22. private int orgId;
  23. private int _peopleId;
  24. public int OrgId
  25. {
  26. get { return orgId; }
  27. set
  28. {
  29. orgId = value;
  30. var org = DbUtil.Db.LoadOrganizationById(value);
  31. var m = new Settings(org.RegSetting, DbUtil.Db, value);
  32. Subject = m.SupportSubject;
  33. Body = m.SupportBody;
  34. }
  35. }
  36. public List<RecipientItem> Recipients { get; set; }
  37. public string Subject { get; set; }
  38. public string Body { get; set; }
  39. public class SearchInfo
  40. {
  41. public string url { get; set; }
  42. public string line1 { get; set; }
  43. public string line2 { get; set; }
  44. }
  45. public static IEnumerable<SearchInfo> Search(int pid, string text)
  46. {
  47. text = text.Trim();
  48. if (!Util.ValidEmail(text) && text.Trim().GetDigits().Length != 10)
  49. return new List<SearchInfo>();
  50. var qp = from p in DbUtil.Db.People
  51. where p.DeceasedDate == null
  52. select p;
  53. var phone = text.GetDigits();
  54. if (phone.Length == 10)
  55. qp = from p in qp
  56. where p.CellPhone.Contains(phone)
  57. || p.Family.HomePhone.Contains(phone)
  58. || p.WorkPhone.Contains(phone)
  59. select p;
  60. else if (Util.ValidEmail(text))
  61. qp = from p in qp
  62. where p.EmailAddress == text || p.EmailAddress2 == text
  63. select p;
  64. const string addsupport = "/MissionTripEmail/AddSupporter/{0}/{1}";
  65. var rp = from p in qp
  66. where (p.EmailAddress.Length > 0 && (p.SendEmailAddress1 ?? true))
  67. || (p.EmailAddress2.Length > 0 && p.SendEmailAddress2 == true)
  68. let age = p.Age != null ? " (" + p.Age + ")" : ""
  69. orderby p.Name2
  70. select new SearchInfo()
  71. {
  72. url = addsupport.Fmt(pid, p.PeopleId),
  73. line1 = p.Name2 + age,
  74. line2 = p.PrimaryAddress ?? "",
  75. };
  76. var list = rp.Take(8).ToList();
  77. if (list.Count == 0 && Util.ValidEmail(text))
  78. list.Add(new SearchInfo()
  79. {
  80. url = addsupport.Fmt(pid, text),
  81. line1 = text,
  82. });
  83. return list;
  84. }
  85. public class RecipientItem
  86. {
  87. public int Id { get; set; }
  88. public string NameOrEmail { get; set; }
  89. public bool Active { get; set; }
  90. public string Salutation { get; set; }
  91. public decimal SenderAmt { get; set; }
  92. }
  93. public List<RecipientItem> GetRecipients()
  94. {
  95. var q = from g in DbUtil.Db.GoerSupporters
  96. where g.GoerId == PeopleId
  97. where (g.Unsubscribe ?? false) == false
  98. let sender = (from s in DbUtil.Db.GoerSenderAmounts
  99. where s.GoerId == g.GoerId
  100. where s.OrgId == OrgId
  101. where (s.InActive ?? false) == false
  102. where s.SupporterId == g.SupporterId
  103. where (s.NoNoticeToGoer ?? false) == false
  104. select s.Amount).Sum()
  105. select new RecipientItem()
  106. {
  107. Id = g.Id,
  108. NameOrEmail = g.SupporterId != null
  109. ? g.Supporter.Name
  110. : g.NoDbEmail,
  111. Active = g.Active ?? false,
  112. SenderAmt = sender ?? 0,
  113. Salutation = (g.Salutation.Length > 0)
  114. ? g.Salutation
  115. : g.SupporterId == null
  116. ? "Dear Friend"
  117. : (g.Goer.Age < 30 && (g.Supporter.Age - g.Goer.Age) > 10)
  118. ? "Dear " + g.Supporter.TitleCode + " " + g.Supporter.LastName
  119. : "Hi " + g.Supporter.PreferredName
  120. };
  121. return q.ToList();
  122. }
  123. public void UpdateRecipients()
  124. {
  125. var q = (from g in DbUtil.Db.GoerSupporters
  126. where g.GoerId == PeopleId
  127. where (g.Unsubscribe ?? false) == false
  128. select new { g, goer = g.Goer, supporter = g.Supporter }).ToList();
  129. var list = (from gs in q
  130. join r in Recipients on gs.g.Id equals r.Id
  131. select new { gs, r }).ToList();
  132. foreach (var i in list)
  133. {
  134. var goersupporter = i.gs.g;
  135. var goer = i.gs.goer;
  136. var supporter = i.gs.supporter;
  137. var recipient = i.r;
  138. goersupporter.Active = i.r.Active;
  139. goersupporter.Salutation = recipient.Salutation;
  140. if (recipient.Salutation.HasValue())
  141. continue;
  142. goersupporter.Salutation = supporter == null
  143. ? "Dear Friend"
  144. : (goer.Age < 30 && (supporter.Age - goer.Age) > 10)
  145. ? "Dear " + supporter.TitleCode + " " + supporter.LastName
  146. : "Hi " + supporter.PreferredName;
  147. }
  148. DbUtil.Db.SubmitChanges();
  149. Recipients = GetRecipients();
  150. }
  151. public void RemoveSupporter(int id)
  152. {
  153. var q = from e in DbUtil.Db.GoerSupporters
  154. where e.Id == id
  155. select e;
  156. var r = q.Single();
  157. DbUtil.Db.GoerSupporters.DeleteOnSubmit(r);
  158. DbUtil.Db.SubmitChanges();
  159. Recipients = GetRecipients();
  160. }
  161. public int AddRecipient(int? supporterid, string email)
  162. {
  163. var q = from e in DbUtil.Db.GoerSupporters
  164. where e.GoerId == PeopleId
  165. where e.SupporterId == supporterid || e.NoDbEmail == email
  166. select e;
  167. var r = q.SingleOrDefault();
  168. if (r == null)
  169. {
  170. r = new GoerSupporter()
  171. {
  172. GoerId = PeopleId,
  173. SupporterId = supporterid,
  174. NoDbEmail = email,
  175. Active = true,
  176. Created = DateTime.Now,
  177. };
  178. var supporter = DbUtil.Db.People.SingleOrDefault(pp => pp.PeopleId == supporterid);
  179. var goer = DbUtil.Db.People.SingleOrDefault(pp => pp.PeopleId == PeopleId);
  180. if (supporter == null)
  181. r.Salutation = "Dear Friend";
  182. else
  183. if (goer != null && goer.Age < 30 && (supporter.Age - goer.Age) > 10)
  184. r.Salutation = "Dear " + supporter.TitleCode + " " + supporter.LastName;
  185. else
  186. r.Salutation = "Hi " + supporter.PreferredName;
  187. DbUtil.Db.GoerSupporters.InsertOnSubmit(r);
  188. }
  189. else
  190. r.Active = true;
  191. DbUtil.Db.SubmitChanges();
  192. Recipients = GetRecipients();
  193. return r.Id;
  194. }
  195. public string TestSend()
  196. {
  197. var p = DbUtil.Db.LoadPersonById(Util.UserPeopleId.Value);
  198. try
  199. {
  200. DbUtil.Db.Email(p.EmailAddress, p, null, Subject, Body, false);
  201. }
  202. catch (Exception ex)
  203. {
  204. return JsonConvert.SerializeObject(new { error = true, message = ex.Message });
  205. }
  206. return JsonConvert.SerializeObject(new { message = "Test Email Sent" });
  207. }
  208. public string Send()
  209. {
  210. var p = DbUtil.Db.LoadPersonById(PeopleId);
  211. if (!Subject.HasValue() || !Body.HasValue())
  212. return "Subject needs some text";
  213. DbUtil.LogActivity("MissionTripEmail Send {0}".Fmt(PeopleId));
  214. var glist = from g in DbUtil.Db.GoerSupporters
  215. where (g.Unsubscribe ?? false) == false
  216. where (g.Active == true)
  217. where g.GoerId == PeopleId
  218. select g;
  219. var elist = (from g in glist
  220. where g.SupporterId == null
  221. select g).ToList();
  222. var plist = from g in glist
  223. where g.SupporterId != null
  224. select g;
  225. if (plist.Any())
  226. {
  227. DbUtil.Db.CopySession();
  228. var from = new MailAddress(p.EmailAddress ?? p.EmailAddress2, p.Name);
  229. DbUtil.Db.CurrentOrgId = OrgId;
  230. var emailQueue = DbUtil.Db.CreateQueueForSupporters(p.PeopleId, from, Subject, Body, null, plist, false);
  231. DbUtil.Db.SendPeopleEmail(emailQueue.Id);
  232. }
  233. foreach (var e in elist)
  234. {
  235. SendNoDbEmail(p, e);
  236. }
  237. return null;
  238. }
  239. private void SendNoDbEmail(Person goer, GoerSupporter gs)
  240. {
  241. var sysFromEmail = Util.SysFromEmail;
  242. var from = new MailAddress(goer.EmailAddress ?? goer.EmailAddress2, goer.Name);
  243. try
  244. {
  245. var text = Body;
  246. //var aa = DbUtil.Db.DoReplacements(ref text, goer, null);
  247. text = text.Replace("{salutation}", gs.Salutation, ignoreCase: true);
  248. var qs = "OptOut/UnSubscribe/?gid=" + Util.EncryptForUrl("{0}".Fmt(gs.Id));
  249. var url = DbUtil.Db.ServerLink(qs);
  250. var link = @"<a href=""{0}"">Unsubscribe</a>".Fmt(url);
  251. text = text.Replace("{unsubscribe}", link, ignoreCase: true);
  252. text = text.Replace("%7Bfromemail%7D", from.Address, ignoreCase: true);
  253. text = text.Replace("http://supportlink", DbUtil.Db.ServerLink("/OnlineReg/{0}?gsid={1}".Fmt(OrgId, gs.Id)), ignoreCase: true);
  254. Util.SendMsg(sysFromEmail, DbUtil.Db.CmsHost, from, Subject, text, Util.ToMailAddressList(gs.NoDbEmail), gs.Id, null);
  255. }
  256. catch (Exception ex)
  257. {
  258. Util.SendMsg(sysFromEmail, DbUtil.Db.CmsHost, from, "sent emails - error", ex.ToString(),
  259. Util.ToMailAddressList(from), gs.Id, null);
  260. throw;
  261. }
  262. }
  263. }
  264. }