PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsCheckin/Util.cs

https://bitbucket.org/mahalowe/bvcms
C# | 609 lines | 575 code | 30 blank | 4 comment | 51 complexity | e6e652d384f5fc3ffb5642cce20f5cce 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.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Configuration;
  7. using System.Xml.Linq;
  8. using System.Net;
  9. using System.Windows.Forms;
  10. using System.Collections.Specialized;
  11. using System.Text.RegularExpressions;
  12. using System.Globalization;
  13. using System.Threading;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Xml.Serialization;
  17. namespace CmsCheckin
  18. {
  19. public static partial class Util
  20. {
  21. public static WebClient CreateWebClient()
  22. {
  23. var wc = new WebClient();
  24. wc.Headers.Add("Authorization",
  25. "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("{0}:{1}"
  26. .Fmt(Program.Username, Program.Password))));
  27. return wc;
  28. }
  29. public static string GetDigits(this string s)
  30. {
  31. if (string.IsNullOrEmpty(s))
  32. return "";
  33. var digits = new StringBuilder();
  34. foreach (var c in s.ToCharArray())
  35. if (Char.IsDigit(c))
  36. digits.Append(c);
  37. return digits.ToString();
  38. }
  39. public static string Fmt(this string fmt, params object[] p)
  40. {
  41. return string.Format(fmt, p);
  42. }
  43. public static XDocument GetDocument(this Control f, string page)
  44. {
  45. var wc = Util.CreateWebClient();
  46. var url = new Uri(new Uri(Program.URL), page);
  47. var str = wc.DownloadString(url);
  48. var x = XDocument.Parse(str);
  49. return x;
  50. }
  51. public static int ToInt(this string s)
  52. {
  53. int i = 0;
  54. int.TryParse(s, out i);
  55. return i;
  56. }
  57. public static int? ToInt2(this string s)
  58. {
  59. int? r = null;
  60. int i;
  61. if (int.TryParse(s, out i))
  62. r = i;
  63. return r;
  64. }
  65. public static void AddPerson(this Control f,
  66. string first,
  67. string last,
  68. string goesby,
  69. string dob,
  70. string email,
  71. string addr,
  72. string zip,
  73. string cell,
  74. string home,
  75. string allergies,
  76. string grade,
  77. string parent,
  78. string emfriend,
  79. string emphone,
  80. string churchname,
  81. CheckState activeother,
  82. int marital,
  83. int gender)
  84. {
  85. var wc = CreateWebClient();
  86. var coll = new NameValueCollection();
  87. coll.Add("first", first);
  88. coll.Add("last", last);
  89. coll.Add("goesby", goesby);
  90. coll.Add("dob", dob);
  91. coll.Add("email", email);
  92. coll.Add("addr", addr);
  93. coll.Add("zip", zip);
  94. coll.Add("cell", cell);
  95. if (home.HasValue())
  96. coll.Add("home", home);
  97. else
  98. coll.Add("home", cell);
  99. coll.Add("marital", marital.ToString());
  100. coll.Add("gender", gender.ToString());
  101. coll.Add("campusid", Program.CampusId.ToString());
  102. coll.Add("allergies", allergies);
  103. if (Program.AskGrade)
  104. {
  105. coll.Add("grade", grade);
  106. coll.Add("AskGrade", Program.AskGrade.ToString());
  107. }
  108. if (Program.AskChurchName)
  109. {
  110. coll.Add("churchname", churchname);
  111. coll.Add("AskChurchName", Program.AskChurchName.ToString());
  112. }
  113. if (Program.AskEmFriend)
  114. {
  115. coll.Add("parent", parent);
  116. coll.Add("emphone", emphone.GetDigits());
  117. coll.Add("AskEmFriend", Program.AskEmFriend.ToString());
  118. coll.Add("emfriend", emfriend);
  119. }
  120. if (Program.AskChurch)
  121. {
  122. coll.Add("activeother", (activeother == CheckState.Checked).ToString());
  123. coll.Add("AskChurch", Program.AskChurch.ToString());
  124. }
  125. var url = new Uri(new Uri(Program.URL), "Checkin2/AddPerson/" + Program.FamilyId);
  126. var resp = wc.UploadValues(url, "POST", coll);
  127. var s = Encoding.ASCII.GetString(resp);
  128. var a = s.Split('.');
  129. Program.FamilyId = a[0].ToInt();
  130. Program.PeopleId = a[1].ToInt();
  131. }
  132. public static void EditPerson(this Control f,
  133. int id,
  134. string first,
  135. string last,
  136. string goesby,
  137. string dob,
  138. string email,
  139. string addr,
  140. string zip,
  141. string cell,
  142. string home,
  143. string allergies,
  144. string grade,
  145. string parent,
  146. string emfriend,
  147. string emphone,
  148. string churchname,
  149. CheckState activeother,
  150. int marital,
  151. int gender)
  152. {
  153. var wc = CreateWebClient();
  154. var coll = new NameValueCollection();
  155. coll.Add("first", first);
  156. coll.Add("last", last);
  157. coll.Add("goesby", goesby);
  158. coll.Add("dob", dob);
  159. coll.Add("email", email);
  160. coll.Add("addr", addr);
  161. coll.Add("zip", zip);
  162. coll.Add("cell", cell);
  163. if (home.HasValue())
  164. coll.Add("home", home);
  165. else
  166. coll.Add("home", cell);
  167. coll.Add("marital", marital.ToString());
  168. coll.Add("gender", gender.ToString());
  169. coll.Add("campusid", Program.CampusId.ToString());
  170. coll.Add("allergies", allergies);
  171. if (Program.AskGrade)
  172. {
  173. coll.Add("grade", grade);
  174. coll.Add("AskGrade", Program.AskGrade.ToString());
  175. }
  176. if (Program.AskChurchName)
  177. {
  178. coll.Add("churchname", churchname);
  179. coll.Add("AskChurchName", Program.AskChurchName.ToString());
  180. }
  181. if (Program.AskEmFriend)
  182. {
  183. coll.Add("parent", parent);
  184. coll.Add("emphone", emphone.GetDigits());
  185. coll.Add("AskEmFriend", Program.AskEmFriend.ToString());
  186. coll.Add("emfriend", emfriend);
  187. }
  188. if (Program.AskChurch)
  189. {
  190. coll.Add("activeother", (activeother == CheckState.Checked).ToString());
  191. coll.Add("AskChurch", Program.AskChurch.ToString());
  192. }
  193. var url = new Uri(new Uri(Program.URL), "Checkin2/EditPerson/" + id);
  194. var resp = wc.UploadValues(url, "POST", coll);
  195. var s = Encoding.ASCII.GetString(resp);
  196. }
  197. public static void EditPerson(this Control f,
  198. int id,
  199. string first,
  200. string last,
  201. string goesby,
  202. string dob,
  203. string email,
  204. string addr,
  205. string zip,
  206. string cell,
  207. string home,
  208. int marital,
  209. int gender)
  210. {
  211. var wc = CreateWebClient();
  212. var coll = new NameValueCollection();
  213. coll.Add("first", first);
  214. coll.Add("last", last);
  215. coll.Add("goesby", goesby);
  216. coll.Add("dob", dob);
  217. coll.Add("email", email);
  218. coll.Add("addr", addr);
  219. coll.Add("zip", zip);
  220. coll.Add("cell", cell);
  221. if (home.HasValue())
  222. coll.Add("home", home);
  223. else
  224. coll.Add("home", cell);
  225. coll.Add("marital", marital.ToString());
  226. coll.Add("gender", gender.ToString());
  227. coll.Add("campusid", Program.CampusId.ToString());
  228. var url = new Uri(new Uri(Program.URL), "Checkin2/EditPerson/" + id);
  229. var resp = wc.UploadValues(url, "POST", coll);
  230. var s = Encoding.ASCII.GetString(resp);
  231. }
  232. public static bool AllDigits(string str)
  233. {
  234. Regex patt = new Regex("[^0-9]");
  235. return !(patt.IsMatch(str));
  236. }
  237. public static string FmtFone(this string phone)
  238. {
  239. var ph = phone.GetDigits();
  240. if (string.IsNullOrEmpty(ph))
  241. return "";
  242. var t = new StringBuilder(ph);
  243. if (ph.Length >= 4)
  244. t.Insert(3, "-");
  245. if (ph.Length >= 8)
  246. t.Insert(7, "-");
  247. return t.ToString();
  248. }
  249. public static string FmtDate(this string d)
  250. {
  251. var dt = d.GetDigits();
  252. if (string.IsNullOrEmpty(dt))
  253. return "";
  254. var t = new StringBuilder(dt);
  255. if (dt.Length >= 2)
  256. t.Insert(2, "/");
  257. if (dt.Length >= 8)
  258. t.Insert(7, "-");
  259. return t.ToString();
  260. }
  261. public static string FmtZip(this string zip)
  262. {
  263. if (!zip.HasValue())
  264. return "";
  265. var t = new StringBuilder(zip.GetDigits());
  266. if (t.Length != 9)
  267. return zip;
  268. t.Insert(5, "-");
  269. return t.ToString();
  270. }
  271. public static bool DateIsOK(string s)
  272. {
  273. var re = new Regex(@"\A(([1-9]|1[012])(/(?<day>3[01]|[12][0-9]|[1-9])?)?(?<g3>/([0-9]{1,2})?)?)\Z");
  274. if (re.IsMatch(s))
  275. {
  276. var m = re.Match(s);
  277. if (m.Groups["day"].Length == 0 && m.Groups["g3"].Length > 0)
  278. return false;
  279. return true;
  280. }
  281. return false;
  282. }
  283. public static bool DateValid(string s)
  284. {
  285. var dt = DateTime.MinValue;
  286. if (!s.HasValue())
  287. return true;
  288. if (Regex.IsMatch(s, @"\A(?:\A(0?[1-9]|1[012])[-/](0?[1-9]|[12][0-9]|3[01])[-/](19|20)?[0-9]{2}\s*\z)\Z"))
  289. if (DateTime.TryParse(s, out dt))
  290. {
  291. //if (dt > DateTime.Now.Date)
  292. // dt = dt.AddYears(-100);
  293. return true;
  294. }
  295. return false;
  296. }
  297. public static bool HasValue(this string s)
  298. {
  299. if (s != null)
  300. s = s.Trim();
  301. return !string.IsNullOrEmpty(s);
  302. }
  303. public static string ToTitleCase(this string s)
  304. {
  305. return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s);
  306. }
  307. public static void Swap(this UserControl c1, UserControl c2)
  308. {
  309. Program.TimerStop();
  310. c1.Visible = false;
  311. c2.Visible = true;
  312. }
  313. public static void GoHome(this UserControl c, string s)
  314. {
  315. var tb = Program.baseform.textbox;
  316. c.Swap(tb.Parent as UserControl);
  317. Program.ClearFields();
  318. Program.CursorHide();
  319. tb.Text = s.FmtFone();
  320. tb.Focus();
  321. tb.Select(tb.Text.Length, 0);
  322. }
  323. public static bool IsDebug()
  324. {
  325. var d = false;
  326. #if DEBUG
  327. d = true;
  328. #endif
  329. return d;
  330. }
  331. public static string Age(this string birthday)
  332. {
  333. DateTime bd;
  334. if (!DateTime.TryParse(birthday, out bd))
  335. return "?";
  336. DateTime td = DateTime.Now;
  337. int age = td.Year - bd.Year;
  338. if (td.Month < bd.Month || (td.Month == bd.Month && td.Day < bd.Day))
  339. age--;
  340. if (age < 0)
  341. return "?";
  342. return age.ToString();
  343. }
  344. public class ClassCheckedInfo
  345. {
  346. public bool ischecked { get; set; }
  347. public ClassInfo c { get; set; }
  348. }
  349. public static void AttendUnAttend(ClassCheckedInfo info)
  350. {
  351. if (info.c.oid == 0)
  352. return;
  353. try
  354. {
  355. var wc = CreateWebClient();
  356. var coll = new NameValueCollection();
  357. coll.Add("PeopleId", info.c.pid.ToString());
  358. coll.Add("OrgId", info.c.oid.ToString());
  359. Uri url = null;
  360. coll.Add("Present", info.ischecked.ToString());
  361. coll.Add("hour", info.c.hour.Value.ToString("g"));
  362. coll.Add("kiosk", Program.KioskName);
  363. url = new Uri(new Uri(Program.URL), "Checkin2/RecordAttend2/");
  364. var resp = wc.UploadValues(url, "POST", coll);
  365. #if DEBUG
  366. //System.Threading.Thread.Sleep(1500);
  367. #endif
  368. var s = Encoding.ASCII.GetString(resp);
  369. }
  370. catch (Exception)
  371. {
  372. }
  373. }
  374. public static int GetGuestCount( int PersonID )
  375. {
  376. try
  377. {
  378. var wc = CreateWebClient();
  379. var coll = new NameValueCollection();
  380. coll.Add("id", PersonID.ToString());
  381. var s = "Checkin2/FetchGuestCount/";
  382. var url = new Uri(new Uri(Program.URL), s);
  383. var resp = wc.UploadValues(url, "POST", coll);
  384. return Encoding.ASCII.GetString(resp).ToInt();
  385. }
  386. catch (Exception ex) {}
  387. return 0;
  388. }
  389. public static bool AddIDCard(string cardID, int personID)
  390. {
  391. try
  392. {
  393. var c = new NameValueCollection();
  394. c.Add("cardid", cardID);
  395. c.Add("personid", personID.ToString());
  396. c.Add("overwrite", "true");
  397. var path = "Checkin2/AddIDCard/";
  398. var url = new Uri(new Uri(Program.URL), path);
  399. var wc = CreateWebClient();
  400. var resp = wc.UploadValues(url, "POST", c);
  401. int ret = Encoding.ASCII.GetString(resp).ToInt();
  402. return (ret == 0);
  403. }
  404. catch (Exception ex) { }
  405. return false;
  406. }
  407. public static int BuildingCheckin(int pid, List<Activity> activities, int accesstype )
  408. {
  409. try
  410. {
  411. var xs = new XmlSerializer(typeof(List<Activity>), new XmlRootAttribute("Activities"));
  412. var sw = new StringWriter();
  413. xs.Serialize(sw, activities);
  414. var bits = Encoding.UTF8.GetBytes(sw.ToString());
  415. var wc = CreateWebClient();
  416. var s = "Checkin2/BuildingCheckin/{0}?location={1}&accesstype={2}".Fmt(pid,Program.Building,accesstype);
  417. var g = Program.GuestOf();
  418. if (g != null)
  419. s += "&guestof=" + g.CheckinId;
  420. var url = new Uri(new Uri(Program.URL), s);
  421. var ret = wc.UploadData(url, "POST", bits);
  422. return Encoding.ASCII.GetString(ret).ToInt();
  423. }
  424. catch (Exception ex)
  425. {
  426. }
  427. return 0;
  428. }
  429. public static void BuildingUnCheckin(int pid)
  430. {
  431. try
  432. {
  433. var wc = CreateWebClient();
  434. var url = new Uri(new Uri(Program.URL), "Checkin2/BuildingUnCheckin/" + pid);
  435. wc.UploadString(url, "POST", "");
  436. }
  437. catch (Exception)
  438. {
  439. }
  440. }
  441. public static void JoinUnJoin(ClassInfo c, bool joining)
  442. {
  443. if (c.oid == 0)
  444. return;
  445. try
  446. {
  447. var wc = CreateWebClient();
  448. var coll = new NameValueCollection();
  449. coll.Add("PeopleId", c.pid.ToString());
  450. coll.Add("OrgId", c.oid.ToString());
  451. Uri url = null;
  452. coll.Add("Member", joining.ToString());
  453. url = new Uri(new Uri(Program.URL), "Checkin2/Membership/");
  454. var resp = wc.UploadValues(url, "POST", coll);
  455. #if DEBUG
  456. //System.Threading.Thread.Sleep(1500);
  457. #endif
  458. var s = Encoding.ASCII.GetString(resp);
  459. }
  460. catch (Exception)
  461. {
  462. }
  463. }
  464. public static void AddUpdateNotes(int peopleid, string notes)
  465. {
  466. try
  467. {
  468. var wc = CreateWebClient();
  469. var coll = new NameValueCollection();
  470. coll.Add("peopleid", peopleid.ToString());
  471. coll.Add("field", Program.Building + "-notes");
  472. coll.Add("value", notes);
  473. var url = new Uri(new Uri(Program.URL), "APIPerson/AddEditExtraValue/");
  474. var resp = wc.UploadValues(url, "POST", coll);
  475. var s = Encoding.ASCII.GetString(resp);
  476. }
  477. catch (Exception)
  478. {
  479. }
  480. }
  481. public static void UnLockFamily()
  482. {
  483. if (Program.FamilyId == 0)
  484. return;
  485. try
  486. {
  487. var wc = CreateWebClient();
  488. var coll = new NameValueCollection();
  489. coll.Add("fid", Program.FamilyId.ToString());
  490. var url = new Uri(new Uri(Program.URL), "Checkin2/UnLockFamily/");
  491. var resp = wc.UploadValues(url, "POST", coll);
  492. var s = Encoding.ASCII.GetString(resp);
  493. }
  494. catch (Exception)
  495. {
  496. }
  497. }
  498. public static void ReportPrinterProblem()
  499. {
  500. try
  501. {
  502. var wc = CreateWebClient();
  503. var coll = new NameValueCollection();
  504. coll.Add("kiosk", Program.KioskName);
  505. coll.Add("campusid", Program.CampusId.ToString());
  506. var url = new Uri(new Uri(Program.URL), "Checkin2/ReportPrinterProblem/");
  507. var resp = wc.UploadValues(url, "POST", coll);
  508. var s = Encoding.ASCII.GetString(resp);
  509. }
  510. catch (Exception)
  511. {
  512. }
  513. }
  514. public static void UploadPrintJob(IEnumerable<LabelInfo> q)
  515. {
  516. var j = new PrintJob { securitycode = Program.SecurityCode, list = q.ToList() };
  517. var xs = new XmlSerializer(typeof(PrintJob));
  518. var sw = new StringWriter();
  519. xs.Serialize(sw, j);
  520. var bits = Encoding.UTF8.GetBytes(sw.ToString());
  521. var url = new Uri(new Uri(Program.URL), "Checkin2/UploadPrintJob/" + Program.KioskName);
  522. var wc = CreateWebClient();
  523. wc.UploadData(url, "POST", bits);
  524. }
  525. public static PrintJobs FetchPrintJob()
  526. {
  527. var wc = Util.CreateWebClient();
  528. var url = new Uri(new Uri(Program.URL), "Checkin2/FetchPrintJobs/" + Program.PrintKiosks);
  529. var xml = wc.DownloadString(url);
  530. var xs = new XmlSerializer(typeof(PrintJobs));
  531. var sr = new StringReader(xml);
  532. var j = (PrintJobs)xs.Deserialize(sr);
  533. return j;
  534. }
  535. public static BaseBuildingInfo FetchBuildingInfo()
  536. {
  537. var wc = Util.CreateWebClient();
  538. var url = new Uri(new Uri(Program.URL), "Checkin2/FetchBuildingActivities/" + Program.Building);
  539. var xml = wc.DownloadString(url);
  540. var xs = new XmlSerializer(typeof(BaseBuildingInfo), new XmlRootAttribute("BuildingCheckin"));
  541. var sr = new StringReader(xml);
  542. var a = (BaseBuildingInfo)xs.Deserialize(sr);
  543. return a;
  544. }
  545. public static Image GetImage(int peopleid)
  546. {
  547. var wc = Util.CreateWebClient();
  548. var url = new Uri(new Uri(Program.URL), "Checkin2/FetchImage/" + peopleid);
  549. var bits = wc.DownloadData(url);
  550. var istream = new MemoryStream(bits);
  551. var img = Image.FromStream(istream);
  552. istream.Close();
  553. return img;
  554. }
  555. public static string GetNotes(int pid)
  556. {
  557. var wc = CreateWebClient();
  558. var url = new Uri(new Uri(Program.URL), "APIPerson/ExtraValues/" + pid + "?fields=" + Program.Building + "-notes");
  559. var str = wc.DownloadString(url);
  560. var x = XDocument.Parse(str);
  561. var n = x.Root.Element(Program.Building + "-notes");
  562. if (n != null)
  563. return n.Value;
  564. return "";
  565. }
  566. }
  567. public class EventArgs<T> : EventArgs
  568. {
  569. public EventArgs(T value)
  570. {
  571. m_value = value;
  572. }
  573. private T m_value;
  574. public T Value
  575. {
  576. get { return m_value; }
  577. }
  578. }
  579. }