Next, the edit.
private string writeBlog(int blogID)
{
string content = null, blogTitle = null, blogText = "", blogDisplayName = displayName, blogEmail = email;
DateTime displayDate = DateTime.Now, blogCreateDate = DateTime.Now, modifiedDate = DateTime.Now;
bool visible = true, blogShowEmail = showEmail;
int numComments = 0;
if (blogID > 0)
{
//editing blog
string select = @"SELECT a.userID, blogTitle, blogText, visible, a.createDate, displayDate, modifiedDate,
(SELECT COUNT(*) FROM comments WHERE blogID=a.blogID and visible=1) as Comments,
displayName, email, showEmail
FROM blogs a
JOIN users b
on a.userID = b.userID
WHERE a.blogID = " + blogID;
bool mayEdit = false;
SqlDataReader dr = query(select);
if (dr.HasRows)
{
dr.Read();
if (accessLevel == 255 || (userid == Convert.ToInt16(dr["userID"])))
{
mayEdit = true;
blogTitle = Convert.ToString(dr["blogTitle"]);
blogText = Convert.ToString(dr["blogText"]);
visible = (Convert.ToInt16(dr["visible"]) == 0) ? false : true;
try { blogCreateDate = Convert.ToDateTime(dr["createDate"]); } catch { }
try { modifiedDate = Convert.ToDateTime(dr["modifiedDate"]); } catch { }
numComments = Convert.ToInt16(dr["Comments"]);
blogDisplayName = Convert.ToString(dr["displayName"]);
blogEmail = Convert.ToString(dr["email"]);
blogShowEmail = (Convert.ToInt16(dr["showEmail"]) == 1) ? true : false;
}
}
dr.Dispose();
if (!mayEdit)
{
return "<div class='error'>Error: Not Authorized</div>";
}
}
content += @"<form method='POST' action='default.aspx' style='display: inline;'><div class='blogEntry'>
<div class='blogTitle'>
<div class='title'>Title:<input name='blogTitle' size=60 value='" + blogTitle + @"'> Visible:<input type='checkbox' name='visible'";
if (visible) { content += " checked"; }
content += @"></div>
</div>
<div class='blogSubTitle'>
<div class='author'>By " + blogDisplayName;
if (blogShowEmail) { content += " (<a href=\"mailto: " + blogEmail + "\">" + blogEmail + "</a>)"; }
content += "</div><div class='date'>";
if (blogID < 0)
{
content += "Display<input name='displayDate' value='" + displayDate + @"'><br>" +
"Modified<input name='displayDate' value='" + modifiedDate + @"' disabled><br>" +
"Created<input name='displayDate' value='" + blogCreateDate + @"' disabled><br>";
}
else
{
content += "<input name='displayDate' value='" + displayDate + @"'>";
}
content += @"</div>
</div>
<div class='text'><textarea name='blogText' rows=10 style='width: 100%;'>" + blogText + @"</textarea></div>
<div class='comments'>Comments (" + numComments + @")</div>
</div>
<input type='hidden' name='blogID' value='" + blogID + @"'>
<input type='submit' name='option' value='Save Blog'>
<input type='reset' value='Clear'></form>
<form action='default.aspx' style='display: inline;'>
<input type='submit' value='Cancel'>
</form>";
return content;
}
Ok, we started out with the
writeBlog() method we had before, but made quite a few changes. In the first section, we initialized a bunch more variables. These include the
modifiedDate and
blogCreateDate variables which wouldn't have been used when creating a new blog. We also have some variables relating to the author's displayname and email. If you're creating a blog, you're the author and the info is your own. However, if you're an admin you can edit someone else's blog, so we need to keep the original author's name, email and email visibility settings, hence the reset of the new variables.
Within the
if(blogID > 0) block, we set up our editing. We query the database for all the info we need, then process that request. Like in the delete command, if the user isn't an admin and not the creator of the blog, we just throw an unauthorized error and exit. Otherwise, we populate all our variables. We then head into the form for editing the blog.
This is almost the same, with a few changes. We now use the email, email visibility and displayname settings that we created earlier, rather than just defaulting to the current user's information. As mentioned, this is in case an admin edits someone else's blog. We don't want the blog to contain the admin's info, we want it to contain the info for the original creator. We also change the way we display dates. We don't actually allow the user to change the created and modified dates, but at least you can see them here. For all intents and purposes, that's it.