This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathManageSkin.ascx.vb
354 lines (290 loc) · 15 KB
/
ManageSkin.ascx.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
Option Strict On
Option Explicit On
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Portals
Imports DotNetNuke.Services.Exceptions
Imports Dotnetnuke.Services.Localization
Imports System.Collections.Generic
Imports System.IO
Namespace DNNStuff.Aggregator
Partial Class ManageSkin
Inherits Entities.Modules.PortalModuleBase
' other
Private Const ALLTEMPLATES As String = "_All"
Private Const NONE As String = "_None"
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
MyBase.HelpURL = "http://www.dnnstuff.com/"
End Sub
#End Region
#Region " Page Level"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
DNNUtilities.InjectCSS(Me.Page, ResolveUrl("Resources/Support/edit.css"))
Page.ClientScript.RegisterClientScriptInclude(Me.GetType, "yeti", ResolveUrl("resources/support/yetii-min.js"))
If Page.IsPostBack = False Then
LoadSettings()
End If
Catch ex As Exception 'Module failed to load
ProcessModuleLoadException(Me, ex)
End Try
End Sub
Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
Try
ReturnToPage()
Catch ex As Exception 'Module failed to load
ProcessModuleLoadException(Me, ex)
End Try
End Sub
Private Sub cmdCopySkin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCopySkin.Click
Try
If Page.IsValid Then
CopySkin()
End If
Catch ex As Exception 'Module failed to load
ProcessModuleLoadException(Me, ex)
End Try
End Sub
Private Sub cmdSaveFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSaveFile.Click
Try
If Page.IsValid Then
SaveTextToFile(txtSkinText.Text, cboEditTabFile.SelectedItem.Value)
phEditSkinResults.Controls.Add(New LiteralControl(String.Format("<span class=""normal"">{0} saved</span>", cboEditTabFile.SelectedItem.Value)))
End If
Catch ex As Exception
ProcessModuleLoadException(Me, ex)
End Try
End Sub
Private Sub ReturnToPage()
Entities.Modules.ModuleController.SynchronizeModule(ModuleId)
' clear the tab cache
DataCache.ClearModuleCache(TabId)
' Redirect back to the portal home page
Response.Redirect(NavigateURL(), True)
End Sub
#End Region
#Region " Process"
Private Sub CopySkin()
Dim templateFolder As String = ""
If cboTabTemplate.SelectedValue = ALLTEMPLATES Then
CopySkin(cboTabSkin.SelectedValue, txtNewSkinName.Text)
Else
CopyTemplate(cboTabSkin.SelectedValue, cboTabTemplate.SelectedValue, txtNewSkinName.Text)
End If
End Sub
Private Sub CopySkin(ByVal FromSkin As String, ByVal ToSkin As String)
' create to skin folder if it doesn't exist
EnsureToFolder(ToSkin)
' loop through from folder and copy each template
Dim fromSkinFolder As String = IO.Path.Combine(MapPath("Skins"), FromSkin)
Dim fromSkinDirectory As DirectoryInfo = New DirectoryInfo(fromSkinFolder)
For Each fromTemplateDirectory As DirectoryInfo In fromSkinDirectory.GetDirectories
CopyTemplate(FromSkin, fromTemplateDirectory.Name, ToSkin)
Next
End Sub
Private Sub EnsureToFolder(ByVal ToSkin As String)
' create to skin folder if it doesn't exist
Dim toSkinFolder As String = IO.Path.Combine(MapPath("Skins"), ToSkin)
If Not IO.Directory.Exists(toSkinFolder) Then IO.Directory.CreateDirectory(toSkinFolder)
End Sub
Private Sub CopyTemplate(ByVal FromSkin As String, ByVal FromTemplate As String, ByVal ToSkin As String)
' create to skin folder if it doesn't exist
EnsureToFolder(ToSkin)
' copy files
Dim toTemplateFolder As String = IO.Path.Combine(MapPath("Skins"), ToSkin & IO.Path.DirectorySeparatorChar & FromTemplate)
Dim fromTemplateFolder As String = IO.Path.Combine(MapPath("Skins"), FromSkin & IO.Path.DirectorySeparatorChar & FromTemplate)
Try
CopyDirectory(fromTemplateFolder, toTemplateFolder, chkOverwrite.Checked)
' report to user
phCopySkinResults.Controls.Add(New LiteralControl(String.Format("<li>Copied {0}/{1} to {2}/{1}</li>", FromSkin, FromTemplate, ToSkin)))
' fix styles css file
Dim stylesFile As IO.FileInfo = New IO.FileInfo(IO.Path.Combine(toTemplateFolder, "styles.css"))
If stylesFile.Exists Then
' replace selectors
Dim contents As String = GetFileContents(stylesFile.FullName)
contents = contents.Replace(FromSkin & "_" & FromTemplate, ToSkin & "_" & FromTemplate)
SaveTextToFile(contents, stylesFile.FullName)
' report to user
phCopySkinResults.Controls.Add(New LiteralControl(String.Format("<li>Fixed styles.css in {2}/{1}</li>", "", FromTemplate, ToSkin)))
End If
Catch ex As Exception
phCopySkinResults.Controls.Add(New LiteralControl(String.Format("<li>Error copying {0}/{1} to {2}/{1}</li>", FromSkin, FromTemplate, ToSkin)))
phCopySkinResults.Controls.Add(New LiteralControl(String.Format("<li>Error was: {0}</li>", Err.Description)))
End Try
End Sub
Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)
' the source directory must exist, otherwise throw an exception
If SourceDir.Exists Then
' if destination SubDir's parent SubDir does not exist throw an exception
If Not DestDir.Parent.Exists Then
Throw New DirectoryNotFoundException _
("Destination directory does not exist: " + DestDir.Parent.FullName)
End If
If Not DestDir.Exists Then
DestDir.Create()
End If
' copy all the files of the current directory
Dim ChildFile As FileInfo
For Each ChildFile In SourceDir.GetFiles()
If Overwrite Then
ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True)
Else
' if Overwrite = false, copy the file only if it does not exist
' this is done to avoid an IOException if a file already exists
' this way the other files can be copied anyway...
If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then
ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), False)
End If
End If
Next
' copy all the sub-directories by recursively calling this same routine
Dim SubDir As DirectoryInfo
For Each SubDir In SourceDir.GetDirectories()
CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, _
SubDir.Name), Overwrite)
Next
Else
Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
End If
End Sub
Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String
Dim strContents As String
Dim objReader As IO.StreamReader
Try
objReader = New IO.StreamReader(FullPath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return ""
End Function
Public Function SaveTextToFile(ByVal strData As String, _
ByVal FullPath As String, _
Optional ByVal ErrInfo As String = "") As Boolean
Dim bAns As Boolean = False
Dim objReader As IO.StreamWriter
Try
objReader = New IO.StreamWriter(FullPath)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return bAns
End Function
#End Region
#Region " Settings"
Private Sub UpdateSettings()
End Sub
Private Sub LoadSettings()
' settings
Dim ms As ModuleSettings = New ModuleSettings(ModuleId)
' theme/skin
BindSkinFolder(cboTabSkin)
BindSkinFolder(cboEditTabSkin, IncludeNone:=True, Selected:=ms.TabSkin)
' template
BindTemplateFolder(cboTabTemplate, cboTabSkin.SelectedItem.Value)
BindTemplateFolder(cboEditTabTemplate, cboEditTabSkin.SelectedItem.Value, False, IncludeNone:=True, Selected:=ms.TabTemplate)
' file
BindTemplateFile(cboEditTabFile, cboEditTabSkin.SelectedItem.Value, cboEditTabTemplate.SelectedItem.Value, IncludeNone:=True)
End Sub
Private Sub BindSkinFolder(ByVal o As ListControl, Optional ByVal IncludeNone As Boolean = False, Optional ByVal Selected As String = "")
Dim skinFolder As New IO.DirectoryInfo(Server.MapPath(ResolveUrl("Skins")))
o.Items.Clear()
For Each folder As IO.DirectoryInfo In skinFolder.GetDirectories()
If folder.GetDirectories.Length > 0 Then
o.Items.Add(folder.Name)
End If
Next
If IncludeNone Then
o.Items.Insert(0, New ListItem("<None>", NONE))
End If
If Selected <> "" Then
Dim si As ListItem = o.Items.FindByValue(Selected)
If si IsNot Nothing Then si.Selected = True
End If
End Sub
Private Sub BindTemplateFolder(ByVal o As ListControl, ByVal skinName As String, Optional ByVal IncludeAll As Boolean = True, Optional ByVal IncludeNone As Boolean = False, Optional ByVal Selected As String = "")
If Not skinName = NONE Then
Dim skinFolder As New IO.DirectoryInfo(IO.Path.Combine(Server.MapPath(ResolveUrl("Skins")), skinName))
o.Items.Clear()
For Each folder As IO.DirectoryInfo In skinFolder.GetDirectories()
If Not folder.Name.StartsWith("_") Then o.Items.Add(folder.Name)
Next
If IncludeAll Then
o.Items.Insert(0, New ListItem("All Templates", ALLTEMPLATES))
End If
End If
If IncludeNone Then
o.Items.Insert(0, New ListItem("<None>", NONE))
End If
If Selected <> "" Then
Dim si As ListItem = o.Items.FindByValue(Selected)
If si IsNot Nothing Then si.Selected = True
End If
End Sub
Private Sub BindTemplateFile(ByVal o As ListControl, ByVal skinName As String, ByVal templateName As String, Optional ByRef IncludeNone As Boolean = False)
If Not skinName = NONE Then
Dim templateFolder As New IO.DirectoryInfo(IO.Path.Combine(IO.Path.Combine(Server.MapPath(ResolveUrl("Skins")), skinName), templateName))
o.Items.Clear()
For Each file As IO.FileInfo In templateFolder.GetFiles()
If Not ".gif.jpg.jpeg.png".Contains(file.Extension.ToLower) Then
o.Items.Add(New ListItem(file.Name, file.FullName))
End If
Next
End If
If IncludeNone Then
o.Items.Insert(0, New ListItem("<None>", NONE))
End If
End Sub
Private Sub BindFile(ByVal txt As TextBox, ByVal fileName As String)
txt.Text = GetFileContents(fileName)
End Sub
Private Sub cboTabSkin_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTabSkin.SelectedIndexChanged
BindTemplateFolder(cboTabTemplate, cboTabSkin.SelectedItem.Value, IncludeAll:=True, IncludeNone:=False)
End Sub
Private Sub cboEditTabSkin_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboEditTabSkin.SelectedIndexChanged
If cboEditTabSkin.SelectedItem.Value <> NONE Then
BindTemplateFolder(cboEditTabTemplate, cboEditTabSkin.SelectedItem.Value, IncludeAll:=False, IncludeNone:=True)
cboEditTabFile.SelectedValue = NONE
Else
cboEditTabTemplate.Items.Clear()
End If
cboEditTabFile.Items.Clear()
txtSkinText.Text = ""
End Sub
Private Sub cboEditTabTemplate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboEditTabTemplate.SelectedIndexChanged
If cboEditTabTemplate.SelectedItem.Value <> NONE Then
BindTemplateFile(cboEditTabFile, cboEditTabSkin.SelectedItem.Value, cboEditTabTemplate.SelectedItem.Value, IncludeNone:=True)
Else
cboEditTabFile.Items.Clear()
End If
txtSkinText.Text = ""
End Sub
Private Sub cboEditTabFile_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboEditTabFile.SelectedIndexChanged
If cboEditTabFile.SelectedItem.Value <> NONE Then
BindFile(txtSkinText, cboEditTabFile.SelectedItem.Value)
End If
End Sub
#End Region
#Region " Validation"
Private Sub vldNewSkinName_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles vldNewSkinName.ServerValidate
args.IsValid = Not (args.Value.Length = 0 Or args.Value.Contains(" "))
End Sub
#End Region
End Class
End Namespace