// ********************************************************************* // Initializeskin // /// <summary> /// Initialize the control template and populate the control with values /// </summary> // ***********************************************************************/ protected override void InitializeSkin(Control skin) { try { postToDelete = Posts.GetPost(PostID, ForumUser.Username); } catch (PostNotFoundException) { HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.PostDoesNotExist)); HttpContext.Current.Response.End(); } // Text box containing the reason why the post was deleted. This note will be // sent to the end user. reasonForDelete = (TextBox) skin.FindControl("DeleteReason"); // Does the post have any replies? hasReplies = (Label) skin.FindControl("HasReplies"); if (null != hasReplies) { if (postToDelete.Replies > 0) hasReplies.Text = "true (" + postToDelete.Replies + ") "; else hasReplies.Text = "false "; } // Perform the delete deleteButton = (LinkButton) skin.FindControl("DeletePost"); if (null != deleteButton) deleteButton.Click += new System.EventHandler(DeletePost_Click); // Cancel the delete cancelButton = (HyperLink) skin.FindControl("CancelDelete"); if (null != cancelButton) cancelButton.NavigateUrl = base.ReturnURL; }
// ********************************************************************* // AddPost // /// <summary> /// This method Adds a new post and returns a Post object containing information about the /// newly added post. /// </summary> /// <param name="PostToAdd">A Post object containing information about the post to add. /// This Post object need only have the following properties set: Subject, Body, Username, /// and ParentID or ForumID. If the post is a new post, set ForumID; if it is a reply to /// an existing post, set the ParentID to the ID of the Post that is being replied to.</param> /// <returns>A Post object containing information about the newly added post.</returns> /// <remarks>The Post object being returned by the AddPost method indicates the PostID of the /// newly added post and specifies if the post is approved for viewing or not.</remarks> /// // ********************************************************************/ public static Post AddPost(Post postToAdd) { // convert the subject to the formatted version before adding the post postToAdd.Subject = PostSubjectRawToFormatted(postToAdd.Subject); // Create Instance of the IDataProviderBase IDataProviderBase dp = DataProvider.Instance(); Post newPost = dp.AddPost(postToAdd, (UlterSystems.PortalLib.BusinessObjects.Person.RequestUser().ID).ToString()); return newPost; }
// ********************************************************************* // UpdatePost // /// <summary> /// This method updates a post (called from the admin/moderator editing the post). /// </summary> /// <param name="UpdatedPost">Changes needing to be made to a particular post. The PostID /// represents to post to update.</param> /// // ********************************************************************/ public static void UpdatePost(Post post, bool ChangePin) { post.Subject = PostSubjectRawToFormatted(post.Subject); // Create Instance of the IDataProviderBase IDataProviderBase dp = DataProvider.Instance(); dp.UpdatePost(post, ChangePin); }
/*********************************************************************** // PostButton_Click // /// <summary> /// This event handler fires when the preview button is clicked. It needs /// to show/hide the appropriate panels. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> ************************************************************************/ private void PostButton_Click(Object sender, EventArgs e) { Control form; // Only proceed if the post is valid if (!Page.IsValid) return; // Get the user control that the click originated from form = ((Control)sender).Parent; // When we add a new post, we want to get back the NewPostID, so that we // can automagically redirect the user to the page showing the post. // If iNewPostID comes back as 0, though, that means that the post needs // to be approved first, so the user is taken to a page explaining this. Post newPost = null; Post postToAdd = new Post(); postToAdd.Username = (UlterSystems.PortalLib.BusinessObjects.Person.RequestUser().ID).ToString(); postToAdd.ForumID = postToAdd.ParentID = 0; postToAdd.Subject = ((TextBox)form.FindControl("PostSubject")).Text; postToAdd.Body = ((TextBox)form.FindControl("PostBody")).Text; postToAdd.IsLocked = allowNoReplies.Checked; postToAdd.PinnedPost = chkPinnedPost.Checked; // Are we pinning the post? if ((pinnedPost != null) && (Convert.ToInt32(pinnedPost.SelectedItem.Value) > 0)) { switch (Convert.ToInt32(pinnedPost.SelectedItem.Value)) { case 1: postToAdd.PostDate = DateTime.Now.Date.AddDays(1); break; case 3: postToAdd.PostDate = DateTime.Now.Date.AddDays(3); break; case 7: postToAdd.PostDate = DateTime.Now.Date.AddDays(7); break; case 14: postToAdd.PostDate = DateTime.Now.Date.AddDays(14); break; case 30: postToAdd.PostDate = DateTime.Now.Date.AddMonths(1); break; case 90: postToAdd.PostDate = DateTime.Now.Date.AddMonths(3); break; case 180: postToAdd.PostDate = DateTime.Now.Date.AddMonths(6); break; case 360: postToAdd.PostDate = DateTime.Now.Date.AddYears(1); break; case 999: postToAdd.PostDate = DateTime.Now.Date.AddYears(25); break; } } // Are we adding a new post, editing an existing one, or replying to an existing one? switch (Mode) { case CreateEditPostMode.NewPost: // adding a new post postToAdd.ForumID = ForumID; // specify the forum ID that the new post belongs try { newPost = Posts.AddPost(postToAdd); } catch (PostDuplicateException) { Context.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.DuplicatePost) + "&ForumId=" + ForumID); Context.Response.End(); } break; case CreateEditPostMode.ReplyToPost: // replying to an existing post try { postToAdd.ParentID = PostID; // specify the post we are replying to newPost = Posts.AddPost(postToAdd); } catch (Components.PostNotFoundException) { // uh-oh, something is off... are we replying to a message that has been deleted? Context.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.ProblemPosting)); Context.Response.End(); } catch (PostDuplicateException) { Context.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.DuplicatePost) + "&PostId=" + PostID); Context.Response.End(); } break; case CreateEditPostMode.EditPost: postToAdd.PostID = PostID; // specify the ID of the post we are updating string editedBy = Users.GetLoggedOnUser().Username; // update the post if ((pinnedPost != null) && (Convert.ToInt32(pinnedPost.SelectedItem.Value) < 0)) Posts.UpdatePost(postToAdd, false); else Posts.UpdatePost(postToAdd, true); // send the user back to from where they came Context.Response.Redirect(Globals.UrlShowPost + PostID); Context.Response.End(); // exit from the event handler return; } // now that we've added the post, redirect the user to the post display (if the post // has been approved) //if (newPost.Approved) { if (Mode == CreateEditPostMode.NewPost) Context.Response.Redirect(Globals.UrlShowPost + newPost.ThreadID); else Context.Response.Redirect(Globals.UrlShowPost + newPost.ThreadID + "#" + newPost.PostID); Context.Response.End(); }
/// <summary> /// Adds a new Post. This method checks the allowDuplicatePosts settings to determine whether /// or not to allow for duplicate posts. If allowDuplicatePosts is set to false and the user /// attempts to enter a duplicate post, a PostDuplicateException exception is thrown. /// </summary> /// <param name="postToAdd">A Post object containing the information needed to add a new /// post. The essential fields of the Post class that must be set are: the Subject, the /// Body, the Username, and a ForumID or a ParentID (depending on whether the post to add is /// a new post or a reply to an existing post, respectively).</param> /// <returns>A Post object with information on the newly inserted post. This returned Post /// object includes the ID of the newly added Post (PostID) as well as if the Post is /// Approved or not.</returns> public Post AddPost(Post postToAdd, string username) { // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString); SqlParameter param; myConnection.Open(); SqlParameter parameterUserName = new SqlParameter("@UserID", SqlDbType.Int); parameterUserName.Value = int.Parse(postToAdd.Username); SqlParameter parameterBody = new SqlParameter("@Body", SqlDbType.NText); parameterBody.Value = postToAdd.Body; if (!Globals.AllowDuplicatePosts) { SqlCommand checkForDupsCommand = new SqlCommand("dbo.forums_IsDuplicatePost", myConnection); checkForDupsCommand.CommandType = CommandType.StoredProcedure; // Mark the Command as a SPROC checkForDupsCommand.Parameters.Add(parameterUserName); checkForDupsCommand.Parameters.Add(parameterBody); if (((int) checkForDupsCommand.ExecuteScalar()) > 0) { myConnection.Close(); throw new PostDuplicateException("Attempting to insert a duplicate post."); } checkForDupsCommand.Parameters.Clear(); // clear the parameters } SqlCommand myCommand = new SqlCommand("dbo.forums_AddPost", myConnection); // Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure; // Add Parameters to SPROC SqlParameter parameterForumId = new SqlParameter("@ForumID", SqlDbType.Int, 4); parameterForumId.Value = postToAdd.ForumID; myCommand.Parameters.Add(parameterForumId); SqlParameter parameterPostId = new SqlParameter("@ReplyToPostID", SqlDbType.Int, 4); parameterPostId.Value = postToAdd.ParentID; myCommand.Parameters.Add(parameterPostId); SqlParameter parameterSubject = new SqlParameter("@Subject", SqlDbType.NVarChar, 256); parameterSubject.Value = postToAdd.Subject; myCommand.Parameters.Add(parameterSubject); param = new SqlParameter("@IsLocked", SqlDbType.Bit); param.Value = postToAdd.IsLocked; myCommand.Parameters.Add(param); param = new SqlParameter("@IsPinnedPost", SqlDbType.Bit); param.Value = postToAdd.PinnedPost; myCommand.Parameters.Add(param); param = new SqlParameter("@Pinned", SqlDbType.DateTime); if (postToAdd.PostDate > DateTime.Now) param.Value = postToAdd.PostDate; else param.Value = System.DBNull.Value; myCommand.Parameters.Add(param); myCommand.Parameters.Add(parameterUserName); myCommand.Parameters.Add(parameterBody); // Execute the command int iNewPostID = 0; try { // Get the new PostID iNewPostID = Convert.ToInt32(myCommand.ExecuteScalar().ToString()); } catch (Exception e) { myConnection.Close(); // if an exception occurred, throw it back throw new Exception(e.Message); } myConnection.Close(); // Return a Post instance with info from the newly inserted post. return GetPost(iNewPostID, username, false); }
/// <summary> /// Builds and returns an instance of the Post class based on the current row of an /// aptly populated SqlDataReader object. /// </summary> /// <param name="dr">The SqlDataReader object that contains, at minimum, the following /// columns: PostID, ParentID, Body, ForumID, PostDate, PostLevel, SortOrder, Subject, /// ThreadDate, ThreadID, Replies, Username, and Approved.</param> /// <returns>An instance of the Post class that represents the current row of the passed /// in SqlDataReader, dr.</returns> private Post PopulatePostFromSqlDataReader(SqlDataReader dr) { Post post = new Post(); post.PostID = Convert.ToInt32(dr["PostID"]); post.ParentID = Convert.ToInt32(dr["ParentID"]); post.Body = Convert.ToString(dr["Body"]); post.ForumName = Convert.ToString(dr["ForumName"]); post.ForumID = Convert.ToInt32(dr["ForumID"]); post.PostDate = Convert.ToDateTime(dr["PostDate"]); post.Subject = Convert.ToString(dr["Subject"]); post.ThreadDate = Convert.ToDateTime(dr["ThreadDate"]); post.ThreadID = Convert.ToInt32(dr["ThreadID"]); post.Replies = Convert.ToInt32(dr["Replies"]); post.Username = Convert.ToString(dr["UserID"]); post.IsLocked = Convert.ToBoolean(dr["IsLocked"]); post.Views = Convert.ToInt32(dr["TotalViews"]); post.HasRead = Convert.ToBoolean(dr["HasRead"]); post.PinnedPost = Convert.ToBoolean(dr["IsPostPinned"]); return post; }
/// <summary> /// Updates a post. /// </summary> /// <param name="post">The Post data used to update the Post. The ID of the UpdatedPost /// Post object corresponds to what post is to be updated. The only other fields used to update /// the Post are the Subject and Body.</param> public void UpdatePost(Post post, bool ChangePin) { // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString); SqlCommand myCommand = new SqlCommand("dbo.forums_UpdatePost", myConnection); // Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure; // Add Parameters to SPROC myCommand.Parameters.Add("@PostID", SqlDbType.Int, 4).Value = post.PostID; myCommand.Parameters.Add("@Subject", SqlDbType.NVarChar, 256).Value = post.Subject; myCommand.Parameters.Add("@Body", SqlDbType.NText).Value = post.Body; myCommand.Parameters.Add("@IsLocked", SqlDbType.Bit).Value = post.IsLocked; myCommand.Parameters.Add("@IsPinnedPost", SqlDbType.Bit).Value = post.PinnedPost; SqlParameter param = new SqlParameter("@Pinned", SqlDbType.DateTime); if (post.PostDate > DateTime.Now) param.Value = post.PostDate; else param.Value = System.DBNull.Value; myCommand.Parameters.Add(param); myCommand.Parameters.Add("@ChangePin", SqlDbType.Bit, 50).Value = ChangePin; // Execute the command myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception e) { myConnection.Close(); // oops, something went wrong throw new Exception(e.Message); } myConnection.Close(); }