HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/CMSWebParts/Pux/Content/Logo/WebsiteLogo.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CMS.PortalControls;
using CMS.Helpers;
using CMS.DocumentEngine;
using System.Net;
using CMS.DataEngine;

public partial class CMSWebParts_Pux_Content_Logo_Logo : CMSAbstractWebPart
{
    #region "Properties"

    /// <summary>
    /// Custom CSS class to be used for the H1/SPAN element with site name
    /// </summary>
    public string CustomClass
    {
        get
        {
            return ValidationHelper.GetString(GetValue("CustomClass"), String.Empty);
        }
        set
        {
            SetValue("CustomClass", value);
        }
    }

    /// <summary>
    /// Site name to be rendered inside H1/SPAN element
    /// </summary>
    public string WebsiteTitle
    {
        get
        {
            return ValidationHelper.GetString(GetValue("WebsiteTitle"), String.Empty);
        }
        set
        {
            SetValue("WebsiteTitle", value);
        }
    }

    /// <summary>
    /// Custom HTML to be used instead of default one
    /// </summary>
    public string CustomHTML
    {
        get
        {
            return ValidationHelper.GetString(GetValue("CustomHTML"), String.Empty);
        }
        set
        {
            SetValue("CustomHTML", value);
        }
    }

    /// <summary>
    /// Path to the PNG image (used as a fallback for SVG)
    /// </summary>
    public string ImagePNG
    {
        get
        {
            return ValidationHelper.GetString(GetValue("ImagePNG"), String.Empty);
        }
        set
        {
            SetValue("ImagePNG", value);
        }
    }

    /// <summary>
    /// Path to the SVG image (primary image of the logo)
    /// </summary>
    public string ImageSVG
    {
        get
        {
            return ValidationHelper.GetString(GetValue("ImageSVG"), String.Empty);
        }
        set
        {
            SetValue("ImageSVG", value);
        }
    }

    #endregion

    #region "Page events"

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        GenerateHtml();
    }

    #endregion

    #region "Html generation"

    /// <summary>
    /// Generates html for the logo.
    /// </summary>
    public void GenerateHtml()
    {
        // wrapper <div>
        var div = new HtmlGenericControl("div");

        if (!String.IsNullOrEmpty(CustomClass))
        {
            // apply custom CSS class
            div.Attributes["class"] = CustomClass;
        }
        else
        {
            // use default CSS class "logo"
            div.Attributes["class"] = "logo";
        }

        var controlType = "";
        var generateLinkWrapper = false;

        string settingHP = SettingsKeyInfoProvider.GetValue("CMSDefaultAliasPath", CurrentSite.SiteID);
        string currentAlias = CurrentDocument.NodeAliasPath;
        var isHomepage = String.Equals(settingHP, currentAlias, StringComparison.OrdinalIgnoreCase);

        // For homepage generate <h1> tag
        if (isHomepage)
        {
            controlType = "h1";
        }
        // For other pages generate <span> tag
        else
        {
            controlType = "span";
            generateLinkWrapper = true;
        }

        var container = div;

        if (generateLinkWrapper)
        {
            // link <a> should be generated and content is inside this link
            var link = new HtmlGenericControl("a");
            link.Attributes["href"] = URLHelper.ResolveUrl("~/");
            link.Attributes["title"] = WebsiteTitle;

            container = link;
            div.Controls.Add(link);
        }

        // add site title
        container.Controls.Add(new HtmlGenericControl(controlType) { InnerText = WebsiteTitle });

        if (String.IsNullOrEmpty(CustomHTML))
        {
            // generate standard HTML output (image with SVG/PNG image and H1/SPAN with website title)
            var img = String.Format(
                "<img src=\"{0}\" onerror=\"this.onerror=null; this.src='{1}'\" alt=\"{2}\" />",
                HTMLHelper.EncodeForHtmlAttribute(URLHelper.ResolveUrl(ImageSVG)),
                HTMLHelper.EncodeForHtmlAttribute(URLHelper.ResolveUrl(ImagePNG.Replace("'", ""))),
                HTMLHelper.EncodeForHtmlAttribute(WebsiteTitle)
            );

            container.Controls.Add(new Literal() { Text = img });
        }
        else
        {
            // generate custom HTML
            container.Controls.Add(new Literal() { Text = CustomHTML });
        }

        this.Controls.Add(div);
    }

    #endregion
}