td id="1" class="controlpaneltabinactive" align="center" nowrap> <a href="<%=globals.getsiteurls().myfavorites%>"><%=resourcemanager.getstring("myfavorites_title")%></a> </td>修改: <td colspan=11 class="controlpaneltabline"><img width="1" height=1 alt=""></td>跨跃列数五.增加相应文件表现层1,收藏夹主视图在web/user/目录增加myfavorites.aspx,最终用户页面在controls/views目录增加myfavoritesview.cs,页面视图服务器控件(主要表现为页面处理逻辑)界面视图:在web/themes/default kins中增加view-myfavorites.ascx 收藏夹视图(主要表现为页面ui)组件在components目录增加favorites.cs(相当于业务逻辑层,加s表业务处理),此例中未在子目录components/components中增加favorite.cs(相当于业务实体层,未加s表实体),因并不需要,完整的asp.net forums模式应该还有这一层。表现层2,用户点击收藏按钮后呈现的ui(这个比较简单)在web目录增加myfavoritesadd.aspx文件处理加入收藏时服务器控件, 在controls目录增加myfavoritesadd.cs(页面处理逻辑)在web/themes/default kins中增加skin-myfavoritesadd.ascx将主题加入收藏时的视图(ui)六.数据库增加表forums_favoritesuserid int 4 0threadid int 4 0favoritedate datetime 8 0创建存储过程forums_favorites_createdeletecreate procedure forums_favorites_createdelete( @userid int, @threadid int, @action int)asbeginif @action = 0begin -- does the user already have the ability to see this thread? if exists (select userid from forums_favorites where userid = @userid and threadid = @threadid) return insert into forums_favorites values ( @userid, @threadid, getdate() ) returnendif @action = 2begin delete forums_favorites where userid = @userid and threadid = @threadid returnendendgoset quoted_identifier off goset ansi_nulls on go七.数据处理1.components/provider/forumsdataprovider.cs增加#region 收藏夹 public abstract void createfavorites(arraylist users, int threadid); public abstract void deletefavorites(int userid, arraylist deletelist); #endregion2. data providers qldataprovider qldataprovider.cs增加实现方法#region #### 收藏夹 #### by venjiang 0912 /// <summary> /// 追加主题到收藏夹 /// </summary> /// <param name="userid">用户id</param> /// <param name="threadid">主题id</param> public override void createfavorites(int userid,int threadid) { using( sqlconnection myconnection = getsqlconnection() ) { sqlcommand mycommand = new sqlcommand(databaseowner + ".forums_favorites_createdelete", myconnection); mycommand.commandtype = commandtype.storedprocedure; mycommand.parameters.add("@action", sqldbtype.bit).value = dataprovideraction.create; mycommand.parameters.add("@userid", sqldbtype.int); mycommand.parameters.add("@threadid", sqldbtype.int); myconnection.open(); mycommand.parameters["@userid"].value = userid; mycommand.parameters["@threadid"].value = threadid; mycommand.executenonquery(); } } /// <summary> /// 从收藏夹中删除主题 /// </summary> /// <param name="userid">用户id</param> /// <param name="deletelist">删除列表</param> public override void deletefavorites(int userid, arraylist deletelist) { // create instance of connection and command object using( sqlconnection myconnection = getsqlconnection() ) { sqlcommand mycommand = new sqlcommand(databaseowner + ".forums_favorites_createdelete", myconnection); mycommand.commandtype = commandtype.storedprocedure; mycommand.parameters.add("@action", sqldbtype.int).value = dataprovideraction.delete; mycommand.parameters.add("@userid", sqldbtype.int).value = userid; mycommand.parameters.add("@threadid", sqldbtype.int); // open the connection myconnection.open(); // add multiple times // foreach (int threadid in deletelist) { mycommand.parameters["@threadid"].value = threadid; mycommand.executenonquery(); } } } #endregion3.在data providers qldataprovider qldataprovider.cs修改getthreads方法,以支持收藏功能#region #### threads #### // 增加贴子收藏 by venjiang 0911 public override threadset getthreads( int forumid, int pageindex, int pagesize, int userid, datetime threadsnewerthan, sortthreadsby sortby, sortorder sortorder, threadstatus threadstatus, threadusersfilter userfilter, bool activetopics, bool unreadonly, bool unansweredonly, bool returnrecordcount, // 增加新参数,是否仅显示收藏的主题 bool favoriteonly ) { // create instance of connection and command object // using( sqlconnection connection = getsqlconnection() ) { sqlcommand command = new sqlcommand(databaseowner + ".forums_threads_getthreadset", connection); command.commandtype = commandtype.storedprocedure; threadset threadset = new threadset(); stringbuilder sqlcountselect = new stringbuilder("select count(t.threadid) "); stringbuilder sqlpopulateselect = new stringbuilder("select t.threadid, hasread = "); stringbuilder fromclause = new stringbuilder(" from " + this.databaseowner + ".forums_threads t "); stringbuilder whereclause = new stringbuilder(" where "); stringbuilder orderclause = new stringbuilder(" order by "); // 增加收藏判断 by venjiang 0911 if (favoriteonly == true) { fromclause.append("," + this.databaseowner + ".forums_favorites fav "); } // ensure datetime is min value for sql // threadsnewerthan = sqldataprovider.getsafesqldatetime(threadsnewerthan); // construct the clauses #region constrain forums // contrain the selectivness to a set of specified forums. the forumid is our // clustered index so we want this to be first if (forumid > 0) { whereclause.append("t.forumid = "); whereclause.append(forumid); } else if (forumid < 0) { whereclause.append("(t.forumid = "); // get a list of all the forums the user has access to // arraylist forumlist = forums.getforums(userid, false, true); for (int i = 0; i < forumlist.count; i++) { if ( ((forum) forumlist[i]).forumid > 0 ) { if ( (i + 1) < forumlist.count) { whereclause.append( ((forum) forumlist[i]).forumid + " or t.forumid = "); } else { whereclause.append( ((forum) forumlist[i]).forumid ); whereclause.append(")"); } } } } else { whereclause.append("t.forumid = 0 and p.userid = "); whereclause.append(userid); whereclause.append(" and p.threadid = t.threadid "); fromclause.append(", " + this.databaseowner + ".forums_privatemessages p "); } #endregion #region constrain date whereclause.append(" and stickydate >= '"); whereclause.append( threadsnewerthan.tostring( system.globalization.cultureinfo.currentculture.datetimeformat.sortabledatetimepattern )); whereclause.append(" '"); #endregion #region constain approval whereclause.append(" and isapproved = 1"); #endregion #region constrain read/unread if (userid > 0) { sqlpopulateselect.append("(select " + this.databaseowner + ".hasreadpost("); sqlpopulateselect.append(userid); sqlpopulateselect.append(", t.threadid, t.forumid)) "); if (unreadonly) { whereclause.append(" and " + this.databaseowner + ".hasreadpost("); whereclause.append(userid); whereclause.append(", t.threadid, t.forumid) = 0"); } } else { sqlpopulateselect.append("0"); } #endregion #region unanswered topics if (unansweredonly) { whereclause.append(" and totalreplies = 0 and islocked = 0"); } #endregion #region active topics // 热门贴子 if (activetopics) { whereclause.append(" and totalreplies > 2 and islocked = 0 and totalviews > 50"); } #endregion #region 收藏 // 尽显示收藏的主题 if (favoriteonly) { whereclause.append(" and t.threadid = fav.threadid and fav.userid = "); whereclause.append(userid); } #endregion #region users filter if (userfilter != threadusersfilter.all) { if ((userfilter == threadusersfilter.hidetopicsparticipatedin) || (userfilter == threadusersfilter.hidetopicsnotparticipatedin)) { whereclause.append(" and "); whereclause.append(userid); if (userfilter == threadusersfilter.hidetopicsnotparticipatedin) whereclause.append(" not"); whereclause.append(" in (select userid from " + this.databaseowner + ".forums_posts p where p.threadid = t.threadid)"); } else { if (userfilter == threadusersfilter.hidetopicsbynonanonymoususers) whereclause.append(" and 0 not"); else whereclause.append(" and 0"); whereclause.append("in (select userid from " + this.databaseowner + ".forums_posts p where threadid = t.threadid and p.userid = 0)"); } } #endregion #region thread status if (threadstatus != threadstatus.notset) { switch (threadstatus) { case threadstatus.open: whereclause.append(" and threadstatus = 0"); break; case threadstatus.closed: whereclause.append(" and threadstatus = 0"); break; case threadstatus.resolved: whereclause.append(" and threadstatus = 0"); break; default: break; } } #endregion #region order by switch (sortby) { case sortthreadsby.lastpost: if (sortorder == sortorder.ascending) { if (activetopics || unansweredonly) orderclause.append("threaddate"); else orderclause.append("issticky, stickydate"); } else { if (activetopics || unansweredonly) orderclause.append("threaddate desc"); else orderclause.append("issticky desc, stickydate desc"); } break; case sortthreadsby.totalratings: if (sortorder == sortorder.ascending) orderclause.append("totalratings"); else orderclause.append("totalratings desc"); break; case sortthreadsby.totalreplies: if (sortorder == sortorder.ascending) orderclause.append("totalreplies"); else orderclause.append("totalreplies desc"); break; case sortthreadsby.threadauthor: if (sortorder == sortorder.ascending) orderclause.append("postauthor desc"); else orderclause.append("postauthor"); break; case sortthreadsby.totalviews: if (sortorder == sortorder.ascending) orderclause.append("totalviews"); else orderclause.append("totalviews desc"); break; } #endregion // build the sql statements sqlcountselect.append(fromclause.tostring()); sqlcountselect.append(whereclause.tostring()); sqlpopulateselect.append(fromclause.tostring()); sqlpopulateselect.append(whereclause.tostring()); sqlpopulateselect.append(orderclause.tostring()); // add parameters to sproc // command.parameters.add("@forumid", sqldbtype.int).value = forumid; command.parameters.add("@pageindex", sqldbtype.int, 4).value = pageindex; command.parameters.add("@pagesize", sqldbtype.int, 4).value = pagesize; command.parameters.add("@sqlcount", sqldbtype.nvarchar, 4000).value = sqlcountselect.tostring(); command.parameters.add("@sqlpopulate", sqldbtype.nvarchar, 4000).value = sqlpopulateselect.tostring(); command.parameters.add("@userid", sqldbtype.int).value = userid; command.parameters.add("@returnrecordcount", sqldbtype.bit).value = returnrecordcount; // execute the command connection.open(); sqldatareader dr = command.executereader(); // populate the threadset // while (dr.read()) { // add threads // if (forumid == 0) threadset.threads.add( forumsdataprovider.populateprivatemessagefromidatareader (dr) ); else threadset.threads.add( forumsdataprovider.populatethreadfromidatareader(dr) ); } // do we need to return record count? // if (returnrecordcount) { dr.nextresult(); dr.read(); // read the total records // threadset.totalrecords = (int) dr[0]; } // get the recipients if this is a request for // the private message list if ((forumid == 0) && (dr.nextresult()) ) { hashtable recipientslookuptable = new hashtable(); while(dr.read()) { int threadid = (int) dr["threadid"]; if (recipientslookuptable[threadid] == null) { recipientslookuptable[threadid] = new arraylist(); } ((arraylist) recipientslookuptable[threadid]).add(forumsdataprovider.populateuserfromidatareader(dr) ); } // map recipients to the threads // foreach (privatemessage thread in threadset.threads) { thread.recipients = (arraylist) recipientslookuptable[thread.threadid]; } } dr.close(); connection.close(); return threadset; } } #endregion八.增加新方法在components/threads.cs增加新的重载方法,以不必修改原来的方法调用.// 为了不影响以前的程序,单独加一个重载方法,以获得收藏夹主题 public static threadset getthreads(int forumid, int pageindex, int pagesize, int userid, datetime threadsnewerthan, sortthreadsby sortby, sortorder sortorder, threadstatus threadstatus, threadusersfilter userfilter, bool activetopics, bool unreadonly, bool unansweredonly, bool returnrecordcount,bool favoriteonly) // 多了一个参数favoriteonly { forumcontext forumcontext = forumcontext.current; string anonymouskey = "thread-" + forumid + pagesize.tostring() + pageindex.tostring() + threadsnewerthan.dayofyear.tostring() + sortby + sortorder + activetopics.tostring() + unansweredonly.tostring() + favoriteonly.tostring(); threadset threadset; // if the user is anonymous take some load off the db // if (userid == 0) { if (forumcontext.context.cache[anonymouskey] != null) return (threadset) forumcontext.context.cache[anonymouskey]; } // create instance of the idataprovider // forumsdataprovider dp = forumsdataprovider.instance(); // get the threads // threadset = dp.getthreads(forumid, pageindex, pagesize, userid, threadsnewerthan, sortby, sortorder, threadstatus, userfilter, activetopics, unreadonly, unansweredonly, returnrecordcount,favoriteonly); if (userid == 0) forumcontext.context.cache.insert(anonymouskey, threadset, null, datetime.now.addminutes(2), timespan.zero, cacheitempriority.low, null); return threadset; }九.业务逻辑层components目录中增加favorites.cs,实现主题的增加删除方法public static void addfavoritespost (int userid,int threadid) { forumsdataprovider dp = forumsdataprovider.instance(); dp.createfavorites(userid, threadid); } /// <summary> /// 删除收藏 /// </summary> /// <param name="userid">用户id</param> /// <param name="deletelist">删除列表</param> public static void deletefavorites (int userid, arraylist deletelist) { // forumsdataprovider dp = forumsdataprovider.instance(); dp.deletefavorites(userid, deletelist); }十.表现层调用1.收藏夹主视图加载收藏主题列表 threadset = threads.getthreads(forumid, pager.pageindex, pager.pagesize, users.getuser().userid, datefiltervalue, threadsortddl.selectedvalue, sortorderddl.selectedvalue, threadstatus.notset, threadusersfilter.all, false, hidereadposts.selectedvalue, false, true,true);注意最后一个参数是true,即返回收藏夹的数据集。2.增加主题到收藏夹 favorites.addfavoritespost(user.userid,post.threadid); httpcontext.current.response.redirect(globals.applicationpath+"/myfavoritesadd.aspx",true);3.删除收藏的主题 favorites.deletefavorites(…)
,欢迎访问网页设计爱好者web开发。