public ActionResult Index() { using (var db = new GenerateHtmlDbContext()) { var compList = db.HtmlComponents .GroupBy(x => new HtmlComponentGroup { Id = x.CategoryId, Name = x.HtmlComponentCategory.Name }, (key, group) => new ListComponentWithCategory { CategoryId = key.Id, CategoryName = key.Name, ListComponent = group.Select(y => new HtmlComponentViewModel { Id = y.Id, Name = y.Name, ImageName = y.ImageName, CategoryId = y.CategoryId, CategoryName = y.HtmlComponentCategory.Name }).ToList() }) .ToList(); ViewBag.Plugins = db.Plugins.Select(x => new PluginViewModel { Id = x.Id, Name = x.Name }).ToList(); return(View(compList)); } }
public ActionResult AddOrEdit(int id = 0) { using (var db = new GenerateHtmlDbContext()) { ViewBag.Category = db.HtmlComponentCategories .Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }).ToList(); if (id == 0) { return(View(new HtmlComponentViewModel())); } var compList = db.HtmlComponents .Select(x => new HtmlComponentViewModel { Id = x.Id, Name = x.Name, HtmlBody = x.HtmlBody, ScriptPath = x.ScriptPath, CssPath = x.CssPath, ImageName = x.ImageName, CategoryId = x.CategoryId, CategoryName = x.HtmlComponentCategory.Name }).FirstOrDefault(x => x.Id == id); if (compList != null && Directory.Exists(Server.MapPath(@"/Media/" + id))) { var di = new DirectoryInfo(Server.MapPath(@"/Media/" + id)); var count = di.GetFiles().Length; compList.MediaInfo = count + "files"; } return(View(compList)); } }
public ActionResult AddOrEdit(HtmlComponentViewModel comp) { using (var db = new GenerateHtmlDbContext()) { if (comp.Id == 0) { var obj = new HtmlComponent { Name = comp.Name }; db.HtmlComponents.Add(obj); db.SaveChanges(); return(Json(new { success = true, message = "Saved successfully!!!" }, JsonRequestBehavior.AllowGet)); } else { var obj = new HtmlComponent { Name = comp.Name }; db.Entry(obj).State = EntityState.Modified; db.SaveChanges(); return(Json(new { success = true, message = "Updated successfully!!!" }, JsonRequestBehavior.AllowGet)); } } }
public ActionResult HandleSaveHtmlFile(string html, string elementIds, string websiteName, string fileName, string pluginIds) { using (var db = new GenerateHtmlDbContext()) { websiteName = ChangeSymbol.DoChange(websiteName.Trim()); fileName = ChangeSymbol.DoChange(fileName.Trim()); var compList = db.HtmlComponents .Select(x => new HtmlComponentViewModel { Id = x.Id, ScriptPath = x.ScriptPath, CssPath = x.CssPath }).Where(x => elementIds.Contains(x.Id.ToString())).ToList(); var listPlugin = new List <PluginViewModel>(); if (!string.IsNullOrEmpty(pluginIds)) { var ids = pluginIds.Split(','); listPlugin = db.Plugins .Select(x => new PluginViewModel { Id = x.Id, Name = x.Name, ScriptPath = x.ScriptPath, CssPath = x.CssPath }).Where(x => ids.Contains(x.Id.ToString())).ToList(); } var listStyleSheetLinks = compList.Where(x => x.CssPath != null).Select(x => x.CssPath).ToList(); var strStyleSheets = "<link href='" + Path.Combine(Utils.CssPath, Utils.ComponentsCss).Replace("\\", "/") + "' rel='stylesheet' />"; var listScriptLinks = compList.Where(x => x.ScriptPath != null).Select(x => x.ScriptPath).ToList(); var strScript = "<script src='" + Path.Combine(Utils.ScriptsPath, Utils.ComponentsScript).Replace("\\", "/") + "'></script>"; var libCssLinks = listPlugin.Where(x => x.CssPath != null).Select(x => new PluginItemViewModel { Name = x.Name, FilePath = x.CssPath }); var libScriptLinks = listPlugin.Where(x => x.ScriptPath != null).Select(x => new PluginItemViewModel { Name = x.Name, FilePath = x.ScriptPath }); // replace html body vào layout var textFile = ReplaceTextFile(strStyleSheets, html, strScript, libCssLinks, libScriptLinks); // tạo file html GenarateHtmlFile(textFile, websiteName, fileName); // chép file css vào folder GenarateFile(websiteName, listStyleSheetLinks, Utils.CssPath, Utils.ComponentsCss); // chép file script vào folder GenarateFile(websiteName, listScriptLinks, Utils.ScriptsPath, Utils.ComponentsScript); if (listPlugin.Any()) { var libsName = listPlugin.Where(x => x.ScriptPath != null).Select(x => x.Name); // chép libs vào folder GenarateLibsFolder(websiteName, libsName); } foreach (var item in compList) { DirectoryCopy(Server.MapPath(Path.Combine(Utils.MediaFolder, item.Id.ToString()).Replace("\\", "/")), Server.MapPath(Path.Combine(Utils.DownLoadFolder, websiteName, Utils.ImagePath)), true); } return(Json(new { success = true, message = "Add new html page success !!!" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult GetData() { using (var db = new GenerateHtmlDbContext()) { var compList = db.HtmlComponents .Select(x => new HtmlComponentViewModel { Name = x.Name }).ToList(); return(Json(new { data = compList }, JsonRequestBehavior.AllowGet)); } }
public ActionResult Delete(int id) { using (var db = new GenerateHtmlDbContext()) { var comp = db.HtmlComponents.FirstOrDefault(x => x.Id == id); if (comp != null) { db.HtmlComponents.Remove(comp); db.SaveChanges(); return(Json(new { success = true, message = "Xóa thành công." }, JsonRequestBehavior.AllowGet)); } return(Json(new { success = false, message = "Đã có lỗi xảy ra." }, JsonRequestBehavior.AllowGet)); } }
public ActionResult AddOrEdit(int id = 0) { if (id == 0) { return(View(new HtmlComponentCategoryViewModel())); } using (var db = new GenerateHtmlDbContext()) { var compList = db.HtmlComponentCategories .Select(x => new HtmlComponentCategoryViewModel { Id = x.Id, Name = x.Name }).FirstOrDefault(x => x.Id == id); return(View(compList)); } }
public ActionResult AddOrEdit(int id = 0) { if (id == 0) { return(View(new HtmlComponentViewModel())); } using (var db = new GenerateHtmlDbContext()) { var compList = db.HtmlComponents .Select(x => new HtmlComponentViewModel { Name = x.Name }).ToList(); var view = compList.FirstOrDefault(x => x.Id == id); return(View(view)); } }
public ActionResult AddOrEdit(int id = 0) { if (id == 0) { return(View(new PluginViewModel())); } using (var db = new GenerateHtmlDbContext()) { var plList = db.Plugins .Select(x => new PluginViewModel { Id = x.Id, Name = x.Name, CssPath = x.CssPath, ScriptPath = x.ScriptPath }).FirstOrDefault(x => x.Id == id); return(View(plList)); } }
public ActionResult AddOrEdit(HtmlComponentCategoryViewModel comp) { if (!ModelState.IsValid) { return(PartialView("~/Areas/Admin/Views/Category/_form.cshtml", comp)); } using (var db = new GenerateHtmlDbContext()) { if (comp.Id == 0) { var item = db.HtmlComponentCategories.FirstOrDefault(x => x.Name.Trim().ToLower() == comp.Name.Trim().ToLower()); if (item != null) { return(Json(new { success = false, message = "Category đã tồn tại." }, JsonRequestBehavior.AllowGet)); } var obj = new HtmlComponentCategory { Name = comp.Name }; db.HtmlComponentCategories.Add(obj); db.SaveChanges(); return(Json(new { success = true, message = "Lưu thành công." }, JsonRequestBehavior.AllowGet)); } else { var item = db.HtmlComponentCategories.FirstOrDefault(x => x.Name.Trim().ToLower() == comp.Name.Trim().ToLower() && x.Id != comp.Id); if (item != null) { return(Json(new { success = false, message = "Category đã tồn tại." }, JsonRequestBehavior.AllowGet)); } var obj = new HtmlComponentCategory { Id = comp.Id, Name = comp.Name }; db.Entry(obj).State = EntityState.Modified; db.SaveChanges(); return(Json(new { success = true, message = "Sửa thành công." }, JsonRequestBehavior.AllowGet)); } } }
public ActionResult GetData() { try { using (var db = new GenerateHtmlDbContext()) { var plList = db.Plugins.Select(x => new PluginViewModel { Id = x.Id, Name = x.Name }).ToList(); return(Json(new { data = plList }, JsonRequestBehavior.AllowGet)); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public ActionResult LoadHtmlComponent(int elementId) { using (var db = new GenerateHtmlDbContext()) { var compList = db.HtmlComponents .Select(x => new HtmlComponentViewModel { Id = x.Id, Name = x.Name, HtmlBody = x.HtmlBody, ScriptPath = x.ScriptPath, CssPath = x.CssPath }).FirstOrDefault(x => x.Id == elementId); var strHtml = string.Empty; if (compList != null) { try { if (!string.IsNullOrEmpty(compList.CssPath)) { var fileStream = new FileStream(Server.MapPath(Path.Combine(Utils.UploadFolder, Utils.CssPath, compList.CssPath)), FileMode.Open, FileAccess.Read); using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) { var textFile = streamReader.ReadToEnd(); textFile = textFile.Replace("../images/", "/Media/" + compList.Id + "/"); compList.CssPath = textFile; } } } catch (Exception e) { Console.WriteLine(e); throw; } strHtml = compList.HtmlBody; } return(compList != null ? Json(new { success = true, css = compList.CssPath, script = compList.ScriptPath, html = strHtml }, JsonRequestBehavior.AllowGet) : Json(new { success = false, message = "an error occurred !!!" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult HandleAddPlugins(string pluginIds) { if (!string.IsNullOrEmpty(pluginIds)) { try { using (var db = new GenerateHtmlDbContext()) { var ids = pluginIds.Split(','); var compList = db.Plugins .Select(x => new PluginViewModel { Id = x.Id, Name = x.Name, ScriptPath = x.ScriptPath, CssPath = x.CssPath }).Where(x => ids.Contains(x.Id.ToString())).ToList(); var cssFiles = new List <string>(); var scriptFiles = new List <string>(); if (compList.Any()) { foreach (var item in compList) { cssFiles.Add(Path.Combine(Utils.LibsFolder, ChangeSymbol.DoChange(item.Name), Utils.CssPath, item.CssPath).Replace("\\", "/")); scriptFiles.Add(Path.Combine(Utils.LibsFolder, ChangeSymbol.DoChange(item.Name), Utils.ScriptsPath, item.ScriptPath).Replace("\\", "/")); } } return(Json(new { success = true, css = cssFiles, script = scriptFiles }, JsonRequestBehavior.AllowGet)); } } catch (Exception e) { Console.WriteLine(e); throw; } } return(null); }
public ActionResult AddOrEdit(HtmlComponentViewModel comp, HttpPostedFileBase styleSheetFile, HttpPostedFileBase scriptFile, HttpPostedFileBase imageFile, List <HttpPostedFileBase> mediaFiles) { try { using (var db = new GenerateHtmlDbContext()) { ViewBag.Category = db.HtmlComponentCategories.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }).ToList(); if (!ModelState.IsValid) { return(PartialView("~/Areas/Admin/Views/Home/_form.cshtml", comp)); } if (comp.Id == 0) { var obj = new HtmlComponent { Name = comp.Name, HtmlBody = comp.HtmlBody, ScriptPath = comp.ScriptPath, CssPath = comp.CssPath, ImageName = comp.ImageName, CategoryId = comp.CategoryId }; db.HtmlComponents.Add(obj); db.SaveChanges(); if (imageFile != null) { var imageFilePath = HandleUploadFile(obj.Id, imageFile, Utils.ImagePath); obj.ImageName = imageFilePath; } if (styleSheetFile != null && Path.GetExtension(styleSheetFile.FileName) == ".css") { var cssFilePath = HandleUploadFile(obj.Id, styleSheetFile, Utils.CssPath); obj.CssPath = cssFilePath; } if (scriptFile != null && Path.GetExtension(scriptFile.FileName) == ".js") { var scriptFilePath = HandleUploadFile(obj.Id, scriptFile, Utils.ScriptsPath); obj.ScriptPath = scriptFilePath; } if (mediaFiles.Any()) { HandleMediaImages(obj.Id, mediaFiles); } db.Entry(obj).State = EntityState.Modified; db.SaveChanges(); return(Json(new { success = true, message = "Lưu thành công." }, JsonRequestBehavior.AllowGet)); } else { var item = db.HtmlComponents.FirstOrDefault(x => x.Id == comp.Id); if (item != null) { item.Name = comp.Name; item.HtmlBody = comp.HtmlBody; item.CategoryId = comp.CategoryId; if (imageFile != null) { HandleDeleteFile(comp.ImageName, Utils.ImagePath); var imageFilePath = HandleUploadFile(item.Id, imageFile, Utils.ImagePath); item.ImageName = imageFilePath; } if (styleSheetFile != null && Path.GetExtension(styleSheetFile.FileName) == ".css") { HandleDeleteFile(comp.CssPath, Utils.CssPath); var cssFilePath = HandleUploadFile(item.Id, styleSheetFile, Utils.CssPath); item.CssPath = cssFilePath; } if (scriptFile != null && Path.GetExtension(scriptFile.FileName) == ".js") { HandleDeleteFile(comp.ScriptPath, Utils.ScriptsPath); var scriptFilePath = HandleUploadFile(item.Id, scriptFile, Utils.ScriptsPath); item.ScriptPath = scriptFilePath; } if (mediaFiles.Any()) { HandleMediaImages(item.Id, mediaFiles); } db.Entry(item).State = EntityState.Modified; db.SaveChanges(); return(Json(new { success = true, message = "Sửa thành công." }, JsonRequestBehavior.AllowGet)); } return(Json(new { success = false, message = "Sửa thất bại" }, JsonRequestBehavior.AllowGet)); } } } catch (Exception ex) { return(Json(new { success = false, message = "Sửa thất bại" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult AddOrEdit(PluginViewModel plugin, HttpPostedFileBase styleSheetFile, HttpPostedFileBase scriptFile) { if (!ModelState.IsValid) { return(PartialView("~/Areas/Admin/Views/Plugin/_form.cshtml", plugin)); } using (var db = new GenerateHtmlDbContext()) { if (plugin.Id == 0) { var item = db.Plugins.FirstOrDefault(x => x.Name.Trim().ToLower() == plugin.Name.Trim().ToLower()); if (item != null) { return(Json(new { success = false, message = "Plugin đã tồn tại." }, JsonRequestBehavior.AllowGet)); } var obj = new Plugin { Id = plugin.Id, Name = plugin.Name }; db.Plugins.Add(obj); db.SaveChanges(); if (styleSheetFile != null && Path.GetExtension(styleSheetFile.FileName) == ".css") { var cssFilePath = HandleUploadFile(obj.Id, styleSheetFile, obj.Name, Utils.CssPath); obj.CssPath = cssFilePath; } if (scriptFile != null && Path.GetExtension(scriptFile.FileName) == ".js") { var scriptFilePath = HandleUploadFile(obj.Id, scriptFile, obj.Name, Utils.ScriptsPath); obj.ScriptPath = scriptFilePath; } db.Entry(obj).State = EntityState.Modified; db.SaveChanges(); return(Json(new { success = true, message = "Lưu thành công." }, JsonRequestBehavior.AllowGet)); } else { var item = db.Plugins.FirstOrDefault(x => x.Id == plugin.Id); if (item != null) { var data = db.Plugins.FirstOrDefault(x => x.Name.Trim().ToLower() == plugin.Name.Trim().ToLower() && x.Id != plugin.Id); if (data != null) { return(Json(new { success = false, message = "Plugin đã tồn tại." }, JsonRequestBehavior.AllowGet)); } item.Name = plugin.Name; if (styleSheetFile != null && Path.GetExtension(styleSheetFile.FileName) == ".css") { HandleDeleteFile(plugin.CssPath, Utils.CssPath, plugin.Name); var cssFilePath = HandleUploadFile(item.Id, styleSheetFile, item.Name, Utils.CssPath); item.CssPath = cssFilePath; } if (scriptFile != null && Path.GetExtension(scriptFile.FileName) == ".js") { HandleDeleteFile(plugin.ScriptPath, Utils.ScriptsPath, plugin.Name); var scriptFilePath = HandleUploadFile(item.Id, scriptFile, item.Name, Utils.ScriptsPath); item.ScriptPath = scriptFilePath; } db.Entry(item).State = EntityState.Modified; db.SaveChanges(); return(Json(new { success = true, message = "Sửa thành công." }, JsonRequestBehavior.AllowGet)); } return(Json(new { success = false, message = "Lỗi." }, JsonRequestBehavior.AllowGet)); } } }