📄 entrycomments.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
namespace CommunityServer.Blogs.Controls
{
/// <summary>
/// Summary description for EntryComments.
/// </summary>
public class EntryComments : WeblogThemedControl
{
#region Public Properties
[DefaultValue( false )]
public virtual bool HideOnNoComments
{
get
{
Object state = ViewState["HideOnNoComments"];
if(state != null)
return (bool)state;
return false;
}
set { ViewState["HideOnNoComments"] = value; }
}
#endregion
private Repeater Comments = null;
private WeblogPost parent = null;
public EntryComments()
{
//
// TODO: Add constructor logic here
//
}
private PostSet _dataSource;
/// <summary>
/// Property DataSource (object)
/// </summary>
public PostSet DataSource
{
get
{ return this._dataSource; }
set
{ this._dataSource = value; }
}
public override void DataBind()
{
this.EnsureChildControls();
if(DataSource != null && Comments !=null)
{
DataSource.Organize();
parent = DataSource.ThreadStarter as WeblogPost;
Comments.DataSource = DataSource.Replies;
Comments.DataBind();
if(DataSource.Replies.Count == 0 && HideOnNoComments)
this.Visible = false;
}
else if(HideOnNoComments)
this.Visible = false;
}
protected override void AttachChildControls()
{
Comments = FindControl( "Comments" ) as Repeater;
if(Comments == null)
{
this.Controls.Clear();
this.Visible = false;
return;
}
Comments.ItemCreated +=new RepeaterItemEventHandler(Comments_ItemCreated);
Comments.ItemCommand +=new RepeaterCommandEventHandler(Comments_ItemCommand);
}
const string anchortag = "<a name=\"{0}\"></a>";
private string Anchor(int ID)
{
return string.Format(anchortag,ID);
}
private int _anonymousUserID = -1;
private bool IsAnonymousUserID(int userID)
{
if(_anonymousUserID == -1)
_anonymousUserID = Users.GetAnonymousUser(true).UserID;
return _anonymousUserID == userID;
}
private void Comments_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if(parent == null)
return;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
WeblogPost entry = e.Item.DataItem as WeblogPost;
if(entry != null)
{
Literal title = (Literal)(e.Item.FindControl("Title"));
if(title != null)
{
// we should probably change skin format to dynamically wire up to
// skin located title and permalinks at some point
title.Text = String.Format("{0}{1}", entry.Subject, Anchor(entry.PostID));
}
HyperLink perma = (HyperLink)e.Item.FindControl("PermaLink");
perma.NavigateUrl = BlogUrls.Instance().Post(parent,CurrentWeblog,entry);
HyperLink namelink = (HyperLink)e.Item.FindControl("NameLink");
if(namelink != null)
{
if(entry.HasTitleUrl)
{
namelink.NavigateUrl = Globals.GetSiteUrls().Redirect(entry.TitleUrl);
if(IsAnonymousUserID(entry.AuthorID))
namelink.Attributes.Add("rel", "nofollow");
}
else
{
if(IsAnonymousUserID(entry.AuthorID))
namelink.NavigateUrl = Globals.GetSiteUrls().UserProfile(entry.AuthorID);
}
if(entry.BlogPostType == BlogPostType.Comment)
{
if(entry.HasSubmittedUserName)
namelink.Text = entry.SubmittedUserName;
else
namelink.Text = entry.Username;
//NOTE: Should this be the true username...we use it to allow users to know if the user is real or anonymous?
namelink.ToolTip = namelink.Text;
}
else if(entry.BlogPostType == BlogPostType.Trackback)
{
namelink.Text = entry.TrackBackName != null ? entry.TrackBackName : "TrackBack";
namelink.Attributes.Add("title","TrackBack");
}
}
Literal PostDate = (Literal)(e.Item.FindControl("PostDate"));
if(PostDate != null)
{
PostDate.Text = entry.PostDate.ToString(ThemeConfig.DateFormat);
}
Literal Post = (Literal)(e.Item.FindControl("PostText"));
if(Post != null)
{
Post.Text = entry.FormattedBody;
}
LinkButton editlink = e.Item.FindControl("EditLink") as LinkButton;
if(editlink != null)
{
if(Permissions.ValidatePermissions(this.CurrentWeblog,Permission.Post, this.CurrentUser ))
{
editlink.Text = "Remove Comment " + entry.PostID.ToString();
editlink.CommandName = entry.PostID.ToString();
editlink.Attributes.Add("onclick","return confirm(\"Are you sure you want to delete comment " + entry.PostID.ToString() + "?\");");
editlink.Visible = true;
editlink.CommandArgument = entry.PostID.ToString();
}
else
{
editlink.Visible = false;
}
}
}
}
}
private void Comments_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int entryID = Int32.Parse(e.CommandArgument as string);
WeblogPosts.Delete(CurrentWeblog.SectionID, entryID, BlogPostType.Comment, CSContext.Current.User.UserID);
//Need to do this a bit cleaner.
if(this.DataSource != null)
{
System.Collections.ArrayList items = DataSource.Replies;
for(int i = 0; i< items.Count; i++)
{
if((items[i] as WeblogPost).PostID == entryID)
{
items.RemoveAt(i);
break;
}
}
this.Comments.DataSource = items;
this.Comments.DataBind();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -