using System; using System.Linq; using CMS.CMSHelper; using CMS.Ecommerce; using CMS.FormControls; using CMS.GlobalHelper; public partial class CMSModules_Ecommerce_FormControls_SelectSKUBinding : FormEngineUserControl { #region "Constants and variables" public const string CREATE_NEW = "CREATE_NEW"; public const string CREATE_NEW_GLOBAL = "CREATE_NEW_GLOBAL"; public const string USE_EXISTING = "USE_EXISTING"; public const string DO_NOT_CREATE = "DO_NOT_CREATE"; private bool mAllowDoNotCreate = true; private bool mAllowUseExisting = true; private bool mAllowCreateNewGlobal = true; private bool mAllowCreateNew = true; #endregion #region "Properties - general" /// /// Gets or sets the selected SKU binding option value as string. /// Use the defined string constants to compare or set the binding value. /// public override object Value { get { return Binding; } set { Binding = ValidationHelper.GetString(value, null); } } /// /// Gets or sets the selected SKU binding option value as string. /// Use the defined string constants to compare or set the binding value. /// public string Binding { get { if (radCreateNew.Checked) { return CREATE_NEW; } else if (radCreateNewGlobal.Checked) { return CREATE_NEW_GLOBAL; } else if (radUseExisting.Checked) { return USE_EXISTING; } else if (radDoNotCreate.Checked) { return DO_NOT_CREATE; } return CREATE_NEW; } set { radCreateNew.Checked = value == CREATE_NEW; radCreateNewGlobal.Checked = value == CREATE_NEW_GLOBAL; radUseExisting.Checked = value == USE_EXISTING; radDoNotCreate.Checked = value == DO_NOT_CREATE; } } /// /// Gets the SKU ID of the selected SKU for the "Use an existing SKU" binding option. /// public int SelectedProduct { get { return ValidationHelper.GetInteger(skuSelectorElem.Value, 0); } } /// /// Gets or sets information text which is displayed above the binding options. /// public string InfoText { get { return lblInfo.Text; } set { lblInfo.Text = value; } } #endregion #region "Properties - options" /// /// Allows the "Create a new SKU" option to be available for selection. /// public bool AllowCreateNew { get { return mAllowCreateNew; } set { mAllowCreateNew = value; } } /// /// Allows the "Create a new global SKU" option to be available for selection. /// public bool AllowCreateNewGlobal { get { return ECommerceSettings.AllowGlobalProducts(CMSContext.CurrentSiteName) && mAllowCreateNewGlobal; } set { mAllowCreateNewGlobal = value; } } /// /// Allows the "Use an existing SKU" option to be available for selection. /// public bool AllowUseExisting { get { return mAllowUseExisting; } set { mAllowUseExisting = value; } } /// /// Allows the "Do not create an SKU" option to be available for selection. /// public bool AllowDoNotCreate { get { return mAllowDoNotCreate; } set { mAllowDoNotCreate = value; } } #endregion #region "Lifecycle" protected override void OnLoad(EventArgs e) { base.OnLoad(e); InitSkuSelector(); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); InitOptions(); } #endregion #region "Initialization" private void InitOptions() { // Display or hide info text lblInfo.Visible = !string.IsNullOrEmpty(InfoText); // Display or hide options radCreateNew.Visible = AllowCreateNew; pnlCreateNewGlobal.Visible = AllowCreateNewGlobal; radUseExisting.Visible = AllowUseExisting; pnlDoNotCreate.Visible = AllowDoNotCreate; } private void InitSkuSelector() { var user = CMSContext.CurrentUser; skuSelectorElem.UserID = user.IsAuthorizedPerResource("CMS.Ecommerce", "AccessAllDepartments") ? 0 : user.UserID; skuSelectorElem.SiteID = CMSContext.CurrentSiteID; skuSelectorElem.UniSelector.AddGlobalObjectSuffix = true; skuSelectorElem.DisplayGlobalProducts = AllowCreateNewGlobal; pnlSkuSelector.Visible = (Binding == USE_EXISTING); } #endregion #region "Validation" /// /// Validates the input data and returns true if it is valid, otherwise returns false. /// public override bool IsValid() { if ((Binding == USE_EXISTING) && (SelectedProduct <= 0)) { ValidationError = GetString("com.selectskubinding.useexistingempty"); return false; } return true; } #endregion #region "Other" /// /// On post back gets the SKU binding value directly from the request. /// Returns null if the value is not present. /// public string GetValueFromRequest() { if (RequestHelper.IsPostBack()) { string radioGoupKey = Request.Form.AllKeys.FirstOrDefault(k => (!string.IsNullOrEmpty(k)) && k.EndsWith("SelectSKUBindingRadioGroup")); if (!string.IsNullOrEmpty(radioGoupKey)) { string skuBinding = Request.Form[radioGoupKey]; if (skuBinding == radCreateNew.ID) { return CREATE_NEW; } else if (skuBinding == radCreateNewGlobal.ID) { return CREATE_NEW_GLOBAL; } else if (skuBinding == radUseExisting.ID) { return USE_EXISTING; } else if (skuBinding == radDoNotCreate.ID) { return DO_NOT_CREATE; } } } return Binding; } #endregion }