using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.PortalControls; using CMS.GlobalHelper; using CMS.CMSHelper; using CMS.PortalEngine; public partial class CMSWebParts_General_CookieLaw : CMSAbstractWebPart { #region "Properties" /// /// Default user cookie level /// public string DefaultLevel { get { return ValidationHelper.GetString(this.GetValue("DefaultLevel"), ""); } set { this.SetValue("DefaultLevel", value); } } /// /// Compare current cookie level to /// public string MatchLevel { get { return ValidationHelper.GetString(this.GetValue("MatchLevel"), "Essential"); } set { this.SetValue("MatchLevel", value); } } /// /// Level simulated in preview mode /// public string PreviewLevel { get { return ValidationHelper.GetString(this.GetValue("PreviewLevel"), "Essential"); } set { this.SetValue("PreviewLevel", value); } } /// /// If level is below, display /// public bool BelowLevelVisible { get { return ValidationHelper.GetBoolean(this.GetValue("BelowLevelVisible"), true); } set { this.SetValue("BelowLevelVisible", value); } } /// /// Text /// public string BelowLevelText { get { return ValidationHelper.GetString(this.GetValue("BelowLevelText"), ""); } set { this.SetValue("BelowLevelText", value); } } /// /// Show deny button /// public bool BelowShowDeny { get { return ValidationHelper.GetBoolean(this.GetValue("BelowShowDeny"), false); } set { this.SetValue("BelowShowDeny", value); } } /// /// Show allow specific button /// public bool BelowShowSpecific { get { return ValidationHelper.GetBoolean(this.GetValue("BelowShowSpecific"), true); } set { this.SetValue("BelowShowSpecific", value); } } /// /// Show allow all button /// public bool BelowShowAll { get { return ValidationHelper.GetBoolean(this.GetValue("BelowShowAll"), true); } set { this.SetValue("BelowShowAll", value); } } /// /// If level matches, display /// public bool ExactLevelVisible { get { return ValidationHelper.GetBoolean(this.GetValue("ExactLevelVisible"), true); } set { this.SetValue("ExactLevelVisible", value); } } /// /// Text /// public string ExactLevelText { get { return ValidationHelper.GetString(this.GetValue("ExactLevelText"), ""); } set { this.SetValue("ExactLevelText", value); } } /// /// Show deny button /// public bool ExactShowDeny { get { return ValidationHelper.GetBoolean(this.GetValue("ExactShowDeny"), true); } set { this.SetValue("ExactShowDeny", value); } } /// /// Show allow specific button /// public bool ExactShowSpecific { get { return ValidationHelper.GetBoolean(this.GetValue("ExactShowSpecific"), false); } set { this.SetValue("ExactShowSpecific", value); } } /// /// Show allow all button /// public bool ExactShowAll { get { return ValidationHelper.GetBoolean(this.GetValue("ExactShowAll"), true); } set { this.SetValue("ExactShowAll", value); } } /// /// If level is above, display /// public bool AboveLevelVisible { get { return ValidationHelper.GetBoolean(this.GetValue("AboveLevelVisible"), true); } set { this.SetValue("AboveLevelVisible", value); } } /// /// Text /// public string AboveLevelText { get { return ValidationHelper.GetString(this.GetValue("AboveLevelText"), ""); } set { this.SetValue("AboveLevelText", value); } } /// /// Show deny button /// public bool AboveShowDeny { get { return ValidationHelper.GetBoolean(this.GetValue("AboveShowDeny"), true); } set { this.SetValue("AboveShowDeny", value); } } /// /// Show allow specific button /// public bool AboveShowSpecific { get { return ValidationHelper.GetBoolean(this.GetValue("AboveShowSpecific"), true); } set { this.SetValue("AboveShowSpecific", value); } } /// /// Show allow all button /// public bool AboveShowAll { get { return ValidationHelper.GetBoolean(this.GetValue("AboveShowAll"), false); } set { this.SetValue("AboveShowAll", value); } } /// /// Deny all button text /// public string DenyAllText { get { return ValidationHelper.GetString(this.GetValue("DenyAllText"), "Deny all cookies"); } set { this.SetValue("DenyAllText", value); } } /// /// Allow specific button text /// public string AllowSpecificText { get { return ValidationHelper.GetString(this.GetValue("AllowSpecificText"), "Allow only essential cookies"); } set { this.SetValue("AllowSpecificText", value); } } /// /// Allow specific button sets level /// public string AllowSpecificSetLevel { get { return ValidationHelper.GetString(this.GetValue("AllowSpecificSetLevel"), "Essential"); } set { this.SetValue("AllowSpecificSetLevel", value); } } /// /// Allow all button text /// public string AllowAllText { get { return ValidationHelper.GetString(this.GetValue("AllowAllText"), "Allow all cookies"); } set { this.SetValue("AllowAllText", value); } } #endregion #region "Methods" /// /// Content loaded event handler /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties /// protected void SetupControl() { if (this.StopProcessing) { // Do not process } else { btnAllowAll.Text = AllowAllText; btnDenyAll.Text = DenyAllText; btnAllowSpecific.Text = AllowSpecificText; } } /// /// Reloads the control data /// public override void ReloadData() { base.ReloadData(); SetupControl(); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (this.StopProcessing) { // Do not process } else { int level = CookieLevel.Unknown; bool liveSite = (CMSContext.ViewMode == ViewModeEnum.LiveSite); if (!liveSite) { // Use simulated cookie level level = CookieHelper.GetCookieLevel(PreviewLevel, CookieLevel.Unknown); } if (level == CookieLevel.Unknown) { // Ensure default level int defaultLevel = CookieHelper.GetCookieLevel(DefaultLevel, CookieLevel.Unknown); if (!liveSite) { defaultLevel = CookieLevel.Unknown; } level = CookieHelper.GetCurrentCookieLevel(defaultLevel); } int matchLevel = CookieHelper.GetCookieLevel(MatchLevel, level); if (level < matchLevel) { // Set the components to the Above level SetComponents(BelowLevelVisible, BelowLevelText, BelowShowAll, BelowShowSpecific, BelowShowDeny); } else if (level == matchLevel) { // Set the components to the Above level SetComponents(ExactLevelVisible, ExactLevelText, ExactShowAll, ExactShowSpecific, ExactShowDeny); } else { // Set the components to the Above level SetComponents(AboveLevelVisible, AboveLevelText, AboveShowAll, AboveShowSpecific, AboveShowDeny); } } } /// /// Initializes the components based on the given properties /// /// Flag whether this mode should be visible /// Information text /// Show allow all button /// Show allow Specific button /// Show allow deny button private void SetComponents(bool visible, string text, bool allowAll, bool allowSpecific, bool allowDeny) { if (!visible) { this.Visible = false; return; } lblText.Text = text; btnAllowAll.Visible = !String.IsNullOrEmpty(btnAllowAll.Text) && allowAll; btnAllowSpecific.Visible = !String.IsNullOrEmpty(btnAllowSpecific.Text) && allowSpecific; btnDenyAll.Visible = !String.IsNullOrEmpty(btnDenyAll.Text) && allowDeny; // Hide the web part in case no content or button is displayed if (String.IsNullOrEmpty(text) && !btnAllowAll.Visible && !btnAllowSpecific.Visible && !btnDenyAll.Visible) { this.Visible = false; } } /// /// Deny all click /// protected void btnDenyAll_Click(object sender, EventArgs e) { ChangeLevel(CookieLevel.System); } /// /// Allow Specific click /// protected void btnAllowSpecific_Click(object sender, EventArgs e) { int specificLevel = CookieHelper.GetCookieLevel(AllowSpecificSetLevel, CookieLevel.Essential); ChangeLevel(specificLevel); } /// /// Allow all click /// protected void btnAllowAll_Click(object sender, EventArgs e) { ChangeLevel(CookieLevel.All); } /// /// Changes the cookie level /// /// New cookie level to set private static void ChangeLevel(int newLevel) { if (CMSContext.ViewMode == ViewModeEnum.LiveSite) { CookieHelper.ChangeCookieLevel(newLevel); } } #endregion }