public static List <ListItem> GetTemplatesFiles(PortalSettings portalSettings, int moduleId, TemplateManifest selectedTemplate, string moduleSubDir, FileUri otherModuleTemplate) { //bool otherModuleSkinTemplate = otherModuleTemplate != null && otherModuleTemplate.PhysicalFilePath.Contains(HostingEnvironment.MapPath(GetSkinTemplateFolder(portalSettings, moduleSubDir))); string basePath = HostingEnvironment.MapPath(GetSiteTemplateFolder(portalSettings, moduleSubDir)); if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } var dirs = Directory.GetDirectories(basePath); if (otherModuleTemplate != null) { var selDir = otherModuleTemplate.PhysicalFullDirectory; if (!dirs.Contains(selDir)) { selDir = Path.GetDirectoryName(selDir); } if (dirs.Contains(selDir)) { dirs = new string[] { selDir } } ; else { dirs = new string[] { } }; } List <ListItem> lst = new List <ListItem>(); foreach (var dir in dirs) { string templateCat = "Site"; //string dirName = Path.GetFileNameWithoutExtension(dir); string dirName = dir.Substring(basePath.Length); int modId = -1; if (int.TryParse(dirName, out modId)) { // if numeric directory name --> module template if (modId == moduleId) { // this module -> show templateCat = "Module"; } else { // if it's from an other module -> don't show continue; } } IEnumerable <string> files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories); IEnumerable <string> manifestfiles = files.Where(s => s.EndsWith("manifest.json")); var manifestTemplateFound = false; if (manifestfiles.Any()) { foreach (string manifestFile in manifestfiles) { FileUri manifestFileUri = FileUri.FromPath(manifestFile); var manifest = ManifestUtils.GetFileManifest(manifestFileUri); if (manifest != null && manifest.HasTemplates) { manifestTemplateFound = true; foreach (var template in manifest.Templates) { FileUri templateUri = new FileUri(manifestFileUri.FolderPath, template.Key); string templateName = Path.GetDirectoryName(manifestFile).Substring(basePath.Length).Replace("\\", " / "); if (!string.IsNullOrEmpty(template.Value.Title)) { templateName = templateName + " - " + template.Value.Title; } var item = new ListItem((templateCat == "Site" ? "" : templateCat + " : ") + templateName, templateUri.FilePath); if (selectedTemplate != null && templateUri.FilePath.ToLowerInvariant() == selectedTemplate.Key.ToString().ToLowerInvariant()) { item.Selected = true; } lst.Add(item); } } } } if (!manifestTemplateFound) { IEnumerable <string> scriptfiles = files.Where(s => s.EndsWith(".cshtml") || s.EndsWith(".vbhtml") || s.EndsWith(".hbs")); foreach (string script in scriptfiles) { FileUri templateUri = FileUri.FromPath(script); string scriptName = script.Remove(script.LastIndexOf(".")).Replace(basePath, ""); if (templateCat == "Module") { if (scriptName.ToLower().EndsWith("template")) { scriptName = ""; } else { scriptName = scriptName.Substring(scriptName.LastIndexOf("\\") + 1); } } else if (scriptName.ToLower().EndsWith("template")) { scriptName = scriptName.Remove(scriptName.LastIndexOf("\\")); } else { scriptName = scriptName.Replace("\\", " - "); } var item = new ListItem((templateCat == "Site" ? "" : templateCat + " : ") + scriptName, templateUri.FilePath); if (selectedTemplate != null && templateUri.FilePath.ToLowerInvariant() == selectedTemplate.Key.ToString().ToLowerInvariant()) { item.Selected = true; } lst.Add(item); } } } // skin basePath = HostingEnvironment.MapPath(GetSkinTemplateFolder(portalSettings, moduleSubDir)); if (Directory.Exists(basePath)) { dirs = Directory.GetDirectories(basePath); if (otherModuleTemplate != null /*&& */) { var selDir = otherModuleTemplate.PhysicalFullDirectory; if (!dirs.Contains(selDir)) { selDir = Path.GetDirectoryName(selDir); } if (dirs.Contains(selDir)) { dirs = new string[] { selDir } } ; else { dirs = new string[] { } }; } foreach (var dir in dirs) { string templateCat = "Skin"; IEnumerable <string> files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories); IEnumerable <string> manifestfiles = files.Where(s => s.EndsWith("manifest.json")); var manifestTemplateFound = false; if (manifestfiles.Any()) { foreach (string manifestFile in manifestfiles) { FileUri manifestFileUri = FileUri.FromPath(manifestFile); var manifest = ManifestUtils.GetFileManifest(manifestFileUri); if (manifest != null && manifest.HasTemplates) { manifestTemplateFound = true; foreach (var template in manifest.Templates) { FileUri templateUri = new FileUri(manifestFileUri.FolderPath, template.Key); string templateName = Path.GetDirectoryName(manifestFile).Substring(basePath.Length).Replace("\\", " / "); if (!string.IsNullOrEmpty(template.Value.Title)) { templateName = templateName + " - " + template.Value.Title; } var item = new ListItem((templateCat == "Site" ? "" : templateCat + " : ") + templateName, templateUri.FilePath); if (selectedTemplate != null && templateUri.FilePath.ToLowerInvariant() == selectedTemplate.Key.ToString().ToLowerInvariant()) { item.Selected = true; } lst.Add(item); } } } } if (!manifestTemplateFound) { var scriptfiles = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".cshtml") || s.EndsWith(".vbhtml") || s.EndsWith(".hbs")); foreach (string script in scriptfiles) { string scriptName = script.Remove(script.LastIndexOf(".")).Replace(basePath, ""); if (scriptName.ToLower().EndsWith("template")) { scriptName = scriptName.Remove(scriptName.LastIndexOf("\\")); } else { scriptName = scriptName.Replace("\\", " - "); } FileUri templateUri = FileUri.FromPath(script); var item = new ListItem(templateCat + " : " + scriptName, templateUri.FilePath); if (selectedTemplate != null && templateUri.FilePath.ToLowerInvariant() == selectedTemplate.Key.ToString().ToLowerInvariant()) { item.Selected = true; } lst.Add(item); } } } } return(lst); }
private static string[] GetDirs(TemplateManifest selectedTemplate, FileUri otherModuleTemplate, bool advanced, string basePath, string[] dirs, List <ListItem> lst, string folderType) { if (Directory.Exists(basePath)) { dirs = Directory.GetDirectories(basePath); if (otherModuleTemplate != null /*&& */) { var selDir = otherModuleTemplate.PhysicalFullDirectory; if (!dirs.Contains(selDir)) { selDir = Path.GetDirectoryName(selDir); } if (dirs.Contains(selDir)) { dirs = new string[] { selDir } } ; else { dirs = new string[] { } }; } foreach (var dir in dirs) { string templateCat = folderType; IEnumerable <string> files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories); IEnumerable <string> manifestfiles = files.Where(s => s.EndsWith("manifest.json")); var manifestTemplateFound = false; if (manifestfiles.Any()) { foreach (string manifestFile in manifestfiles) { FileUri manifestFileUri = FileUri.FromPath(manifestFile); var manifest = ManifestUtils.LoadManifestFileFromCacheOrDisk(manifestFileUri); if (manifest != null && manifest.HasTemplates) { manifestTemplateFound = true; foreach (var template in manifest.Templates) { FileUri templateUri = new FileUri(manifestFileUri.FolderPath, template.Key); string templateName = Path.GetDirectoryName(manifestFile).Substring(basePath.Length).Replace("\\", " / "); if (!String.IsNullOrEmpty(template.Value.Title)) { if (advanced) { templateName = templateName + " - " + template.Value.Title; } } var item = new ListItem((templateCat == "Site" ? "" : templateCat + " : ") + templateName, templateUri.FilePath); if (selectedTemplate != null && templateUri.FilePath.ToLowerInvariant() == selectedTemplate.Key.ToString().ToLowerInvariant()) { item.Selected = true; } lst.Add(item); if (!advanced) { break; } } } } } if (!manifestTemplateFound) { var scriptfiles = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(".cshtml") || s.EndsWith(".vbhtml") || s.EndsWith(".hbs")); foreach (string script in scriptfiles) { string scriptName = script.Remove(script.LastIndexOf(".")).Replace(basePath, ""); if (scriptName.ToLower().EndsWith("template")) { scriptName = scriptName.Remove(scriptName.LastIndexOf("\\")); } else { scriptName = scriptName.Replace("\\", " - "); } FileUri templateUri = FileUri.FromPath(script); var item = new ListItem(templateCat + " : " + scriptName, templateUri.FilePath); if (selectedTemplate != null && templateUri.FilePath.ToLowerInvariant() == selectedTemplate.Key.ToString().ToLowerInvariant()) { item.Selected = true; } lst.Add(item); } } } } return(dirs); }