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開發。