using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace VwdCms.Admin
{
public class Editor
{
public enum EditorCellTypes
{
ID,
Date,
Text,
TextBox,
CheckBox,
DropDownList,
Delete
}
public static void UpdateTextBoxProperty(VwdCms.IBizObject obj, string propertyName, StringBuilder sbResults, Control parentControl)
{
HttpContext context = HttpContext.Current;
HttpServerUtility server = context.Server;
HttpRequest request = context.Request;
string containerName = GetContainerName(parentControl);
string ID = obj.ID.ToString();
string oldPropValue = Convert.ToString(obj.GetProperty(propertyName));
string formValue = request.Form[containerName + "txt" + propertyName + obj.ID.ToString()];
if (formValue != null formValue != oldPropValue)
{
oldPropValue = server.HtmlEncode(oldPropValue);
string newPropValue = server.HtmlEncode(formValue);
if (ValidateFormInput(formValue))
{
try
{
obj.SetProperty(propertyName, formValue);
sbResults.Append("<font class=\"editormsgsuccess\"><br>'");
sbResults.Append(oldPropValue);
sbResults.Append("' (ID=");
sbResults.Append(ID);
sbResults.Append(") has been changed to '");
sbResults.Append(newPropValue);
sbResults.Append("'</font>");
}
catch
{
sbResults.Append("<font class=\"editormsgerror\"><br>'");
sbResults.Append(oldPropValue);
sbResults.Append("' (ID=");
sbResults.Append(ID);
sbResults.Append(") <u>cannot be changed</u> to '");
sbResults.Append(newPropValue);
sbResults.Append("' because the name is already in use.</font>");
}
}
else
{
sbResults.Append("<font class=\"editormsgerror\"><br>'");
sbResults.Append(oldPropValue);
sbResults.Append("' (ID=");
sbResults.Append(ID);
sbResults.Append(") <u>cannot be changed</u> to '");
sbResults.Append(newPropValue);
sbResults.Append("' it containts invalid or protentially dangerous text.</font>");
}
}
}
public static void UpdateCheckBoxProperty(VwdCms.IBizObject obj, string propertyName, StringBuilder sbResults, Control parentControl)
{
HttpContext context = HttpContext.Current;
HttpServerUtility server = context.Server;
HttpRequest request = context.Request;
string containerName = GetContainerName(parentControl);
string name = Convert.ToString(obj.GetProperty("name"));
if (!string.IsNullOrEmpty(name))
{
name = server.HtmlEncode(name);
}
string ID = obj.ID.ToString();
bool oldPropValue = Convert.ToBoolean(obj.GetProperty(propertyName));
// checkbox is only submitted with the form when checked, unchecked will be null
string formValue = request.Form[containerName + "chk" + propertyName + obj.ID.ToString()];
if (formValue != null !oldPropValue)
{
sbResults.Append("<font class=\"editormsgsuccess\"><br>'");
sbResults.Append(name);
sbResults.Append("' (ID=");
sbResults.Append(ID);
sbResults.Append(") " + propertyName + " has been set to True.</font>");
obj.SetProperty(propertyName, true);
}
else if (formValue == null oldPropValue)
{
sbResults.Append("<font class=\"editormsgsuccess\"><br>'");
sbResults.Append(name);
sbResults.Append("' (ID=");
sbResults.Append(ID);
sbResults.Append(") " + propertyName + " has been set to False.</font>");
obj.SetProperty(propertyName, false);
}
}
public static void UpdateDropDownListProperty(VwdCms.IBizObject obj, string controlName, string propertyName, StringBuilder sbResults, Control parentControl)
{
HttpContext context = HttpContext.Current;
HttpServerUtility server = context.Server;
HttpRequest request = context.Request;
string containerName = GetContainerName(parentControl);
string ID = obj.ID.ToString();
string formValue = request.Form[containerName + "ddl" + controlName + obj.ID.ToString()];
if (formValue != null formValue != Convert.ToString(obj.GetProperty(propertyName)))
{
obj.SetProperty(propertyName, formValue);
sbResults.Append("<font class=\"editormsgsuccess\"><br>'");
sbResults.Append("The '" + controlName + "' has been changed for ID=");
sbResults.Append(ID);
sbResults.Append("</font>");
}
}
public static void DeleteItems(VwdCms.IBizCollection col, StringBuilder sbResults, Control parentControl)
{
HttpContext context = HttpContext.Current;
HttpServerUtility server = context.Server;
HttpRequest request = context.Request;
string containerName = GetContainerName(parentControl);
string formValue = null;
// check for deleted items in the request.form
int delindex = 0;
VwdCms.IBizObject objdel = null;
string dependencies = null;
for (delindex = col.Count - 1; delindex = 0; delindex--)
{
objdel = col[delindex];
//checkbox is only submitted when checked
formValue = request.Form[containerName + "chkDelete" + objdel.ID.ToString()];
if (formValue != null)
{
//dependencies = CheckDependencies(cn, objdel);
if (string.IsNullOrEmpty(dependencies))
{
sbResults.Append("<font class=\"editormsgsuccess\"><br>'");
sbResults.Append(server.HtmlEncode(Convert.ToString(objdel.GetProperty("name"))));
sbResults.Append("' (ID=");
sbResults.Append(objdel.ID.ToString());
sbResults.Append(") has been deleted.</font>");
col.Remove(objdel);
}
else
{
sbResults.Append("<font class=\"editormsgerror\"><br>'");
sbResults.Append(server.HtmlEncode(Convert.ToString(objdel.GetProperty("name"))));
sbResults.Append(" (ID=");
sbResults.Append(objdel.ID.ToString());
sbResults.Append(") could not be deleted because the following objects depend on it:</font>");
sbResults.Append("<div style=\"border:1px solid silver;font-size:8pt;width:600px;height:60px;overflow:auto;\">");
sbResults.Append(dependencies);
sbResults.Append("</div>");
}
}
}
}
public static bool ValidateFormInput(string input)
{
input = HttpUtility.HtmlDecode(input);
// create a regex that matches HTML markup
System.Text.RegularExpressions.Regex reg = null;
reg = new System.Text.RegularExpressions.Regex(@"[<>]");
if (reg.IsMatch(input))
{
//html tags are not allowed
return false;
}
else
{
return true;
}
}
public static string GetContainerName(Control control)
{
return GetContainerName(control, true);
}
public static string GetContainerName(Control control, bool includeSeparatorChar)
{
string name = string.Empty;
if (control != null)
{
Control container = control.NamingContainer;
if (container != null container != control)
{
name = container.UniqueID;
if (includeSeparatorChar)
{
name += "$";
}
}
}
return name;
}
public static string GetContainerID(Control control)
{
return GetContainerID(control, true);
}
public static string GetContainerID(Control control, bool includeSeparatorChar)
{
string id = string.Empty;
if (control != null)
{
Control container = control.NamingContainer;
if (container != null container != control)
{
id = container.ClientID;
if (includeSeparatorChar)
{
id += "_";
}
}
}
return id;
}
public static Control AddCell(EditorCellTypes cellType, TableRow tr, VwdCms.IBizObject obj, string propertyName, int newitemnum)
{
TableCell tc = new TableCell();
TextBox txt = null;
CheckBox chk = null;
DateTime date = DateTime.MinValue;
Control control = null;
switch (cellType)
{
case EditorCellTypes.ID:
tc.CssClass = "editorcellcenter";
if (obj.ID == 0)
{
tc.Text = "*";
}
else
{
tc.Text = obj.ID.ToString();
}
control = tc;
break;
case EditorCellTypes.Date:
tc.CssClass = "editorcellcenter";
date = Convert.ToDateTime(obj.GetProperty(propertyName));
if (date = System.Data.SqlTypes.SqlDateTime.MinValue.Value)
{
tc.Text = "-";
}
else
{
tc.Text = date.ToShortDateString() + " " + date.ToShortTimeString();
}
control = tc;
break;
case EditorCellTypes.Text:
tc.CssClass = "editorcellcenter";
if (obj.ID == 0)
{
tc.Text = "*";
}
else
{
tc.Text = obj.ID.ToString();
}
control = tc;
break;
case EditorCellTypes.TextBox:
txt = new TextBox();
tc.Controls.Add(txt);
txt.CssClass = "editorhilitetextboxoff";
if (obj.ID == 0)
{
txt.ID = "txt" + propertyName + "New" + newitemnum.ToString();
}
else
{
txt.ID = "txt" + propertyName + obj.ID.ToString();
}
tc.CssClass = "editorcellleft";
txt.Text = Convert.ToString(obj.GetProperty(propertyName));
txt.Attributes.Add("onfocus", "showTextboxBorder(this);");
txt.EnableViewState = false;
control = txt;
break;
case EditorCellTypes.CheckBox:
chk = new CheckBox();
tc.Controls.Add(chk);
tc.CssClass = "editorcellcenter";
if (obj.ID == 0)
{
chk.ID = "chk" + propertyName + "New" + newitemnum.ToString();
}
else
{
chk.ID = "chk" + propertyName + obj.ID.ToString();
}
chk.Checked = Convert.ToBoolean(obj.GetProperty(propertyName));
chk.EnableViewState = false;
control = chk;
break;
case EditorCellTypes.Delete:
if (obj.ID == 0)
{
tc.Text = " ";
}
else
{
chk = new CheckBox();
tc.Controls.Add(chk);
tc.HorizontalAlign = HorizontalAlign.Center;
chk.ID = "chkDelete" + obj.ID.ToString();
chk.Checked = false;
chk.EnableViewState = false;
chk.Attributes.Add("onclick", "deleteChecked(this);");
}
control = chk;
break;
}
tr.Cells.Add(tc);
return control;
}
public static TableCell AddHeaderCell(TableRow tr, string title, CommandEventHandler sortHandler)
{
return AddHeaderCell(tr, title, sortHandler, null, null);
}
public static TableCell AddHeaderCell(TableRow tr, string title, CommandEventHandler sortHandler, string sortKey)
{
return AddHeaderCell(tr, title, sortHandler, sortKey, null);
}
public static TableCell AddHeaderCell(TableRow tr, string title, CommandEventHandler sortHandler, string sortKey, string cssClass)
{
TableCell tc = null;
LinkButton lnk = null;
if (string.IsNullOrEmpty(sortKey))
{
sortKey = title;
}
tc = new TableCell();
if (sortHandler != null)
{
if (cssClass == null)
{
tc.CssClass = "editorheadercellcenter";
}
else
{
tc.CssClass = cssClass;
}
lnk = new LinkButton();
lnk.ID = "lnkSort" + sortKey;
lnk.CssClass = "editorheaderlink";
lnk.CommandArgument = sortKey;
lnk.CausesValidation = false;
lnk.Command += sortHandler;
lnk.Text = title;
tc.Controls.Add(lnk);
}
else
{
if (cssClass == null)
{
tc.CssClass = "editorheaderlabel";
}
else
{
tc.CssClass = cssClass;
}
tc.Text = title;
}
tr.Cells.Add(tc);
return tc;
}
}
}