示例#1
0
 partial void Deletetag(tag instance);
示例#2
0
 partial void Inserttag(tag instance);
示例#3
0
 partial void Updatetag(tag instance);
示例#4
0
        /// <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 (LangDataContext dataContext = new LangDataContext())
            {

                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;
        }