如何通过代码设置DotNetNuke页面的皮肤?

发布于 2024-10-10 07:05:03 字数 12961 浏览 2 评论 0原文

我正在开发一个 DNN 模块,该模块创建 DNN 页面(选项卡)并通过代码在其上放置 DNN 模块。到目前为止,效果非常好。但是,我希望它也能够以编程方式设置页面的皮肤并将模块放置在适当的窗格中。

有谁知道如何使用代码来做到这一点?


解决方案:


我按照 mika 建议设置了 SkinSrcContainerSrc

这是我的来源,如果你有兴趣的话。这是我设置SkinSrc的地方。

''' <summary>Create new DNN tab/page.</summary>
        Private Function CreatePage(ByVal ParentID As Integer, ByVal PageName As String, ByVal PageTitle As String, ByVal Description As String, ByVal Keywords As String, ByVal Permissions As TabPermissionCollection, Optional ByVal SkinSrc As String = "", Optional ByVal isVisible As Boolean = True, Optional ByVal LoadDefaultModules As Boolean = True) As Tabs.TabInfo
            Try
                Dim tabCtrlr As New TabController
                Dim newTab As New Tabs.TabInfo
                Dim newPermissions As TabPermissionCollection = newTab.TabPermissions
                Dim permissionProvider As PermissionProvider = permissionProvider.Instance
                Dim infPermission As TabPermissionInfo

                ' set new page properties
                newTab.PortalID = PortalId
                newTab.TabName = PageName
                newTab.Title = PageTitle
                newTab.Description = Description
                newTab.KeyWords = Keywords
                newTab.IsDeleted = False
                newTab.IsSuperTab = False
                newTab.IsVisible = isVisible
                newTab.DisableLink = False
                newTab.IconFile = ""
                newTab.Url = ""
                newTab.ParentId = ParentID

                'add skinsrc if specified
                If (SkinSrc.Length > 0) Then newTab.SkinSrc = SkinSrc

                ' create new page
                tabCtrlr.AddTab(newTab, LoadDefaultModules)

                ' copy permissions selected in Permissions collection
                For index As Integer = 0 To (Permissions.Count - 1)
                    infPermission = New TabPermissionInfo

                    infPermission.AllowAccess = Permissions(index).AllowAccess
                    infPermission.RoleID = Permissions(index).RoleID
                    infPermission.RoleName = Permissions(index).RoleName
                    infPermission.TabID = Permissions(index).TabID
                    infPermission.PermissionID = Permissions(index).PermissionID

                    'save permission info
                    newPermissions.Add(infPermission, True)
                    permissionProvider.SaveTabPermissions(newTab)
                Next index

                'return TabInfo of new page
                Return newTab

            Catch ex As Exception
                'failure
                Return New Tabs.TabInfo
            End Try
        End Function

接下来的两个函数取自 DNN 源代码并稍作调整,因此我不能将其中的大部分归功于它们。此外,如果您在自己的模块中使用这些,则在升级 DNN 时可能会出现问题。虽然从5.05升级到5.06对我来说很顺利。

AddNewModule 函数中,我使用 ContainerSrc 指定要使用的自定义容器。 PaneName 是用于指定模块应放置在哪个面板中的属性。

#Region "From DNN Source --mostly"

#Region "Enums"

        Private Enum ViewPermissionType
            View = 0
            Edit = 1
        End Enum

#End Region

        ''' -----------------------------------------------------------------------------
        ''' <summary>Adds a New Module to a Pane</summary>
        ''' <param name="align">The alignment for the Module</param>
        ''' <param name="desktopModuleId">The Id of the DesktopModule</param>
        ''' <param name="permissionType">The View Permission Type for the Module</param>
        ''' <param name="title">The Title for the resulting module</param>
        ''' <param name="paneName">The pane to add the module to</param>
        ''' <param name="position">The relative position within the pane for the module</param>
        ''' -----------------------------------------------------------------------------
        Private Function AddNewModule(ByVal TabID As Integer, ByVal title As String, ByVal desktopModuleId As Integer, ByVal paneName As String, ByVal position As Integer, ByVal permissionType As ViewPermissionType, ByVal align As String) As Integer

            Dim objTabController As New TabController
            Dim objTabPermissions As TabPermissionCollection = objTabController.GetTab(TabID, PortalId, True).TabPermissions
            Dim objPermissionController As New PermissionController
            Dim objModules As New ModuleController
            Dim objModuleDefinition As ModuleDefinitionInfo
            Dim objEventLog As New Services.Log.EventLog.EventLogController
            Dim newModuleID As Integer
            Dim j As Integer

            Try
                Dim desktopModule As DesktopModuleInfo = Nothing
                If Not DesktopModuleController.GetDesktopModules(PortalSettings.PortalId).TryGetValue(desktopModuleId, desktopModule) Then
                    Throw New ArgumentException("desktopModuleId")
                End If
            Catch ex As Exception
                LogException(ex)
            End Try

            Dim UserId As Integer = -1
            If Request.IsAuthenticated Then
                Dim objUserInfo As Users.UserInfo = UserController.GetCurrentUserInfo
                UserId = objUserInfo.UserID
            End If

            For Each objModuleDefinition In ModuleDefinitionController.GetModuleDefinitionsByDesktopModuleID(desktopModuleId).Values
                Dim objModule As New ModuleInfo
                objModule.Initialize(PortalSettings.PortalId)

                objModule.PortalID = PortalSettings.PortalId
                objModule.TabID = TabID
                objModule.ModuleOrder = position
                If title = "" Then
                    objModule.ModuleTitle = objModuleDefinition.FriendlyName
                Else
                    objModule.ModuleTitle = title
                End If
                objModule.PaneName = paneName
                objModule.ModuleDefID = objModuleDefinition.ModuleDefID
                If objModuleDefinition.DefaultCacheTime > 0 Then
                    objModule.CacheTime = objModuleDefinition.DefaultCacheTime
                    If Portals.PortalSettings.Current.DefaultModuleId > Null.NullInteger AndAlso Portals.PortalSettings.Current.DefaultTabId > Null.NullInteger Then
                        Dim defaultModule As ModuleInfo = objModules.GetModule(Portals.PortalSettings.Current.DefaultModuleId, Portals.PortalSettings.Current.DefaultTabId, True)
                        If Not defaultModule Is Nothing Then
                            objModule.CacheTime = defaultModule.CacheTime
                        End If
                    End If
                End If

                Select Case permissionType
                    Case ViewPermissionType.View
                        objModule.InheritViewPermissions = True
                    Case ViewPermissionType.Edit
                        objModule.InheritViewPermissions = False
                End Select

                ' get the default module view permissions
                Dim arrSystemModuleViewPermissions As ArrayList = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_MODULE_DEFINITION", "VIEW")

                ' get the permissions from the page
                For Each objTabPermission As TabPermissionInfo In objTabPermissions
                    If objTabPermission.PermissionKey = "VIEW" AndAlso permissionType = ViewPermissionType.View Then
                        'Don't need to explicitly add View permisisons if "Same As Page"
                        Continue For
                    End If

                    ' get the system module permissions for the permissionkey
                    Dim arrSystemModulePermissions As ArrayList = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_MODULE_DEFINITION", objTabPermission.PermissionKey)
                    ' loop through the system module permissions
                    For j = 0 To arrSystemModulePermissions.Count - 1
                        ' create the module permission
                        Dim objSystemModulePermission As PermissionInfo
                        objSystemModulePermission = CType(arrSystemModulePermissions(j), PermissionInfo)
                        If objSystemModulePermission.PermissionKey = "VIEW" AndAlso permissionType = ViewPermissionType.Edit AndAlso _
                             objTabPermission.PermissionKey <> "EDIT" Then
                            'Only Page Editors get View permissions if "Page Editors Only"
                            Continue For
                        End If

                        Dim objModulePermission As ModulePermissionInfo = AddModulePermission(objModule, _
                                                                                objSystemModulePermission, _
                                                                                objTabPermission.RoleID, objTabPermission.UserID, _
                                                                                objTabPermission.AllowAccess)

                        ' ensure that every EDIT permission which allows access also provides VIEW permission
                        If objModulePermission.PermissionKey = "EDIT" And objModulePermission.AllowAccess Then
                            Dim objModuleViewperm As ModulePermissionInfo = AddModulePermission(objModule, _
                                                                                CType(arrSystemModuleViewPermissions(0), PermissionInfo), _
                                                                                objModulePermission.RoleID, objModulePermission.UserID, _
                                                                                True)
                        End If
                    Next

                    'Get the custom Module Permissions,  Assume that roles with Edit Tab Permissions
                    'are automatically assigned to the Custom Module Permissions
                    If objTabPermission.PermissionKey = "EDIT" Then
                        Dim arrCustomModulePermissions As ArrayList = objPermissionController.GetPermissionsByModuleDefID(objModule.ModuleDefID)

                        ' loop through the custom module permissions
                        For j = 0 To arrCustomModulePermissions.Count - 1
                            ' create the module permission
                            Dim objCustomModulePermission As PermissionInfo
                            objCustomModulePermission = CType(arrCustomModulePermissions(j), PermissionInfo)

                            AddModulePermission(objModule, objCustomModulePermission, _
                                                                    objTabPermission.RoleID, objTabPermission.UserID, _
                                                                    objTabPermission.AllowAccess)
                        Next
                    End If
                Next

                objModule.AllTabs = False
                objModule.Alignment = align

                'apply Custom Container to module
                objModule.ContainerSrc = CONTAINER_TRANSPARENT_PLAIN

                newModuleID = objModules.AddModule(objModule)
            Next

            Return newModuleID
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>Adds a Module Permission</summary>
        ''' <param name="permission">The permission to add</param>
        ''' <param name="roleId">The Id of the role to add the permission for.</param>
        ''' -----------------------------------------------------------------------------
        Private Function AddModulePermission(ByVal objModule As ModuleInfo, ByVal permission As PermissionInfo, ByVal roleId As Integer, ByVal userId As Integer, ByVal allowAccess As Boolean) As ModulePermissionInfo
            Dim objModulePermission As New ModulePermissionInfo
            objModulePermission.ModuleID = objModule.ModuleID
            objModulePermission.PermissionID = permission.PermissionID
            objModulePermission.RoleID = roleId
            objModulePermission.UserID = userId
            objModulePermission.PermissionKey = permission.PermissionKey
            objModulePermission.AllowAccess = allowAccess

            ' add the permission to the collection
            If Not objModule.ModulePermissions.Contains(objModulePermission) Then
                objModule.ModulePermissions.Add(objModulePermission)
            End If

            Return objModulePermission
        End Function

#End Region

I'm working on a DNN module that creates DNN pages (tabs) and places DNN modules on them through code. So, far that's working very well. However, I'd like it to also be able to programmatically set the page's skin and place the modules in the appropriate pane.

Does anyone know how to do this using code?


Solution:

I set SkinSrc and ContainerSrc as mika suggested.

Here's my source, if you're interested. This is where I set SkinSrc.

''' <summary>Create new DNN tab/page.</summary>
        Private Function CreatePage(ByVal ParentID As Integer, ByVal PageName As String, ByVal PageTitle As String, ByVal Description As String, ByVal Keywords As String, ByVal Permissions As TabPermissionCollection, Optional ByVal SkinSrc As String = "", Optional ByVal isVisible As Boolean = True, Optional ByVal LoadDefaultModules As Boolean = True) As Tabs.TabInfo
            Try
                Dim tabCtrlr As New TabController
                Dim newTab As New Tabs.TabInfo
                Dim newPermissions As TabPermissionCollection = newTab.TabPermissions
                Dim permissionProvider As PermissionProvider = permissionProvider.Instance
                Dim infPermission As TabPermissionInfo

                ' set new page properties
                newTab.PortalID = PortalId
                newTab.TabName = PageName
                newTab.Title = PageTitle
                newTab.Description = Description
                newTab.KeyWords = Keywords
                newTab.IsDeleted = False
                newTab.IsSuperTab = False
                newTab.IsVisible = isVisible
                newTab.DisableLink = False
                newTab.IconFile = ""
                newTab.Url = ""
                newTab.ParentId = ParentID

                'add skinsrc if specified
                If (SkinSrc.Length > 0) Then newTab.SkinSrc = SkinSrc

                ' create new page
                tabCtrlr.AddTab(newTab, LoadDefaultModules)

                ' copy permissions selected in Permissions collection
                For index As Integer = 0 To (Permissions.Count - 1)
                    infPermission = New TabPermissionInfo

                    infPermission.AllowAccess = Permissions(index).AllowAccess
                    infPermission.RoleID = Permissions(index).RoleID
                    infPermission.RoleName = Permissions(index).RoleName
                    infPermission.TabID = Permissions(index).TabID
                    infPermission.PermissionID = Permissions(index).PermissionID

                    'save permission info
                    newPermissions.Add(infPermission, True)
                    permissionProvider.SaveTabPermissions(newTab)
                Next index

                'return TabInfo of new page
                Return newTab

            Catch ex As Exception
                'failure
                Return New Tabs.TabInfo
            End Try
        End Function

These next two functions were taken from the DNN source and tweaked slightly, so I can't take credit for much of them. Also, if you use these in your own modules there could be issues when upgrading DNN. Although the upgrade from 5.05 to 5.06 went smoothly for me.

In the AddNewModule function, I used ContainerSrc to specify the custom container to use. PaneName is the property used to specify which panel the module should be placed in.

#Region "From DNN Source --mostly"

#Region "Enums"

        Private Enum ViewPermissionType
            View = 0
            Edit = 1
        End Enum

#End Region

        ''' -----------------------------------------------------------------------------
        ''' <summary>Adds a New Module to a Pane</summary>
        ''' <param name="align">The alignment for the Module</param>
        ''' <param name="desktopModuleId">The Id of the DesktopModule</param>
        ''' <param name="permissionType">The View Permission Type for the Module</param>
        ''' <param name="title">The Title for the resulting module</param>
        ''' <param name="paneName">The pane to add the module to</param>
        ''' <param name="position">The relative position within the pane for the module</param>
        ''' -----------------------------------------------------------------------------
        Private Function AddNewModule(ByVal TabID As Integer, ByVal title As String, ByVal desktopModuleId As Integer, ByVal paneName As String, ByVal position As Integer, ByVal permissionType As ViewPermissionType, ByVal align As String) As Integer

            Dim objTabController As New TabController
            Dim objTabPermissions As TabPermissionCollection = objTabController.GetTab(TabID, PortalId, True).TabPermissions
            Dim objPermissionController As New PermissionController
            Dim objModules As New ModuleController
            Dim objModuleDefinition As ModuleDefinitionInfo
            Dim objEventLog As New Services.Log.EventLog.EventLogController
            Dim newModuleID As Integer
            Dim j As Integer

            Try
                Dim desktopModule As DesktopModuleInfo = Nothing
                If Not DesktopModuleController.GetDesktopModules(PortalSettings.PortalId).TryGetValue(desktopModuleId, desktopModule) Then
                    Throw New ArgumentException("desktopModuleId")
                End If
            Catch ex As Exception
                LogException(ex)
            End Try

            Dim UserId As Integer = -1
            If Request.IsAuthenticated Then
                Dim objUserInfo As Users.UserInfo = UserController.GetCurrentUserInfo
                UserId = objUserInfo.UserID
            End If

            For Each objModuleDefinition In ModuleDefinitionController.GetModuleDefinitionsByDesktopModuleID(desktopModuleId).Values
                Dim objModule As New ModuleInfo
                objModule.Initialize(PortalSettings.PortalId)

                objModule.PortalID = PortalSettings.PortalId
                objModule.TabID = TabID
                objModule.ModuleOrder = position
                If title = "" Then
                    objModule.ModuleTitle = objModuleDefinition.FriendlyName
                Else
                    objModule.ModuleTitle = title
                End If
                objModule.PaneName = paneName
                objModule.ModuleDefID = objModuleDefinition.ModuleDefID
                If objModuleDefinition.DefaultCacheTime > 0 Then
                    objModule.CacheTime = objModuleDefinition.DefaultCacheTime
                    If Portals.PortalSettings.Current.DefaultModuleId > Null.NullInteger AndAlso Portals.PortalSettings.Current.DefaultTabId > Null.NullInteger Then
                        Dim defaultModule As ModuleInfo = objModules.GetModule(Portals.PortalSettings.Current.DefaultModuleId, Portals.PortalSettings.Current.DefaultTabId, True)
                        If Not defaultModule Is Nothing Then
                            objModule.CacheTime = defaultModule.CacheTime
                        End If
                    End If
                End If

                Select Case permissionType
                    Case ViewPermissionType.View
                        objModule.InheritViewPermissions = True
                    Case ViewPermissionType.Edit
                        objModule.InheritViewPermissions = False
                End Select

                ' get the default module view permissions
                Dim arrSystemModuleViewPermissions As ArrayList = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_MODULE_DEFINITION", "VIEW")

                ' get the permissions from the page
                For Each objTabPermission As TabPermissionInfo In objTabPermissions
                    If objTabPermission.PermissionKey = "VIEW" AndAlso permissionType = ViewPermissionType.View Then
                        'Don't need to explicitly add View permisisons if "Same As Page"
                        Continue For
                    End If

                    ' get the system module permissions for the permissionkey
                    Dim arrSystemModulePermissions As ArrayList = objPermissionController.GetPermissionByCodeAndKey("SYSTEM_MODULE_DEFINITION", objTabPermission.PermissionKey)
                    ' loop through the system module permissions
                    For j = 0 To arrSystemModulePermissions.Count - 1
                        ' create the module permission
                        Dim objSystemModulePermission As PermissionInfo
                        objSystemModulePermission = CType(arrSystemModulePermissions(j), PermissionInfo)
                        If objSystemModulePermission.PermissionKey = "VIEW" AndAlso permissionType = ViewPermissionType.Edit AndAlso _
                             objTabPermission.PermissionKey <> "EDIT" Then
                            'Only Page Editors get View permissions if "Page Editors Only"
                            Continue For
                        End If

                        Dim objModulePermission As ModulePermissionInfo = AddModulePermission(objModule, _
                                                                                objSystemModulePermission, _
                                                                                objTabPermission.RoleID, objTabPermission.UserID, _
                                                                                objTabPermission.AllowAccess)

                        ' ensure that every EDIT permission which allows access also provides VIEW permission
                        If objModulePermission.PermissionKey = "EDIT" And objModulePermission.AllowAccess Then
                            Dim objModuleViewperm As ModulePermissionInfo = AddModulePermission(objModule, _
                                                                                CType(arrSystemModuleViewPermissions(0), PermissionInfo), _
                                                                                objModulePermission.RoleID, objModulePermission.UserID, _
                                                                                True)
                        End If
                    Next

                    'Get the custom Module Permissions,  Assume that roles with Edit Tab Permissions
                    'are automatically assigned to the Custom Module Permissions
                    If objTabPermission.PermissionKey = "EDIT" Then
                        Dim arrCustomModulePermissions As ArrayList = objPermissionController.GetPermissionsByModuleDefID(objModule.ModuleDefID)

                        ' loop through the custom module permissions
                        For j = 0 To arrCustomModulePermissions.Count - 1
                            ' create the module permission
                            Dim objCustomModulePermission As PermissionInfo
                            objCustomModulePermission = CType(arrCustomModulePermissions(j), PermissionInfo)

                            AddModulePermission(objModule, objCustomModulePermission, _
                                                                    objTabPermission.RoleID, objTabPermission.UserID, _
                                                                    objTabPermission.AllowAccess)
                        Next
                    End If
                Next

                objModule.AllTabs = False
                objModule.Alignment = align

                'apply Custom Container to module
                objModule.ContainerSrc = CONTAINER_TRANSPARENT_PLAIN

                newModuleID = objModules.AddModule(objModule)
            Next

            Return newModuleID
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>Adds a Module Permission</summary>
        ''' <param name="permission">The permission to add</param>
        ''' <param name="roleId">The Id of the role to add the permission for.</param>
        ''' -----------------------------------------------------------------------------
        Private Function AddModulePermission(ByVal objModule As ModuleInfo, ByVal permission As PermissionInfo, ByVal roleId As Integer, ByVal userId As Integer, ByVal allowAccess As Boolean) As ModulePermissionInfo
            Dim objModulePermission As New ModulePermissionInfo
            objModulePermission.ModuleID = objModule.ModuleID
            objModulePermission.PermissionID = permission.PermissionID
            objModulePermission.RoleID = roleId
            objModulePermission.UserID = userId
            objModulePermission.PermissionKey = permission.PermissionKey
            objModulePermission.AllowAccess = allowAccess

            ' add the permission to the collection
            If Not objModule.ModulePermissions.Contains(objModulePermission) Then
                objModule.ModulePermissions.Add(objModulePermission)
            End If

            Return objModulePermission
        End Function

#End Region

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-10-17 07:05:03

外观和容器由 SkinSrcContainerSrc 值确定。
这些保留在 Tabs 和 TabModules 表中,但您也可以在查询字符串中传递它们来为每个请求设置外观或容器。

选项卡的持久 SkinSrc 值是使用 TabInfo 对象通过 TabController 方法设置的。
分别使用ModuleControllerModuleInfo设置ContainerSrc。
(ModuleController 还用于将模块放置在窗格中。)

查询字符串参数 SkinSrc 和 ContainerSrc 采用不带文件扩展名的外观或容器路径值,例如:

&SkinSrc=/portals/_default/skins/_default/no%20skin
&ContainerSrc=/portals/_default/containers/_default/no%20container

Skin and container are determined by SkinSrc and ContainerSrc values.
These are persisted in the Tabs and TabModules tables, but you can also pass them in querystring to set a skin or container for each request.

Persistent SkinSrc value for a tab is set with TabController methods using TabInfo objects.
Respectively, use ModuleController and ModuleInfo to set ContainerSrc.
(ModuleController is also used to place modules in panes.)

Querystring parameters SkinSrc and ContainerSrc take the skin or container path value without a file extension, for example:

&SkinSrc=/portals/_default/skins/_default/no%20skin
&ContainerSrc=/portals/_default/containers/_default/no%20container
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文