http://localhost/ccs1.03/forums/AddPost.aspx?ForumID=8
<%@ Page SmartNavigation="False" Language="C#" EnableViewState="True" %>
1<%@ Page SmartNavigation="False" Language="C#" EnableViewState="True" %>
2<%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %>
3<%@ Register TagPrefix="CSD" Namespace="CommunityServer.Discussions.Controls" Assembly="CommunityServer.Discussions" %>
4<%@ Import Namespace="CommunityServer.Galleries.Components" %>
5<%@ Import Namespace="CommunityServer.Blogs.Components" %>
6<%@ Import Namespace="CommunityServer.Components" %>
7
8<CS:ContentContainer runat="server" id="MPContainer" ThemeMasterFile = "ForumMaster.ascx" >
9 <CS:Content id="BodyContentRegion" runat="server">
10 <CSD:CreateEditPost runat="server" ID="Createeditpost1" />
11 </CS:Content>
12</CS:ContentContainer>
13
2<%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %>
3<%@ Register TagPrefix="CSD" Namespace="CommunityServer.Discussions.Controls" Assembly="CommunityServer.Discussions" %>
4<%@ Import Namespace="CommunityServer.Galleries.Components" %>
5<%@ Import Namespace="CommunityServer.Blogs.Components" %>
6<%@ Import Namespace="CommunityServer.Components" %>
7
8<CS:ContentContainer runat="server" id="MPContainer" ThemeMasterFile = "ForumMaster.ascx" >
9 <CS:Content id="BodyContentRegion" runat="server">
10 <CSD:CreateEditPost runat="server" ID="Createeditpost1" />
11 </CS:Content>
12</CS:ContentContainer>
13
锁定提交页面容器,<CSD:CreateEditPost runat="server" ID="Createeditpost1" />
在项目中搜索CreateEditPost ,查到CommunityServer.Discussions.Controls.CreateEditPost.cs文件。
里面这行代码指定了皮肤文件名:string skinFilename = "View-CreateEditPost.ascx";
皮肤文件所在的位置:CommunityServerWeb.Themes.default.Skins.View-CreateEditPost.ascx
打开皮肤文件,找到PostBody控件,这就是我要找的东西。freeTextBox的编辑器部分。
<%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %><CS:Editor id=PostBody DESIGNTIMESP="29326" runat="Server"
Width="100%"></CS:Editor>
找到类文件所在位置:CommunityServerControls.Editor.Editor.cs
CommunityServer.Discussions.Components.Posts.cs
<%@ Register TagPrefix="CSD" Namespace="CommunityServer.Discussions.Controls" Assembly="CommunityServer.Discussions" %>
CommunityServerForums.Posts.cs
1//------------------------------------------------------------------------------
2// <copyright company="Telligent Systems">
3// Copyright (c) Telligent Systems Corporation. All rights reserved.
4// </copyright>
5//------------------------------------------------------------------------------
6
7using System;
8using System.Collections;
9using System.Web;
10using System.IO;
11using System.Security;
12using System.Web.Caching;
13using CommunityServer.Components;
14using CommunityServer.Configuration;
15
16namespace CommunityServer.Discussions.Components {
17
18 // *********************************************************************
19 // Posts
20 //
21 /// <summary>
22 /// This class contains methods for working with an individual post. There are methods to
23 /// Add a New Post, Update an Existing Post, retrieve a single post, etc.
24 /// </summary>
25 // ***********************************************************************/
26 public class Posts {
27
28 #region GetPost
29 // *********************************************************************
30 // GetPost
31 //
32 /// <summary>
33 /// Returns information about a particular post.
34 /// </summary>
35 /// <param name="PostID">The ID of the Post to return.</param>
36 /// <returns>A Post object with the spcified Post's information.</returns>
37 /// <remarks>This method returns information about a particular post. If the post specified is
38 /// not found, a PostNotFoundException exception is thrown. If you need more detailed
39 /// information, such as the PostID of the next/prev posts in the thread, or if the current user
40 /// has email tracking enabled for the thread the post appears in, use the GetPostDetails
41 /// method.<seealso cref="GetPostDetails"/></remarks>
42 ///
43 // ***********************************************************************/
44 public static ForumPost GetPost(int postID, int userID) {
45 return Posts.GetPost(postID, userID, false);
46 }
47
48 public static ForumPost GetPost(int postID, int userID, bool trackViews) {
49 CSContext csContext = CSContext.Current;
50
51 // We only want to call this code once per request
52 // LN 6/22/04: Added one more cond. to get the post from CSContext.Items
53 // only when we don't want to track views, which is
54 // anywhere (?!) but PostFlatView control. :)
55 if (csContext.Items["Post" + postID] != null && !trackViews) {
56 return (ForumPost) csContext.Items["Post" + postID];
57 } else {
58 ForumPost post;
59
60 // Create Instance of the CommonDataProvider
61 ForumDataProvider dp = ForumDataProvider.Instance();
62
63 post = dp.GetPost(postID, userID, trackViews);
64
65 // Store in context of current request
66 csContext.Items["Post" + postID] = post;
67
68 return post;
69 }
70 }
71 #endregion
72
73 // *********************************************************************
74 // MarkPostAsRead
75 //
76 /// <summary>
77 /// Given a post id, marks it as read in the database for a user.
78 /// </summary>
79 /// <param name="postID">Id of post to mark as read</param>
80 /// <param name="username">Mark read for this user</param>
81 ///
82 // ********************************************************************/
83 public static void MarkPostAsRead(int postID, string username) {
84 // Create Instance of the CommonDataProvider
85 ForumDataProvider dp = ForumDataProvider.Instance();
86
87 dp.MarkPostAsRead(postID, username);
88 }
89
90 // *********************************************************************
91 // GetTop25NewPosts
92 //
93 /// <summary>
94 /// This method returns the top 25 new posts. These are the 25 posts
95 /// most recently posted to on the boards.
96 /// </summary>
97 /// <param name="PostID">Specifies the PostID of a post that belongs to the thread that we are
98 /// interested in grabbing the messages from.</param>
99 /// <returns>A PostCollection containing the posts in the thread.</returns>
100 ///
101 // ********************************************************************/
102 public static PostSet GetTopNPopularPosts(string username, int postCount, int days)
103 {
104 return ForumDataProvider.Instance().GetTopNPopularPosts(username, postCount, days);
105 }
106
107 public static PostSet GetTopNNewPosts(string username, int postCount)
108 {
109 return ForumDataProvider.Instance().GetTopNNewPosts(username, postCount);
110 }
111
112 // *********************************************************************
113 // GetPosts
114 //
115 /// <summary>
116 /// This method returns a listing of the messages in a given thread using paging.
117 /// </summary>
118 /// <param name="PostID">Specifies the PostID of a post that belongs to the thread that we are
119 /// interested in grabbing the messages from.</param>
120 /// <returns>A PostCollection containing the posts in the thread.</returns>
121 ///
122 // ********************************************************************/
123 public static PostSet GetPosts(int postID, int pageIndex, int pageSize, int sortBy, int sortOrder)
124 {
125 PostSet postSet;
126 CSContext csContext = CSContext.Current;
127 string key = "Forum-Posts::P:{0}-PI:{1}-PS:{2}-SB:{3}-SO:{4}";
128 string postCollectionKey = string.Format(key,postID,pageIndex,pageSize, sortBy, sortOrder);
129
130 // Attempt to retrieve from Cache
131 postSet = CSCache.Get(postCollectionKey) as PostSet; // forumContext.Context.Cache[postCollectionKey];
132
133 if (postSet == null) {
134 // Create Instance of the CommonDataProvider
135 ForumDataProvider dp = ForumDataProvider.Instance();
136
137 postSet = dp.GetPosts(postID, pageIndex, pageSize, sortBy, sortOrder, CSContext.Current.User.UserID, true);
138
139 CSCache.Insert(postCollectionKey,postSet,6);
140 }
141
142 return postSet;
143 }
144
145 /// <summary>
146 /// This command clears the PostSet cache for a particular post collection.
147 /// </summary>
148 public static void ClearPosts(int postID, int pageIndex, int pageSize, int sortBy, int sortOrder)
149 {
150 string key = "Forum-Posts::P:{0}-PI:{1}-PS:{2}-SB:{3}-SO:{4}";
151 string postCollectionKey = string.Format(key,postID,pageIndex,pageSize, sortBy, sortOrder);
152 CSCache.Remove(postCollectionKey);
153 }
154
155 #region Removed GetAllMessages Method
156 // *********************************************************************
157 // GetAllMessages
158 //
159 // <summary>
160 // This method returns all of the messages for a particular forum
161 // (specified by ForumID) and returns the messages in a particular
162 // format (specified by ForumView).
163 // </summary>
164 // <param name="ForumID">The ID of the Forum whose posts you are interested in retrieving.</param>
165 // <param name="ForumView">How to view the posts. The three options are: Flat, Mixed, and Threaded.</param>
166 // <param name="PagesBack">How many pages back of posts to view. Each forum has a
167 // parameter indicating how many days worth of posts to show per page.</param>
168 // <returns>A PostCollection object containing the posts for the particular forum that fall within
169 // the particular page specified by PagesBack.</returns>
170 //
171 // ********************************************************************/
172 //public static PostSet GetAllMessages(int forumID, ViewOptions forumView, int pagesBack) {
173 // Create Instance of the CommonDataProvider
174 //ForumDataProvider dp = ForumDataProvider.Instance();
175
176 // make sure ForumView is set
177 //if (forumView == ViewOptions.NotSet)
178 //forumView = (ViewOptions) Globals.DefaultForumView;
179
180 //return dp.GetAllMessages(forumID, forumView, pagesBack);
181 //}
182 #endregion
183
184
185 // *********************************************************************
186 // GetTotalPostCount
187 //
188 /// <summary>
189 /// Returns the total count of all posts in the system
190 /// </summary>
191 /// <returns>A count of the total posts</returns>
192 ///
193 // ********************************************************************/
194 public static int GetTotalPostCount()
195 {
196 // Create Instance of the CommonDataProvider
197 ForumDataProvider dp = ForumDataProvider.Instance();
198
199 return dp.GetTotalPostCount();
200
201 }
202
203 // *********************************************************************
204 // AddPost
205 //
206 /// <summary>
207 /// This method Adds a new post and returns a Post object containing information about the
208 /// newly added post.
209 /// </summary>
210 /// <param name="PostToAdd">A Post object containing information about the post to add.
211 /// This Post object need only have the following properties set: Subject, Body, Username,
212 /// and ParentID or ForumID. If the post is a new post, set ForumID; if it is a reply to
213 /// an existing post, set the ParentID to the ID of the Post that is being replied to.</param>
214 /// <returns>A Post object containing information about the newly added post.</returns>
215 /// <remarks>The Post object being returned by the AddPost method indicates the PostID of the
216 /// newly added post and specifies if the post is approved for viewing or not.</remarks>
217 ///
218 // ********************************************************************/
219 public static ForumPost AddPost(ForumPost post) {
220
221 return AddPost (post, CSContext.Current.User);
222 }
223
224 public static ForumPost AddPost (ForumPost post, User postAuthor) {
225
226 CSContext csContext = CSContext.Current;
227
228 if (csContext.SiteSettings.EnableTrackPostsByIP && csContext.IsWebRequest)
229 if (csContext.Context.Request.UserHostAddress != null)
230 post.UserHostAddress = CSContext.Current.Context.Request.UserHostAddress;
231
232
233 post.Subject = Globals.HtmlEncode(post.Subject);
234 post.FormattedBody = post.Body;
235
236
237 CSEvents.PrePost(post,ObjectState.Create,ApplicationType.Forum);
238 CSEvents.BeforePost(post,ObjectState.Create,ApplicationType.Forum);
239
240 // Create Instance of the CommonDataProvider
241 //
242 ForumDataProvider dp = ForumDataProvider.Instance();
243
244 ForumPost newPost = dp.AddPost(post, postAuthor.UserID, (postAuthor.IsAdministrator | postAuthor.IsModerator));
245
246 // Update user's last post datetime
247 //
248 Users.UpdateUserLastPostDate(postAuthor);
249
250 // Send forum tracking mail
251 //
252 if (newPost.IsApproved)
253 ForumEmails.ForumTracking(newPost);
254 else
255 ForumEmails.NotifyModerators(newPost);
256
257 CSCache.RemoveByPattern(string.Format("Forum-Threads::S:{0}",newPost.SectionID));
258 //This will only work for post's at level 1 or 2. need to figure out clean way to find a post's root part
259 if(newPost.PostLevel > 1)
260 CSCache.RemoveByPattern(string.Format("Forum-Posts::P:{0}",newPost.ParentID));
261
262 CSEvents.AfterPost(newPost,ObjectState.Create,ApplicationType.Forum);
263
264 return newPost;
265 }
266
267 // *********************************************************************
268 // UpdatePost
269 //
270 /// <summary>
271 /// This method updates a post (called from the admin/moderator editing the post).
272 /// </summary>
273 /// <param name="post">Changes needing to be made to a particular post. The PostID
274 /// represents to post to update.</param>
275 ///
276 // ********************************************************************/
277 public static void UpdatePost(ForumPost post, int editedBy) {
278
279 post.Subject = Globals.HtmlEncode(post.Subject);
280 post.FormattedBody = post.Body;
281
282 CSEvents.PrePost(post,ObjectState.Update,ApplicationType.Forum);
283 CSEvents.BeforePost(post,ObjectState.Update,ApplicationType.Forum);
284
285 // Create Instance of the CommonDataProvider
286 ForumDataProvider dp = ForumDataProvider.Instance();
287 dp.UpdatePost(post, editedBy);
288
289 CSCache.RemoveByPattern(string.Format("Forum-Threads::S:{0}",post.SectionID));
290
291 //This will only work for post's at level 1 or 2. need to figure out clean way to find a post's root part
292 CSCache.RemoveByPattern(string.Format("Forum-Posts::P:{0}",post.PostID));
293 if(post.PostLevel > 1)
294 CSCache.RemoveByPattern(string.Format("Forum-Posts::P:{0}",post.ParentID));
295
296 CSEvents.AfterPost(post,ObjectState.Update,ApplicationType.Forum);
297 }
298
299
300
301
302 public static void AddAttachment (Post post, PostAttachment attachment) {
303
304 //保存在磁盘中
305 if (CSConfiguration.GetConfig().AttachmentSaveMode == FileSaveMode.Disk)
306 {
307 string path = PostAttachment.GetAttachmentPath(post.User.UserID);
308 try
309 {
310 if(!Directory.Exists(path))
311 {
312 DirectoryEx.CreateDirectory(path);
313 }
314
315 string fullPath = Path.Combine(path, attachment.RealFileName);
316
317 FileStream stream = new FileStream(fullPath, FileMode.Create);
318 try
319 {
320 stream.Write(attachment.Content, 0, attachment.Length);
321 stream.Flush();
322 }
323 finally
324 {
325 stream.Close();
326 }
327
328 }
329 catch(SecurityException)
330 {
331 throw new CSException(CSExceptionType.SecurityException);
332 }
333 catch(UnauthorizedAccessException)
334 {
335 throw new CSException(CSExceptionType.UnauthorizedAccessException);
336 }
337 attachment.Content = new byte[0];
338 }
339
340 // Create Instance of the CommonDataProvider
341 CommonDataProvider dp = CommonDataProvider.Instance();
342 dp.AddPostAttachment(post, attachment);
343 }
344
345 public static void DeleteAttachments (int postID)
346 {
347 ArrayList attachments = GetAttachments(postID);
348 for(int i=0; i<attachments.Count; i++)
349 {
350 PostAttachment attachment = (PostAttachment)attachments[i];
351 DeleteAttachment(attachment.AttachmentID);
352 }
353 }
354
355 public static void DeleteAttachment (Guid attachmentID)
356 {
357 // 如果附件存在则先删除附件
358 PostAttachment attachment = GetAttachment (attachmentID);
359 if (attachment.RealFileName != "")
360 {
361 string path = PostAttachment.GetAttachmentPath(attachment.UserID);
362 try
363 {
364 string fullPath = Path.Combine(path, attachment.RealFileName);
365 if (File.Exists(fullPath))
366 {
367 File.Delete(fullPath);
368 }
369 }
370 catch
371 {
372 throw new CSException(CSExceptionType.UnauthorizedAccessException);
373 }
374 }
375 CommonDataProvider.Instance().DeletePostAttachment(attachmentID);
376 }
377
378 public static PostAttachment GetAttachment (Guid attachmentID)
379 {
380 // Create Instance of the CommonDataProvider
381 CommonDataProvider dp = CommonDataProvider.Instance();
382
383 return dp.GetPostAttachment(attachmentID);
384 }
385
386 public static ArrayList GetAttachments (int postID) {
387 // Create Instance of the CommonDataProvider
388 CommonDataProvider dp = CommonDataProvider.Instance();
389
390 return dp.GetPostAttachments(postID);
391 }
392
393 public static string GetPostInPageUrl(ForumPost post)
394 {
395 return GetPostInPageUrl(post, CSContext.Current.User);
396 }
397
398 public static string GetPostInPageUrl(ForumPost post, User user)
399 {
400 Thread thread = Threads.GetThread(post.ThreadID);
401 string url;
402 if (thread != null && thread.Replies > CSContext.Current.SiteSettings.PostsPerPage)
403 {
404 int page;
405
406 if (user.PostSortOrder == SortOrder.Descending)
407 page = 1;
408 else
409 page = 1 + thread.Replies / CSContext.Current.SiteSettings.PostsPerPage;
410
411 url = Globals.GetSiteUrls().PostPaged(post.PostID, page);
412 }
413 else
414 {
415 url = Globals.GetSiteUrls().PostInPage(post.PostID, post.PostID);
416 }
417
418 return url;
419
420 }
421
422 }
423}
424
2// <copyright company="Telligent Systems">
3// Copyright (c) Telligent Systems Corporation. All rights reserved.
4// </copyright>
5//------------------------------------------------------------------------------
6
7using System;
8using System.Collections;
9using System.Web;
10using System.IO;
11using System.Security;
12using System.Web.Caching;
13using CommunityServer.Components;
14using CommunityServer.Configuration;
15
16namespace CommunityServer.Discussions.Components {
17
18 // *********************************************************************
19 // Posts
20 //
21 /// <summary>
22 /// This class contains methods for working with an individual post. There are methods to
23 /// Add a New Post, Update an Existing Post, retrieve a single post, etc.
24 /// </summary>
25 // ***********************************************************************/
26 public class Posts {
27
28 #region GetPost
29 // *********************************************************************
30 // GetPost
31 //
32 /// <summary>
33 /// Returns information about a particular post.
34 /// </summary>
35 /// <param name="PostID">The ID of the Post to return.</param>
36 /// <returns>A Post object with the spcified Post's information.</returns>
37 /// <remarks>This method returns information about a particular post. If the post specified is
38 /// not found, a PostNotFoundException exception is thrown. If you need more detailed
39 /// information, such as the PostID of the next/prev posts in the thread, or if the current user
40 /// has email tracking enabled for the thread the post appears in, use the GetPostDetails
41 /// method.<seealso cref="GetPostDetails"/></remarks>
42 ///
43 // ***********************************************************************/
44 public static ForumPost GetPost(int postID, int userID) {
45 return Posts.GetPost(postID, userID, false);
46 }
47
48 public static ForumPost GetPost(int postID, int userID, bool trackViews) {
49 CSContext csContext = CSContext.Current;
50
51 // We only want to call this code once per request
52 // LN 6/22/04: Added one more cond. to get the post from CSContext.Items
53 // only when we don't want to track views, which is
54 // anywhere (?!) but PostFlatView control. :)
55 if (csContext.Items["Post" + postID] != null && !trackViews) {
56 return (ForumPost) csContext.Items["Post" + postID];
57 } else {
58 ForumPost post;
59
60 // Create Instance of the CommonDataProvider
61 ForumDataProvider dp = ForumDataProvider.Instance();
62
63 post = dp.GetPost(postID, userID, trackViews);
64
65 // Store in context of current request
66 csContext.Items["Post" + postID] = post;
67
68 return post;
69 }
70 }
71 #endregion
72
73 // *********************************************************************
74 // MarkPostAsRead
75 //
76 /// <summary>
77 /// Given a post id, marks it as read in the database for a user.
78 /// </summary>
79 /// <param name="postID">Id of post to mark as read</param>
80 /// <param name="username">Mark read for this user</param>
81 ///
82 // ********************************************************************/
83 public static void MarkPostAsRead(int postID, string username) {
84 // Create Instance of the CommonDataProvider
85 ForumDataProvider dp = ForumDataProvider.Instance();
86
87 dp.MarkPostAsRead(postID, username);
88 }
89
90 // *********************************************************************
91 // GetTop25NewPosts
92 //
93 /// <summary>
94 /// This method returns the top 25 new posts. These are the 25 posts
95 /// most recently posted to on the boards.
96 /// </summary>
97 /// <param name="PostID">Specifies the PostID of a post that belongs to the thread that we are
98 /// interested in grabbing the messages from.</param>
99 /// <returns>A PostCollection containing the posts in the thread.</returns>
100 ///
101 // ********************************************************************/
102 public static PostSet GetTopNPopularPosts(string username, int postCount, int days)
103 {
104 return ForumDataProvider.Instance().GetTopNPopularPosts(username, postCount, days);
105 }
106
107 public static PostSet GetTopNNewPosts(string username, int postCount)
108 {
109 return ForumDataProvider.Instance().GetTopNNewPosts(username, postCount);
110 }
111
112 // *********************************************************************
113 // GetPosts
114 //
115 /// <summary>
116 /// This method returns a listing of the messages in a given thread using paging.
117 /// </summary>
118 /// <param name="PostID">Specifies the PostID of a post that belongs to the thread that we are
119 /// interested in grabbing the messages from.</param>
120 /// <returns>A PostCollection containing the posts in the thread.</returns>
121 ///
122 // ********************************************************************/
123 public static PostSet GetPosts(int postID, int pageIndex, int pageSize, int sortBy, int sortOrder)
124 {
125 PostSet postSet;
126 CSContext csContext = CSContext.Current;
127 string key = "Forum-Posts::P:{0}-PI:{1}-PS:{2}-SB:{3}-SO:{4}";
128 string postCollectionKey = string.Format(key,postID,pageIndex,pageSize, sortBy, sortOrder);
129
130 // Attempt to retrieve from Cache
131 postSet = CSCache.Get(postCollectionKey) as PostSet; // forumContext.Context.Cache[postCollectionKey];
132
133 if (postSet == null) {
134 // Create Instance of the CommonDataProvider
135 ForumDataProvider dp = ForumDataProvider.Instance();
136
137 postSet = dp.GetPosts(postID, pageIndex, pageSize, sortBy, sortOrder, CSContext.Current.User.UserID, true);
138
139 CSCache.Insert(postCollectionKey,postSet,6);
140 }
141
142 return postSet;
143 }
144
145 /// <summary>
146 /// This command clears the PostSet cache for a particular post collection.
147 /// </summary>
148 public static void ClearPosts(int postID, int pageIndex, int pageSize, int sortBy, int sortOrder)
149 {
150 string key = "Forum-Posts::P:{0}-PI:{1}-PS:{2}-SB:{3}-SO:{4}";
151 string postCollectionKey = string.Format(key,postID,pageIndex,pageSize, sortBy, sortOrder);
152 CSCache.Remove(postCollectionKey);
153 }
154
155 #region Removed GetAllMessages Method
156 // *********************************************************************
157 // GetAllMessages
158 //
159 // <summary>
160 // This method returns all of the messages for a particular forum
161 // (specified by ForumID) and returns the messages in a particular
162 // format (specified by ForumView).
163 // </summary>
164 // <param name="ForumID">The ID of the Forum whose posts you are interested in retrieving.</param>
165 // <param name="ForumView">How to view the posts. The three options are: Flat, Mixed, and Threaded.</param>
166 // <param name="PagesBack">How many pages back of posts to view. Each forum has a
167 // parameter indicating how many days worth of posts to show per page.</param>
168 // <returns>A PostCollection object containing the posts for the particular forum that fall within
169 // the particular page specified by PagesBack.</returns>
170 //
171 // ********************************************************************/
172 //public static PostSet GetAllMessages(int forumID, ViewOptions forumView, int pagesBack) {
173 // Create Instance of the CommonDataProvider
174 //ForumDataProvider dp = ForumDataProvider.Instance();
175
176 // make sure ForumView is set
177 //if (forumView == ViewOptions.NotSet)
178 //forumView = (ViewOptions) Globals.DefaultForumView;
179
180 //return dp.GetAllMessages(forumID, forumView, pagesBack);
181 //}
182 #endregion
183
184
185 // *********************************************************************
186 // GetTotalPostCount
187 //
188 /// <summary>
189 /// Returns the total count of all posts in the system
190 /// </summary>
191 /// <returns>A count of the total posts</returns>
192 ///
193 // ********************************************************************/
194 public static int GetTotalPostCount()
195 {
196 // Create Instance of the CommonDataProvider
197 ForumDataProvider dp = ForumDataProvider.Instance();
198
199 return dp.GetTotalPostCount();
200
201 }
202
203 // *********************************************************************
204 // AddPost
205 //
206 /// <summary>
207 /// This method Adds a new post and returns a Post object containing information about the
208 /// newly added post.
209 /// </summary>
210 /// <param name="PostToAdd">A Post object containing information about the post to add.
211 /// This Post object need only have the following properties set: Subject, Body, Username,
212 /// and ParentID or ForumID. If the post is a new post, set ForumID; if it is a reply to
213 /// an existing post, set the ParentID to the ID of the Post that is being replied to.</param>
214 /// <returns>A Post object containing information about the newly added post.</returns>
215 /// <remarks>The Post object being returned by the AddPost method indicates the PostID of the
216 /// newly added post and specifies if the post is approved for viewing or not.</remarks>
217 ///
218 // ********************************************************************/
219 public static ForumPost AddPost(ForumPost post) {
220
221 return AddPost (post, CSContext.Current.User);
222 }
223
224 public static ForumPost AddPost (ForumPost post, User postAuthor) {
225
226 CSContext csContext = CSContext.Current;
227
228 if (csContext.SiteSettings.EnableTrackPostsByIP && csContext.IsWebRequest)
229 if (csContext.Context.Request.UserHostAddress != null)
230 post.UserHostAddress = CSContext.Current.Context.Request.UserHostAddress;
231
232
233 post.Subject = Globals.HtmlEncode(post.Subject);
234 post.FormattedBody = post.Body;
235
236
237 CSEvents.PrePost(post,ObjectState.Create,ApplicationType.Forum);
238 CSEvents.BeforePost(post,ObjectState.Create,ApplicationType.Forum);
239
240 // Create Instance of the CommonDataProvider
241 //
242 ForumDataProvider dp = ForumDataProvider.Instance();
243
244 ForumPost newPost = dp.AddPost(post, postAuthor.UserID, (postAuthor.IsAdministrator | postAuthor.IsModerator));
245
246 // Update user's last post datetime
247 //
248 Users.UpdateUserLastPostDate(postAuthor);
249
250 // Send forum tracking mail
251 //
252 if (newPost.IsApproved)
253 ForumEmails.ForumTracking(newPost);
254 else
255 ForumEmails.NotifyModerators(newPost);
256
257 CSCache.RemoveByPattern(string.Format("Forum-Threads::S:{0}",newPost.SectionID));
258 //This will only work for post's at level 1 or 2. need to figure out clean way to find a post's root part
259 if(newPost.PostLevel > 1)
260 CSCache.RemoveByPattern(string.Format("Forum-Posts::P:{0}",newPost.ParentID));
261
262 CSEvents.AfterPost(newPost,ObjectState.Create,ApplicationType.Forum);
263
264 return newPost;
265 }
266
267 // *********************************************************************
268 // UpdatePost
269 //
270 /// <summary>
271 /// This method updates a post (called from the admin/moderator editing the post).
272 /// </summary>
273 /// <param name="post">Changes needing to be made to a particular post. The PostID
274 /// represents to post to update.</param>
275 ///
276 // ********************************************************************/
277 public static void UpdatePost(ForumPost post, int editedBy) {
278
279 post.Subject = Globals.HtmlEncode(post.Subject);
280 post.FormattedBody = post.Body;
281
282 CSEvents.PrePost(post,ObjectState.Update,ApplicationType.Forum);
283 CSEvents.BeforePost(post,ObjectState.Update,ApplicationType.Forum);
284
285 // Create Instance of the CommonDataProvider
286 ForumDataProvider dp = ForumDataProvider.Instance();
287 dp.UpdatePost(post, editedBy);
288
289 CSCache.RemoveByPattern(string.Format("Forum-Threads::S:{0}",post.SectionID));
290
291 //This will only work for post's at level 1 or 2. need to figure out clean way to find a post's root part
292 CSCache.RemoveByPattern(string.Format("Forum-Posts::P:{0}",post.PostID));
293 if(post.PostLevel > 1)
294 CSCache.RemoveByPattern(string.Format("Forum-Posts::P:{0}",post.ParentID));
295
296 CSEvents.AfterPost(post,ObjectState.Update,ApplicationType.Forum);
297 }
298
299
300
301
302 public static void AddAttachment (Post post, PostAttachment attachment) {
303
304 //保存在磁盘中
305 if (CSConfiguration.GetConfig().AttachmentSaveMode == FileSaveMode.Disk)
306 {
307 string path = PostAttachment.GetAttachmentPath(post.User.UserID);
308 try
309 {
310 if(!Directory.Exists(path))
311 {
312 DirectoryEx.CreateDirectory(path);
313 }
314
315 string fullPath = Path.Combine(path, attachment.RealFileName);
316
317 FileStream stream = new FileStream(fullPath, FileMode.Create);
318 try
319 {
320 stream.Write(attachment.Content, 0, attachment.Length);
321 stream.Flush();
322 }
323 finally
324 {
325 stream.Close();
326 }
327
328 }
329 catch(SecurityException)
330 {
331 throw new CSException(CSExceptionType.SecurityException);
332 }
333 catch(UnauthorizedAccessException)
334 {
335 throw new CSException(CSExceptionType.UnauthorizedAccessException);
336 }
337 attachment.Content = new byte[0];
338 }
339
340 // Create Instance of the CommonDataProvider
341 CommonDataProvider dp = CommonDataProvider.Instance();
342 dp.AddPostAttachment(post, attachment);
343 }
344
345 public static void DeleteAttachments (int postID)
346 {
347 ArrayList attachments = GetAttachments(postID);
348 for(int i=0; i<attachments.Count; i++)
349 {
350 PostAttachment attachment = (PostAttachment)attachments[i];
351 DeleteAttachment(attachment.AttachmentID);
352 }
353 }
354
355 public static void DeleteAttachment (Guid attachmentID)
356 {
357 // 如果附件存在则先删除附件
358 PostAttachment attachment = GetAttachment (attachmentID);
359 if (attachment.RealFileName != "")
360 {
361 string path = PostAttachment.GetAttachmentPath(attachment.UserID);
362 try
363 {
364 string fullPath = Path.Combine(path, attachment.RealFileName);
365 if (File.Exists(fullPath))
366 {
367 File.Delete(fullPath);
368 }
369 }
370 catch
371 {
372 throw new CSException(CSExceptionType.UnauthorizedAccessException);
373 }
374 }
375 CommonDataProvider.Instance().DeletePostAttachment(attachmentID);
376 }
377
378 public static PostAttachment GetAttachment (Guid attachmentID)
379 {
380 // Create Instance of the CommonDataProvider
381 CommonDataProvider dp = CommonDataProvider.Instance();
382
383 return dp.GetPostAttachment(attachmentID);
384 }
385
386 public static ArrayList GetAttachments (int postID) {
387 // Create Instance of the CommonDataProvider
388 CommonDataProvider dp = CommonDataProvider.Instance();
389
390 return dp.GetPostAttachments(postID);
391 }
392
393 public static string GetPostInPageUrl(ForumPost post)
394 {
395 return GetPostInPageUrl(post, CSContext.Current.User);
396 }
397
398 public static string GetPostInPageUrl(ForumPost post, User user)
399 {
400 Thread thread = Threads.GetThread(post.ThreadID);
401 string url;
402 if (thread != null && thread.Replies > CSContext.Current.SiteSettings.PostsPerPage)
403 {
404 int page;
405
406 if (user.PostSortOrder == SortOrder.Descending)
407 page = 1;
408 else
409 page = 1 + thread.Replies / CSContext.Current.SiteSettings.PostsPerPage;
410
411 url = Globals.GetSiteUrls().PostPaged(post.PostID, page);
412 }
413 else
414 {
415 url = Globals.GetSiteUrls().PostInPage(post.PostID, post.PostID);
416 }
417
418 return url;
419
420 }
421
422 }
423}
424