StyleSheetsManage class for DotNetNuke

I found that code to create CSS links is duplicated in a few places. I’ve created a common class, that can be used from Default.aspx,ComponentsSkinsSkin.vb and third-party modules if required.

Originally I’ve posted it to DNN support , but it is not searchable by Google. So I desided to post it here as well.

 The code is the following:

Imports System.Diagnostics

Imports System.IO

Imports Microsoft.VisualBasic

Imports DotNetNuke

Imports DotNetNuke.Framework

 

Public Class StyleSheetsManage

    ‘Based on C:ProjectsFuncSolnFSDNNDefault.aspx.vb ManageStyleSheets

    Public Shared Sub AddStyleSheetFile(ByVal objCSS As Control, ByVal sFileName As String)

        ‘ initialize reference paths to load the cascading style sheets

        ‘Dim objCSS As Control = Me.FindControl(“CSS”)

        Dim ID As String

        Dim PortalSettings As PortalSettings = PortalController.GetCurrentPortalSettings

        Dim sHref As String = “”

        Dim objCSSCache As Hashtable = CType(DataCache.GetCache(“CSS”), Hashtable)

        If objCSSCache Is Nothing Then

            objCSSCache = New Hashtable

        End If

        If Not objCSS Is Nothing Then

            Select Case (sFileName.ToLower())

                Case “default.css”

                    AddStyleSheetLink(objCSS, Common.Globals.HostPath, sFileName)

                Case “skin.css”

                    ‘ skin package style sheet

                    sHref = AddToCacheIfRequired(objCSSCache, PortalSettings.ActiveTab.SkinPath, “skin.css”)

                    If sHref <> “” Then

                        AddStyleSheetLink(objCSS, PortalSettings.ActiveTab.SkinPath, sFileName)

                    End If

                Case “skinsrc”

                    ‘ skin file style sheet

                    ID = CreateValidID(Replace(PortalSettings.ActiveTab.SkinSrc, “.ascx”, “.css”))

                    sHref = AddToCacheIfRequired(objCSSCache, ID, “”)

                    If sHref <> “” Then

                        AddStyleSheetLink(objCSS, ID, “”)

                    End If

                Case “portal.css”

                    AddStyleSheetLink(objCSS, PortalSettings.HomeDirectory, “portal.css”)

                Case Else

                    Debug.Assert(False)

            End Select

        End If

    End Sub

    Public Shared Function AddStyleSheetLink(ByVal objCSS As Control, ByVal sPath As String, ByVal sFileName As String) As String

        Dim sHref As String = “”

        If Not objCSS Is Nothing Then

            Dim objLink As HtmlGenericControl = New HtmlGenericControl(“LINK”)

            Dim ID As String = CreateValidID(sPath)

            objLink.ID = ID

            objLink.Attributes(“rel”) = “stylesheet”

            objLink.Attributes(“type”) = “text/css”

            sHref = sPath & sFileName

            objLink.Attributes(“href”) = sHref

            objCSS.Controls.Add(objLink)

        End If

        Return sHref

    End Function

    Public Shared Function AddToCacheIfRequired(ByVal objCSSCache As Hashtable, ByVal sPath As String, ByVal sFileName As String)

        Dim PortalSettings As PortalSettings = PortalController.GetCurrentPortalSettings

        Dim sHref As String = “”

        Dim ID As String = CreateValidID(sPath)

        If objCSSCache.ContainsKey(ID) = False Then

            If File.Exists(HttpContext.Current.Server.MapPath(sPath) & sFileName) Then

                sHref = sPath & sFileName

            End If

            objCSSCache(ID) = sHref

            If Not Common.Globals.PerformanceSetting = Common.Globals.PerformanceSettings.NoCaching Then

                DataCache.SetCache(“CSS”, objCSSCache)

            End If

        Else

            sHref = objCSSCache(ID)

        End If

        Return sHref

    End Function

End Class

Advertisements