public ActionResult ChooseDownloadBook() { ComfortModel cdb = new ComfortModel(); var bookList = cdb.Books.Where(x => x.Published).OrderByDescending(x => x.ModifyDate).ToList(); return(View(bookList)); }
public AssetModel(int pageId) { ComfortModel cdb = new ComfortModel(); this.FileList = cdb.Files.Where(x => x.PageId == pageId).ToList(); this.PageId = pageId; }
public ActionResult UploadAsset(AssetModel model) { ComfortModel cdb = new ComfortModel(); // if file is there... File f = new File(); // Convert the httppostedfilebase to a byte[] MemoryStream target = new MemoryStream(); model.UploadedFile.InputStream.CopyTo(target); f.Content = target.ToArray(); f.ContentType = model.UploadedFile.ContentType; f.FileName = model.UploadedFile.FileName; // check if comatable/etc // use ContentType ("image/jpeg" etc) if (f.ContentType.Contains("image")) { f.FileType = FileType.Photo; } else if (f.ContentType.Contains("video")) { f.FileType = FileType.Video; } f.PageId = model.PageId; cdb.Files.Add(f); cdb.SaveChanges(); return(RedirectToAction("ViewAssets", new { id = model.PageId })); }
public ActionResult ListBooks() { // TODO: remove this AddTitlesToPages(); ComfortModel cdb = new ComfortModel(); return(View(cdb.Books.ToList())); }
public void AddTitlesToPages() { ComfortModel cdb = new ComfortModel(); foreach (var page in cdb.Pages.ToList()) { if (string.IsNullOrEmpty(page.Title) || page.Title == "") { page.Title = "New Page"; } } cdb.SaveChanges(); }
public ActionResult DeleteFile(int id) { int pageId = 0; ComfortModel cdb = new ComfortModel(); var file = cdb.Files.Where(x => x.FileId == id).FirstOrDefault(); pageId = file.PageId; cdb.Files.Remove(file); cdb.SaveChanges(); return(RedirectToAction("ViewAssets", new { id = pageId })); }
public ActionResult TempDbRewrite() { // find all pages and change "position-x" to "left" and "position-y" to "top" ComfortModel cdb = new ComfortModel(); foreach (var page in cdb.Pages.ToList()) { string content = page.PageContent.Replace("position-x", "left"); content = content.Replace("position-y", "top"); page.PageContent = content; } cdb.SaveChanges(); return(Content("success")); }
public PageListModel(int bookId) { this.PageList = new List <Page>(); this.BookPages = new List <BookPage>(); //List<Page> pageList = new List<Page>(); ComfortModel cdb = new ComfortModel(); if (bookId == 0) { // must find all pages not associated with a book // get all pages var pages = cdb.Pages.Select(x => x.PageId).ToList(); // get all BookPages that contain these pages var usedBookPages = cdb.BookPages.Where(x => pages.Contains(x.PageId)).Select(x => x.PageId).ToList(); // find all pages that are not in the previous list this.PageList = cdb.Pages.Where(x => !usedBookPages.Contains(x.PageId)).ToList(); this.Modules = new List <Module>(); this.Sections = new List <Section>(); this.Chapters = new List <Chapter>(); this.GetBook = new Book(); } else { this.BookPages = cdb.BookPages.Where(x => x.BookId == bookId).OrderBy(x => x.SortOrder).ToList(); List <int> bpId = this.BookPages.Select(x => x.PageId).ToList(); this.PageList = cdb.Pages.Where(x => bpId.Contains(x.PageId)).ToList(); this.GetBook = cdb.Books.Where(x => x.BookId == bookId).FirstOrDefault(); this.Modules = cdb.Modules.Where(x => x.BookId == bookId).ToList(); var moduleIdList = this.Modules.Select(x => x.ModuleId).ToList(); this.Sections = cdb.Sections.Where(x => moduleIdList.Contains(x.ModuleId)).ToList(); var sectionIdList = this.Sections.Select(x => x.SectionId).ToList(); this.Chapters = cdb.Chapters.Where(x => sectionIdList.Contains(x.SectionId)).ToList(); } }
// when selecting a page for editing, you'll go here. // grab the info from the db and create a PageViewModel // send that to Index to load the page. public ActionResult ViewPage(int id) { PageViewModel model = new PageViewModel(); ComfortModel cdb = new ComfortModel(); if (id == 0) { model.PageId = 0; model.PageName = "New Page"; model.XmlContent = "<page type=\"content\"></page>"; } else { var page = cdb.Pages.Where(x => x.PageId == id).FirstOrDefault(); model.PageId = id; model.XmlContent = page.PageContent; model.PageName = page.Title; } return(View("Index", model)); }
public ActionResult ViewEditor(int id) { ComfortModel cdb = new ComfortModel(); PageViewModel model = new PageViewModel { PageId = id }; var page = cdb.Pages.Where(x => x.PageId == id).FirstOrDefault(); if (page != null) { model.PageName = page.Title; string oldContent = page.PageContent; model.XmlContent = oldContent.Replace("href=\"javascript:void(0)\"", "href=\"#\""); } else { model.PageName = "New Page"; model.XmlContent = "<page type=\"content\"></page>"; } return(View(model)); }
public ActionResult SavePage(string xml, List <string> images, string email, int pageId, string name) { ComfortModel cdb = new ComfortModel(); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(xml); } catch (Exception e) { return(Content("bad xml " + e.InnerException.Message)); } var page = cdb.Pages.Where(x => x.PageId == pageId).FirstOrDefault(); if (page == null) { page = new Page(email); cdb.Pages.Add(page); } else { page.Modify(email); } page.Title = name; // TODO: this page.Type = "content"; page.PageContent = ""; cdb.SaveChanges(); // these are page elements foreach (XmlElement node in doc.ChildNodes) { // replace page id string newPageId = "p_" + page.PageId.ToString(); node.SetAttribute("id", newPageId); // process the image nodes further // update the page ids so they're all associated with that page foreach (XmlElement child in node.ChildNodes) { if (child.Name.ToLower() == "image") { string fileId = child.Attributes["source"].Value; try { int id = Convert.ToInt32(fileId.Split('_')[1]); var dbImage = cdb.Files.Where(x => x.FileId == id).FirstOrDefault(); if (dbImage != null) { dbImage.PageId = page.PageId; } } catch { } } if (child.Name.ToLower() == "text") { foreach (var el in child.ChildNodes) { try { XmlElement e = (XmlElement)el; if (e.Name.ToLower() == "a") { e.SetAttribute("href", "javascript:void(0)"); } } catch { } } } } } page.PageContent = doc.OuterXml; cdb.SaveChanges(); return(Content("success")); }
public ActionResult DownloadBook(int id) { // delete all the old stuff DeleteOldFiles(); // grab all the stuff ComfortModel cdb = new ComfortModel(); ViewController.BookModel model = new ViewController.BookModel(id); var theBook = cdb.Books.Where(x => x.BookId == id).FirstOrDefault(); // TODO validate // loop through model.PageContent, and separate out all the XML pieces List <string> contentLs = new List <string>(); foreach (var item in model.PageContent) { contentLs.Add(item.content); item.content = ""; } // change the offline load to read two different files string dt = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"); // make the name of the book safe for filenames string bookName = theBook.Name; foreach (var c in Path.GetInvalidFileNameChars()) { bookName = bookName.Replace(c, '-'); } string fullFileName = Server.MapPath("~/ZipDump/" + bookName + "_" + dt + ".zip"); string fileName = bookName + "_" + dt + ".zip"; string configFile1 = Server.MapPath("~/ZipDump/1config_" + dt + ".js"); string configFile2 = Server.MapPath("~/ZipDump/2config_" + dt + ".js"); string homeFile = Server.MapPath("~/ZipDump/index.html"); using (ZipFile zip = new ZipFile()) { zip.AddDirectory(Server.MapPath("~/Content/"), "/Content/"); zip.AddDirectory(Server.MapPath("~/fonts/"), "/fonts/"); // Add Index.html // I need to strip down this file, and include the variables "config1.js" and config2.js" using (var tw = new StreamWriter(homeFile, true)) { string text = System.IO.File.ReadAllText(Server.MapPath("~/Views/View/Index.cshtml")); text = text.Replace("~/", "./"); string scriptText = "<script src=\"./Content/js/offline.js\"></script>"; scriptText += "<script src=\"./Content/js/config1.js\"></script>"; scriptText += "<script src=\"./Content/js/config2.js\"></script>"; text = text.Replace("<script src=\"./Content/js/offline.js\"></script>", scriptText); tw.Write(text); zip.AddFile(homeFile).FileName = "index.html"; } using (var tw = new StreamWriter(configFile1, true)) { // create the config file tw.Write("var offline_ConfigXml = $.parseXML(`"); tw.Write(model.ConfigXml.OuterXml); tw.Write("`);"); zip.AddFile(configFile1).FileName = "/Content/js/config1.js"; } using (var tw = new StreamWriter(configFile2, true)) { // create the config file tw.Write("var offline_PageContents = `"); var jsonSerialiser = new JavaScriptSerializer(); var json = jsonSerialiser.Serialize(model.PageContent); tw.Write(json); tw.Write("`;"); tw.WriteLine(Environment.NewLine); tw.WriteLine("var offline_PageGuts = [];"); tw.WriteLine(Environment.NewLine); tw.WriteLine(Environment.NewLine); tw.WriteLine("function loadGuts() {"); foreach (var page in contentLs) { tw.WriteLine("offline_PageGuts.push(`" + page + "`);"); } tw.WriteLine("}"); zip.AddFile(configFile2).FileName = "/Content/js/config2.js"; } // map the images too // first get all the pages in the book var bookPages = cdb.BookPages.Where(x => x.BookId == theBook.BookId).Select(x => x.PageId).ToList(); var pages = cdb.Pages.Where(x => bookPages.Contains(x.PageId)).ToList(); foreach (var p in pages) { // find all associated files with that page var files = cdb.Files.Where(x => x.PageId == p.PageId).ToList(); int count = 1; foreach (var f in files) { if (f.Content != null) { string fName = Server.MapPath("~/ZipDump/f_" + count.ToString() + "_" + dt); string newFileName = "/Content/"; switch (f.FileType) { case FileType.Photo: switch (f.ContentType) { case "image/jpg": case "image/jpeg": fName += ".jpg"; newFileName += "images/i_" + f.FileId.ToString() + ".jpg"; break; case "image/png": fName += ".png"; newFileName += "images/i_" + f.FileId.ToString() + ".png"; break; } break; case FileType.Video: break; } // write the file to the stream using (var tw = new StreamWriter(fName, true)) { tw.BaseStream.Write(f.Content, 0, f.Content.Length); zip.AddFile(fName).FileName = newFileName; } } } } zip.Save(fullFileName); return(File(fullFileName, "application/zip", fileName)); } }
// initialize them all first so I can keep track of IDs as it recurses static void ReadElement(XmlElement element, int bookId, int moduleId, int sectionId, int chapterId, int pageId, int m_sortCount, int s_sortCount, int c_sortCount, int p_sortCount) { ComfortModel cdb = new ComfortModel(); switch (element.Name) { case "book": // Create book Book book = new Book(); book.Create("*****@*****.**"); book.Name = element.Attributes["name"].Value; book.Published = true; book.Version = element.Attributes["version"].Value; cdb.Books.Add(book); cdb.SaveChanges(); bookId = book.BookId; m_sortCount = 10; break; case "module": Module module = new Module(); module.BookId = bookId; module.Name = element.Attributes["name"].Value; module.SortOrder = m_sortCount; m_sortCount += 10; module.Theme = element.Attributes["theme"].Value; cdb.Modules.Add(module); cdb.SaveChanges(); moduleId = module.ModuleId; s_sortCount = 10; break; case "section": Section section = new Section(); section.ModuleId = moduleId; section.Name = element.Attributes["name"].Value; section.SortOrder = s_sortCount; s_sortCount += 10; cdb.Sections.Add(section); cdb.SaveChanges(); sectionId = section.SectionId; c_sortCount = 10; break; case "chapter": Chapter chapter = new Chapter(); chapter.Name = element.Attributes["name"].Value; chapter.SectionId = sectionId; chapter.SortOrder = c_sortCount; c_sortCount += 10; cdb.Chapters.Add(chapter); cdb.SaveChanges(); chapterId = chapter.ChapterId; p_sortCount = 10; break; case "page": Page page = new Page(); page.Create("*****@*****.**"); page.PageContent = element.OuterXml.Trim(); page.Type = element.Attributes["type"].Value; cdb.Pages.Add(page); cdb.SaveChanges(); BookPage bp = new BookPage(); bp.BookId = bookId; bp.ChapterId = chapterId; bp.PageId = page.PageId; bp.SortOrder = p_sortCount; cdb.BookPages.Add(bp); cdb.SaveChanges(); pageId = bp.BookPageId; break; default: break; } if (element.HasChildNodes) { foreach (XmlNode child in element.ChildNodes) { if (child is XmlElement) { ReadElement(child as XmlElement, bookId, moduleId, sectionId, chapterId, pageId, m_sortCount, s_sortCount, c_sortCount, p_sortCount); } } } }
public BookModel(int id) { // Load the book. // I'll be passing 2 variables to the page: // 1. ConfigXml - outlines of the chapters, content, etc. // does not include *page* content // 2. PageContent - contains strictly the pages and what's on them (text, etc.) this.ConfigXml = new XmlDocument(); this.PageContent = new List <PageContentItem>(); ComfortModel cdb = new ComfortModel(); var book = cdb.Books.Where(x => x.BookId == id).FirstOrDefault(); var modules = cdb.Modules.Where(x => x.BookId == book.BookId).ToList(); var moduleIds = modules.Select(x => x.ModuleId).ToList(); var sections = cdb.Sections.Where(x => moduleIds.Contains(x.ModuleId)).ToList(); var sectionIds = sections.Select(x => x.SectionId).ToList(); var chapters = cdb.Chapters.Where(x => sectionIds.Contains(x.SectionId)).ToList(); var chapterIds = chapters.Select(x => x.ChapterId).ToList(); var bookPages = cdb.BookPages.Where(x => chapterIds.Contains(x.ChapterId)).ToList(); var bookPageIds = bookPages.Select(x => x.PageId).ToList(); var pages = cdb.Pages.Where(x => bookPageIds.Contains(x.PageId)).ToList(); // configXml.DocumentElement --> book XmlElement root = this.ConfigXml.CreateElement("book"); root.SetAttribute("id", "b_" + book.BookId); root.SetAttribute("name", book.Name); root.SetAttribute("version", book.Version); root.SetAttribute("author", book.CreatedBy); root.SetAttribute("createdate", book.CreateDate.ToString("YYYYMMdd")); root.SetAttribute("modifydate", book.ModifyDate.ToString("YYYYMMdd")); this.ConfigXml.AppendChild(root); int moduleCount = 1; int sectionCount = 1; int chapterCount = 1; foreach (var m in modules.OrderBy(x => x.SortOrder).ToList()) { XmlElement mod = this.ConfigXml.CreateElement("module"); mod.SetAttribute("name", m.Name); mod.SetAttribute("theme", m.Theme); mod.SetAttribute("id", "m_" + moduleCount); root.AppendChild(mod); foreach (var s in sections.Where(x => x.ModuleId == m.ModuleId).OrderBy(x => x.SortOrder).ToList()) { XmlElement sec = this.ConfigXml.CreateElement("section"); sec.SetAttribute("name", s.Name); sec.SetAttribute("id", "s_" + sectionCount); mod.AppendChild(sec); foreach (var c in chapters.Where(x => x.SectionId == s.SectionId).OrderBy(x => x.SortOrder).ToList()) { XmlElement cha = this.ConfigXml.CreateElement("chapter"); cha.SetAttribute("name", c.Name); cha.SetAttribute("id", "c_" + chapterCount); sec.AppendChild(cha); foreach (var p in bookPages.Where(x => x.ChapterId == c.ChapterId).OrderBy(x => x.SortOrder).ToList()) { var foundPage = pages.Where(x => x.PageId == p.PageId).FirstOrDefault(); XmlElement bpa = this.ConfigXml.CreateElement("page"); bpa.SetAttribute("id", "p_" + foundPage.PageId); bpa.SetAttribute("type", foundPage.Type); cha.AppendChild(bpa); XmlDocument xml_page = new XmlDocument(); try { PageContentItem newPage = new PageContentItem(); newPage.Chapter = "c_" + chapterCount; newPage.content = foundPage.PageContent; newPage.Module = "m_" + moduleCount; newPage.Page = "p_" + foundPage.PageId; newPage.Section = "s_" + sectionCount; newPage.Book = "b_" + book.BookId; this.PageContent.Add(newPage); } catch { } } chapterCount++; } sectionCount++; } moduleCount++; } }