public ActionResult SubmitCreate(Clan clan, HttpPostedFileBase image, HttpPostedFileBase bgimage, bool noFaction = false)
        {
            //using (var scope = new TransactionScope())
            //{
            ZkDataContext db      = new ZkDataContext();
            bool          created = clan.ClanID == 0; // existing clan vs creation

            //return Content(noFaction ? "true":"false");
            if (noFaction)
            {
                clan.FactionID = null;
            }

            var new_faction = db.Factions.SingleOrDefault(x => x.FactionID == clan.FactionID);

            if ((new_faction != null) && new_faction.IsDeleted)
            {
                return(Content("Cannot create clans in deleted factions"));
            }

            Clan orgClan = null;

            if (!created)
            {
                if (!Global.Account.HasClanRight(x => x.RightEditTexts) || clan.ClanID != Global.Account.ClanID)
                {
                    return(Content("Unauthorized"));
                }
                orgClan             = db.Clans.Single(x => x.ClanID == clan.ClanID);
                orgClan.ClanName    = clan.ClanName;
                orgClan.Shortcut    = clan.Shortcut;
                orgClan.Description = clan.Description;
                orgClan.SecretTopic = clan.SecretTopic;
                orgClan.Password    = clan.Password;
            }
            else
            {
                if (Global.Clan != null)
                {
                    return(Content("You already have a clan"));
                }
                // should just change their faction for them?
                if (Global.FactionID != 0 && Global.FactionID != clan.FactionID)
                {
                    return(Content("Clan must belong to same faction you are in"));
                }
            }
            if (string.IsNullOrEmpty(clan.ClanName) || string.IsNullOrEmpty(clan.Shortcut))
            {
                return(Content("Name and shortcut cannot be empty!"));
            }
            if (!ZkData.Clan.IsShortcutValid(clan.Shortcut))
            {
                return(Content("Shortcut must have at least 1 characters and contain only numbers and letters"));
            }

            // check if our name or shortcut conflicts with existing clans
            // if so, allow us to create a new clan over it if it's a deleted clan, else block action
            var existingClans = db.Clans.Where(x => ((SqlFunctions.PatIndex(clan.Shortcut, x.Shortcut) > 0 || SqlFunctions.PatIndex(clan.ClanName, x.ClanName) > 0) && x.ClanID != clan.ClanID));

            if (existingClans.Count() > 0)
            {
                if (existingClans.Any(x => !x.IsDeleted))
                {
                    return(Content("Clan with this shortcut or name already exists"));
                }
                if (created)
                {
                    Clan deadClan = existingClans.First();
                    clan = deadClan;
                    if (noFaction)
                    {
                        clan.FactionID = null;
                    }
                }
            }
            else if (created)
            {
                db.Clans.InsertOnSubmit(clan);
            }

            if (created && (image == null || image.ContentLength == 0))
            {
                return(Content("A clan image is required"));
            }
            if (image != null && image.ContentLength > 0)
            {
                var im = Image.FromStream(image.InputStream);
                if (im.Width != 64 || im.Height != 64)
                {
                    im = im.GetResized(64, 64, InterpolationMode.HighQualityBicubic);
                }
                db.SubmitChanges(); // needed to get clan id for image url - stupid way really
                im.Save(Server.MapPath(clan.GetImageUrl()));
            }
            if (bgimage != null && bgimage.ContentLength > 0)
            {
                var im = Image.FromStream(bgimage.InputStream);
                db.SubmitChanges(); // needed to get clan id for image url - stupid way really
                // DW - Actually its not stupid, its required to enforce locking.
                // It would be possbile to enforce a pre-save id
                im.Save(Server.MapPath(clan.GetBGImageUrl()));
            }
            db.SubmitChanges();

            if (created) // we created a new clan, set self as founder and rights
            {
                var acc = db.Accounts.Single(x => x.AccountID == Global.AccountID);
                acc.ClanID = clan.ClanID;

                var leader = db.RoleTypes.FirstOrDefault(x => x.RightKickPeople && x.IsClanOnly);
                if (leader != null)
                {
                    db.AccountRoles.InsertOnSubmit(new AccountRole()
                    {
                        AccountID    = acc.AccountID,
                        Clan         = clan,
                        RoleType     = leader,
                        Inauguration = DateTime.UtcNow
                    });
                }

                db.Events.InsertOnSubmit(Global.CreateEvent("New clan {0} formed by {1}", clan, acc));
            }
            else
            {
                if (clan.FactionID != orgClan.FactionID)   // set factions of members
                {
                    var originalID = orgClan.FactionID;
                    orgClan.FactionID = clan.FactionID;     // <- not possible to change faction // now it is!
                    if (orgClan.Faction != null && orgClan.Faction.IsDeleted)
                    {
                        orgClan.FactionID = originalID;
                        throw new ApplicationException("You cannot join deleted faction");
                    }
                    db.Events.InsertOnSubmit(Global.CreateEvent("Clan {0} moved to faction {1}", clan, orgClan.Faction));

                    db.SubmitAndMergeChanges();

                    foreach (Account member in orgClan.Accounts)
                    {
                        if (member.FactionID != clan.FactionID && member.FactionID != null)
                        {
                            FactionsController.PerformLeaveFaction(member.AccountID, true, db);
                        }
                        member.Faction = orgClan.Faction;
                    }
                }
            }

            db.SubmitChanges();
            //scope.Complete();
            Global.Nightwatch.Tas.AdminSetTopic(clan.GetClanChannel(), clan.SecretTopic);
            Global.Nightwatch.Tas.AdminSetChannelPassword(clan.GetClanChannel(), clan.Password);
            //}
            return(RedirectToAction("Detail", new { id = clan.ClanID }));
        }
        public ActionResult SubmitCreate(Clan clan, HttpPostedFileBase image, HttpPostedFileBase bgimage, bool noFaction = false)
        {
            //using (var scope = new TransactionScope())
            //{
            ZkDataContext db      = new ZkDataContext();
            bool          created = clan.ClanID == 0; // existing clan vs creation
            var           acc     = db.Accounts.Single(x => x.AccountID == Global.AccountID);

            //return Content(noFaction ? "true":"false");
            if (noFaction)
            {
                clan.FactionID = null;
            }

            Faction new_faction = db.Factions.SingleOrDefault(x => x.FactionID == clan.FactionID);

            if ((new_faction != null) && new_faction.IsDeleted)
            {
                return(Content("Cannot create clans in deleted factions"));
            }

            if (string.IsNullOrEmpty(clan.ClanName) || string.IsNullOrEmpty(clan.Shortcut))
            {
                return(Content("Name and shortcut cannot be empty!"));
            }
            if (!ZkData.Clan.IsShortcutValid(clan.Shortcut))
            {
                return(Content("Shortcut must have at least 1 characters and contain only numbers and letters"));
            }

            if (created && (image == null || image.ContentLength == 0))
            {
                return(Content("A clan image is required"));
            }

            Clan orgClan = null;

            if (!created)
            {
                if (!Global.IsModerator && (!Global.Account.HasClanRight(x => x.RightEditTexts) || clan.ClanID != Global.Account.ClanID))
                {
                    return(Content("Unauthorized"));
                }

                // check if our name or shortcut conflicts with existing clans
                var existingClans = db.Clans.Where(x => ((SqlFunctions.PatIndex(clan.Shortcut, x.Shortcut) > 0 || SqlFunctions.PatIndex(clan.ClanName, x.ClanName) > 0) && x.ClanID != clan.ClanID));
                if (existingClans.Count() > 0)
                {
                    if (existingClans.Any(x => !x.IsDeleted))
                    {
                        return(Content("Clan with this shortcut or name already exists"));
                    }
                }

                orgClan = db.Clans.Single(x => x.ClanID == clan.ClanID);
                string orgImageUrl   = Server.MapPath(orgClan.GetImageUrl());
                string orgBGImageUrl = Server.MapPath(orgClan.GetBGImageUrl());
                string orgShortcut   = orgClan.Shortcut;
                string newImageUrl   = Server.MapPath(clan.GetImageUrl());
                string newBGImageUrl = Server.MapPath(clan.GetBGImageUrl());

                if (Global.IsModerator && (!Global.Account.HasClanRight(x => x.RightEditTexts) || clan.ClanID != Global.Account.ClanID))
                {
                    Global.Server.GhostChanSay(GlobalConst.ModeratorChannel, string.Format("{0} edited clan {1} {2}", Global.Account.Name, orgClan.ClanName, Url.Action("Detail", "Clans", new { id = clan.ClanID }, "http")));
                    if (orgClan.ClanName != clan.ClanName)
                    {
                        Global.Server.GhostChanSay(GlobalConst.ModeratorChannel, string.Format("{0} => {1}", orgClan.ClanName, clan.ClanName));
                    }
                }

                orgClan.ClanName    = clan.ClanName;
                orgClan.Shortcut    = clan.Shortcut;
                orgClan.Description = clan.Description;
                orgClan.SecretTopic = clan.SecretTopic;
                orgClan.Password    = clan.Password;
                bool shortcutChanged = orgShortcut != clan.Shortcut;

                if (image != null && image.ContentLength > 0)
                {
                    var im = Image.FromStream(image.InputStream);
                    if (im.Width != 64 || im.Height != 64)
                    {
                        im = im.GetResized(64, 64, InterpolationMode.HighQualityBicubic);
                    }
                    im.Save(newImageUrl);
                }
                else if (shortcutChanged)
                {
                    //if (System.IO.File.Exists(newImageUrl)) System.IO.File.Delete(newImageUrl);
                    //System.IO.File.Move(orgImageUrl, newImageUrl);
                    try {
                        //var im = Image.FromFile(orgImageUrl);
                        //im.Save(newImageUrl);
                        System.IO.File.Copy(orgImageUrl, newImageUrl, true);
                    } catch (System.IO.FileNotFoundException fnfex) // shouldn't happen but hey
                    {
                        return(Content("A clan image is required"));
                    }
                }

                if (bgimage != null && bgimage.ContentLength > 0)
                {
                    var im = Image.FromStream(bgimage.InputStream);
                    im.Save(newBGImageUrl);
                }
                else if (shortcutChanged)
                {
                    //if (System.IO.File.Exists(newBGImageUrl)) System.IO.File.Delete(newBGImageUrl);
                    //System.IO.File.Move(orgBGImageUrl, newBGImageUrl);
                    try
                    {
                        //var im = Image.FromFile(orgBGImageUrl);
                        //im.Save(newBGImageUrl);
                        System.IO.File.Copy(orgBGImageUrl, newBGImageUrl, true);
                    }
                    catch (System.IO.FileNotFoundException fnfex)
                    {
                        // there wasn't an original background image, do nothing
                    }
                }

                if (clan.FactionID != orgClan.FactionID)
                {
                    // set factions of members
                    Faction oldFaction = orgClan.Faction;
                    orgClan.FactionID = clan.FactionID;
                    foreach (Account member in orgClan.Accounts)
                    {
                        if (member.FactionID != clan.FactionID && member.FactionID != null)
                        {
                            FactionsController.PerformLeaveFaction(member.AccountID, true, db);
                        }
                        member.FactionID = clan.FactionID;
                    }
                    db.SaveChanges();
                    if (clan.FactionID != null)
                    {
                        db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("Clan {0} moved to faction {1}", orgClan, orgClan.Faction));
                    }
                    else
                    {
                        db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("Clan {0} left faction {1}", orgClan, oldFaction));
                    }
                }
                db.SaveChanges();
            }
            else
            {
                if (Global.Clan != null)
                {
                    return(Content("You already have a clan"));
                }
                // should just change their faction for them?
                if (Global.FactionID != 0 && Global.FactionID != clan.FactionID)
                {
                    return(Content("Clan must belong to same faction you are in"));
                }

                // check if our name or shortcut conflicts with existing clans
                // if so, allow us to create a new clan over it if it's a deleted clan, else block action
                var existingClans = db.Clans.Where(x => ((SqlFunctions.PatIndex(clan.Shortcut, x.Shortcut) > 0 || SqlFunctions.PatIndex(clan.ClanName, x.ClanName) > 0) && x.ClanID != clan.ClanID));
                if (existingClans.Count() > 0)
                {
                    if (existingClans.Any(x => !x.IsDeleted))
                    {
                        return(Content("Clan with this shortcut or name already exists"));
                    }
                    Clan deadClan  = existingClans.First();
                    Clan inputClan = clan;
                    clan = deadClan;
                    if (noFaction)
                    {
                        clan.FactionID = null;
                    }
                    clan.IsDeleted   = false;
                    clan.ClanName    = inputClan.ClanName;
                    clan.Password    = inputClan.Password;
                    clan.Description = inputClan.Description;
                    clan.SecretTopic = inputClan.SecretTopic;
                }
                else
                {
                    db.Clans.InsertOnSubmit(clan);
                }

                db.SaveChanges();

                acc.ClanID = clan.ClanID;

                // we created a new clan, set self as founder and rights
                AddClanLeader(acc.AccountID, clan.ClanID, db);

                db.SaveChanges();

                if (image != null && image.ContentLength > 0)
                {
                    var im = Image.FromStream(image.InputStream);
                    if (im.Width != 64 || im.Height != 64)
                    {
                        im = im.GetResized(64, 64, InterpolationMode.HighQualityBicubic);
                    }
                    im.Save(Server.MapPath(clan.GetImageUrl()));
                }
                if (bgimage != null && bgimage.ContentLength > 0)
                {
                    var im = Image.FromStream(bgimage.InputStream);
                    im.Save(Server.MapPath(clan.GetBGImageUrl()));
                }

                db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("New clan {0} formed by {1}", clan, acc));
                db.SaveChanges();
            }

            Global.Server.PublishAccountUpdate(acc);
            //scope.Complete();
            Global.Server.ChannelManager.AddClanChannel(clan);;
            Global.Server.SetTopic(clan.GetClanChannel(), clan.SecretTopic, Global.Account.Name);
            //}
            return(RedirectToAction("Detail", new { id = clan.ClanID }));
        }