⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commentlisting.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class CommentListing : GalleryThemedControl
	{

		#region Child Controls

		private Repeater Comments;

		private TextBox CommentName;
		private TextBox CommentWebsite;
		private TextBox CommentBody;
		private Button CommentSubmit;

		#endregion

		#region Public Properties

		private Picture dataSource = null;
		public Picture DataSource
		{
			get 
            { 
                // Pull source picture from cache on post back
                if(dataSource == null)
                    dataSource = Pictures.GetPicture(CSContext.Current.PostID);

                return dataSource; 
            }
			set { dataSource = value; }
		}


		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			Comments = (Repeater)FindControl( "Comments" );

			CommentName = (TextBox)FindControl( "CommentName" );
			CommentWebsite = (TextBox)FindControl( "CommentWebsite" );
			CommentBody = (TextBox)FindControl( "CommentBody" );
			CommentSubmit = (Button)FindControl( "CommentSubmit" );

			InitializeChildControls();
		}

		private void InitializeChildControls()
		{
			Comments.ItemDataBound += new RepeaterItemEventHandler(Comments_ItemDataBound);
			Comments.ItemCommand += new RepeaterCommandEventHandler(Comments_ItemCommand);
			CommentSubmit.Click += new EventHandler(CommentSubmit_Click);
		}

		#endregion

		public override void DataBind()
		{
			base.DataBind ();
			BindComments();
		}
		
		private void BindComments()
		{
			bool allowUnapproved = false;

			// Check if comments are enabled
			this.Visible = DataSource.EnableComments;

			// Check if anonymous comments are allowed and if the current user is anonymous
			GalleryPermission permission = CurrentGallery.ResolvePermission( CSContext.Current.User ) as GalleryPermission;
			if(Pictures.GetPicture(CSContext.Current.PostID).IsLocked || !permission.Reply)
				FindControl( "AddCommentArea" ).Visible = false;

			// Add some resource labels
			CommentSubmit.Text = ResourceManager.GetString( "Add" );
			if(CurrentGallery.IsModerated)
				CommentSubmit.Attributes.Add("onclick", "alert('" + ResourceManager.GetString("Gallery_CommentListing_ModerationAlert").Replace("'", "\'") + "');" );

			if(CurrentUser.IsGalleryAdministrator || Permissions.ValidatePermissions( CurrentGallery, Permission.Moderate, CurrentUser ))
				allowUnapproved = true;

			Comments.DataSource = GalleryComments.GetComments(DataSource.PostID, allowUnapproved);
			Comments.DataBind();

			if(CSContext.Current.User.IsAnonymous)
			{
				FindControl( "AnonymousUser" ).Visible = true;
				FindControl( "RegisteredUser" ).Visible = false;
			}
			else
			{
				FindControl( "AnonymousUser" ).Visible = false;
				FindControl( "RegisteredUser" ).Visible = true;
				((Literal)FindControl( "UserName" )).Text = CSContext.Current.User.Username;
			}
		}

		private void Comments_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
		    GalleryComment dataItem = e.Item.DataItem as GalleryComment;

			switch( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:

					PlaceHolder Who = (PlaceHolder)e.Item.FindControl( "ComWho" );
					Literal Date = (Literal)e.Item.FindControl( "ComDate" );
					Literal Text = (Literal)e.Item.FindControl( "ComBody" );
					LinkButton Approve = (LinkButton)e.Item.FindControl( "ApproveButton" );
					LinkButton Delete = (LinkButton)e.Item.FindControl( "DeleteButton" );

					Gallery gallery = Galleries.GetGallery(dataItem.SectionID);

					Date.Text = dataItem.PostDate.ToString();
					Text.Text = dataItem.FormattedBody;

					// Configure the link to approve the post
					if(!dataItem.IsApproved)
					{
						Approve.Visible = true;
						Approve.Text = ResourceManager.GetString( "Gallery_CommentListing_Approve" );
						Approve.CommandName = "ApproveComment";
						Approve.CommandArgument = dataItem.PostID.ToString();
					}
					else
						Approve.Visible = false;

					// Configure the link to remove comments
					if(CSContext.Current.User.IsGalleryAdministrator || Permissions.ValidatePermissions( gallery, Permission.Moderate | Permission.Delete, CSContext.Current.User ))
					{
						Delete.Visible = true;
						Delete.Text = ResourceManager.GetString( "Gallery_CommentListing_Delete" );
						Delete.CommandName = "DeleteComment";
						Delete.CommandArgument = dataItem.PostID.ToString();
					}
					else
						Delete.Visible = false;


					// Decide what info to show for the user
					if(!dataItem.User.IsAnonymous)
					{
						// Was a logged in user
						HyperLink UserLink = new HyperLink();
						UserLink.Text = dataItem.User.Username;
						UserLink.NavigateUrl = Globals.GetSiteUrls().UserProfile(dataItem.User.UserID);
						Who.Controls.Add( UserLink );
					}
					else
					{
						// Use was anonymous, show the name they specified
						if(dataItem.GetExtendedAttribute("Website") != string.Empty)
						{
							HyperLink WebsiteName = new HyperLink();
							WebsiteName.Text = dataItem.GetExtendedAttribute("Name");
							WebsiteName.NavigateUrl = dataItem.GetExtendedAttribute("Website");
                            WebsiteName.Attributes.Add("rel", "nofollow");
							Who.Controls.Add( WebsiteName );
						}
						else
						{
							Label Name = new Label();
							Name.Text = dataItem.GetExtendedAttribute("Name");
							Who.Controls.Add( Name );
						}
					}
					break;
			}
		}

		private void CommentSubmit_Click(object sender, EventArgs e)
		{
		    GalleryComment comment = new GalleryComment();

			// Make sure the comment body isn't blank
			if(CommentBody.Text.Trim() == string.Empty)
			{
				FindControl( "CommentBodyErrMsg" ).Visible = true;
				Page.RegisterClientScriptBlock( "commentError", "<script language='JavaScript'>alert('Please include some content in the comment body');</script>" );
				return;
			}

			// Apply all of the settings
			comment.User = CSContext.Current.User;
			comment.Username = comment.User.Username;
			comment.Body = CommentBody.Text;
			comment.UserHostAddress = Page.Request.UserHostAddress;

			// See if posts are moderated
			if(CurrentGallery.ModerateComments)
			{
				if(Permissions.ValidatePermissions( CurrentGallery, Permission.Moderate, comment.User ))
					comment.IsApproved = true;
				else
					comment.IsApproved = false;
			}
			else
				comment.IsApproved = true;

			// Put "Anonymous" in the name if the user is anonymous and they don't give a name
			if(CSContext.Current.User.IsAnonymous && (CommentName.Text.Trim() == string.Empty))
				comment.SetExtendedAttribute("Name", comment.User.Username);
			else
				comment.SetExtendedAttribute("Name", CommentName.Text);

			// Add check for http:// in the website URL
			if(CommentWebsite.Text.StartsWith("http://"))
				comment.SetExtendedAttribute("Website", CommentWebsite.Text);
			else
				comment.SetExtendedAttribute("Website", "http://" + CommentWebsite.Text);

			// Send to the database
			comment = GalleryComments.CreateComment(DataSource.PostID, comment);

			// Email the user
			GalleryEmails.NewComment(CurrentGallery.ApplicationKey, -1, DataSource.PostID, comment);

			// Redirect to the page so user's can hit refresh and have the post show up twice
			Picture picture = Pictures.GetPicture(DataSource.PostID);
			Context.Response.Redirect( GalleryUrls.Instance().ViewPicture(CurrentGallery.ApplicationKey, picture) );
		}

		private void Comments_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			switch(e.CommandName)
			{
				case "ApproveComment":
					GalleryComments.ApproveComment(int.Parse((string)e.CommandArgument));
					Page.DataBind();
					break;
				case "DeleteComment":
					GalleryComments.DeleteComment(int.Parse((string)e.CommandArgument));
					Page.DataBind();
					break;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -