PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsWeb/Areas/OnlineReg/Models/OnlineReg/HelperMethods.cs

https://bitbucket.org/mahalowe/bvcms
C# | 402 lines | 350 code | 6 blank | 46 comment | 135 complexity | 2fef2021ee1576bbe4ff9b3d3f49b976 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data.Linq;
  5. using System.Web;
  6. using System.Xml.Serialization;
  7. using CmsData;
  8. using System.Web.Mvc;
  9. using System.Text;
  10. using System.Configuration;
  11. using CmsData.Registration;
  12. using UtilityExtensions;
  13. using System.Data.Linq.SqlClient;
  14. using System.Net.Mail;
  15. using System.Text.RegularExpressions;
  16. using System.Collections;
  17. using System.Runtime.Serialization;
  18. using CmsData.Codes;
  19. namespace CmsWeb.Models
  20. {
  21. public partial class OnlineRegModel
  22. {
  23. public static Organization CreateAccountOrg()
  24. {
  25. var settings = HttpContext.Current.Items["RegSettings"] as Dictionary<int, Settings>;
  26. if (settings == null)
  27. {
  28. settings = new Dictionary<int, Settings>();
  29. HttpContext.Current.Items.Add("RegSettings", settings);
  30. }
  31. var o = new Organization { OrganizationId = Util.CreateAccountCode, OrganizationName = "My Data" };
  32. o.RegistrationTypeId = RegistrationTypeCode.CreateAccount;
  33. if (!settings.ContainsKey(Util.CreateAccountCode))
  34. settings.Add(Util.CreateAccountCode, ParseSetting("AllowOnlyOne: true", Util.CreateAccountCode));
  35. return o;
  36. }
  37. private Dictionary<int, Settings> _settings;
  38. public Dictionary<int, Settings> settings
  39. {
  40. get
  41. {
  42. if (_settings == null)
  43. _settings = HttpContext.Current.Items["RegSettings"] as Dictionary<int, Settings>;
  44. return _settings;
  45. }
  46. }
  47. public bool DisplayLogin()
  48. {
  49. return (List.Count == 0 && !UserPeopleId.HasValue && nologin == false);
  50. }
  51. public string LoginName
  52. {
  53. get
  54. {
  55. if (user != null)
  56. return user.Name;
  57. return "anonymous";
  58. }
  59. }
  60. public string MeetingTime
  61. {
  62. get { return meeting().MeetingDate.ToString2("f"); }
  63. }
  64. public OnlineRegPersonModel last
  65. {
  66. get
  67. {
  68. if (list.Count > 0)
  69. return list[list.Count - 1];
  70. return null;
  71. }
  72. }
  73. public string qtesting
  74. {
  75. get { return testing == true ? "?testing=true" : ""; }
  76. }
  77. public bool IsCreateAccount()
  78. {
  79. if (org != null)
  80. return org.RegistrationTypeId == RegistrationTypeCode.CreateAccount;
  81. return false;
  82. }
  83. public bool IsEnded()
  84. {
  85. if (div != null && UserSelectsOrganization())
  86. return UserSelectClasses(div.Id).Count() == 0;
  87. else if (org != null)
  88. return org.ClassFilled == true;
  89. return false;
  90. }
  91. public string Filled()
  92. {
  93. var msg = "";
  94. if (div != null && UserSelectsOrganization())
  95. msg = UserSelectClasses(div.Id).Count() == 0 ? "all registration options are full" : "";
  96. else if (org != null)
  97. {
  98. msg = ((org.ClassFilled ?? false) || (org.Limit > 0 && org.Limit <= org.MemberCount)) ? "registration is full" : "";
  99. if (msg.HasValue())
  100. {
  101. org.ClassFilled = true;
  102. DbUtil.Db.SubmitChanges();
  103. }
  104. }
  105. return msg;
  106. }
  107. public bool NotAvailable()
  108. {
  109. var dt = DateTime.Now;
  110. var dt1 = DateTime.Parse("1/1/1900");
  111. var dt2 = DateTime.Parse("1/1/2200");
  112. if (divid != null)
  113. return DbUtil.Db.Organizations.Any(o =>
  114. o.DivOrgs.Any(di => di.DivId == divid) &&
  115. (o.RegistrationClosed == true
  116. || dt < (o.RegStart ?? dt1)
  117. || dt > (o.RegEnd ?? dt2)
  118. ));
  119. if (masterorgid.HasValue)
  120. return masterorg.RegistrationClosed == true
  121. || masterorg.OrganizationStatusId == OrgStatusCode.Inactive
  122. || dt < (masterorg.RegStart ?? dt1)
  123. || dt > (masterorg.RegEnd ?? dt2);
  124. return org.RegistrationClosed == true
  125. || org.OrganizationStatusId == OrgStatusCode.Inactive
  126. || dt < (org.RegStart ?? dt1)
  127. || dt > (org.RegEnd ?? dt2);
  128. }
  129. public IEnumerable<Organization> GetOrgsInDiv()
  130. {
  131. return from o in DbUtil.Db.Organizations
  132. where o.DivOrgs.Any(di => di.DivId == divid)
  133. select o;
  134. }
  135. public bool UserSelectsOrganization()
  136. {
  137. if (masterorgid.HasValue && masterorg.RegistrationTypeId == RegistrationTypeCode.UserSelectsOrganization2)
  138. return true;
  139. if (divid == null)
  140. return false;
  141. var q = from o in GetOrgsInDiv()
  142. where o.RegistrationTypeId == RegistrationTypeCode.UserSelectsOrganization
  143. select o;
  144. return q.Count() > 0;
  145. }
  146. public bool OnlyOneAllowed()
  147. {
  148. if (org != null)
  149. {
  150. var setting = settings[org.OrganizationId];
  151. return org.RegistrationTypeId == RegistrationTypeCode.ChooseVolunteerTimes
  152. || org.RegistrationTypeId == RegistrationTypeCode.CreateAccount
  153. || setting.AllowOnlyOne || setting.AskVisible("AskTickets")
  154. || setting.GiveOrgMembAccess;
  155. }
  156. if (settings != null)
  157. {
  158. var q = from o in settings.Values
  159. where o.AllowOnlyOne || o.AskVisible("AskTickets")
  160. select o;
  161. return q.Any();
  162. }
  163. return false;
  164. }
  165. public bool ChoosingSlots()
  166. {
  167. if (org != null)
  168. return org.RegistrationTypeId == RegistrationTypeCode.ChooseVolunteerTimes;
  169. return false;
  170. }
  171. public bool ManagingSubscriptions()
  172. {
  173. if (masterorgid.HasValue && masterorg.RegistrationTypeId == RegistrationTypeCode.ManageSubscriptions2)
  174. return true;
  175. if (org != null)
  176. return org.RegistrationTypeId == RegistrationTypeCode.ManageSubscriptions;
  177. var q = from o in GetOrgsInDiv()
  178. where o.RegistrationTypeId == RegistrationTypeCode.ManageSubscriptions
  179. select o;
  180. return q.Count() > 0;
  181. }
  182. public bool OnlinePledge()
  183. {
  184. if (org != null)
  185. return org.RegistrationTypeId == RegistrationTypeCode.OnlinePledge;
  186. return false;
  187. }
  188. public bool ManageGiving()
  189. {
  190. if (org != null)
  191. return org.RegistrationTypeId == RegistrationTypeCode.ManageGiving;
  192. return false;
  193. }
  194. public bool OnlineGiving()
  195. {
  196. if (org != null)
  197. return org.RegistrationTypeId == RegistrationTypeCode.OnlineGiving;
  198. return false;
  199. }
  200. public bool AskDonation()
  201. {
  202. if (org != null)
  203. return settings[org.OrganizationId].AskDonation;
  204. if (settings == null)
  205. return false;
  206. return settings.Values.Any(o => o.AskDonation);
  207. }
  208. public string DonationLabel()
  209. {
  210. if (org != null)
  211. return settings[org.OrganizationId].DonationLabel;
  212. return settings.Values.First(o => o.AskDonation).DonationLabel;
  213. }
  214. public string Header
  215. {
  216. get
  217. {
  218. if (masterorgid.HasValue)
  219. return masterorg.OrganizationName;
  220. if (div != null)
  221. return div.Name;
  222. else if (settings != null && org != null && settings[org.OrganizationId] != null)
  223. return Util.PickFirst(settings[org.OrganizationId].Title, org.OrganizationName);
  224. else if (org != null)
  225. return org.OrganizationName;
  226. return "no organization";
  227. }
  228. }
  229. public string Instructions
  230. {
  231. get
  232. {
  233. if (masterorg != null)
  234. {
  235. var setting1 = new Settings();
  236. if(settings.ContainsKey(masterorg.OrganizationId))
  237. setting1 = settings[masterorg.OrganizationId];
  238. var setting2 = setting1;
  239. if (last != null && last.org != null && settings.ContainsKey(last.org.OrganizationId))
  240. setting1 = settings[last.org.OrganizationId];
  241. return @"
  242. <div class=""instructions login"">{0}</div>
  243. <div class=""instructions select"">{1}</div>
  244. <div class=""instructions find"">{2}</div>
  245. <div class=""instructions options"">{3}</div>
  246. <div class=""instructions submit"">{4}</div>
  247. <div class=""instructions sorry"">{5}</div>
  248. " .Fmt(Util.PickFirst(setting1.InstructionLogin, setting2.InstructionLogin),
  249. Util.PickFirst(setting1.InstructionSelect, setting2.InstructionSelect),
  250. Util.PickFirst(setting1.InstructionFind, setting2.InstructionFind),
  251. Util.PickFirst(setting1.InstructionOptions, setting2.InstructionOptions),
  252. Util.PickFirst(setting1.InstructionSubmit, setting2.InstructionSubmit),
  253. Util.PickFirst(setting1.InstructionSorry, setting2.InstructionSorry)
  254. );
  255. }
  256. if (org != null)
  257. {
  258. var setting = new Settings();
  259. if (settings.ContainsKey(org.OrganizationId))
  260. setting = settings[org.OrganizationId];
  261. if (setting.InstructionAll != null)
  262. if (setting.InstructionAll.ToString().HasValue())
  263. return setting.InstructionAll.ToString();
  264. var v = "{0}{1}{2}{3}{4}{5}".Fmt(
  265. setting.InstructionLogin,
  266. setting.InstructionSelect,
  267. setting.InstructionFind,
  268. setting.InstructionOptions,
  269. setting.InstructionSubmit,
  270. setting.InstructionSorry);
  271. string ins = null;
  272. if (v.HasValue())
  273. ins = @"<div class=""instructions login"">{0}</div>
  274. <div class=""instructions select"">{1}</div>
  275. <div class=""instructions find"">{2}</div>
  276. <div class=""instructions options"">{3}</div>
  277. <div class=""instructions submit"">{4}</div>
  278. <div class=""instructions sorry"">{5}</div>".Fmt(
  279. setting.InstructionLogin,
  280. setting.InstructionSelect,
  281. setting.InstructionFind,
  282. setting.InstructionOptions,
  283. setting.InstructionSubmit,
  284. setting.InstructionSorry
  285. );
  286. if (ins.Contains("{ev:", ignoreCase: true))
  287. ins = DoReplaceForExtraValueCode(ins, last.person);
  288. return Util.PickFirst(ins, div != null ? div.Instructions : "") + "\n";
  289. }
  290. if (div != null)
  291. return div.Instructions;
  292. return "";
  293. }
  294. }
  295. public static string DoReplaceForExtraValueCode(string text, Person p)
  296. {
  297. const string RE = @"{ev:(?<name>.+?)}";
  298. var re = new Regex(RE, RegexOptions.Singleline | RegexOptions.Multiline);
  299. var match = re.Match(text);
  300. while (match.Success)
  301. {
  302. var tag = match.Value;
  303. var name = match.Groups["name"].Value;
  304. if (p == null)
  305. text = text.Replace(tag, "");
  306. else
  307. text = text.Replace(tag, p.GetExtra(name));
  308. match = match.NextMatch();
  309. }
  310. return text;
  311. }
  312. public string Terms
  313. {
  314. get
  315. {
  316. if (masterorgid.HasValue)
  317. return Util.PickFirst("{0}".Fmt(settings[masterorgid.Value].Terms), "");
  318. if (org != null)
  319. return Util.PickFirst("{0}".Fmt(settings[org.OrganizationId].Terms),
  320. div != null ? div.Terms : "");
  321. if (div != null)
  322. return div.Terms;
  323. return "";
  324. }
  325. }
  326. public OnlineRegPersonModel LoadExistingPerson(int id, int index)
  327. {
  328. var person = DbUtil.Db.LoadPersonById(id);
  329. var p = new OnlineRegPersonModel
  330. {
  331. index = index,
  332. dob = person.DOB,
  333. email = person.EmailAddress.HasValue() ? person.EmailAddress : user.EmailAddress,
  334. first = person.PreferredName,
  335. last = person.LastName,
  336. PeopleId = id,
  337. phone = Util.PickFirst(person.CellPhone, person.HomePhone),
  338. orgid = orgid,
  339. masterorgid = masterorgid,
  340. divid = divid,
  341. classid = classid,
  342. IsFamily = true,
  343. LoggedIn = true,
  344. Found = true,
  345. IsValidForExisting = true,
  346. };
  347. return p;
  348. }
  349. // public void SavePaymentInfo()
  350. // {
  351. // try
  352. // {
  353. // var gateway = OnlineRegModel.GetTransactionGateway();
  354. // if (gateway == "authorizenet")
  355. // {
  356. // var au = new AuthorizeNet(DbUtil.Db, m.testing);
  357. // au.AddUpdateCustomerProfile(m.pid,
  358. // m.SemiEvery,
  359. // m.Day1,
  360. // m.Day2,
  361. // m.EveryN,
  362. // m.Period,
  363. // m.StartWhen,
  364. // m.StopWhen,
  365. // m.Type,
  366. // m.Cardnumber,
  367. // m.Expires,
  368. // m.Cardcode,
  369. // m.Routing,
  370. // m.Account,
  371. // m.testing);
  372. // }
  373. // else if (gateway == "sage")
  374. // {
  375. // var sg = new CmsData.SagePayments(DbUtil.Db, m.testing);
  376. // sg.storeVault(m.pid,
  377. // m.SemiEvery,
  378. // m.Day1,
  379. // m.Day2,
  380. // m.EveryN,
  381. // m.Period,
  382. // m.StartWhen,
  383. // m.StopWhen,
  384. // m.Type,
  385. // m.Cardnumber,
  386. // m.Expires,
  387. // m.Cardcode,
  388. // m.Routing,
  389. // m.Account,
  390. // m.testing);
  391. // }
  392. // else
  393. // throw new Exception("ServiceU not supported");
  394. // }
  395. }
  396. }