using System;
using System.Web.UI.WebControls;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.ExtendedControls;
using CMS.FormEngine;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.TranslationServices;
using CMS.UIControls;
public partial class CMSModules_AdminControls_Controls_Class_FieldEditor_DatabaseConfiguration : CMSUserControl
{
#region "Events"
///
/// Event raised when drop-down list is changed.
///
public event EventHandler DropChanged;
///
/// Event raised when attribute is changed.
///
public event EventHandler AttributeChanged;
#endregion
#region "Properties"
///
/// Field editor mode.
///
public FieldEditorModeEnum Mode
{
get;
set;
}
///
/// FormFieldInfo of given field.
///
public FormFieldInfo FieldInfo
{
get;
set;
}
///
/// Gets or sets value indicating if current field is primary.
///
public bool IsFieldPrimary
{
get;
set;
}
///
/// Gets or sets value indicating if System fields should be enabled.
///
public bool EnableSystemFields
{
get;
set;
}
///
/// Gets or sets value indicating if System fields should be displayed.
///
public bool ShowSystemFields
{
get;
set;
}
///
/// Gets or sets attribute name.
///
public string AttributeName
{
get
{
return txtAttributeName.Text.Trim();
}
set
{
txtAttributeName.Text = value;
}
}
///
/// Gets value indicating new item is being edited.
///
public bool IsNewItemEdited
{
get
{
return ValidationHelper.GetBoolean(ViewState["IsNewItemEdited"], false);
}
set
{
ViewState["IsNewItemEdited"] = value;
}
}
///
/// Indicates if document type is edited.
///
public bool IsDocumentType
{
get;
set;
}
///
/// Indicates if field editor works in development mode.
///
public bool DevelopmentMode
{
get;
set;
}
///
/// Gets or sets value which should be selected in group selector.
///
public string GroupValue
{
get
{
return drpGroup.SelectedValue;
}
}
///
/// Gets or sets value which should be selected in system fields selector.
///
public string SystemValue
{
get
{
return drpSystemFields.SelectedValue;
}
}
///
/// Returns True if system fields are enabled and one of them is selected.
///
public bool IsSystemFieldSelected
{
get
{
return (EnableSystemFields && plcGroup.Visible);
}
}
///
/// Returns text value from Attribute size textbox.
///
public string AttributeSize
{
get
{
return txtAttributeSize.Text.Trim();
}
}
///
/// Returns text value from Default value textbox.
///
public string DefaultValueText
{
get
{
return txtDefaultValue.Text.Trim();
}
}
///
/// Returns text value from Large Default value text area.
///
public string LargeDefaultValueText
{
get
{
return txtLargeDefaultValue.Value.ToString().Trim();
}
}
///
/// Gets value which is contained in default Date time picker.
///
public string DefaultDateTime
{
get
{
return datetimeDefaultValue.DateTimeTextBox.Text.Trim();
}
}
///
/// Gets value which indicates if checkbox Default value is checked.
///
public bool DefaultBoolValue
{
get
{
return chkDefaultValue.Checked;
}
}
///
/// Gets or sets selected attribute type.
///
public string AttributeType
{
get
{
return drpAttributeType.SelectedValue.ToLowerCSafe();
}
set
{
if (!String.IsNullOrEmpty(value))
{
drpAttributeType.SelectedValue = value;
}
}
}
///
/// Gets or sets value indicating if field allows empty values.
///
public bool AllowEmpty
{
get
{
return chkAllowEmpty.Checked;
}
}
///
/// Gets or sets value indicating if field is system field.
///
public bool IsSystem
{
get
{
return chkIsSystem.Checked;
}
}
///
/// Gets or sets value indicating if a field is included to translation services export.
///
public bool TranslateField
{
get
{
return chkTranslateField.Checked && plcTranslation.Visible;
}
}
///
/// Indicates if Field Editor is used as alternative form.
///
public bool IsAlternativeForm
{
get;
set;
}
///
/// Class name.
///
public string ClassName
{
get;
set;
}
///
/// Coupled class name.
///
public string CoupledClassName
{
get;
set;
}
#endregion
#region "Public methods"
protected void Page_Load(object sender, EventArgs e)
{
pnlDatabase.GroupingText = GetString("templatedesigner.section.database");
}
///
/// Loads group drop-down list with data.
///
public void LoadGroupField()
{
// Set field types
if (EnableSystemFields && (drpGroup.Items.Count == 0))
{
drpGroup.Items.Add(new ListItem(GetString("TemplateDesigner.DocumentAttributes"), "cms_document"));
drpGroup.Items.Add(new ListItem(GetString("TemplateDesigner.NodeAttributes"), "cms_tree"));
}
}
///
/// Loads field with values from FormFieldInfo object.
///
public void Reload(string attributeName, bool enableSystemDDL)
{
if (EnableSystemFields && ShowSystemFields)
{
plcGroup.Visible = true;
drpSystemFields.Visible = true;
txtAttributeName.Visible = false;
drpGroup.Enabled = enableSystemDDL;
drpSystemFields.Enabled = enableSystemDDL;
// Get system table name
string tableName;
if ((string.IsNullOrEmpty(attributeName)) || (attributeName.ToLowerCSafe().StartsWithCSafe("document")))
{
// Fields from table CMS_Document
tableName = "cms_document";
}
else
{
// Fields from table CMS_Node
tableName = "cms_tree";
}
drpGroup.SelectedValue = tableName;
// Select system attribute name in dropdownlist
SetSystemAttributeName(tableName, attributeName);
}
else
{
// Show textbox only
plcGroup.Visible = false;
drpSystemFields.Visible = false;
txtAttributeName.Visible = true;
txtAttributeName.Text = AttributeName;
}
LoadAttributeTypes();
if (FieldInfo != null)
{
if (DevelopmentMode)
{
lblGuidValue.Text = FieldInfo.Guid.ToString();
}
txtAttributeName.Text = FieldInfo.Name;
txtAttributeSize.Text = (FieldInfo.Size == 0) ? "" : Convert.ToString(FieldInfo.Size);
// Select attribute type
string selectedItem = FormHelper.GetFormFieldDataTypeString(FieldInfo.DataType);
if ((FieldInfo.DataType == FormFieldDataTypeEnum.Text) && (FieldInfo.Size == 500) && ((Mode == FieldEditorModeEnum.BizFormDefinition) || (Mode == FieldEditorModeEnum.SystemTable))
&& IsFileControl())
{
selectedItem = FormFieldDataTypeCode.FILE;
}
if (drpAttributeType.Items.FindByValue(selectedItem) != null)
{
drpAttributeType.SelectedValue = selectedItem;
}
chkAllowEmpty.Checked = FieldInfo.AllowEmpty;
chkIsSystem.Checked = (FieldInfo.System || (FieldInfo.PrimaryKey && !IsDocumentType));
plcIsSystem.Visible = DevelopmentMode;
chkTranslateField.Checked = FieldInfo.TranslateField;
SetDefaultValue();
ShowDefaultControl();
}
// Clear form in FormFieldInfo not specified
else
{
lblGuidValue.Text = null;
drpAttributeType.SelectedValue = null;
txtAttributeName.Text = null;
txtDefaultValue.Text = "";
txtLargeDefaultValue.Value = "";
txtAttributeSize.Text = null;
chkDefaultValue.Checked = false;
chkAllowEmpty.Checked = (Mode == FieldEditorModeEnum.SystemTable);
chkIsSystem.Checked = DevelopmentMode && !IsDocumentType;
datetimeDefaultValue.SelectedDateTime = DateTimePicker.NOT_SELECTED;
plcIsSystem.Visible = DevelopmentMode;
chkTranslateField.Checked = false;
}
// If key is primary, disable "Allow null" checkbox
if (IsFieldPrimary)
{
chkAllowEmpty.Enabled = false;
chkAllowEmpty.Checked = false;
}
// Primary key not allowed to change
// System field not allowed to change unless development mode
if ((FieldInfo != null) && (FieldInfo.PrimaryKey || (FieldInfo.System && !EnableSystemFields)))
{
bool enableDefault = ((Mode != FieldEditorModeEnum.SystemTable) || (IsAlternativeForm)) && !FieldInfo.PrimaryKey;
DisableFieldEditing(FieldInfo.External, enableDefault);
}
else
{
EnableFieldEditing();
EnableDisableSections();
}
chkIsSystem.Enabled &= !IsDocumentType;
EnableOrDisableAttributeSize();
}
///
/// Sets default value according to attribute type.
///
public void SetDefaultValue()
{
txtDefaultValue.Text = "";
txtLargeDefaultValue.Value = "";
chkDefaultValue.Checked = false;
datetimeDefaultValue.SelectedDateTime = DateTimePicker.NOT_SELECTED;
if (FieldInfo != null)
{
switch (AttributeType)
{
case FormFieldDataTypeCode.DATETIME:
if (string.IsNullOrEmpty(FieldInfo.DefaultValue))
{
datetimeDefaultValue.DateTimeTextBox.Text = "";
}
else if (FieldInfo.DefaultValue.ToLowerCSafe() == DateTimePicker.DATE_TODAY.ToLowerCSafe())
{
datetimeDefaultValue.DateTimeTextBox.Text = DateTimePicker.DATE_TODAY;
}
else if (FieldInfo.DefaultValue.ToLowerCSafe() == DateTimePicker.TIME_NOW.ToLowerCSafe())
{
datetimeDefaultValue.DateTimeTextBox.Text = DateTimePicker.TIME_NOW;
}
else if (ValidationHelper.IsMacro(FieldInfo.DefaultValue))
{
datetimeDefaultValue.DateTimeTextBox.Text = FieldInfo.DefaultValue;
}
else
{
datetimeDefaultValue.SelectedDateTime = FormHelper.GetDateTimeValueInCurrentCulture(FieldInfo.DefaultValue);
}
break;
case FormFieldDataTypeCode.BOOLEAN:
chkDefaultValue.Checked = ValidationHelper.GetBoolean(FieldInfo.DefaultValue, false);
break;
case FormFieldDataTypeCode.LONGTEXT:
txtLargeDefaultValue.Value = FieldInfo.DefaultValue;
break;
case FormFieldDataTypeCode.DOUBLE:
if (ValidationHelper.IsMacro(FieldInfo.DefaultValue))
{
txtDefaultValue.Text = FieldInfo.DefaultValue;
}
else
{
txtDefaultValue.Text = FormHelper.GetDoubleValueInCurrentCulture(FieldInfo.DefaultValue);
}
break;
default:
txtDefaultValue.Text = FieldInfo.DefaultValue;
break;
}
}
}
///
/// Returns attribute name.
///
public string GetAttributeName()
{
if (drpSystemFields.Visible)
{
return drpSystemFields.SelectedValue;
}
return txtAttributeName.Text.Trim();
}
///
/// Enable or disable size attribute.
///
public void EnableOrDisableAttributeSize()
{
if (AttributeType == "text")
{
if (IsAlternativeForm || IsSystemFieldSelected)
{
txtAttributeSize.Enabled = false;
}
else
{
txtAttributeSize.Enabled = true;
}
txtAttributeSize.MaxLength = 9;
}
else
{
txtAttributeSize.Enabled = false;
txtAttributeSize.Text = "";
}
if (AttributeType == "docattachments")
{
chkAllowEmpty.Checked = true;
}
chkAllowEmpty.Enabled = ((AttributeType != "docattachments") && (((Mode == FieldEditorModeEnum.SystemTable) && IsAlternativeForm) || (Mode != FieldEditorModeEnum.SystemTable)));
if (AttributeType == "binary")
{
chkAllowEmpty.Checked = true;
chkAllowEmpty.Enabled = false;
txtDefaultValue.Enabled = false;
}
if (IsAlternativeForm && !GetFormAllowEmpty())
{
chkAllowEmpty.Enabled = false;
}
}
///
/// Disables field editing controls.
///
/// Indicates if field name should remain enabled
/// Indicates if control for default value should remain enabled
public void DisableFieldEditing(bool enableName, bool enableDefault)
{
txtDefaultValue.Enabled = enableDefault;
txtLargeDefaultValue.Enabled = enableDefault;
chkDefaultValue.Enabled = enableDefault;
datetimeDefaultValue.Enabled = enableDefault;
drpAttributeType.Enabled = false;
chkAllowEmpty.Enabled = false;
EnableDisableAttributeName(enableName);
chkIsSystem.Enabled = false;
txtAttributeSize.Enabled = false;
}
///
/// Enables field editing controls, except field name.
///
public void EnableFieldEditing()
{
txtDefaultValue.Enabled = true;
txtLargeDefaultValue.Enabled = true;
chkDefaultValue.Enabled = true;
datetimeDefaultValue.Enabled = true;
drpAttributeType.Enabled = true;
chkAllowEmpty.Enabled = (Mode != FieldEditorModeEnum.SystemTable) && !IsAlternativeForm;
chkIsSystem.Enabled = true;
EnableDisableAttributeName(true);
}
///
/// Show default value control according to attribute type.
///
public void ShowDefaultControl()
{
plcDefaultValue.Visible = true;
SetFieldForTranslations();
switch (AttributeType)
{
case FormFieldDataTypeCode.DATETIME:
datetimeDefaultValue.Visible = true;
chkDefaultValue.Visible = false;
txtLargeDefaultValue.Visible = false;
txtDefaultValue.Visible = false;
break;
case FormFieldDataTypeCode.BOOLEAN:
chkDefaultValue.Visible = true;
txtLargeDefaultValue.Visible = false;
txtDefaultValue.Visible = false;
datetimeDefaultValue.Visible = false;
break;
case FormFieldDataTypeCode.LONGTEXT:
txtLargeDefaultValue.Visible = true;
chkDefaultValue.Visible = false;
txtDefaultValue.Visible = false;
datetimeDefaultValue.Visible = false;
break;
case FormFieldDataTypeCode.FILE:
// Hide default value for File-type fields within Document types
if (Mode == FieldEditorModeEnum.ClassFormDefinition)
{
plcDefaultValue.Visible = false;
}
// Display textbox otherwise
else
{
txtDefaultValue.Visible = true;
chkDefaultValue.Visible = false;
txtLargeDefaultValue.Visible = false;
datetimeDefaultValue.Visible = false;
}
break;
default:
txtDefaultValue.Visible = true;
chkDefaultValue.Visible = false;
txtLargeDefaultValue.Visible = false;
datetimeDefaultValue.Visible = false;
break;
}
}
#endregion
#region "Private methods"
///
/// Fill attribute types list.
///
private void LoadAttributeTypes()
{
if (drpAttributeType.Items.Count > 0)
{
return;
}
drpAttributeType.DataSource = FormHelper.GetFieldTypes(IsDocumentType, DevelopmentMode);
drpAttributeType.DataBind();
// Remove file type when editing webpart properties
if ((Mode == FieldEditorModeEnum.WebPartProperties) || (Mode == FieldEditorModeEnum.CustomTable) || (Mode == FieldEditorModeEnum.SystemTable) || (Mode == FieldEditorModeEnum.FormControls))
{
ListItem item = drpAttributeType.Items.FindByValue(FormFieldDataTypeCode.FILE);
if (item != null)
{
drpAttributeType.Items.Remove(item);
}
}
if (drpAttributeType.Items.Count > 0)
{
drpAttributeType.SelectedIndex = 0;
}
}
///
/// Sets system attribute name.
///
private void SetSystemAttributeName(string tableName, string attributeName)
{
TableManager tm = new TableManager(null);
// Load system fields from specified table
drpSystemFields.DataSource = tm.GetColumnInformation(tableName);
drpSystemFields.DataBind();
try
{
drpSystemFields.SelectedValue = attributeName;
}
catch
{
if (drpSystemFields.Items.Count > 0)
{
drpSystemFields.SelectedIndex = 0;
}
}
}
///
/// Enable or disable field for translation according to actual Attribute Type (from DropDown list)
///
private void SetFieldForTranslations()
{
FormFieldDataTypeEnum dataType = FormHelper.GetFormFieldDataType(AttributeType);
if (TranslationServiceHelper.IsFieldForTranslation(dataType))
{
plcTranslation.Visible = true;
}
else
{
plcTranslation.Visible = false;
chkTranslateField.Checked = false;
}
}
///
/// Enables or disables control related to attribute name.
///
private void EnableDisableAttributeName(bool enable)
{
drpGroup.Enabled = enable && IsNewItemEdited;
drpSystemFields.Enabled = enable && IsNewItemEdited;
txtAttributeName.Enabled = enable;
}
///
/// Enables or disables sections of the unigrid according to the selected mode.
///
private void EnableDisableSections()
{
EnableDisableAttributeName(!IsAlternativeForm);
drpAttributeType.Enabled = !IsAlternativeForm && (!IsSystem || !IsSystemFieldSelected && DevelopmentMode);
txtAttributeSize.Enabled = !IsAlternativeForm;
}
///
/// Used only for alternative forms. If current field in class allows empty then it returns TRUE.
///
private bool GetFormAllowEmpty()
{
// Check if field exists in class
FormInfo fi = FormHelper.GetFormInfo(ClassName, false);
FormFieldInfo ffi;
if (fi != null)
{
ffi = fi.GetFormField(FieldInfo.Name);
if (ffi != null)
{
return ffi.AllowEmpty;
}
}
// Check if field exists in coupled class
if (!String.IsNullOrEmpty(CoupledClassName))
{
fi = FormHelper.GetFormInfo(CoupledClassName, false);
if (fi != null)
{
ffi = fi.GetFormField(FieldInfo.Name);
if (ffi != null)
{
return ffi.AllowEmpty;
}
}
}
return false;
}
///
/// Returns if field's control is of file type.
///
private bool IsFileControl()
{
if (FieldInfo == null)
{
return false;
}
// Compare the control against default upload control
if (FormHelper.IsFieldOfType(FieldInfo, FormFieldControlTypeEnum.UploadControl))
{
return true;
}
// Get form user control
string controlName = ValidationHelper.GetString(FieldInfo.Settings["controlname"], string.Empty);
if (!string.IsNullOrEmpty(controlName))
{
FormUserControlInfo controlInfo = FormUserControlInfoProvider.GetFormUserControlInfo(controlName);
if (controlInfo != null)
{
return ((Mode == FieldEditorModeEnum.SystemTable) && controlInfo.UserControlForFile)
|| ((Mode == FieldEditorModeEnum.BizFormDefinition) && (controlInfo.UserControlDefaultDataType.EqualsCSafe("file", StringComparison.InvariantCultureIgnoreCase)
|| controlInfo.UserControlForFile));
}
}
return false;
}
#endregion
#region "Event handlers"
///
/// Drop-down list event handler.
///
protected void drpGroup_SelectedIndexChanged(object sender, EventArgs e)
{
SetSystemAttributeName(drpGroup.SelectedValue, "");
if (DropChanged != null)
{
DropChanged(this, EventArgs.Empty);
}
}
///
/// Drop-down system fields event handler.
///
protected void drpSystemFields_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropChanged != null)
{
DropChanged(this, EventArgs.Empty);
}
}
///
/// Listbox attribute type event handler.
///
protected void drpAttributeType_SelectedIndexChanged(object sender, EventArgs e)
{
if (AttributeChanged != null)
{
AttributeChanged(this, EventArgs.Empty);
}
}
#endregion
}