Пример #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        string bannerCategoryCodeName = ValidationHelper.GetString(GetValue("BannerCategoryCodeName"), "");

        BannerCategoryInfo bannerCategory = BannerCategoryInfoProvider.GetBannerCategoryInfoFromSiteOrGlobal(bannerCategoryCodeName, SiteContext.CurrentSiteID);

        if ((bannerCategory == null) || (bannerCategory.BannerCategoryEnabled == false))
        {
            Visible = !HideIfBannerNotFound;
            return;
        }

        if (URLHelper.IsPostback() && KeepPreviousBannerOnPostBack && BannerIDViewState.HasValue)
        {
            bannerReused = true;
            banner       = BannerInfoProvider.GetBannerInfo(BannerIDViewState.Value);
        }

        // If random banner should be picked or banner from viewstate was not found
        if (banner == null)
        {
            bannerReused = false;
            // Get random banner from selected category. Decrement hits left for this banner only if page is displayed on the live site.
            banner = BannerInfoProvider.GetRandomValidBanner(bannerCategory.BannerCategoryID, (currentViewMode.IsLiveSite()));
        }

        // Exits if no banner was found
        if (banner == null)
        {
            Visible = !HideIfBannerNotFound;
            return;
        }

        // Store banner id in the viewstate if the same banner should be used if request is postback
        if (KeepPreviousBannerOnPostBack)
        {
            BannerIDViewState = banner.BannerID;
        }

        string width       = ValidationHelper.GetString(GetValue("Width"), "");
        string height      = ValidationHelper.GetString(GetValue("Height"), "");
        string anchorClass = ValidationHelper.GetString(GetValue("AnchorClass"), "");
        bool   fakeLink    = ValidationHelper.GetBoolean(GetValue("FakeLink"), true);

        if (width != "")
        {
            lnkBanner.Style["width"] = width;
        }
        if (height != "")
        {
            lnkBanner.Style["height"] = height;
        }

        lnkBanner.CssClass = string.Format("CMSBanner {0}", anchorClass).Trim();

        lnkBanner.Visible = true;



        // Do not set link if we are not on the live site.
        if (currentViewMode.IsOneOf(ViewModeEnum.LiveSite, ViewModeEnum.Preview))
        {
            // Link pointing to our custom handler which logs click and redirects
            string bannerRedirectURL = URLHelper.ResolveUrl("~/CMSModules/BannerManagement/CMSPages/BannerRedirect.ashx?bannerID=" + banner.BannerID);

            if (fakeLink)
            {
                // By default href attribute will be set to 'nice' URL
                lnkBanner.Attributes.Add("href", URLHelper.ResolveUrl(banner.BannerURL));

                // After clicking href will be set to URL pointing to custom handler which counts clicks
                lnkBanner.Attributes.Add("onclick", string.Format("this.href='{0}';", bannerRedirectURL));

                // GECKO doesn't count middle mouse click as click, so onmouseup (or down) needs to be added
                lnkBanner.Attributes.Add("onmouseup", string.Format("this.href='{0}';", bannerRedirectURL));
            }
            else
            {
                // If faking links is disabled, set href to redirect url
                lnkBanner.Attributes.Add("href", bannerRedirectURL);
            }

            // Add target="_blank" attribute if link should be opened in new window
            if (banner.BannerBlank)
            {
                lnkBanner.Target = "_blank";
            }
        }

        if (banner.BannerType == BannerTypeEnum.Image)
        {
            BannerImageAttributes bannerImageAttributes = BannerManagementHelper.DeserializeBannerImageAttributes(banner.BannerContent);

            imgBanner.AlternateText = bannerImageAttributes.Alt;
            imgBanner.ToolTip       = bannerImageAttributes.Title;
            imgBanner.CssClass      = bannerImageAttributes.Class;
            imgBanner.Style.Value   = HTMLHelper.HTMLEncode(bannerImageAttributes.Style);

            imgBanner.ImageUrl = URLHelper.ResolveUrl(bannerImageAttributes.Src);

            imgBanner.Visible = true;
            ltrBanner.Visible = false;
        }
        else
        {
            string text = MacroResolver.Resolve(banner.BannerContent);

            ltrBanner.Text    = HTMLHelper.ResolveUrls(text, null, false);
            imgBanner.Visible = false;
            ltrBanner.Visible = true;

            if (banner.BannerType == BannerTypeEnum.HTML)
            {
                ControlsHelper.ResolveDynamicControls(this);
            }
        }
    }
Пример #2
0
    /// <summary>
    /// Handles the PreRender event of the Page control.
    /// </summary>
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (stopProcessing)
        {
            Visible = false;
            return;
        }

        // Show the warning panel when there is a running MVT test
        if ((viewMode != ViewModeEnum.Preview) &&
            MVTestInfoProvider.ContainsRunningMVTest(DocumentContext.CurrentAliasPath, SiteContext.CurrentSiteID, DocumentContext.CurrentPageInfo.DocumentCulture))
        {
            plcRunningTestWarning.Visible = true;
        }

        if (RequestHelper.IsPostBack())
        {
            // Reload the combination panel because one of the combination could have been removed
            ReloadData(true);
        }

        // Set the OnChange attribute => Save the variant slider configuration into a cookie and raise a postback
        combinationSelector.DropDownSelect.Attributes.Add("onchange", "SaveCombinationPanelSelection(); " + Page.ClientScript.GetPostBackEventReference(this, "combinationchanged") + "; return false;");


        MVTCombinationInfo ci = null;

        if (DocumentContext.CurrentDocument != null)
        {
            int templateId = DocumentContext.CurrentDocument.GetUsedPageTemplateId();

            // Get the combination name from cookie
            string combinationName = CookieHelper.GetValue(cookieTestName);
            if (string.IsNullOrEmpty(combinationName))
            {
                // CombinationName is not defined, use the default combination for the page template
                ci = MVTCombinationInfoProvider.GetDefaultCombinationInfo(templateId);
            }
            else
            {
                // Keep current instance node
                TreeNode tn = DocumentContext.CurrentDocument;

                // Use the defined combination
                ci = MVTCombinationInfoProvider.GetMVTCombinationInfo(tn.NodeAliasPath, combinationName, SiteContext.CurrentSiteName, tn.DocumentCulture);

                if (ci == null)
                {
                    // Combination not found (can happen after deleting a variant), use the default combination for the page template
                    ci = MVTCombinationInfoProvider.GetDefaultCombinationInfo(templateId);
                }
            }
        }

        // Show the combination panel only if there are any combinations for the document
        pnlMvtCombination.Enabled = combinationSelector.HasData;

        if (ci != null)
        {
            int combinationId = ci.MVTCombinationID;

            // Setup the combination panel values
            combinationSelector.DropDownSelect.SelectedValue = ci.MVTCombinationName;
            chkEnabled.Checked = ci.MVTCombinationEnabled;
            txtCustomName.Text = ResHelper.LocalizeString(ci.MVTCombinationCustomName, currentUser.PreferredUICultureCode);

            // Create javascript variables of the combination panel. Used when changing combination by the variation slider/arrows
            StringBuilder combinationJSList = new StringBuilder();
            combinationJSList.Append("var mvtCPselector = document.getElementById('" + combinationSelector.DropDownSelect.ClientID + "');");
            combinationJSList.Append("var mvtCPenabled = document.getElementById('" + chkEnabled.ClientID + "');");
            combinationJSList.Append("var mvtCPcustomName = document.getElementById('" + txtCustomName.ClientID + "');");
            combinationJSList.Append("var mvtCPcurrentCombinationName = document.getElementById('" + hdnCurrentCombination.ClientID + "');");

            // Generate the JS configuration array for the Edit and Design view modes only (modes where the variant sliders can be used)
            if (viewMode.IsOneOf(ViewModeEnum.Edit, ViewModeEnum.EditDisabled) || PortalContext.IsDesignMode(viewMode))
            {
                // Get variants for the selected combination
                DataSet dsSelectedCombinationVariants = MVTVariantInfoProvider.GetMVTVariants(ci.MVTCombinationPageTemplateID, combinationId);


                #region "Generate javascript arrays used for changing the selected combination according to the selected variants"

                // List of compulsory combination variants (used for the combination JS array only).
                // For example:
                // Selected combination contains WidgetVariantID3 + ZoneVariantID5 and the user is in the Edit mode.
                // Therefore compulsory combination variants will be only containing the zone variant ID 5-> that means: only combinations with this compulsory variant will be proceed
                // This ensures a correct behavior of the combination panel when changing the variant sliders.
                combinationJSList.Append("var compulsoryCombinationVariants = [");
                int compulsoryCombinationVariantsCounter = 0;

                // Fill the array 'compulsoryCombinationVariants'
                if (!DataHelper.DataSourceIsEmpty(dsSelectedCombinationVariants))
                {
                    // Edit mode
                    if ((viewMode.IsEdit()) || (viewMode == ViewModeEnum.EditDisabled))
                    {
                        foreach (DataRow row in dsSelectedCombinationVariants.Tables[0].Rows)
                        {
                            // Process web part and zones only
                            if (ValidationHelper.GetInteger(row["MVTVariantDocumentID"], 0) == 0)
                            {
                                if (compulsoryCombinationVariantsCounter > 0)
                                {
                                    combinationJSList.Append(",");
                                }
                                // Add the web part/zone to the JS array
                                combinationJSList.Append(ValidationHelper.GetInteger(row["MVTVariantID"], 0));
                                compulsoryCombinationVariantsCounter++;
                            }
                        }
                    }
                    // Design mode
                    else if (PortalContext.IsDesignMode(viewMode))
                    {
                        foreach (DataRow row in dsSelectedCombinationVariants.Tables[0].Rows)
                        {
                            // Process widgets only
                            if (ValidationHelper.GetInteger(row["MVTVariantDocumentID"], 0) > 0)
                            {
                                if (compulsoryCombinationVariantsCounter > 0)
                                {
                                    combinationJSList.Append(",");
                                }
                                // Add the widget to the JS array
                                combinationJSList.Append(ValidationHelper.GetInteger(row["MVTVariantID"], 0));
                                compulsoryCombinationVariantsCounter++;
                            }
                        }
                    }
                }

                combinationJSList.Append("];");

                // combinationsArray - array containing configuration of each combination displayed in the combination panel.
                // This array is used after the user changes a variant slider and the new combination (selected by the combination panel) is to be calculated.
                combinationJSList.Append("var combinationsArray = [");
                int combinationCount = 0;

                foreach (ListItem item in combinationSelector.DropDownSelect.Items)
                {
                    // Get the combination object
                    MVTCombinationInfo cObj = MVTCombinationInfoProvider.GetMVTCombinationInfo(combinationSelector.PageTemplateID, item.Value);

                    if (cObj != null)
                    {
                        DataSet cVariants = MVTVariantInfoProvider.GetMVTVariants(cObj.MVTCombinationPageTemplateID, cObj.MVTCombinationID);
                        if (combinationCount > 0)
                        {
                            combinationJSList.Append(",");
                        }

                        combinationJSList.Append("['");
                        combinationJSList.Append(cObj.MVTCombinationName);
                        combinationJSList.Append("',");
                        combinationJSList.Append(cObj.MVTCombinationEnabled.ToString().ToLowerCSafe());
                        combinationJSList.Append(",");
                        combinationJSList.Append(ScriptHelper.GetString(ResHelper.LocalizeString(cObj.MVTCombinationCustomName, currentUser.PreferredUICultureCode)));

                        // Generate the unique variant IDs code (format: 155_158_180) - must be ordered by variantID
                        combinationJSList.Append(",'");
                        if (!DataHelper.DataSourceIsEmpty(cVariants))
                        {
                            int variantCount = 0;
                            foreach (DataRow row in cVariants.Tables[0].Rows)
                            {
                                if (variantCount > 0)
                                {
                                    combinationJSList.Append("_");
                                }

                                combinationJSList.Append(ValidationHelper.GetString(row["MVTVariantID"], "0"));
                                variantCount++;
                            }
                        }

                        combinationJSList.Append("']");
                        combinationCount++;
                    }
                }

                combinationJSList.Append("];");

                #endregion


                // Choose the correct variant from all rendered variants for a current web part (used in Content->Design/Edit page)
                if (!DataHelper.DataSourceIsEmpty(dsSelectedCombinationVariants))
                {
                    combinationJSList.Append("function SetCombinationVariants() {");
                    bool variantWasSet = false;

                    // Process all the variants of the selected combination
                    foreach (DataRow row in dsSelectedCombinationVariants.Tables[0].Rows)
                    {
                        bool   itemIsZoneVariant = string.IsNullOrEmpty(ValidationHelper.GetString(row["MVTVariantInstanceGUID"], string.Empty));
                        string itemIdentifier;
                        if (itemIsZoneVariant)
                        {
                            // Zone
                            itemIdentifier = "Variant_Zone_" + HTMLHelper.HTMLEncode(ValidationHelper.GetString(row["MVTVariantZoneID"], string.Empty));
                        }
                        else
                        {
                            // Web part/widget
                            itemIdentifier = "Variant_WP_" + ValidationHelper.GetGuid(row["MVTVariantInstanceGUID"], Guid.Empty).ToString("N");
                        }

                        // Set the appropriate variant
                        int itemVariantId = ValidationHelper.GetInteger(row["MVTVariantID"], 0);
                        combinationJSList.Append("SetVariant('" + itemIdentifier + "', " + itemVariantId + ");");
                        variantWasSet = true;
                    }

                    if (variantWasSet)
                    {
                        // Refresh the combination panel if any variant was set manually
                        combinationJSList.Append("UpdateCombinationPanel();");
                    }

                    combinationJSList.Append("}");
                }
            }

            // Save the current combination id in javascript
            combinationJSList.Append("mvtCPcurrentCombinationName.value = '" + ci.MVTCombinationName + "';");

            // Register the JS arrays and current variants
            ScriptHelper.RegisterStartupScript(this, typeof(string), "combinationJSList", ScriptHelper.GetScript(combinationJSList.ToString()));
        }

        // Display the "set as result" button when there any MVT variants in the page
        if ((currentUser != null) &&
            (currentUser.IsAuthorizedPerResource("CMS.Design", "Design")))
        {
            plcUseCombination.Visible = combinationSelector.DropDownSelect.Items.Count > 1;
        }
    }