/CMSModules/AdminControls/Controls/Class/FieldEditor/FieldEditor.ascx.cs
C# | 4441 lines | 3254 code | 594 blank | 593 comment | 625 complexity | 7e1a44de649d7a392a8359477eecba91 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Text;
- using System.Xml;
- using CMS.CMSHelper;
- using CMS.DataEngine;
- using CMS.EventLog;
- using CMS.ExtendedControls;
- using CMS.FormControls;
- using CMS.FormEngine;
- using CMS.GlobalHelper;
- using CMS.PortalEngine;
- using CMS.SettingsProvider;
- using CMS.SiteProvider;
- using CMS.UIControls;
- using CMS.ExtendedControls.ActionsConfig;
- using CMS.DocumentEngine;
- using CMS.IO;
- [Guid("B1E7505C-1699-4872-9A02-6D17FD661ABA")]
- public partial class CMSModules_AdminControls_Controls_Class_FieldEditor_FieldEditor : CMSUserControl
- {
- #region "Events"
- /// <summary>
- /// Event raised when OK button is clicked and before xml definition is changed.
- /// </summary>
- public event EventHandler OnBeforeDefinitionUpdate;
- /// <summary>
- /// Event raised when OK button is clicked and after xml definition is changed.
- /// </summary>
- public event EventHandler OnAfterDefinitionUpdate;
- /// <summary>
- /// Event raised when new field is created and form definition is saved.
- /// </summary>
- public event OnFieldCreatedEventHandler OnFieldCreated;
- /// <summary>
- /// Event raised when field name was changed.
- /// </summary>
- public event OnFieldNameChangedEventHandler OnFieldNameChanged;
- #endregion
- #region "Variables"
- private FormInfo fi = null;
- private FormFieldInfo ffi = null;
- private FormCategoryInfo fci = null;
- private IEnumerable<string> columnNames = null;
- private bool mShowFieldVisibility = false;
- private bool mDevelopmentMode = false;
- private string mClassName = String.Empty;
- private string mImageDirectoryPath = null;
- private bool mDisplaySourceFieldSelection = true;
- private int mWebPartId = 0;
- private FieldEditorModeEnum mMode;
- private FieldEditorControlsEnum mDisplayedControls = FieldEditorControlsEnum.ModeSelected;
- private bool mEnableSystemFields = false;
- private bool mEnableMacrosForDefaultValue = true;
- private string mDisplayIn = String.Empty;
- private HeaderAction btnSave;
- private HeaderAction btnSimplified;
- private HeaderAction btnAdvanced;
- private bool mIsWizard;
- #endregion
- #region "Properties"
- /// <summary>
- /// Messages placeholder
- /// </summary>
- public override MessagesPlaceHolder MessagesPlaceHolder
- {
- get
- {
- return plcMess;
- }
- }
- /// <summary>
- /// Indicates if control is used on live site.
- /// </summary>
- public override bool IsLiveSite
- {
- get
- {
- return base.IsLiveSite;
- }
- set
- {
- hdrActions.IsLiveSite = value;
- plcMess.IsLiveSite = value;
- base.IsLiveSite = value;
- }
- }
- /// <summary>
- /// Adjust the context in which the attribute can be displayed.
- /// </summary>
- public string DisplayIn
- {
- get
- {
- return mDisplayIn;
- }
- set
- {
- mDisplayIn = value;
- }
- }
- /// <summary>
- /// Indicates if system fields from tables CMS_Tree and CMS_Document are offered to the user.
- /// </summary>
- public bool EnableSystemFields
- {
- get
- {
- return mEnableSystemFields;
- }
- set
- {
- mEnableSystemFields = value;
- databaseConfiguration.EnableSystemFields = value;
- }
- }
- /// <summary>
- /// Indicates if field visibility selector should be displayed.
- /// </summary>
- public bool ShowFieldVisibility
- {
- get
- {
- return mShowFieldVisibility;
- }
- set
- {
- mShowFieldVisibility = value;
- fieldAppearance.ShowFieldVisibility = value;
- }
- }
- /// <summary>
- /// Indicates if field editor works in development mode.
- /// </summary>
- public bool DevelopmentMode
- {
- get
- {
- return mDevelopmentMode;
- }
- set
- {
- fieldAppearance.DevelopmentMode = value;
- mDevelopmentMode = value;
- }
- }
- /// <summary>
- /// Class name.
- /// </summary>
- public string ClassName
- {
- get
- {
- return mClassName;
- }
- set
- {
- mClassName = value;
- fieldAppearance.ClassName = value;
- }
- }
- /// <summary>
- /// Coupled class name.
- /// </summary>
- public string CoupledClassName
- {
- get;
- set;
- }
- /// <summary>
- /// Header actions control.
- /// </summary>
- public override HeaderActions HeaderActions
- {
- get
- {
- return hdrActions;
- }
- }
- /// <summary>
- /// Directory path for images.
- /// </summary>
- public string ImageDirectoryPath
- {
- get
- {
- if (String.IsNullOrEmpty(mImageDirectoryPath))
- {
- mImageDirectoryPath = "CMSModules/CMS_Class/";
- }
- return mImageDirectoryPath;
- }
- set
- {
- if (!value.EndsWithCSafe("/"))
- {
- mImageDirectoryPath = value + "/";
- }
- else
- {
- mImageDirectoryPath = value;
- }
- }
- }
- /// <summary>
- /// Indicates if display source field selection.
- /// </summary>
- public bool DisplaySourceFieldSelection
- {
- get
- {
- return mDisplaySourceFieldSelection;
- }
- set
- {
- mDisplaySourceFieldSelection = value;
- }
- }
- /// <summary>
- /// Webpart ID.
- /// </summary>
- public int WebPartId
- {
- get
- {
- return mWebPartId;
- }
- set
- {
- mWebPartId = value;
- }
- }
- /// <summary>
- /// Field editor mode.
- /// </summary>
- public FieldEditorModeEnum Mode
- {
- get
- {
- return mMode;
- }
- set
- {
- mMode = value;
- fieldAppearance.Mode = value;
- }
- }
- /// <summary>
- /// Type of custom controls that can be selected from the control list in FieldEditor.
- /// </summary>
- public FieldEditorControlsEnum DisplayedControls
- {
- get
- {
- return mDisplayedControls;
- }
- set
- {
- mDisplayedControls = value;
- fieldAppearance.DisplayedControls = value;
- }
- }
- /// <summary>
- /// Indicates if simplified mode is enabled.
- /// </summary>
- public bool EnableSimplifiedMode
- {
- get;
- set;
- }
- /// <summary>
- /// Form XML definition.
- /// </summary>
- public string FormDefinition
- {
- get
- {
- return ValidationHelper.GetString(ViewState["FormDefinition"], "");
- }
- set
- {
- ViewState["FormDefinition"] = value;
- }
- }
- /// <summary>
- /// Enable or disable the option to use macros as default value.
- /// </summary>
- public bool EnableMacrosForDefaultValue
- {
- get
- {
- return mEnableMacrosForDefaultValue;
- }
- set
- {
- mEnableMacrosForDefaultValue = value;
- }
- }
- /// <summary>
- /// Gets or sets value indicating if control is placed in wizard.
- /// </summary>
- public bool IsWizard
- {
- get
- {
- return mIsWizard;
- }
- set
- {
- mIsWizard = value;
- categoryEdit.IsWizard = value;
- fieldAdvancedSettings.IsWizard = value;
- }
- }
- /// <summary>
- /// Indicates if Field Editor is used as alternative form.
- /// </summary>
- public bool IsAlternativeForm
- {
- get;
- set;
- }
- /// <summary>
- /// Gets or sets alternative form full name.
- /// </summary>
- public string AlternativeFormFullName
- {
- get;
- set;
- }
- /// <summary>
- /// Shows in what control is this basic form used.
- /// </summary>
- public FormTypeEnum FormType
- {
- get
- {
- return simpleMode.FormType;
- }
- set
- {
- simpleMode.FormType = value;
- controlSettings.FormType = value;
- }
- }
- /// <summary>
- /// Enables or disables to edit <see cref='CMS.FormEngine.FormFieldInfo.Inheritable'> settings.
- /// </summary>
- public bool ShowInheritanceSettings
- {
- get
- {
- return fieldAppearance.ShowInheritanceSettings;
- }
- set
- {
- fieldAppearance.ShowInheritanceSettings = value;
- }
- }
- #endregion
- #region "Private properties"
- /// <summary>
- /// Returns True if system fields are enabled and one of them is selected.
- /// </summary>
- private bool IsSystemFieldSelected
- {
- get
- {
- return databaseConfiguration.IsSystemFieldSelected;
- }
- }
- /// <summary>
- /// Selected mode.
- /// </summary>
- private FieldEditorSelectedModeEnum SelectedMode
- {
- get
- {
- FieldEditorSelectedModeEnum mode;
- if (ViewState["SelectedMode"] == null)
- {
- mode = GetDefaultSelectedMode();
- }
- else
- {
- switch (ValidationHelper.GetInteger(ViewState["SelectedMode"], 0))
- {
- case 0:
- mode = FieldEditorSelectedModeEnum.Simplified;
- break;
- case 1:
- mode = FieldEditorSelectedModeEnum.Advanced;
- break;
- default:
- mode = GetDefaultSelectedMode();
- break;
- }
- }
- return mode;
- }
- set
- {
- switch (value)
- {
- case FieldEditorSelectedModeEnum.Simplified:
- ViewState["SelectedMode"] = 0;
- break;
- case FieldEditorSelectedModeEnum.Advanced:
- ViewState["SelectedMode"] = 1;
- break;
- }
- }
- }
- /// <summary>
- /// Indicates whether new item is edited.
- /// </summary>
- private bool IsNewItemEdited
- {
- get
- {
- return ValidationHelper.GetBoolean(ViewState["IsNewItemEdited"], false);
- }
- set
- {
- ViewState["IsNewItemEdited"] = value;
- databaseConfiguration.IsNewItemEdited = value;
- simpleMode.IsNewItemEdited = value;
- }
- }
- /// <summary>
- /// Selected item name.
- /// </summary>
- private string SelectedItemName
- {
- get
- {
- return ValidationHelper.GetString(ViewState["SelectedItemName"], "");
- }
- set
- {
- ViewState["SelectedItemName"] = value;
- }
- }
- /// <summary>
- /// Selected item type.
- /// </summary>
- private FieldEditorSelectedItemEnum SelectedItemType
- {
- get
- {
- object obj = ViewState["SelectedItemType"];
- return (obj == null) ? 0 : (FieldEditorSelectedItemEnum)obj;
- }
- set
- {
- ViewState["SelectedItemType"] = value;
- }
- }
- /// <summary>
- /// Is field primary.
- /// </summary>
- private bool IsPrimaryField
- {
- get
- {
- return ValidationHelper.GetBoolean(ViewState["IsPrimaryField"], false);
- }
- set
- {
- ViewState["IsPrimaryField"] = value;
- }
- }
- /// <summary>
- /// Indicates if document type is edited.
- /// </summary>
- private bool IsDocumentType
- {
- get
- {
- object obj = ViewState["IsDocumentType"];
- if ((obj == null) && (!string.IsNullOrEmpty(ClassName)))
- {
- DataClassInfo dci = DataClassInfoProvider.GetDataClass(mClassName);
- ViewState["IsDocumentType"] = ((dci != null) && dci.ClassIsDocumentType);
- }
- return ValidationHelper.GetBoolean(ViewState["IsDocumentType"], false);
- }
- }
- /// <summary>
- /// Gets or sets value indicating what item was selected in field type drop-down list.
- /// </summary>
- private string PreviousField
- {
- get
- {
- return ValidationHelper.GetString(ViewState["PreviousValue"], "");
- }
- set
- {
- ViewState["PreviousValue"] = value;
- }
- }
- /// <summary>
- /// Gets or sets value indicating if detailed controls are visible.
- /// </summary>
- private bool FieldDetailsVisible
- {
- get
- {
- return ValidationHelper.GetBoolean(ViewState["FieldDetailsVisible"], false);
- }
- set
- {
- ViewState["FieldDetailsVisible"] = value;
- }
- }
- /// <summary>
- /// Gets macro resolver name.
- /// </summary>
- private string ResolverName
- {
- get
- {
- if (!string.IsNullOrEmpty(ClassName))
- {
- return "form." + ClassName;
- }
- else
- {
- return "formdefinition." + FormDefinition;
- }
- }
- }
- #endregion
- #region "Global definitions"
- // Constants
- private const string newCategPreffix = "#categ##new#";
- private const string newFieldPreffix = "#field##new#";
- private const string categPreffix = "#categ#";
- private const string fieldPreffix = "#field#";
- private const int preffixLength = 7; // Length of categPreffix = length of fieldPreffix = 7
- private const string controlPrefix = "#uc#";
- #endregion
- #region "Page events"
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- var page = Page as CMSPage;
- if (page != null)
- {
- page.EnsureScriptManager();
- if (page.ScriptManagerControl != null)
- {
- var script = new ScriptReference("~/CMSScripts/RestoreLostFocus.js");
- page.ScriptManagerControl.Scripts.Add(script);
- }
- }
- CreateHeaderActions();
- // Set method delegates
- fieldAppearance.GetControls = GetControls;
- simpleMode.GetControls = GetControls;
- }
- /// <summary>
- /// Creates header actions - save, switch to advanced and switch to simple mode buttons.
- /// </summary>
- private void CreateHeaderActions()
- {
- HeaderActions.ActionsList.Clear();
- btnSave = new HeaderAction()
- {
- RegisterShortcutScript = true,
- CommandName = "save",
- CommandArgument = "save",
- Text = GetString("general.save"),
- ImageUrl = GetImageUrl("CMSModules/CMS_Content/EditMenu/save.png"),
- SmallImageUrl = GetImageUrl("CMSModules/CMS_Content/EditMenu/16/save.png")
- };
- btnSimplified = new HeaderAction()
- {
- CommandName = "simplified",
- Text = GetString("fieldeditor.tabs.simplifiedmode"),
- ControlType = HeaderActionTypeEnum.Hyperlink,
- ImageUrl = GetImageUrl("CMSModules/CMS_FormEngine/arrowup.png"),
- Visible = false
- };
- btnAdvanced = new HeaderAction()
- {
- CommandName = "advanced",
- Text = GetString("fieldeditor.tabs.advancedmode"),
- ControlType = HeaderActionTypeEnum.Hyperlink,
- ImageUrl = GetImageUrl("CMSModules/CMS_FormEngine/arrowdown.png"),
- Visible = false
- };
- HeaderActions.AddAction(btnSave);
- HeaderActions.AddAction(btnSimplified);
- HeaderActions.AddAction(btnAdvanced);
- HeaderActions.ActionPerformed += HeaderActions_ActionPerformed;
- }
- /// <summary>
- /// Actions handler.
- /// </summary>
- protected void HeaderActions_ActionPerformed(object sender, CommandEventArgs e)
- {
- switch (e.CommandName)
- {
- case "save":
- SaveField();
- break;
- case "simplified":
- SwitchToSimpleMode();
- break;
- case "advanced":
- SwitchToAdvancedMode();
- break;
- }
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (EnableSystemFields)
- {
- btnNewSysAttribute.Visible = true;
- btnNewSysAttribute.ImageUrl = GetImageUrl(ImageDirectoryPath + "NewSystem.png");
- btnNewSysAttribute.ToolTip = GetString("TemplateDesigner.NewSysAttribute");
- }
- else
- {
- btnNewSysAttribute.Visible = false;
- }
- fieldAdvancedSettings.ResolverName = categoryEdit.ResolverName = ResolverName;
- fieldAdvancedSettings.ShowDisplayInSimpleModeCheckBox = (Mode == FieldEditorModeEnum.FormControls);
- // Hide New primary attribute if we are not in system development mode
- btnNewPrimaryAttribute.Visible = DevelopmentMode;
- simpleMode.DevelopmentMode = DevelopmentMode;
- simpleMode.Mode = Mode;
- lnkCode.Visible = SettingsKeyProvider.DevelopmentMode && !IsWizard;
- lnkFormDef.Visible = SettingsKeyProvider.DevelopmentMode && !IsWizard;
- // Set images url
- btnNewCategory.ImageUrl = GetImageUrl(ImageDirectoryPath + "NewCategory.png");
- btnNewAttribute.ImageUrl = GetImageUrl(ImageDirectoryPath + "New.png");
- btnNewPrimaryAttribute.ImageUrl = GetImageUrl(ImageDirectoryPath + "NewPK.png");
- btnDeleteItem.ImageUrl = GetImageUrl(ImageDirectoryPath + "Delete.png");
- btnUpAttribute.ImageUrl = GetImageUrl(ImageDirectoryPath + "Up.png");
- btnDownAttribute.ImageUrl = GetImageUrl(ImageDirectoryPath + "Down.png");
- btnDeleteItem.ToolTip = GetString("TemplateDesigner.DeleteItem");
- btnNewCategory.ToolTip = GetString("TemplateDesigner.NewCategory");
- btnNewAttribute.ToolTip = GetString("TemplateDesigner.NewAttribute");
- btnNewPrimaryAttribute.ToolTip = GetString("TemplateDesigner.NewPrimaryAttribute");
- btnDownAttribute.ToolTip = GetString("TemplateDesigner.DownAttribute");
- btnUpAttribute.ToolTip = GetString("TemplateDesigner.UpAttribute");
- if (CMSString.Compare(DisplayIn, FormInfo.DISPLAY_CONTEXT_DASHBOARD, true) == 0)
- {
- pnlDisplayInDashBoard.Visible = true;
- }
- ltlConfirmText.Text = "<input type=\"hidden\" id=\"confirmdelete\" value=\"" + GetString("TemplateDesigner.ConfirmDelete") + "\"/>";
- btnDeleteItem.Attributes.Add("onclick", "javascript:return confirmDelete();");
- btnUpAttribute.Enabled = true;
- btnDownAttribute.Enabled = true;
- btnDeleteItem.Enabled = true;
- btnNewAttribute.Enabled = true;
- btnNewCategory.Enabled = true;
- databaseConfiguration.LoadGroupField();
- if (!URLHelper.IsPostback())
- {
- Reload(null);
- }
- else
- {
- LoadControlSettings(PreviousField, false);
- }
- // Register event handlers
- databaseConfiguration.DropChanged += databaseConfiguration_DropChanged;
- databaseConfiguration.AttributeChanged += databaseConfiguration_AttributeChanged;
- fieldAppearance.OnFieldSelected += control_FieldSelected;
- simpleMode.OnFieldSelected += control_FieldSelected;
- simpleMode.OnGetFieldInfo += simpleMode_OnGetFieldInfo;
- documentSource.OnSourceFieldChanged += documentSource_OnSourceFieldChanged;
- plcValidation.Visible = true;
- plcQuickValidation.Visible = true;
- plcSettings.Visible = true;
- }
- protected void Page_PreRender(object sender, EventArgs e)
- {
- // Display controls and quick links according to current mode
- bool displayDetails = FieldDetailsVisible && chkDisplayInForm.Checked;
- fieldAppearance.Visible = displayDetails;
- fieldAdvancedSettings.Visible = displayDetails;
- cssSettings.Visible = displayDetails;
- validationSettings.DisplayControls();
- validationSettings.Visible = displayDetails && validationSettings.Visible;
- controlSettings.CheckVisibility();
- controlSettings.Visible = displayDetails && controlSettings.Visible;
- plcQuickValidation.Visible = displayDetails && validationSettings.Visible;
- plcQuickSettings.Visible = displayDetails && controlSettings.Visible;
- plcQuickStyles.Visible = displayDetails && cssSettings.Visible;
- plcQuickAppearance.Visible = displayDetails && fieldAppearance.Visible;
- chkDisplayInDashBoard.Enabled = chkDisplayInForm.Checked;
- // Display and store last value for simple mode
- if (SelectedMode == FieldEditorSelectedModeEnum.Simplified)
- {
- PreviousField = simpleMode.FieldType;
- plcQuickSelect.Visible = false;
- }
- // Display and store last value for advanced mode
- else
- {
- PreviousField = fieldAppearance.FieldType;
- plcQuickSelect.Visible = true;
- }
- // Display or hide tabs
- btnAdvanced.Visible = btnSimplified.Visible = false;
- if (EnableSimplifiedMode)
- {
- if (SelectedItemType == FieldEditorSelectedItemEnum.Field)
- {
- btnAdvanced.Visible = (SelectedMode == FieldEditorSelectedModeEnum.Simplified);
- btnSimplified.Visible = (SelectedMode != FieldEditorSelectedModeEnum.Simplified);
- }
- }
- // Hide quick links if only 'database quick link' should be displayed
- if ((SelectedItemType == 0) || (!plcQuickValidation.Visible && !plcQuickSettings.Visible && !plcQuickStyles.Visible && !plcQuickAppearance.Visible))
- {
- plcQuickSelect.Visible = false;
- }
- // Register script for scrolling content using quick links
- ScriptHelper.RegisterJQuery(Page);
- ScriptHelper.RegisterStartupScript(this, typeof(string), "ContentSlider", ScriptHelper.GetScript(@"
- jQuery(document).ready(function(){
- jQuery('a[href*=#]').click(function() {
- if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
- && location.hostname == this.hostname) {
- var $target = jQuery(this.hash);
- $target = $target.length && $target
- || jQuery('[name=' + this.hash.slice(1) +']');
- if ($target.length) {
- var targetOffset = $target." + (IsWizard ? "position" : "offset") + @"().top;
- jQuery('" + (IsWizard ? ".FieldPanelRightWizard" : "html, body") + @"')
- .animate({scrollTop: targetOffset}, 300);
- return false;
- }
- }
- });
- });
- "));
- // Disable padding on the page caused by PageContent CSS class
- Panel contentPanel = ((CMSPage)Page).CurrentMaster.PanelContent;
- if (!IsWizard && contentPanel != null)
- {
- contentPanel.CssClass = string.Empty;
- }
- // Highlight attribute list items
- HighlightListItems();
- }
- /// <summary>
- /// Highlights attribute list items like categories, primary keys and hidden fields.
- /// </summary>
- private void HighlightListItems()
- {
- if (fi == null)
- {
- LoadFormDefinition();
- }
- if ((lstAttributes.Items.Count > 0) && (fi != null))
- {
- FormFieldInfo formField = null;
- string cssClass = null;
- foreach (ListItem li in lstAttributes.Items)
- {
- // Mark category item with different color
- if (li.Value.StartsWithCSafe(categPreffix))
- {
- li.Attributes.Add("class", "FieldEditorCategoryItem");
- }
- else
- {
- // Get form field info
- formField = fi.GetFormField(li.Value.Substring(preffixLength));
- if (formField != null)
- {
- cssClass = string.Empty;
- if (DevelopmentMode && formField.PrimaryKey)
- {
- // Highlight primary keys in the list
- cssClass = "FieldEditorPrimaryAttribute";
- }
- if (!formField.Visible)
- {
- if (!string.IsNullOrEmpty(cssClass))
- {
- cssClass += " ";
- }
- // Highlight fields that are not visible
- cssClass += "FieldEditorHiddenItem";
- }
- if (!string.IsNullOrEmpty(cssClass))
- {
- li.Attributes.Add("class", cssClass);
- }
- }
- }
- }
- }
- }
- #endregion
- #region "Methods"
- /// <summary>
- /// Reload field editor.
- /// </summary>
- /// <param name="selectedValue">Selected field in field list</param>
- public void Reload(string selectedValue)
- {
- Reload(selectedValue, false);
- }
- /// <summary>
- /// Reload field editor.
- /// </summary>
- /// <param name="selectedValue">Selected field in field list</param>
- /// <param name="partialReload">Indicates if not all controls need to be reloaded</param>
- private void Reload(string selectedValue, bool partialReload)
- {
- bool isModeSelected = false;
- bool isItemSelected = false;
- // Check for alternative form mode
- if (IsAlternativeForm)
- {
- if (!string.IsNullOrEmpty(FormDefinition))
- {
- isModeSelected = true;
- }
- else
- {
- // Clear item list
- lstAttributes.Items.Clear();
- }
- }
- else
- {
- switch (mMode)
- {
- case FieldEditorModeEnum.General:
- case FieldEditorModeEnum.FormControls:
- if (!string.IsNullOrEmpty(FormDefinition))
- {
- isModeSelected = true;
- }
- else
- {
- // Clear item list
- lstAttributes.Items.Clear();
- }
- break;
- case FieldEditorModeEnum.ClassFormDefinition:
- case FieldEditorModeEnum.BizFormDefinition:
- case FieldEditorModeEnum.SystemTable:
- case FieldEditorModeEnum.CustomTable:
- if (!string.IsNullOrEmpty(mClassName))
- {
- isModeSelected = true;
- }
- else
- {
- ShowError(GetString("fieldeditor.noclassname"));
- }
- break;
- case FieldEditorModeEnum.WebPartProperties:
- if ((mWebPartId > 0))
- {
- isModeSelected = true;
- }
- else
- {
- ShowError(GetString("fieldeditor.nowebpartid"));
- }
- break;
- default:
- ShowError(GetString("fieldeditor.nomode"));
- break;
- }
- }
- if (!partialReload)
- {
- // Display controls if mode is determined
- ShowOrHideFieldDetails(true);
- }
- if (isModeSelected)
- {
- isItemSelected = LoadInnerControls(selectedValue, partialReload);
- }
- // Hide controls when item isn't selected
- if ((!partialReload) && (!isItemSelected))
- {
- HideAllPanels();
- btnSave.Enabled = false;
- btnSave.ImageUrl = GetImageUrl("CMSModules/CMS_Content/EditMenu/savedisabled.png");
- btnUpAttribute.Enabled = false;
- btnDownAttribute.Enabled = false;
- btnDeleteItem.Enabled = false;
- btnNewAttribute.Enabled = true; // Only new items can be added
- btnNewCategory.Enabled = true; // Only new items can be added
- ShowInformation(GetString("fieldeditor.nofieldsdefined"));
- }
- DisplayOrHideActions();
- if (documentSource.VisibleContent)
- {
- documentSource.Reload();
- }
- // Show or hide field visibility selector
- fieldAppearance.ShowFieldVisibility = ShowFieldVisibility;
- }
- /// <summary>
- /// Loads inner FieldEditor controls.
- /// </summary>
- /// <returns>Returns TRUE if any item is selected</returns>
- private bool LoadInnerControls(string selectedValue, bool partialReload)
- {
- bool isItemSelected = false;
- LoadFormDefinition();
- if (!partialReload)
- {
- LoadAttributesList(selectedValue);
- }
- documentSource.FormInfo = fi;
- documentSource.Mode = Mode;
- documentSource.IsAlternativeForm = IsAlternativeForm;
- documentSource.ClassName = ClassName;
- fieldAppearance.IsAlternativeForm = IsAlternativeForm;
- fieldAppearance.AlternativeFormFullName = AlternativeFormFullName;
- if (SelectedItemType == FieldEditorSelectedItemEnum.Field)
- {
- isItemSelected = true;
- DisplaySelectedTabContent();
- ffi = fi.GetFormField(SelectedItemName);
- if (EnableSimplifiedMode)
- {
- btnAdvanced.Visible = (SelectedMode == FieldEditorSelectedModeEnum.Simplified);
- btnSimplified.Visible = (SelectedMode != FieldEditorSelectedModeEnum.Simplified);
- }
- LoadSelectedField(partialReload);
- }
- else if (SelectedItemType == FieldEditorSelectedItemEnum.Category)
- {
- isItemSelected = true;
- btnAdvanced.Visible = btnSimplified.Visible = false;
- LoadSelectedCategory();
- }
- return isItemSelected;
- }
- /// <summary>
- /// Load xml definition of the form.
- /// </summary>
- private void LoadFormDefinition()
- {
- bool isError = false;
- switch (mMode)
- {
- case FieldEditorModeEnum.General:
- // Definition is loaded from external xml
- break;
- case FieldEditorModeEnum.WebPartProperties:
- if (!IsAlternativeForm)
- {
- // Load xml definition from web part info
- WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(mWebPartId);
- if (wpi != null)
- {
- FormDefinition = wpi.WebPartProperties;
- }
- else
- {
- isError = true;
- }
- }
- break;
- case FieldEditorModeEnum.ClassFormDefinition:
- case FieldEditorModeEnum.BizFormDefinition:
- case FieldEditorModeEnum.SystemTable:
- case FieldEditorModeEnum.CustomTable:
- if (!IsAlternativeForm)
- {
- // Load xml definition from Class info
- DataClassInfo dci = DataClassInfoProvider.GetDataClass(mClassName);
- if (dci != null)
- {
- FormDefinition = dci.ClassFormDefinition;
- }
- else
- {
- isError = true;
- }
- }
- break;
- }
- if (isError)
- {
- ShowError("[ FieldEditor.LoadFormDefinition() ]: " + GetString("FieldEditor.XmlDefinitionNotLoaded"));
- }
- fi = new FormInfo(FormDefinition);
- }
- /// <summary>
- /// Fill attribute list.
- /// </summary>
- /// <param name="selectedValue">Selected value in attribute list, null if first item is selected</param>
- private void LoadAttributesList(string selectedValue)
- {
- FormFieldInfo formField = null;
- FormCategoryInfo formCategory = null;
- ListItem li = null;
- // Reload list only if new item is not edited
- if (!IsNewItemEdited)
- {
- // Clear item list
- lstAttributes.Items.Clear();
- // Get all list items (fields and categories)
- var itemList = fi.GetFormElements(true, true);
- if (itemList != null)
- {
- string itemDisplayName = null;
- string itemCodeName = null;
- foreach (object item in itemList)
- {
- if (item is FormFieldInfo)
- {
- formField = ((FormFieldInfo)(item));
- itemDisplayName = formField.Name;
- if (!formField.AllowEmpty)
- {
- itemDisplayName += ResHelper.RequiredMark;
- }
- itemCodeName = fieldPreffix + formField.Name;
- li = new ListItem(itemDisplayName, itemCodeName);
- }
- else if (item is FormCategoryInfo)
- {
- formCategory = ((FormCategoryInfo)(item));
- itemDisplayName = ResHelper.LocalizeString(formCategory.CategoryCaption);
- itemCodeName = categPreffix + formCategory.CategoryName;
- li = new ListItem(itemDisplayName, itemCodeName);
- }
- // Load list box
- if (li != null)
- {
- lstAttributes.Items.Add(li);
- }
- }
- }
- // Set selected item
- if (lstAttributes.Items.Count > 0)
- {
- if (!string.IsNullOrEmpty(selectedValue) && lstAttributes.Items.FindByValue(selectedValue) != null)
- {
- lstAttributes.SelectedValue = selectedValue;
- }
- else
- {
- // Select first item of the list
- lstAttributes.SelectedIndex = 0;
- }
- }
- // Default values - list is empty
- SelectedItemName = null;
- SelectedItemType = 0;
- // Save selected item info
- if (lstAttributes.SelectedValue != null)
- {
- if (lstAttributes.SelectedValue.StartsWithCSafe(fieldPreffix))
- {
- SelectedItemName = lstAttributes.SelectedValue.Substring(preffixLength);
- SelectedItemType = FieldEditorSelectedItemEnum.Field;
- }
- else if (lstAttributes.SelectedValue.StartsWithCSafe(categPreffix))
- {
- SelectedItemName = lstAttributes.SelectedValue.Substring(preffixLength);
- SelectedItemType = FieldEditorSelectedItemEnum.Category;
- }
- }
- }
- }
- /// <summary>
- /// Sets all values of the category edit form to defaults.
- /// </summary>
- private void LoadDefaultCategoryEditForm()
- {
- plcCategory.Visible = true;
- plcSimple.Visible = false;
- plcAdvanced.Visible = false;
- categoryEdit.Value = string.Empty;
- categoryEdit.Collapsible = false;
- categoryEdit.CollapsedByDefault = false;
- categoryEdit.VisibleMacro = string.Empty;
- categoryEdit.CategoryVisible = true;
- LoadFormDefinition();
- }
- /// <summary>
- /// Sets all values of form to defaults.
- /// </summary>
- /// <param name="system">True - empty form for node or document attribute should be loaded, False - standard form should be loaded</param>
- /// <param name="partialReload">True - indicates that only some controls should be loaded, False - reload all controls</param>
- private void LoadDefaultAttributeEditForm(bool system, bool partialReload)
- {
- ffi = null;
- plcCategory.Visible = false;
- chkDisplayInForm.Checked = true;
- chkDisplayInDashBoard.Checked = true;
- if ((SelectedMode == FieldEditorSelectedModeEnum.Advanced) && !partialReload)
- {
- databaseConfiguration.DevelopmentMode = DevelopmentMode;
- databaseConfiguration.ShowSystemFields = system;
- databaseConfiguration.IsDocumentType = IsDocumentType;
- databaseConfiguration.Mode = Mode;
- databaseConfiguration.ClassName = ClassName;
- databaseConfiguration.CoupledClassName = CoupledClassName;
- databaseConfiguration.IsAlternativeForm = IsAlternativeForm;
- databaseConfiguration.Reload("", IsNewItemEdited);
- databaseConfiguration.ShowDefaultControl();
- }
- if (system)
- {
- LoadSystemField();
- }
- if (SelectedMode == FieldEditorSelectedModeEnum.Advanced)
- {
- fieldAppearance.ClassName = ClassName;
- fieldAppearance.AttributeType = databaseConfiguration.AttributeType;
- fieldAppearance.Reload();
- fieldAdvancedSettings.Reload();
- cssSettings.Reload();
- LoadValidationSettings();
- validationSettings.DisplayControls();
- validationSettings.Reload();
- chkDisplayInForm.Checked = true;
- chkDisplayInDashBoard.Checked = true;
- }
- else
- {
- simpleMode.FieldInfo = null;
- simpleMode.DisplayedControls = DisplayedControls;
- simpleMode.Mode = Mode;
- simpleMode.ClearForm();
- simpleMode.LoadTypes();
- simpleMode.LoadControlSettings(null, true);
- }
- }
- /// <summary>
- /// Fill form with selected category data.
- /// </summary>
- private void LoadSelectedCategory()
- {
- plcAdvanced.Visible = false;
- plcSimple.Visible = false;
- plcCategory.Visible = true;
- fci = fi.GetFormCategory(SelectedItemName);
- if (fci != null)
- {
- HandleInherited(fci.IsInherited);
- categoryEdit.Value = fci.CategoryCaption;
- categoryEdit.Collapsible = fci.CategoryCollapsible;
- categoryEdit.CollapsedByDefault = fci.CategoryCollapsedByDefault;
- categoryEdit.VisibleMacro = fci.VisibleMacro;
- categoryEdit.CategoryVisible = fci.Visible;
- }
- else
- {
- LoadDefaultCategoryEditForm();
- }
- }
- /// <summary>
- /// Displays controls for advanced editing.
- /// </summary>
- private void ShowAdvancedOptions()
- {
- if (SelectedMode != FieldEditorSelectedModeEnum.Simplified)
- {
- plcSimple.Visible = false;
- plcCategory.Visible = false;
- databaseConfiguration.Visible = true;
- controlSettings.Visible = true;
- fieldAppearance.Visible = true;
- fieldAdvancedSettings.Visible = true;
- validationSettings.Visible = true;
- cssSettings.Visible = true;
- FieldDetailsVisible = true;
- }
- }
- /// <summary>
- /// Handles the inheritance of the field.
- /// </summary>
- private void HandleInherited(bool inherited)
- {
- pnlField.Enabled = true;
- btnSave.Enabled = true;
- btnDeleteItem.Visible = true;
- ShowInformation("");
- if (inherited)
- {
- // Get information on inherited class
- DataClassInfo dci = DataClassInfoProvider.GetDataClass(mClassName);
- if (dci != null)
- {
- DataClassInfo parentCi = DataClassInfoProvider.GetDataClass(dci.ClassInheritsFromClassID);
- if (parentCi != null)
- {
- pnlField.Enabled = false;
- btnSave.Enabled = false;
- btnDeleteItem.Visible = false;
- ShowInformation(String.Format(GetString("DocumentType.FieldIsInherited"), parentCi.ClassDisplayName));
- }
- }
- }
- }
- /// <summary>
- /// Fill form with selected attribute data.
- /// </summary>
- /// <param name="partialReload">Indicates if only some controls should be reloaded</param>
- private void LoadSelectedField(bool partialReload)
- {
- // Fill form
- if (ffi != null)
- {
- HandleInherited(ffi.IsInherited);
- IsPrimaryField = ffi.PrimaryKey;
- if (!partialReload)
- {
- bool controlIsInvalid = !ffi.Visible || (CMSString.Compare(ffi.Settings["controlname"] as String, FormHelper.GetFormFieldControlTypeString(FormFieldControlTypeEnum.Unknown), true) != 0);
- chkDisplayInForm.Checked = ffi.Visible && controlIsInvalid;
- chkDisplayInDashBoard.Checked = (CMSString.Compare(ffi.DisplayIn, DisplayIn, true) == 0);
- }
- DisplaySelectedTabContent();
- ShowAdvancedOptions();
- // Load controls for advanced mode
- if (SelectedMode == FieldEditorSelectedModeEnum.Advanced)
- {
- if (!partialReload)
- {
- databaseConfiguration.DevelopmentMode = DevelopmentMode;
- databaseConfiguration.ShowSystemFields = ffi.External;
- databaseConfiguration.FieldInfo = ffi;
- databaseConfiguration.IsDocumentType = IsDocumentType;
- databaseConfiguration.Mode = Mode;
- databaseConfiguration.IsAlternativeForm = IsAlternativeForm;
- databaseConfiguration.ClassName = ClassName;
- databaseConfiguration.CoupledClassName = CoupledClassName;
- databaseConfiguration.Reload(ffi.Name, IsNewItemEdited);
- }
- if (chkDisplayInForm.Checked && fieldAppearance.Visible)
- {
- fieldAppearance.Mode = Mode;
- fieldAppearance.ClassName = ClassName;
- fieldAppearance.FieldInfo = ffi;
- fieldAppearance.AttributeType = databaseConfiguration.AttributeType;
- fieldAppearance.Reload();
- }
- if (chkDisplayInForm.Checked && fieldAdvancedSettings.Visible)
- {
- fieldAdvancedSettings.FieldInfo = ffi;
- fieldAdvancedSettings.Reload();
- }
- if (chkDisplayInForm.Checked && validationSettings.Visible)
- {
- LoadValidationSettings();
- validationSettings.DisplayControls();
- validationSettings.Reload();
- }
- if (chkDisplayInForm.Checked && cssSettings.Visible)
- {
- cssSettings.FieldInfo = ffi;
- cssSettings.Reload();
- cssSettings.Enabled = true;
- }
- }
- // Load controls for simple mode
- else
- {
- simpleMode.FieldInfo = ffi;
- simpleMode.Mode = Mode;
- simpleMode.LoadTypes();
- }
- LoadControlSettings(null, false);
- }
- else
- {
- LoadDefaultAttributeEditForm(false, partialReload);
- }
- }
- /// <summary>
- /// Loads validation settings.
- /// </summary>
- private void LoadValidationSettings()
- {
- validationSettings.IsPrimary = IsPrimaryField;
- validationSettings.FieldInfo = ffi;
- validationSettings.Mode = Mode;
- validationSettings.AttributeType = databaseConfiguration.AttributeType;
- validationSettings.FieldType = fieldAppearance.FieldType;
- }
- /// <summary>
- /// Displays or hides actions according to the selected mode.
- /// </summary>
- protected void DisplayOrHideActions()
- {
- // Hide actions only when alternative form definition is edited
- if (IsAlternativeForm)
- {
- plcActions.Visible = false;
- }
- }
- protected void lnkCode_Click(object sender, EventArgs e)
- {
- GenerateCode(false);
- }
- protected void lnkFormDef_Click(object sender, EventArgs e)
- {
- GetFormDefinition();
- }
- /// <summary>
- /// Outputs the form definition XML of the current view
- /// </summary>
- protected void GetFormDefinition()
- {
- LoadFormDefinition();
- if (fi != null)
- {
- string xml = HTMLHelper.ReformatHTML(fi.GetXmlDefinition());
- // Write directly to response
- Response.Clear();
- Response.AddHeader("Content-Disposition", "attachment; filename=formDefinition.xml");
- Response.ContentType = "text/xml";
- Response.Write(xml);
- Response.End();
- }
- }
- /// <summary>
- /// Generates the class code.
- /// </summary>
- /// <param name="file">If true, the code is generated to a file, if false, it gets directly to output</param>
- protected void GenerateCode(bool file)
- {
- switch (mMode)
- {
- case FieldEditorModeEnum.WebPartProperties:
- case FieldEditorModeEnum.SystemTable:
- case FieldEditorModeEnum.FormControls:
- return;
- }
- try
- {
- // Prepare the folders
- string templateFile = Server.MapPath("~/App_Data/CodeTemplates/");
- string codeFile = Server.MapPath("~/App_Code");
- if (!Directory.Exists(codeFile))
- {
- codeFile = Server.MapPath("~/Old_App_Code");
- }
- codeFile += "/Global/AutoGenerated/";
- // Ensure the directory
- if (file)
- {
- Directory.CreateDirectory(codeFile);
- }
- // Save xml string to CMS_Class table
- DataClassInfo dci = DataClassInfoProvider.GetDataClass(mClassName);
- if (dci != null)
- {
- // Prepare the class name
- string className = dci.ClassName;
- int dotIndex = className.LastIndexOf('.');
- if (dotIndex >= 0)
- {
- className = className.Substring(dotIndex + 1);
- }
- className = ValidationHelper.GetIdentifier(className, "");
- className = (className[0]) + className.Substring(1).ToUpperCSafe();
- string originalClassName = null;
- if (dci.ClassIsDocumentType)
- {
- // Document type
- className += "Document";
- codeFile += "DocumentTypes/";
- templateFile += "DocType.template";
- originalClassName = "DocType";
- }
- else if (dci.ClassIsCustomTable)
- {
- // Custom table
- className += "Item";
- codeFile += "CustomTables/";
- templateFile += "CustomTableType.template";
- originalClassName = "CustomTableType";
- }
- else
- {
- // BizForm
- className += "Item";
- codeFile += "BizForms/";
- templateFile += "BizFormType.template";
- originalClassName = "BizFormType";
- }
- // Generate the code
- string code = File.ReadAllText(templateFile);
- StringBuilder sbInit = new StringBuilder();
- if (fi == null)
- {
- fi = new FormInfo(dci.ClassFormDefinition);
- }
- string propertiesCode = CodeGenerator.GetPropertiesCode(fi, false, null, null, false);
- // Replace in code
- code = code.Replace("##CLASSNAME##", dci.ClassName);
- code = code.Replace(originalClassName, className);
- code = code.Replace("// ##PROPERTIES##", propertiesCode);
- code = code.Replace("##SUMMARY##", dci.ClassDisplayName);
- codeFile += className + ".cs";
- if (file)
- {
- File.WriteAllText(codeFile, code);
- }
- else
- {
- // Write directly to response
- Response.Clear();
- Response.AddHeader("Content-Disposition", "attachment; filename=" + className + ".cs");
- Response.ContentType = "text/plain";
- Response.Write(code);
- Response.End();
- }
- }
- else
- {
- ShowError("[FieldEditor.UpdateFormDefinition]: " + GetString("FieldEditor.ClassNotFound"));
- }
- }
- catch (Exception ex)
- {
- // Log the error silently
- EventLogProvider.LogException("FieldEditor", "CODEGEN", ex);
- }
- }
- /// <summary>
- /// Saves the f…
Large files files are truncated, but you can click here to view the full file