增加CCS1.1的Blog日志访问者记录功能
我相信大家都会想知道有那些人看过了自己的“杰作”,CCS中原来记录了PV(PageView)的来源,通过修改我们可以实现访问者跟踪功能。
一、修改CCS源码
1)打开\src\Components\Components\Referral.cs(访问记录实体类),添加代码
private string _visitor; /// /// getter and setter for Visitor name /// public string Visitor { get { return this._visitor; } set { this._visitor = value; } }
2)打开\src\blogs\controls\entryviewcontainer.cs,修改void BindData(),goto line75把
Referral r = new Referral(); r.SettingsID = CSContext.Current.SiteSettings.SettingsID; r.SectionID = we.SectionID; r.PostID = we.PostID; Referrals.AddToQueue(Context,CurrentWeblog.ReferralFilter,r);
改为
Referral r = new Referral(); r.SettingsID = CSContext.Current.SiteSettings.SettingsID; r.SectionID = we.SectionID; r.PostID = we.PostID; r.Vistor = CSContext.Current.UserName; // record current visitor name Referrals.AddToQueue(Context,CurrentWeblog.ReferralFilter,r);
3)打开src\data providers\sqldataprovider\sqlcommondataprovider.cs,修改public override void SaveReferralList(ArrayList referrals)为
public override void SaveReferralList(ArrayList referrals) { using( SqlConnection connection = GetSqlConnection() ) { SqlCommand command = new SqlCommand( this.databaseOwner + ".cs_referrals_Add", connection); // Mark as stored procedure command.CommandType = CommandType.StoredProcedure; // Add parameters command.Parameters.Add("@SettingsID", SqlDbType.Int, 4); command.Parameters.Add("@SectionID", SqlDbType.Int, 4); command.Parameters.Add("@PostID", SqlDbType.Int, 4); command.Parameters.Add("@UrlID", SqlDbType.Int, 4); command.Parameters.Add("@Url", SqlDbType.NVarChar, 512); command.Parameters.Add("@Visitor", SqlDbType.NVarChar, 64); // visitor name parameter connection.Open(); foreach(Referral referral in referrals) { command.Parameters["@SettingsID"].Value = referral.SettingsID; command.Parameters["@SectionID"].Value = referral.SectionID; command.Parameters["@PostID"].Value = referral.PostID; command.Parameters["@UrlID"].Value = referral.UrlID; command.Parameters["@Url"].Value = referral.Url; command.Parameters["@Visitor"].Value = referral.Vistor; command.ExecuteNonQuery(); } connection.Close(); command.Dispose(); connection.Dispose(); } }
修改public override ReferralSet GetReferrals(Referral r, int pageSize, int pageIndex),把while(reader.read())代码段改为
while(reader.Read()) { referral = new Referral(); referral.PostID = (int)reader["PostID"]; referral.SectionID = (int)reader["SectionID"]; referral.LastDate = (DateTime)reader["LastDate"]; referral.Hits = (int)reader["Hits"]; referral.Url = reader["Url"] as string; referral.Title = reader["Subject"] as string; referral.Vistor = reader["Visitor"] as string; rs.Referrals.Add(referral); }
4)打开\src\themes\default\skins\blog\Skin-WebReferralList.ascx,在html代码
<th class="column" width="*"><CS:ResourceControl runat="server" ResourceName="Referrals_Url" ID="Resourcecontrol1"/>th>
下面插入
<th class="column" width="150px"><CS:ResourceControl runat="server" ResourceName="Referrals_Visitor" ID="Resourcecontrol5"/>th>
然后修改为
<ItemTemplate> <tr> <td><asp:HyperLink ID = "ReferralLink" Runat = "Server" />td> <td align="center"><asp:Literal ID = "Visitor" Runat = "Server" />td> <td><asp:HyperLink ID = "PostLink" Runat = "Server" /> <td align="center"><asp:Literal ID = "ReferralHits" Runat = "server" />td> <td align="center"><asp:Literal ID = "ReferralDate" Runat = "server" />td> tr> ItemTemplate>
5)打开\src\blogs\controls\admin\webblogreferrallist.ascx,修改private void referralRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)为
private void referralRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Referral r = e.Item.DataItem as Referral; if(r != null) { HyperLink link = e.Item.FindControl("ReferralLink") as HyperLink; Literal hits = e.Item.FindControl("ReferralHits") as Literal; Literal last = e.Item.FindControl("ReferralDate") as Literal; Literal visitor = e.Item.FindControl("Visitor") as Literal; // add the visitor columnHyperLink sl = e.Item.FindControl("PostLink") as HyperLink; if(r.Title.Length <= 100) sl.Text = r.Title; else sl.Text = r.Title.Substring(0,100); sl.NavigateUrl = BlogUrls.Instance().ShortLink(r.PostID); if(r.Url.Length <= 100) link.Text = Context.Server.HtmlEncode(r.Url); else link.Text = Context.Server.HtmlEncode(r.Url.Substring(0,100)); link.NavigateUrl = r.Url; visitor.Text = r.Vistor; // bind the visitor column hits.Text = r.Hits.ToString(); last.Text = r.LastDate.ToString(DateFormat); } } }
6)修改相应Language的Resource.xml文件,添加Referrals_Visitor字符串
二、修改数据库
1)修改cs_referrals数据表,添加Visitor列,属性为nvarchar(64), allow null,或直接执行
ALTER TABLE cs_Referrals ADD Visitor nvarchar(64) NULL
2)修改存储过程cs_referrals_Add,可直接执行如下sql语句
ALTER Proc dbo.cs_referrals_Add ( @UrlID int, @Url nvarchar(512), @SettingsID int, @SectionID int, @PostID int, @Visitor nvarchar(64) ) as SET Transaction Isolation Level Read UNCOMMITTED exec cs_Urls_Add @UrlID, @Url Declare @ReferralID int Select @ReferralID = ReferralID FROM cs_Referrals where SettingsID = @SettingsID and SectionID = @SectionID and UrlID = @UrlID if(@ReferralID is null) Begin Insert cs_Referrals (SettingsID, SectionID, PostID, UrlID, Hits, LastDate, Visitor) Values (@SettingsID, @SectionID, @PostID, @UrlID, 1, getdate(), @Visitor) End Else Begin Update cs_Referrals Set Hits = Hits + 1, LastDate = getdate() Where ReferralID = @ReferralID End
3)修改cs_referrals_Get,直接执行下面的sql语句
ALTER Proc dbo.cs_referrals_Get ( @SettingsID int, @SectionID int, @PostID int, @PageSize int, @PageIndex int, @TotalRecords int output ) as SET Transaction Isolation Level Read UNCOMMITTED DECLARE @RowsToReturn int DECLARE @PageLowerBound int DECLARE @PageUpperBound int SET @RowsToReturn = @PageSize * (@PageIndex + 1) SET ROWCOUNT @RowsToReturn SET @PageLowerBound = @PageSize * @PageIndex SET @PageUpperBound = @PageLowerBound + @PageSize + 1 CREATE TABLE #PageIndex ( IndexID int IDENTITY (1, 1) NOT NULL, ReferralID int ) if @PostID <> -1 Begin Insert #PageIndex (ReferralID) Select ReferralID FROM cs_Referrals where SettingsID = @SettingsID and SectionID = @SectionID and PostID = @PostID order by LastDate desc -- Reset rowcount and get count of total records SET ROWCOUNT 0 Select @TotalRecords = count(*) From cs_Referrals Where SettingsID = @SettingsID and SectionID = @SectionID and PostID = @PostID End Else Begin Insert #PageIndex (ReferralID) Select ReferralID FROM cs_Referrals where SettingsID = @SettingsID and SectionID = @SectionID order by ReferralID -- Reset rowcount and get count of total records SET ROWCOUNT 0 Select @TotalRecords = count(*) From cs_Referrals Where SettingsID = @SettingsID and SectionID = @SectionID End Select cs_Posts.Subject, cs_Referrals.ReferralID, cs_Referrals.SettingsID, cs_Referrals.SectionID, cs_Posts.PostID, Url, Hits, LastDate, cs_Referrals.Visitor FROM cs_Referrals, cs_Urls, #PageIndex, cs_Posts Where cs_Referrals.ReferralID = #PageIndex.ReferralID and cs_Urls.UrlID = cs_Referrals.UrlID and #PageIndex.IndexID > @PageLowerBound AND #PageIndex.IndexID < @PageUpperBound AND cs_Posts.PostID = cs_Referrals.PostID Order by IndexID --Select @TotalRecords = Count(*) FROM #PageIndex DROP Table #PageIndex
适用于CCS1.1,学习CCS中,感谢宝玉~~
All the posts in this blog are provided "AS IS" with no warranties, and confer no rights. Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 2.5 China Mainland License.