public IActionResult EditSettings(CMSDatabase db, SettingsModel model, HttpContext context) { ConfigurationHandler configurationHandler = context.RequestServices.GetRequiredService <ConfigurationHandler>(); configurationHandler.ChangeConfigFile(model); if (!string.IsNullOrEmpty(model.ProductBlockTemplate)) { IHostingEnvironment env = context.RequestServices.GetRequiredService <IHostingEnvironment>(); string pathToTemplate = env.GetProductBlockTemplateFullPath(); string oldTemplate = OtherFunctions.GetFileContent(pathToTemplate); if (!model.ProductBlockTemplate.Equals(oldTemplate, StringComparison.Ordinal)) { using (StreamWriter writer = new StreamWriter(pathToTemplate)) { writer.Write(model.ProductBlockTemplate); } string[] additions = { "@using Treynessen.Functions;", "@using Treynessen.Database.Entities;", "@addTagHelper Treynessen.TagHelpers.ImageTagHelper, StoreCMS" }; string cshtmlTemplate = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: model.ProductBlockTemplate, modelType: "ProductPage", env: env, skipChunkName: null, additions: additions ); using (StreamWriter writer = new StreamWriter(env.GetProductBlockCshtmlFullPath())) { writer.Write(cshtmlTemplate); } } } LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: (context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.SettingsEdited ); return(StatusCode(200)); }
public static void DeleteChunk(CMSDatabase db, int?itemID, HttpContext context, out bool successfullyDeleted) { if (!itemID.HasValue) { successfullyDeleted = false; return; } Chunk chunk = db.Chunks.FirstOrDefault(t => t.ID == itemID); if (chunk == null) { successfullyDeleted = false; return; } IHostingEnvironment env = context.RequestServices.GetRequiredService <IHostingEnvironment>(); string pathToChunkFile = $"{env.GetChunksFolderFullPath()}{chunk.Name}.cshtml"; if (File.Exists(pathToChunkFile)) { File.Delete(pathToChunkFile); } db.Chunks.Remove(chunk); db.SaveChanges(); successfullyDeleted = true; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{chunk.Name} (ID-{chunk.ID.ToString()}): {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.ChunkDeleted}" ); // Получаем список чанков и шаблонов, использующих данный чанк и делаем перерендер var templates = db.Templates.AsNoTracking().Where(t => t.TemplateSource.Contains($"[#{chunk.Name}]")).ToList(); var chunks = db.Chunks.AsNoTracking().Where(tc => tc.TemplateSource.Contains($"[#{chunk.Name}]")).ToList(); var renderTask = Task.Run(() => { foreach (var t in templates) { string _cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: t.TemplateSource, modelType: "Page", env: env, skipChunkName: null ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetTemplatesFolderFullPath(), t.Name, _cshtmlContent); } }); foreach (var c in chunks) { string _cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: c.TemplateSource, modelType: "Page", env: env, skipChunkName: c.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetChunksFolderFullPath(), c.Name, _cshtmlContent); } string productBlockFileContent = OtherFunctions.GetFileContent(env.GetProductBlockTemplateFullPath()); if (productBlockFileContent.Contains($"[#{chunk.Name}]")) { string[] additions = { "@using Treynessen.Functions;", "@using Treynessen.Database.Entities;", "@addTagHelper Treynessen.TagHelpers.ImageTagHelper, StoreCMS" }; string productBlockCshtmlTemplate = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: productBlockFileContent, modelType: "ProductPage", env: env, skipChunkName: null, additions: additions ); using (StreamWriter writer = new StreamWriter(env.GetProductBlockCshtmlFullPath())) { writer.Write(productBlockCshtmlTemplate); } } renderTask.Wait(); }
public static void EditChunk(CMSDatabase db, int?itemID, TemplateModel model, HttpContext context, out bool successfullyCompleted) { if (!itemID.HasValue || model == null) { successfullyCompleted = false; return; } Chunk editableChunk = db.Chunks.AsNoTracking().FirstOrDefault(t => t.ID == itemID); if (editableChunk == null) { successfullyCompleted = false; return; } Chunk editedChunk = TemplatesManagementFunctions.TemplateModelToITemplate <Chunk>(model, context); if (editedChunk == null) { successfullyCompleted = false; return; } if (editedChunk.Name.Equals("_ViewImports", StringComparison.OrdinalIgnoreCase)) { editedChunk.Name = "view_imports"; } IHostingEnvironment env = context.RequestServices.GetService <IHostingEnvironment>(); editedChunk.ID = itemID.Value; editedChunk.TemplatePath = env.GetChunksFolderShortPath(); TemplatesManagementFunctions.SetUniqueITemplateName(db, editedChunk); editedChunk.TemplatePath += $"{editedChunk.Name}.cshtml"; db.Chunks.Update(editedChunk); db.SaveChanges(); successfullyCompleted = true; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{editableChunk.Name} (ID-{editableChunk.ID.ToString()}): {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.ChunkEdited}" ); // Изменяем cshtml файл, если изменилось имя шаблона и/или код шаблона bool changedName = !editedChunk.Name.Equals(editableChunk.Name, StringComparison.Ordinal); bool changedTemplateSource = !editedChunk.TemplateSource.Equals(editableChunk.TemplateSource, StringComparison.Ordinal); if (changedName && changedTemplateSource) { string pathToOldFileName = $"{env.GetChunksFolderFullPath()}{editableChunk.Name}.cshtml"; if (File.Exists(pathToOldFileName)) { File.Delete(pathToOldFileName); } string cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: editedChunk.TemplateSource, modelType: "Page", env: env, skipChunkName: editedChunk.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetChunksFolderFullPath(), editedChunk.Name, cshtmlContent); } else if (changedName) { string pathToOldFileName = $"{env.GetChunksFolderFullPath()}{editableChunk.Name}.cshtml"; if (File.Exists(pathToOldFileName)) { File.Move(pathToOldFileName, $"{env.GetChunksFolderFullPath()}{editedChunk.Name}.cshtml"); } else { string cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: editedChunk.TemplateSource, modelType: "Page", env: env, skipChunkName: editedChunk.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetChunksFolderFullPath(), editedChunk.Name, cshtmlContent); } } else if (changedTemplateSource) { string cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: editedChunk.TemplateSource, modelType: "Page", env: env, skipChunkName: editedChunk.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetChunksFolderFullPath(), editedChunk.Name, cshtmlContent); } // Если изменилось название чанка, то получаем список шаблонов и чанков, которые использовали или теперь используют данный чанк // и делаем их перерендер if (changedName) { var templates = db.Templates.AsNoTracking() .Where(t => t.TemplateSource.Contains($"[#{editableChunk.Name}]") || t.TemplateSource.Contains($"[#{editedChunk.Name}]")) .ToList(); var chunks = db.Chunks.AsNoTracking() .Where(tc => tc.ID != itemID.Value && (tc.TemplateSource.Contains($"[#{editableChunk.Name}]") || tc.TemplateSource.Contains($"[#{editedChunk.Name}]"))) .ToList(); var renderTask = Task.Run(() => { foreach (var t in templates) { string cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: t.TemplateSource, modelType: "Page", env: env, skipChunkName: null ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetTemplatesFolderFullPath(), t.Name, cshtmlContent); } }); foreach (var c in chunks) { string cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: c.TemplateSource, modelType: "Page", env: env, skipChunkName: c.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetChunksFolderFullPath(), c.Name, cshtmlContent); } string productBlockFileContent = OtherFunctions.GetFileContent(env.GetProductBlockTemplateFullPath()); if (changedName && productBlockFileContent.Contains($"[#{editableChunk.Name}]")) { string[] additions = { "@using Treynessen.Functions;", "@using Treynessen.Database.Entities;", "@addTagHelper Treynessen.TagHelpers.ImageTagHelper, StoreCMS" }; string productBlockCshtmlTemplate = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: productBlockFileContent, modelType: "ProductPage", env: env, skipChunkName: null, additions: additions ); using (StreamWriter writer = new StreamWriter(env.GetProductBlockCshtmlFullPath())) { writer.Write(productBlockCshtmlTemplate); } } renderTask.Wait(); } }
public static void AddChunk(CMSDatabase db, TemplateModel model, HttpContext context, out bool successfullyCompleted) { Chunk chunk = TemplatesManagementFunctions.TemplateModelToITemplate <Chunk>(model, context); if (chunk == null) { successfullyCompleted = false; return; } IHostingEnvironment env = context.RequestServices.GetService <IHostingEnvironment>(); if (chunk.Name.Equals("_ViewImports", System.StringComparison.OrdinalIgnoreCase)) { chunk.Name = "view_imports"; } chunk.TemplatePath = env.GetChunksFolderShortPath(); TemplatesManagementFunctions.SetUniqueITemplateName(db, chunk); chunk.TemplatePath += $"{chunk.Name}.cshtml"; string cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: chunk.TemplateSource, modelType: "Page", env: env, skipChunkName: chunk.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile( pathToTemplatesFolder: env.GetChunksFolderFullPath(), templateName: chunk.Name, chstmlContent: cshtmlContent ); db.Chunks.Add(chunk); db.SaveChanges(); model.ID = chunk.ID; successfullyCompleted = true; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{chunk.Name} (ID-{chunk.ID.ToString()}): {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.ChunkAdded}" ); // Получаем список шаблонов и чанков, которые содержат данный чанк var templates = db.Templates.AsNoTracking().Where(t => t.TemplateSource.Contains($"[#{chunk.Name}]")).ToList(); var chunks = db.Chunks.AsNoTracking().Where(tc => tc.ID != chunk.ID && (tc.TemplateSource.Contains($"[#{chunk.Name}]"))).ToList(); // Делаем рендер, содержащих данный чанк, шаблонов и чанков var renderTask = Task.Run(() => { foreach (var t in templates) { string _cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: t.TemplateSource, modelType: "Page", env: env, skipChunkName: null ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetTemplatesFolderFullPath(), t.Name, _cshtmlContent); } }); foreach (var c in chunks) { string _cshtmlContent = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: c.TemplateSource, modelType: "Page", env: env, skipChunkName: c.Name ); TemplatesManagementFunctions.WriteCshtmlContentToFile(env.GetChunksFolderFullPath(), c.Name, _cshtmlContent); } // Если шаблон блока товара содержит текущий чанк, то делаем его перерендер string productBlockFileContent = OtherFunctions.GetFileContent(env.GetProductBlockTemplateFullPath()); if (productBlockFileContent.Contains($"[#{chunk.Name}]")) { string[] additions = { "@using Treynessen.Functions;", "@using Treynessen.Database.Entities;", "@addTagHelper Treynessen.TagHelpers.ImageTagHelper, StoreCMS" }; string productBlockCshtmlTemplate = TemplatesManagementFunctions.SourceToCSHTML( db: db, source: productBlockFileContent, modelType: "ProductPage", env: env, skipChunkName: null, additions: additions ); using (StreamWriter writer = new StreamWriter(env.GetProductBlockCshtmlFullPath())) { writer.Write(productBlockCshtmlTemplate); } } renderTask.Wait(); }