using System;
using System.IO;
using System.Web.UI;
using NoticeBoard.Models;
namespace TestProject.NoticeBoard.Controls
{
/// <summary>
/// 에디터 컨트롤
/// </summary>
public partial class EditorControl : UserControl
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// ID
/// </summary>
private string id;
/// <summary>
/// 업로드 디렉토리 경로
/// </summary>
private string uploadDirectoryPath = string.Empty;
/// <summary>
/// 파일명
/// </summary>
private string fileName = string.Empty;
/// <summary>
/// 파일 크기
/// </summary>
private int fileSize = 0;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 폼 타입 - FormType
/// <summary>
/// 폼 타입
/// </summary>
public BoardWriteFormType FormType { get; set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 페이지 로드시 처리하기 - Page_Load(sender, e)
/// <summary>
/// 페이지 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void Page_Load(object sender, EventArgs e)
{
this.id = Request.QueryString["ID"];
if(!Page.IsPostBack)
{
switch(FormType)
{
case BoardWriteFormType.Write :
this.titleDescriptionLabel.Text = "글 쓰기 - 아래 항목을 입력해 주시기 바랍니다.";
break;
case BoardWriteFormType.Modify :
this.titleDescriptionLabel.Text = "글 수정 - 아래 항목을 입력해 주시기 바랍니다.";
DisplayDataForUpdate();
break;
case BoardWriteFormType.Reply :
this.titleDescriptionLabel.Text = "글 답변 - 아래 항목을 입력해 주시기 바랍니다.";
DisplayDataForReply();
break;
}
}
}
#endregion
#region 업로드 체크 박스 체크 변경시 처리하기 - uploadCheckBox_CheckedChanged(sender, e)
/// <summary>
/// 업로드 체크 박스 체크 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void uploadCheckBox_CheckedChanged(object sender, EventArgs e)
{
this.filePanel.Visible = !this.filePanel.Visible;
}
#endregion
#region 저장 버튼 클릭시 처리하기 - saveButton_Click(object sender, EventArgs e)
/// <summary>
/// 저장 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void saveButton_Click(object sender, EventArgs e)
{
if(ValidateSecurtyImageText())
{
UploadFile();
NoticeModel notice = new NoticeModel();
notice.ID = Convert.ToInt32(this.id);
notice.Name = this.nameTextBox.Text;
notice.MailAddress = HTMLHelper.Encode(this.mailAddressTextBox.Text);
notice.Homepage = this.homepageTextBox.Text;
notice.Title = HTMLHelper.Encode(this.titleTextBox.Text);
notice.Content = this.contentTextBox.Text;
notice.FileName = this.fileName;
notice.FileSize = this.fileSize;
notice.Password = this.passwordTextBox.Text;
notice.WriteIP = Request.UserHostAddress;
notice.Encoding = this.encodingRadioButtonList.SelectedValue;
NoticeRepository repository = new NoticeRepository();
switch(FormType)
{
case BoardWriteFormType.Write :
repository.Add(notice);
Response.Redirect("NoticeListPage.aspx");
break;
case BoardWriteFormType.Modify :
notice.UpdateIP = Request.UserHostAddress;
notice.FileName = ViewState["FileName"].ToString();
notice.FileSize = Convert.ToInt32(ViewState["FileSize"]);
int recordCount = repository.Update(notice);
if(recordCount > 0)
{
Response.Redirect($"NoticeViewPage.aspx?ID={this.id}");
}
else
{
this.errorLabel.Text = "수정을 실패했습니다. 암호를 확인해 주시기 바랍니다.";
}
break;
case BoardWriteFormType.Reply :
notice.ParentID = Convert.ToInt32(this.id);
repository.Reply(notice);
Response.Redirect("NoticeListPage.aspx");
break;
default :
repository.Add(notice);
Response.Redirect("NoticeListPage.aspx");
break;
}
}
else
{
this.errorLabel.Text = "보안코드가 일치하지 않습니다.. 다시 입력해 주시기 바랍니다.";
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 수정용 데이터 표시하기 - DisplayDataForUpdate()
/// <summary>
/// 수정용 데이터 표시하기
/// </summary>
private void DisplayDataForUpdate()
{
NoticeModel notice = (new NoticeRepository()).Get(Convert.ToInt32(this.id));
this.nameTextBox.Text = notice.Name;
this.mailAddressTextBox.Text = notice.MailAddress;
this.homepageTextBox.Text = notice.Homepage;
this.titleTextBox.Text = notice.Title;
this.contentTextBox.Text = notice.Content;
string ecoding = notice.Encoding;
if(ecoding == "Text")
{
this.encodingRadioButtonList.SelectedIndex = 0;
}
else if(ecoding == "Mixed")
{
this.encodingRadioButtonList.SelectedIndex = 2;
}
else
{
this.encodingRadioButtonList.SelectedIndex = 1;
}
if(notice.FileName.Length > 1)
{
ViewState["FileName"] = notice.FileName;
ViewState["FileSize"] = notice.FileSize;
this.filePanel.Height = 50;
this.previousFileNameLabel.Visible = true;
this.previousFileNameLabel.Text = $"기존 업로드 파일명 : {notice.FileName}";
}
else
{
ViewState["FileName"] = "";
ViewState["FileSize"] = 0;
}
}
#endregion
#region 답변용 데이터 표시하기 - DisplayDataForReply()
/// <summary>
/// 답변용 데이터 표시하기
/// </summary>
private void DisplayDataForReply()
{
NoticeModel notice = (new NoticeRepository()).Get(Convert.ToInt32(this.id));
this.titleTextBox.Text = $"Re : {notice.Title}";
this.contentTextBox.Text = $"\n\nOn {notice.WriteDate}, '{notice.Name}' wrote:\n----------\n>{notice.Content.Replace("\n", "\n>")}\n---------";
}
#endregion
#region 보안 이미지 텍스트 검증하기 - ValidateSecurtyImageText()
/// <summary>
/// 보안 이미지 텍스트 검증하기
/// </summary>
private bool ValidateSecurtyImageText()
{
if(Page.User.Identity.IsAuthenticated)
{
return true;
}
else
{
if(Session["ImageText"] != null)
{
return (this.imageTextTextBox.Text == Session["ImageText"].ToString());
}
}
return false;
}
#endregion
#region 파일 업로드하기 - UploadFile()
/// <summary>
/// 파일 업로드하기
/// </summary>
private void UploadFile()
{
this.uploadDirectoryPath = Server.MapPath("~/FileUpload");
this.fileName = string.Empty;
this.fileSize = 0;
if(this.fileNameButton.PostedFile != null)
{
if(this.fileNameButton.PostedFile.FileName.Trim().Length > 0 && this.fileNameButton.PostedFile.ContentLength > 0)
{
if(FormType == BoardWriteFormType.Modify)
{
ViewState["FileName"] = FileHelper.GetUniqueFileName
(
this.uploadDirectoryPath,
Path.GetFileName(this.fileNameButton.PostedFile.FileName)
);
ViewState["FileSize"] = this.fileNameButton.PostedFile.ContentLength;
this.fileNameButton.PostedFile.SaveAs
(
Path.Combine
(
this.uploadDirectoryPath,
ViewState["FileName"].ToString()
)
);
}
else
{
this.fileName = FileHelper.GetUniqueFileName
(
this.uploadDirectoryPath,
Path.GetFileName(this.fileNameButton.PostedFile.FileName)
);
this.fileSize = this.fileNameButton.PostedFile.ContentLength;
this.fileNameButton.PostedFile.SaveAs(Path.Combine(this.uploadDirectoryPath, this.fileName));
}
}
}
}
#endregion
}
}