Exemple #1
0
        public JsonResult CreateNewChurch(ChurchSettingsViewModel churchSettings)
        {
            bool sessionTimedOut = false;
            if (Session[SessionVariable.LoggedOnPerson] == null)
            {
                sessionTimedOut = true;
            }
            else
            {
                Person currentPerson = (Person)Session[SessionVariable.LoggedOnPerson];
                SettingsDataAccessor.CreateNewChurch(currentPerson, churchSettings);
            }

            var response = new
            {
                SessionTimeOut = sessionTimedOut,
                Message = "Church Created"
            };

            return Json(response);
        }
        public JsonResult CreateNewChurch(ChurchSettingsViewModel churchSettings)
        {
            var sessionTimedOut = false;
            string message;
            if (Session[SessionVariable.LoggedOnPerson] == null)
            {
                sessionTimedOut = true;
                message = "Session has timed out";
            }
            else
            {
                var currentPerson = (Person)Session[SessionVariable.LoggedOnPerson];
                try
                {
                    SettingsDataAccessor.CreateNewChurch(currentPerson, churchSettings);
                    message = "Church was succesfully created...";
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }

            var response = new
            {
                SessionTimeOut = sessionTimedOut,
                Message = message
            };

            return Json(response);
        }
Exemple #3
0
        public JsonResult SaveChurchContactDetails(ChurchSettingsViewModel churchSettings)
        {
            bool sessionTimedOut = false;
            if (Session[SessionVariable.LoggedOnPerson] == null)
            {
                sessionTimedOut = true;
            }
            else
            {
                Person currentPerson = (Person)Session[SessionVariable.LoggedOnPerson];
                //TODO Check for User Roles
                SettingsDataAccessor.SaveChurchContactDetails(currentPerson, churchSettings);
            }

            var response = new
            {
                SessionTimeOut = sessionTimedOut,
                Message = "Church Contact Details Saved"
            };

            return Json(response, JsonRequestBehavior.AllowGet);
        }
 private static void PopulateChurchModel(ChurchSettingsViewModel churchSettings, Church churchToSave)
 {
     churchToSave.Changed = DateTime.Now;
     churchToSave.Name = churchSettings.ChurchName;
     churchToSave.OfficeEmail = churchSettings.OfficeEmail;
     churchToSave.OfficePhone = churchSettings.OfficePhone;
     churchToSave.SiteHeader = churchSettings.SystemName;
     churchToSave.UITheme = churchSettings.UITheme;
     churchToSave.Url = churchSettings.Url;
     churchToSave.Province = churchSettings.Province;
     churchToSave.BackgroundImage = "default.png";
     churchToSave.StatusId = (int)ChurchStatuses.Pending;
 }
        private static void PopulateChurchAddress(ChurchSettingsViewModel churchSettings, oikonomosEntities context, Church churchToSave)
        {
            //Check to see if the address already exists
            var address = new Address();

            if (churchSettings.AddressId > 0)
            {
                address = (from a in context.Addresses
                           where a.AddressId == churchSettings.AddressId
                           select a).FirstOrDefault();

                if (address == null) //Should never happen, but just to be sure
                {
                    address = new Address();
                    address.Created = DateTime.Now;
                    churchSettings.AddressId = 0;
                }
            }
            else
            {
                address.Created = DateTime.Now;
            }

            address.Line1 = churchSettings.Address1 ?? string.Empty;
            address.Line2 = churchSettings.Address2 ?? string.Empty;
            address.Line3 = churchSettings.Address3 ?? string.Empty;
            address.Line4 = churchSettings.Address4 ?? string.Empty;
            address.AddressType = churchSettings.AddressType ?? string.Empty;
            address.Lat = churchSettings.Lat;
            address.Long = churchSettings.Lng;
            address.Changed = DateTime.Now;

            if (churchSettings.AddressId == 0)
            {
                context.Addresses.AddObject(address);
                churchToSave.Address = address;
            }
        }
        public static void SaveChurchContactDetails(Person currentPerson, ChurchSettingsViewModel churchSettings)
        {
            if (currentPerson.HasPermission(common.Permissions.EditChurchContactDetails))
            {
                using (oikonomosEntities context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
                {
                    var churchToSave = (from c in context.Churches
                                        where c.ChurchId == currentPerson.ChurchId
                                        select c).FirstOrDefault();

                    PopulateChurchModel(churchSettings, churchToSave);
                    PopulateChurchAddress(churchSettings, context, churchToSave);

                    context.SaveChanges();
                }
            }
        }
        public static void CreateNewChurch(Person currentPerson, ChurchSettingsViewModel churchSettings)
        {
            if (currentPerson.HasPermission(common.Permissions.SystemAdministrator))
            {
                using (oikonomosEntities context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
                {
                    var newChurch = new Church();
                    context.AddToChurches(newChurch);
                    newChurch.Created = DateTime.Now;

                    PopulateChurchModel(churchSettings, newChurch);
                    newChurch.EmailLogin = "******";
                    newChurch.EmailPassword = "******";
                    PopulateChurchAddress(churchSettings, context, newChurch);
                    context.SaveChanges();

                    //Save Roles
                    var currentChurchRoles = context.Roles.Where(r => (r.ChurchId == currentPerson.ChurchId && r.Name != "System Administrator")).ToList();
                    foreach (var currentRole in currentChurchRoles)
                    {
                        var newChurchRole = new Role();
                        context.AddToRoles(newChurchRole);
                        newChurchRole.Created = DateTime.Now;
                        newChurchRole.Changed = DateTime.Now;
                        newChurchRole.Name = currentRole.Name;
                        newChurchRole.DisplayName = currentRole.DisplayName;
                        newChurchRole.ChurchId = newChurch.ChurchId;

                        foreach (var permission in currentRole.PermissionRoles)
                        {
                            var newRolePerm = new PermissionRole();
                            context.AddToPermissionRoles(newRolePerm);
                            newRolePerm.Created = DateTime.Now;
                            newRolePerm.Changed = DateTime.Now;
                            newRolePerm.PermissionId = permission.PermissionId;
                            newChurchRole.PermissionRoles.Add(newRolePerm);
                        }
                    }

                    context.SaveChanges();

                    //Update Role that can be set by any role
                    foreach (var currentRole in currentChurchRoles)
                    {
                        var newRole = context.Roles.Where(r => (r.ChurchId == newChurch.ChurchId && r.Name == currentRole.Name)).FirstOrDefault();
                        foreach (var roleToSet in currentRole.CanSetRoles)
                        {
                            if (roleToSet.Name != "System Administrator")
                            {
                                var newRoleToSet = context.Roles.FirstOrDefault(r => (r.ChurchId == newChurch.ChurchId && r.Name == roleToSet.Name));
                                newRole.CanSetRoles.Add(newRoleToSet);
                            }
                        }
                    }

                    context.SaveChanges();

                    var churchAdministrator              = new Person();
                    context.AddToPeople(churchAdministrator);
                    churchAdministrator.Created          = DateTime.Now;
                    churchAdministrator.Changed          = DateTime.Now;
                    churchAdministrator.Firstname        = churchSettings.ContactFirstname;
                    churchAdministrator.Church           = newChurch;
                    var churchAdministratorFamily        = new Family();
                    context.AddToFamilies(churchAdministratorFamily);
                    churchAdministratorFamily.FamilyName = churchSettings.ContactSurname;
                    churchAdministratorFamily.Created    = DateTime.Now;
                    churchAdministratorFamily.Changed    = DateTime.Now;
                    churchAdministrator.Family           = churchAdministratorFamily;

                    context.SaveChanges();

                    //Set the new persons role to administrator
                    churchAdministrator.RoleId = context.Roles.First(r => (r.ChurchId == newChurch.ChurchId && r.Name == "Church Administrator")).RoleId;
                    context.SaveChanges();

                    //Update Church Optional Fields
                    var churchOptionalFields = context.ChurchOptionalFields.Where(c=>c.ChurchId == currentPerson.ChurchId);
                    foreach (var co in churchOptionalFields)
                    {
                        var newCo = new ChurchOptionalField();
                        context.AddToChurchOptionalFields(newCo);
                        newCo.Created = DateTime.Now;
                        newCo.Changed = DateTime.Now;
                        newCo.ChurchId = newChurch.ChurchId;
                        newCo.OptionalFieldId = co.OptionalFieldId;
                        newCo.Visible = co.Visible;
                    }

                    context.SaveChanges();
                }
            }
        }
        public static void CreateNewChurch(Person currentPerson, ChurchSettingsViewModel churchSettings)
        {
            if (!currentPerson.HasPermission(Permissions.SystemAdministrator)) return;
            using (var context = new oikonomosEntities(ConfigurationManager.ConnectionStrings["oikonomosEntities"].ConnectionString))
            {
                var newChurch = new Church();
                context.AddToChurches(newChurch);
                newChurch.Created = DateTime.Now;

                PopulateChurchModel(churchSettings, newChurch);
                newChurch.EmailLogin = "******";
                newChurch.EmailPassword = "******";
                newChurch.Country = "South Africa";
                PopulateChurchAddress(churchSettings, context, newChurch);
                context.SaveChanges();

                //Save Roles
                var currentChurchRoles = context.Roles.Where(r => (r.ChurchId == currentPerson.ChurchId && r.Name != "System Administrator")).ToList();
                foreach (var currentRole in currentChurchRoles)
                {
                    var newChurchRole = new Role();
                    context.AddToRoles(newChurchRole);
                    newChurchRole.Created = DateTime.Now;
                    newChurchRole.Changed = DateTime.Now;
                    newChurchRole.Name = currentRole.Name;
                    newChurchRole.DisplayName = currentRole.DisplayName;
                    newChurchRole.ChurchId = newChurch.ChurchId;

                    foreach (var permission in currentRole.PermissionRoles)
                    {
                        var newRolePerm = new PermissionRole();
                        context.AddToPermissionRoles(newRolePerm);
                        newRolePerm.Created = DateTime.Now;
                        newRolePerm.Changed = DateTime.Now;
                        newRolePerm.PermissionId = permission.PermissionId;
                        newChurchRole.PermissionRoles.Add(newRolePerm);
                    }
                }

                context.SaveChanges();

                //Update Role that can be set by any role
                foreach (var currentRole in currentChurchRoles)
                {
                    var newRole = context.Roles.FirstOrDefault(r => (r.ChurchId == newChurch.ChurchId && r.Name == currentRole.Name));
                    foreach (var newRoleToSet in from roleToSet in currentRole.CanSetRoles where roleToSet.Name != "System Administrator" select context.Roles.FirstOrDefault(r => (r.ChurchId == newChurch.ChurchId && r.Name == roleToSet.Name)))
                    {
                        newRole.CanSetRoles.Add(newRoleToSet);
                    }
                }

                context.SaveChanges();

                var personAddress = new Address {Created = DateTime.Now, Changed = DateTime.Now, Line1 = string.Empty, Line2=string.Empty, Line3=string.Empty, Line4 = string.Empty};
                context.AddToAddresses(personAddress);

                var churchAdministrator              = new Person();
                context.AddToPeople(churchAdministrator);
                churchAdministrator.Created          = DateTime.Now;
                churchAdministrator.Changed          = DateTime.Now;
                churchAdministrator.Firstname        = churchSettings.ContactFirstname;
                churchAdministrator.Church           = newChurch;
                churchAdministrator.Email            = churchSettings.OfficeEmail;
                var churchAdministratorFamily        = new Family();
                context.AddToFamilies(churchAdministratorFamily);
                churchAdministratorFamily.FamilyName = churchSettings.ContactSurname;
                churchAdministratorFamily.Created    = DateTime.Now;
                churchAdministratorFamily.Changed    = DateTime.Now;
                churchAdministrator.Family           = churchAdministratorFamily;
                churchAdministrator.Family.Address   = personAddress;

                context.SaveChanges();

                //Set the new persons role to administrator
                var personChurchRecord = new PersonChurch
                    {
                        Person = churchAdministrator,
                        Church = newChurch,
                        Role = context.Roles.First(r => (r.ChurchId == newChurch.ChurchId && r.Name == "Church Administrator"))
                    };

                context.AddToPersonChurches(personChurchRecord);
                context.SaveChanges();

                //Update Church Optional Fields
                var churchOptionalFields = context.ChurchOptionalFields.Where(c=>c.ChurchId == currentPerson.ChurchId);
                foreach (var co in churchOptionalFields)
                {
                    var newCo = new ChurchOptionalField();
                    context.AddToChurchOptionalFields(newCo);
                    newCo.Created = DateTime.Now;
                    newCo.Changed = DateTime.Now;
                    newCo.ChurchId = newChurch.ChurchId;
                    newCo.OptionalFieldId = co.OptionalFieldId;
                    newCo.Visible = co.Visible;
                }

                context.SaveChanges();
            }
        }