using System; using System.Text; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.UIControls; public partial class CMSModules_Settings_Controls_SettingsCategoryEdit : CMSAdminEditControl { #region "Private Members" private SettingsCategoryInfo mSettingsCategoryObj = null; private int mCategoryOrder = 0; private int mSettingsCategoryId = 0; private int mRootCategoryId = 0; private bool mIncludeRootCategory = false; private bool mIsGroupEdit = false; private bool mIsCustom = false; private bool mEnabled = true; private string mTreeRefreshUrl = null; private string mHeaderRefreshUrl = null; private string mContentRefreshUrl = null; private bool mShowParentSelector = true; #endregion #region "Properties" /// /// Gets or sets Is Custom property. If set to true SettingsCategory will be marked as custom settings category. /// public bool IsCustom { get { return mIsCustom; } set { mIsCustom = value; } } /// /// Gets or sets Category order. Specifies order of the category. /// public int CategoryOrder { get { return mCategoryOrder; } set { mCategoryOrder = value; } } /// /// Gets or sets RootCategoryID. Specifies SettingsCategory which should be set up as the root of the SettingsCategorySelector. /// public int RootCategoryID { get { return mRootCategoryId; } set { mRootCategoryId = value; } } /// /// Gets or sets enabled state of inclusion of RootCategory. Default false. /// public bool IncludeRootCategory { get { if (SettingsCategoryObj == null) { return true; } return mIncludeRootCategory; } set { mIncludeRootCategory = value; } } /// /// Gets or sets SettingsCategoryID. Specifies Id of SettingsCategory object. /// public int SettingsCategoryID { get { return mSettingsCategoryId; } set { mSettingsCategoryId = value; drpCategory.CurrentCategoryId = value; mSettingsCategoryObj = null; } } /// /// Gets or sets SettingsCategory object. Specifies SettingsCategory object which should be edited. /// public SettingsCategoryInfo SettingsCategoryObj { get { return mSettingsCategoryObj ?? (mSettingsCategoryObj = (mSettingsCategoryId > 0) ? SettingsCategoryInfoProvider.GetSettingsCategoryInfo(mSettingsCategoryId) : null); } set { mSettingsCategoryObj = value; if (value != null) { mSettingsCategoryId = value.CategoryID; mCategoryOrder = value.CategoryOrder; } else { mSettingsCategoryId = 0; mCategoryOrder = 0; } } } /// /// Gets or sets visible state of parent category selector. /// public bool ShowParentSelector { get { return mShowParentSelector; } set { mShowParentSelector = value; } } /// /// Gets or sets IsGroupEdit property. If set to true parent category selector will be hidden. /// public bool IsGroupEdit { get { return mIsGroupEdit; } set { mIsGroupEdit = value; } } /// /// Gets or sets Enabled property. If set to false all child control are disabled. /// public bool Enabled { get { return mEnabled; } set { mEnabled = value; } } /// /// Gets or sets DisplayOnlyCategories property of SelectSettingsCategory drop down list. If set to false, groups wil be included. /// public bool DisplayOnlyCategories { get { return drpCategory.DisplayOnlyCategories; } set { drpCategory.DisplayOnlyCategories = value; } } /// /// Url for refreshing tree with settings. /// public string TreeRefreshUrl { get { return mTreeRefreshUrl; } set { mTreeRefreshUrl = value; } } /// /// Url for refreshing header. /// public string HeaderRefreshUrl { get { return mHeaderRefreshUrl; } set { mHeaderRefreshUrl = value; } } public string ContentRefreshUrl { get { return mContentRefreshUrl; } set { mContentRefreshUrl = value; } } #endregion #region "Page Evenets" protected void Page_Load(object sender, EventArgs e) { if (StopProcessing) { return; } InitControls(); // Load the form data if (!URLHelper.IsPostback()) { LoadData(); } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Set enability txtCategoryDisplayName.Enabled = rfvCategoryDisplayName.Enabled = txtCategoryName.Enabled = rfvCategoryName.Enabled = txtIconPath.Enabled = drpCategory.Enabled = btnOk.Enabled = mEnabled; // Set css for the button btnOk.CssClass = mEnabled ? "SubmitButton" : "SubmitButton SubmitButtonDisabled"; // Set the root category if (RootCategoryID > 0) { drpCategory.RootCategoryId = RootCategoryID; drpCategory.IncludeRootCategory = IncludeRootCategory; drpCategory.ReloadData(); } // Set parrent category if ((SettingsCategoryObj != null) && (SettingsCategoryObj.CategoryParentID > 0)) { if (drpCategory.Items.Count == 0) { drpCategory.ReloadData(); } drpCategory.SelectedCategory = SettingsCategoryObj.CategoryParentID; } // Display parent category selector trParentCategory.Visible = trParentCategory.Visible && mShowParentSelector; trIconPath.Visible = !mIsGroupEdit; trIsCustom.Visible = !mIsCustom; } #endregion #region "Private Methods" /// /// Validates the form. If validation succeeds returns true, otherwise returns false. /// private bool IsValid() { // Validate required fields string errMsg = new Validator().NotEmpty(txtCategoryName.Text.Trim(), GetString("General.RequiresCodeName")) .NotEmpty(txtCategoryDisplayName.Text.Trim(), GetString("General.RequiresDisplayName")) .Result; if (!ValidationHelper.IsCodeName(txtCategoryName.Text.Trim())) { errMsg = GetString("General.ErrorCodeNameInIdentifierFormat"); } // Set up error message if (!string.IsNullOrEmpty(errMsg)) { ShowError(errMsg); return false; } return true; } /// /// Initialization of controls. /// private void InitControls() { // Init validators rfvCategoryDisplayName.ErrorMessage = ResHelper.GetString("general.requiresdisplayname"); rfvCategoryName.ErrorMessage = ResHelper.GetString("general.requirescodename"); } /// /// Loads the data into the form. /// private void LoadData() { // Load the form from the Info object if (SettingsCategoryObj != null) { txtCategoryName.Text = SettingsCategoryObj.CategoryName; txtCategoryDisplayName.Text = SettingsCategoryObj.CategoryDisplayName; txtIconPath.Text = SettingsCategoryObj.CategoryIconPath; chkIsCustom.Checked = SettingsCategoryObj.CategoryIsCustom; if (SettingsCategoryObj.CategoryParentID > 0) { if (drpCategory.Items.Count == 0) { drpCategory.ReloadData(); } drpCategory.SelectedCategory = SettingsCategoryObj.CategoryParentID; } } else { trParentCategory.Visible = false; } } #endregion #region "Event Handlers" /// /// Handles OnClick event of btnOk. /// /// Asp Button instance /// EventArgs instance protected void btnOk_Click(object sender, EventArgs e) { if (IsValid()) { // Get category by name SettingsCategoryInfo categoryObj = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName(txtCategoryName.Text.Trim()); // If name is unique OR ids are same if ((categoryObj == null) || (categoryObj.CategoryID == mSettingsCategoryId)) { SettingsCategoryInfo sci = SettingsCategoryObj; if (sci == null) { sci = new SettingsCategoryInfo(); sci.CategoryOrder = mCategoryOrder; } if (sci.CategoryParentID != drpCategory.SelectedCategory) { // When parent has been changed set the order for the category as the last possible order sci.CategoryOrder = SettingsCategoryInfoProvider.GetLastSettingsCategoryOrder(drpCategory.SelectedCategory) + 1; } sci.CategoryName = txtCategoryName.Text.Trim(); sci.CategoryDisplayName = txtCategoryDisplayName.Text.Trim(); sci.CategoryIconPath = txtIconPath.Text.Trim(); sci.CategoryParentID = drpCategory.SelectedCategory; sci.CategoryIsGroup = mIsGroupEdit; sci.CategoryIsCustom = mIsCustom || chkIsCustom.Checked; SettingsCategoryInfoProvider.SetSettingsCategoryInfo(sci); SettingsCategoryObj = sci; RaiseOnSaved(); // Set the info message if (ContentRefreshUrl == null) { ShowChangesSaved(); } // Reload header and content after save int categoryIdToShow = sci.CategoryIsGroup ? sci.CategoryParentID : sci.CategoryID; StringBuilder sb = new StringBuilder(); sb.Append("if (window.parent != null) {"); if (HeaderRefreshUrl != null) { sb.Append("if (window.parent.parent.frames['customsettingscategorytabs'] != null) {"); sb.Append("window.parent.parent.frames['customsettingscategorytabs'].location = '" + ResolveUrl(HeaderRefreshUrl) + "categoryid=" + categoryIdToShow + "';"); sb.Append("}"); sb.Append("if (window.parent.frames['customsettingscategorytabs'] != null) {"); sb.Append("window.parent.frames['customsettingscategorytabs'].location = '" + ResolveUrl(HeaderRefreshUrl) + "categoryid=" + categoryIdToShow + "';"); sb.Append("}"); } if (TreeRefreshUrl != null) { sb.Append("if (window.parent.parent.frames['customsettingstree'] != null) {"); sb.Append("window.parent.parent.frames['customsettingstree'].location = '" + ResolveUrl(TreeRefreshUrl) + "categoryid=" + categoryIdToShow + "';"); sb.Append("}"); sb.Append("if (window.parent.frames['customsettingstree'] != null) {"); sb.Append("window.parent.frames['customsettingstree'].location = '" + ResolveUrl(TreeRefreshUrl) + "categoryid=" + categoryIdToShow + "';"); sb.Append("}"); } if (ContentRefreshUrl != null) { sb.Append("window.location = '" + ResolveUrl(ContentRefreshUrl) + "categoryid=" + sci.CategoryID + "';"); } sb.Append("}"); ltlScript.Text = ScriptHelper.GetScript(sb.ToString()); } else { ShowError(GetString("general.codenameexists")); } } } #endregion }