示例#1
0
		public void MergeAndDelete(Group merge)
		{
			if (this.K==merge.K)
				throw new DsiUserFriendlyException("Can't merge group into itself!");

			Cambro.Web.Helpers.WriteAlertHeader();

			Cambro.Web.Helpers.WriteAlert("Starting group merge...", true);

			Cambro.Web.Helpers.WriteAlert("Merging group members...", true);
			//members
			Query qGu = new Query();
			qGu.QueryCondition=new Q(GroupUsr.Columns.GroupK,merge.K);
			GroupUsrSet gus = new GroupUsrSet(qGu);
			int count = 0;
			foreach (GroupUsr guMerge in gus)
			{
				if (count%10==0)
					Cambro.Web.Helpers.WriteAlert("Merging usr " + guMerge.UsrK + " ("+count+" / "+gus.Count+")...");

				try
				{

					count++;
					GroupUsr guMaster = this.GetGroupUsr(guMerge.Usr);
					if (guMaster == null)
					{
						guMaster = new GroupUsr();
						guMaster.UsrK = guMerge.UsrK;
						guMaster.GroupK = this.K;
						guMaster.Status = guMerge.Status;
						guMaster.StatusChangeDateTime = guMerge.StatusChangeDateTime;
						guMaster.StatusChangeUsrK = guMerge.StatusChangeUsrK;
						guMaster.Owner = guMerge.Owner;
						guMaster.Moderator = guMerge.Moderator;
						guMaster.NewsAdmin = guMerge.NewsAdmin;
						guMaster.MemberAdmin = guMerge.MemberAdmin;
						guMaster.MemberAdminNewUserEmails = guMerge.MemberAdminNewUserEmails;
						guMaster.Favourite = guMerge.Favourite;
						guMaster.InviteMessage = guMerge.InviteMessage;
						guMaster.InviteUsrK = guMerge.InviteUsrK;
						guMaster.Update();

						guMaster.Usr.UpdateIsGroupModerator();

					}
					else
					{
						if (guMaster.StatusPermissionLevel < guMerge.StatusPermissionLevel)
						{
							guMaster.Status = guMerge.Status;
							guMaster.StatusChangeDateTime = guMerge.StatusChangeDateTime;
							guMaster.StatusChangeUsrK = guMerge.StatusChangeUsrK;
							guMaster.InviteMessage = guMerge.InviteMessage;
							guMaster.InviteUsrK = guMerge.InviteUsrK;
						}
						if (this.BrandK == 0 || merge.BrandK > 0)
						{
							if (guMerge.Owner)
								guMaster.Owner = true;
							if (guMerge.Moderator)
								guMaster.Moderator = true;
							if (guMerge.NewsAdmin)
								guMaster.NewsAdmin = true;
							if (guMerge.MemberAdmin)
							{
								guMaster.MemberAdmin = true;
								guMaster.MemberAdminNewUserEmails = guMerge.MemberAdminNewUserEmails;
							}
						}
						if (guMerge.Favourite)
							guMaster.Favourite = true;

						guMaster.Update();

						guMaster.Usr.UpdateIsGroupModerator();
					}

					if (guMaster.IsMember)
					{
						Mailer m = new Mailer();
						m.UsrRecipient = guMerge.Usr;
						m.Subject = "A group you were in has been merged";
						m.Body = "<p><i>" + merge.FriendlyName + "</i> has been merged with <i>" + this.FriendlyName + "</i>. " +
							"Your membership details have been moved across. If you ever want to exit the group, click the button " +
							"on the <a href=\"[LOGIN(" + this.Url() + ")]\">group homepage</a>.</p>";
						m.RedirectUrl = this.Url();
						m.Bulk = true;
						m.Send();
					}

					if (guMaster.IsMember && CommentAlert.IsEnabled(guMerge.UsrK, guMerge.GroupK, Model.Entities.ObjectType.Group))
						CommentAlert.Enable(guMaster.Usr, guMaster.GroupK, Model.Entities.ObjectType.Group);
				}
				catch 
				{
					Cambro.Web.Helpers.WriteAlert("Exception! ... deleting membership for usr " + guMerge.UsrK + " (" + count + " / " + gus.Count + ")...");
					guMerge.Delete();
				}
			}
			Cambro.Web.Helpers.WriteAlert("Done merging usrs...");

			//picture
			if (merge.HasPic && !this.HasPic)
			{
				Cambro.Web.Helpers.WriteAlert("Copying picture...", true);
				try
				{
					Utilities.CopyPic(merge, this);
				}
				catch
				{
					Cambro.Web.Helpers.WriteAlert("Exception while copying picture...", true);
				}
				Cambro.Web.Helpers.WriteAlert("Done copying picture...", true);
			}
			
			//recommended events
			if (this.BrandK == 0)
			{
				Cambro.Web.Helpers.WriteAlert("Merging recommended events...", true);
				Query qEv = new Query();
				qEv.QueryCondition = new Q(GroupEvent.Columns.GroupK, merge.K);
				GroupEventSet ges = new GroupEventSet(qEv);
				foreach (GroupEvent geMerge in ges)
				{
					try
					{
						GroupEvent geMaster = new GroupEvent(this.K, geMerge.EventK);
					}
					catch (BobNotFound)
					{
						GroupEvent geMaster = new GroupEvent();
						geMaster.GroupK = this.K;
						geMaster.EventK = geMerge.EventK;
						geMaster.Update();
					}
				}
				Cambro.Web.Helpers.WriteAlert("Done merging recommended events...");
			}

			Cambro.Web.Helpers.WriteAlert("Merging top photos...", true);
			//top photos
			Query qPh = new Query();
			qPh.QueryCondition=new Q(GroupPhoto.Columns.GroupK,merge.K);
			GroupPhotoSet gps = new GroupPhotoSet(qPh);
			foreach (GroupPhoto gpMerge in gps)
			{
				try
				{
					GroupPhoto gpMaster = new GroupPhoto(this.K,gpMerge.PhotoK);
					if (!gpMaster.ShowOnFrontPage && gpMerge.ShowOnFrontPage)
					{
						gpMaster.Caption=gpMerge.Caption;
						gpMaster.DateTime=gpMerge.DateTime;
						gpMaster.AddedByUsrK=gpMerge.AddedByUsrK;
						gpMaster.ShowOnFrontPage=gpMerge.ShowOnFrontPage;
						gpMaster.Update();
					}
				}
				catch (BobNotFound)
				{
					GroupPhoto gpMaster = new GroupPhoto();
					gpMaster.GroupK=this.K;
					gpMaster.PhotoK=gpMerge.PhotoK;
					gpMaster.Caption=gpMerge.Caption;
					gpMaster.DateTime=gpMerge.DateTime;
					gpMaster.AddedByUsrK=gpMerge.AddedByUsrK;
					gpMaster.ShowOnFrontPage=gpMerge.ShowOnFrontPage;
					gpMaster.Update();
				}
			}
			Cambro.Web.Helpers.WriteAlert("Done merging top photos...");

			//addedbyusrk's
			if (true)
			{
				Cambro.Web.Helpers.WriteAlert("Updating invited users...", true);
				Update update = new Update();
				update.Changes.Add(new Assign(Usr.Columns.AddedByGroupK, this.K));
				update.Table = TablesEnum.Usr;
				update.Where = new Q(Usr.Columns.AddedByGroupK, merge.K);
				update.Run();
				Cambro.Web.Helpers.WriteAlert("Done updating invited users...");
			}

			Cambro.Web.Helpers.WriteAlert("Merging topics (1/4)...", true);
			//chats
			if (merge.PrivateChat != this.PrivateChat ||
				merge.ThemeK != this.ThemeK ||
				merge.MusicTypeK != this.MusicTypeK)
			{
				Update update = new Update();
				update.Table=TablesEnum.Thread;
				update.Changes.Add(new Assign(Thread.Columns.PrivateGroup,this.PrivateChat));
				update.Changes.Add(new Assign(Thread.Columns.ThemeK, this.ThemeK));
				update.Changes.Add(new Assign(Thread.Columns.MusicTypeK, this.MusicTypeK));
				update.Where=new Q(Thread.Columns.GroupK,merge.K);
				update.Run();
			}
			Cambro.Web.Helpers.WriteAlert("Done merging topics (1/4)...");

			if (true)
			{
				Cambro.Web.Helpers.WriteAlert("Merging topics (2/4)...", true);
				Update update = new Update();
				update.Table=TablesEnum.Thread;
				update.Changes.Add(new Assign(Thread.Columns.UrlFragment,"groups/"+this.UrlName));
				update.Changes.Add(new Assign(Thread.Columns.CountryK, this.CountryK));
				update.Changes.Add(new Assign(Thread.Columns.PlaceK,this.PlaceK));
				update.Where=new And(new Q(Thread.Columns.ParentObjectType,Model.Entities.ObjectType.Group),new Q(Thread.Columns.ParentObjectK,merge.K));
				update.Run();
				Cambro.Web.Helpers.WriteAlert("Done merging topics (2/4)...");
			}

			if (true)
			{
				Cambro.Web.Helpers.WriteAlert("Merging topics (3/4)...", true);
				Update update = new Update();
				update.Table=TablesEnum.Thread;
				update.Changes.Add(new Assign(Thread.Columns.GroupK,this.K));
				update.Where=new Q(Thread.Columns.GroupK,merge.K);
				update.Run();
				Cambro.Web.Helpers.WriteAlert("Done merging topics (3/4)...");
			}
			
			if (true)
			{
				Cambro.Web.Helpers.WriteAlert("Merging topics (4/4)...", true);
				Update update = new Update();
				update.Table=TablesEnum.Thread;
				update.Changes.Add(new Assign(Thread.Columns.ParentObjectK,this.K));
				update.Where=new And(new Q(Thread.Columns.ParentObjectType,Model.Entities.ObjectType.Group),new Q(Thread.Columns.ParentObjectK,merge.K));
				update.Run();
				Cambro.Web.Helpers.WriteAlert("Done merging topics (4/4)...");
			}

			Cambro.Web.Helpers.WriteAlert("Deleting old group...", true);
			merge.DeleteAll(null);
			Cambro.Web.Helpers.WriteAlert("Done deleting old group...");

			Cambro.Web.Helpers.WriteAlert("Updating stats...", true);
			this.UpdateTotalComments(null);
			this.UpdateTotalMembers();
			Cambro.Web.Helpers.WriteAlert("Done updating stats...");

			Cambro.Web.Helpers.WriteAlert("Finished merging groups.", true);


		}