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