/// <summary> /// Adds a new comment to the article specifed with given id /// </summary> /// <param name="id">comment id</param> /// <param name="form">form containing data</param> /// <param name="ip">Client IP address</param> /// <returns></returns> public bool add(long id, Form_Comment_New form, string ip) { comment c = new comment(); c.useralias = form["name"].getValue(); c.title = form["title"].getValue(); c.text = form["text"].getValue(); c.parentid = null; c.ip = ip; c.email = form["email"].getValue(); c.articlesid = id; if (this._app.users().isLogged()) { c.usersid = this._app.users().getLogged().id; } try { using (ArticleDataContext a = new ArticleDataContext()) { a.comments.InsertOnSubmit(c); a.SubmitChanges(); } } catch { return false; } return true; }
/// <summary> /// Adds a new child for the given parent category /// </summary> /// <param name="parent">parent category (0 for root)</param> /// <param name="form">form containig data</param> /// <returns></returns> public bool add(long parent, Form_Category_Add form) { category c = new category(); c.date = DateTime.Now; c.parentid = parent; if (parent == 0) { c.parentid = null; } c.name = form["name"].getValue(); c.alias = this._app.makeAlias(form["name"].getValue()); using (ArticleDataContext a = new ArticleDataContext()) { a.categories.InsertOnSubmit(c); try { a.SubmitChanges(); } catch (Exception) { return false; } } return true; }
/// <summary> /// /// </summary> /// <param name="a"></param> /// <returns></returns> public Array getAll(ArticleDataContext a) { using (a) { var data = a.articles; return data.ToArray(); } }
/// <summary> /// Returns all authors /// </summary> /// <returns></returns> public List<author> getAll() { using (ArticleDataContext a = new ArticleDataContext()) { var data = a.authors.ToList(); return data; } }
/// <summary> /// /// </summary> /// <param name="id"></param> public void deleteArticle(long id) { using (ArticleDataContext a = new ArticleDataContext()) { a.articles.DeleteAllOnSubmit(a.articles.Where(x => x.id == id)); try { a.SubmitChanges(); } catch(Exception) { CMS.Services.CMS_Services_Message.getInstance().addError("The article hasn't been deleted."); } } }
/// <summary> /// Deletes a category specified by the given id /// </summary> /// <param name="id">category id</param> /// <returns>success</returns> public bool delete(long id) { using (ArticleDataContext u = new ArticleDataContext()) { try { u.categories.DeleteAllOnSubmit(u.categories.Where(x => x.id == id)); u.SubmitChanges(); } catch { return false; } return true; } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="count"></param> /// <param name="start"></param> /// <returns></returns> public List<article> getByCategoryId(long id, int count, int start) { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles_categories .Where(x => x.categoriesid == id) .Select(x => x.article) .Skip(start) .Take(count).ToList(); } }
/// <summary> /// Returns a list of categories /// </summary> /// <param name="id">Parent category id (0 for root)</param> /// <param name="a">Data context</param> /// <param name="start">How many categories should be skipped</param> /// <param name="count">Amount of returned categories</param> /// <returns>List of categories</returns> public List<category> get(long id, ArticleDataContext a, int start, int count) { using (a) { if (id > 0) { var data = a.categories.Where(x => x.parentid == id).Skip(start).Take(count); return data.ToList(); } else if (id == 0) { var data = a.categories.Where(x => x.parentid == null).Skip(start).Take(count); return data.ToList(); } else { var data = a.categories.Skip(start).Take(count); return data.ToList(); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="a"></param> /// <returns></returns> public article getById(long id, ArticleDataContext a) { using (a) { try { var data = a.articles .Single(x => x.id == id); data.hits++; try { a.SubmitChanges(); } catch (Exception) { } return data; } catch (Exception) { return null; } } }
/// <summary> /// Removes the comment with the given id from data source /// </summary> /// <param name="id"></param> public void deleteById(long id) { using (ArticleDataContext a = new ArticleDataContext()) { try { a.comments.DeleteOnSubmit(a.comments.Where(x => x.id == id).Single()); } catch (Exception) { CMS.Services.CMS_Services_Message.getInstance().addError("The comment cannot be deleted."); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public int getCountByAuthorId(long id) { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles_authors .Where(x => x.authorsid == id) .Select(x => x.article) .Count(); } }
/// <summary> /// Saves a reply to a comment with the given id /// </summary> /// <param name="id">Comment id</param> /// <param name="form">Reply data</param> /// <param name="aid">Article id</param> /// <param name="ip">Client IP address</param> /// <returns></returns> public bool reply(long id, Form_Comment_New form, string ip, out long aid) { comment c = new comment(); c.useralias = form["name"].getValue(); c.title = form["title"].getValue(); c.text = form["text"].getValue(); c.parentid = id; c.ip = ip; c.email = form["email"].getValue(); if (this._app.users().isLogged()) { c.usersid = this._app.users().getLogged().id; } try { using (ArticleDataContext a = new ArticleDataContext()) { comment replied = a.comments.Where(x => x.id == id).Single(); aid = c.articlesid = replied.articlesid; a.comments.InsertOnSubmit(c); a.SubmitChanges(); } } catch { aid = 0; return false; } return true; }
/// <summary> /// /// </summary> /// <param name="p"></param> /// <returns></returns> public string getAuthorsStringById(long p) { using (ArticleDataContext a = new ArticleDataContext()) { StringBuilder authorStr = new StringBuilder(); bool first = true; foreach (author authorItem in a.articles_authors.Where(x => x.articlesid == p).Select(x => x.author).ToList()) { if (!first) authorStr.Append(", "); authorStr.Append(authorItem.lastname + " " + authorItem.name); first = false; } return authorStr.ToString(); } }
/// <summary> /// Get count of children for the given category (0 for root) /// </summary> /// <returns>count of children</returns> public int getCount(long parent) { long? p = parent; if (p == 0) p = null; using (ArticleDataContext a = new ArticleDataContext()) { return a.categories.Where(x=>x.parentid == p).Count(); } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public List<tag> getTagsById(long id) { using (ArticleDataContext a = new ArticleDataContext()) { return a.tags_articles.Where(x => x.articlesid == id).Select(x => x.tag).ToList(); } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public List<comment> getCommentsById(long id) { using (ArticleDataContext a = new ArticleDataContext()) { return a.comments.Where(x => x.articlesid == id).ToList(); } }
/// <summary> /// /// </summary> /// <returns></returns> public int getFrontpageArticlesCount() { List<long> levels = this._app.allowedLevels(); using (ArticleDataContext a = new ArticleDataContext()) { var data = a.articles .Where(x => x.published == 1 && levels.Contains(x.level) && x.date_published <= DateTime.Now && (x.date_pullback <= x.date_published || x.date_pullback > DateTime.Now)) .Count(); return data; } }
/// <summary> /// /// </summary> /// <param name="count"></param> /// <returns></returns> public List<article> getFrontpage(int count, int start, string order) { List<long> allowedLevels = this._app.allowedLevels(); using (ArticleDataContext a = new ArticleDataContext()) { var data = a.articles .Where(x => x.published == 1 && allowedLevels.Contains(x.level) && x.date_published <= DateTime.Now && (x.date_pullback <= x.date_published || x.date_pullback > DateTime.Now)); if (order == "date") { return data.OrderByDescending(x => x.date_published) .Skip(start) .Take(count).ToList(); }else if (order == "id") { return data.OrderBy(x => x.id) .Skip(start) .Take(count).ToList(); } else if (order == "hits") { return data.OrderByDescending(x => x.hits) .Skip(start) .Take(count).ToList(); } else if (order == "title") { return data.OrderBy(x => x.title) .Skip(start) .Take(count).ToList(); }else{ return data.Skip(start).Take(count).ToList(); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public int getCountByCategoryId(long id) { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles_categories .Where(x => x.categoriesid == id) .Select(x => x.article) .Count(); } }
/// <summary> /// Gets all categories /// </summary> /// <returns></returns> public List<category> getAll() { using (ArticleDataContext a = new ArticleDataContext()) { return a.categories.ToList(); } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public List<category> getCategoriesListById(long id) { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles_categories.Where(x => x.articlesid == id).Select(x => x.category).ToList(); } }
/// <summary> /// Returns category identified by the specified ID /// </summary> /// <param name="id">User id</param> /// <returns>Category represented by the specified id</returns> public category getById(long id) { using (ArticleDataContext a = new ArticleDataContext()) { try { var data = a.categories .Where(x => x.id == id) .Single(); return data; } catch (Exception) { return null; } } }
/// <summary> /// /// </summary> /// <returns></returns> public List<long> getAllLevels() { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles.Select(x => x.level).Distinct().ToList(); } }
/// <summary> /// Save changes for the given category /// </summary> /// <param name="form">form containing data</param> /// <param name="c">category</param> /// <returns>success</returns> public bool save(Form_Category_Add form, category c) { category newCat = new category(); newCat.id = c.id; if (c.parentid.HasValue) { newCat.parentid = c.parentid; } newCat.name = form["name"].getValue(); newCat.date = c.date; using (ArticleDataContext a = new ArticleDataContext()) { a.categories.Attach(newCat, c); try { a.SubmitChanges(); } catch (Exception) { return false; } } return true; }
/// <summary> /// /// </summary> /// <returns></returns> public int getCount() { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles.Count(); } }
/// <summary> /// /// </summary> /// <param name="form"></param> /// <param name="Request"></param> /// <returns></returns> public bool saveArticle(CMS_Form form, HttpRequestBase Request) { long id = long.Parse(Request.Params["id"]); using (ArticleDataContext dataContext = new ArticleDataContext()) { DateTime published = this._app.dateFromString(form["published"].getValue()); DateTime pullback = this._app.dateFromString(form["pullback"].getValue()); try { article original = dataContext.articles.Where(x => x.id == id).Single(); article modified = dataContext.articles.Where(x => x.id == id).Single(); modified.date_published = published; modified.date_lastmod = DateTime.Now; modified.alias = this._app.makeAlias(form["title"].getValue()); modified.date_pullback = pullback; modified.fulltext = form["text"].getValue(); modified.modifications_count++; modified.title = form["title"].getValue(); modified.introtext = form["perex"].getValue(); modified.level = int.Parse(form["roles"].getValue()); modified.published = int.Parse(form["published_bool"].getValue()); dataContext.articles_authors.DeleteAllOnSubmit(dataContext.articles_authors.Where(x => x.articlesid == id)); try { dataContext.SubmitChanges(); } catch (Exception) { return false; } foreach (string articleAuthorIdString in ((CMS_Form_Element_Select)form["authors"]).getValues()) { articles_author authorArticles = new articles_author(); authorArticles.articlesid = id; authorArticles.authorsid = long.Parse(articleAuthorIdString); authorArticles.date = DateTime.Now; modified.articles_authors.Add(authorArticles); } string[] tags = form["tags"].getValue().Split(' '); dataContext.tags_articles.DeleteAllOnSubmit(dataContext.tags_articles.Where(x => x.articlesid == id)); try { dataContext.SubmitChanges(); } catch (Exception) { return false; } foreach (string t in tags) { long tagId = 0; try { var tag = dataContext.tags.Where(x => x.name == t).Single(); tagId = tag.id; } catch (InvalidOperationException) { tag newTag = new tag(); newTag.name = t; newTag.date = DateTime.Now; dataContext.tags.InsertOnSubmit(newTag); try { dataContext.SubmitChanges(); } catch { return false; } tagId = newTag.id; } tags_article tagArticle = new tags_article(); tagArticle.articlesid = id; tagArticle.tagsid = tagId; dataContext.tags_articles.InsertOnSubmit(tagArticle); } dataContext.articles_categories.DeleteAllOnSubmit(dataContext.articles_categories.Where(x => x.articlesid == id)); try { dataContext.SubmitChanges(); } catch (Exception) { return false; } CMS_Form_Element_Select cats = (CMS_Form_Element_Select)form["categories"]; foreach (string cat in cats.getValues()) { long catId = long.Parse(cat); articles_category articleCategory = new articles_category(); articleCategory.date = DateTime.Now; articleCategory.articlesid = id; articleCategory.categoriesid = catId; dataContext.articles_categories.InsertOnSubmit(articleCategory); } try { dataContext.SubmitChanges(); } catch (Exception) { return false; } if (Request.Files.Count != 2) { CMS_Services_Message.getInstance().addError("Unexpected count of uploaded files, skipping."); } else { HttpPostedFileBase small = Request.Files[0]; if (small.ContentLength > 0 && small.ContentType == "image/jpeg") { string filename = id.ToString(); var path = Path.Combine(Request.MapPath("./../images"), filename + "_small.jpg"); small.SaveAs(path); System.Drawing.Image i = System.Drawing.Image.FromFile(path); if (i.Width != 100 || i.Height != 100) { CMS_Services_Message.getInstance().addError("Invalid image size - small icon should be 100x100 pixels"); FileInfo f = new FileInfo(path); f.Delete(); } } else { CMS_Services_Message.getInstance().addError("Invalid image - small icon"); } HttpPostedFileBase big = Request.Files[1]; if (big.ContentLength > 0 && big.ContentType == "image/jpeg") { string filename = id.ToString(); var path = Path.Combine(Request.MapPath("./../images"), filename + "_big.jpg"); big.SaveAs(path); System.Drawing.Image i = System.Drawing.Image.FromFile(path); if (i.Width != 320 || i.Height != 240) { CMS_Services_Message.getInstance().addError("Invalid image size - big icon should be 320x240 pixels"); FileInfo f = new FileInfo(path); f.Delete(); } } else { CMS_Services_Message.getInstance().addError("Invalid image - big icon"); } } } catch(InvalidOperationException) { CMS_Services_Message.getInstance().addError("Article with specified ID does not exit"); return false; } } return true; }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public List<author> getAuthorsListById(long id) { using (ArticleDataContext a = new ArticleDataContext()) { return a.articles_authors.Where(x => x.articlesid == id).Select(x => x.author).ToList(); } }