public static MvcHtmlString PrintClan(this HtmlHelper helper, Clan clan, bool colorize = true) {
     var url = Global.UrlHelper();
     if (clan == null) return new MvcHtmlString(string.Format("<a href='{0}'>No Clan</a>", url.Action("Index", "Clans")));
     {
         string color = Clan.ClanColor(clan, Global.ClanID);
         if (String.IsNullOrEmpty(color)) color = "#B0D0C0";
         return
             new MvcHtmlString(
                 string.Format("<a href='{0}' nicetitle='$clan${4}'><img src='{1}' width='16'><span style='color:{2}'>{3}</span></a>",
                               url.Action("Detail", "Clans", new { id = clan.ClanID }),
                               clan.GetImageUrl(),
                               colorize ? color : "",
                               HttpUtility.HtmlEncode(clan.Shortcut),
                               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

            //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.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());
                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.SubmitChanges(); // make sure event gets correct details
                    if (clan.FactionID != null) 
                        db.Events.InsertOnSubmit(Global.CreateEvent("Clan {0} moved to faction {1}", orgClan, orgClan.Faction));
                    else
                        db.Events.InsertOnSubmit(Global.CreateEvent("Clan {0} left faction {1}", orgClan, oldFaction));
                }
                db.SubmitChanges();
            }
            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);

                var acc = db.Accounts.Single(x => x.AccountID == Global.AccountID);
                acc.ClanID = clan.ClanID;

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

                db.SubmitChanges(); // needed to get clan id for images

                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(Global.CreateEvent("New clan {0} formed by {1}", clan, acc));
                db.SubmitChanges();
            }

            //scope.Complete();
            Global.Server.ChannelManager.AddClanChannel(clan);;
            Global.Server.SetTopic(clan.GetClanChannel(), clan.SecretTopic, Global.Account.Name);
            //}
            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

            //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);
                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 });
        }