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