Beispiel #1
0
        private bool CreateEdit(Article article, int[] selectedProglangs, bool isNew = true)
        {
            bool error = false;

            List<string> errImgTags = GetErrImgTags(article);

            if (ModelState.IsValid && selectedProglangs != null && article.Asubject1 != 0 && errImgTags == null)
            {
                Article newArticle;
                int tempId = article.Id;

                if (isNew)
                {
                    newArticle = new Article();
                }
                else
                {
                    newArticle = db.Articles.Find(article.Id);
                    newArticle.Proglangs.Clear();
                }

                newArticle.Title = article.Title;
                newArticle.Articletext = article.Articletext;
                newArticle.Asubject1 = article.Asubject1;
                newArticle.Userid = article.Userid;

                foreach (var lang in db.Proglangs.Where(l => selectedProglangs.Contains(l.Id)))
                {
                    newArticle.Proglangs.Add(lang);
                }

                if (isNew)
                {
                    newArticle = db.Articles.Add(newArticle);
                    ArticleRate articlerate = new ArticleRate()
                    {
                        Article = newArticle,
                        Plus = 0,
                        Minus = 0
                    };
                    db.ArticleRates.Add(articlerate);
                }
                else
                {
                    db.Entry(newArticle).State = EntityState.Modified;
                }

                db.SaveChanges();

                string imgResult = SaveImages(newArticle, tempId);

                if (imgResult.CompareTo("true") != 0)
                {
                    ModelState.AddModelError("Images", "Произошел сбой при сохранении изображений. Попробуйте еще раз. " + imgResult);
                    error = true;
                }

                removeArticleCookies(tempId);

                if (!error)
                {
                    return true;
                    //return RedirectToAction("Index");
                }
            }
            else
            {
                error = true;

                if (selectedProglangs == null)
                {
                    ModelState.AddModelError("Proglangs", "Укажите языки программирования");
                }
                else
                {
                    foreach (var lang in db.Proglangs.Where(l => selectedProglangs.Contains(l.Id)))
                    {
                        article.Proglangs.Add(lang);
                    }
                }

                if (article.Asubject1 == 0)
                {
                    ModelState.AddModelError("Asubject1", "Укажите тему статьи");
                }

                if (errImgTags != null)
                {
                    ModelState.AddModelError("Articletext", "В статье находятся ошибочно вставленные теги изображения: " + String.Join(", ", errImgTags));
                }
            }

            if (error)
            {
                ArticleAdditionData articleAdditionData = new ArticleAdditionData();

                //GetCurrentArticleData(article, articleAdditionData);

                ViewBag.Asubject1 = articleAdditionData.Asubjects;

                ViewBag.articleAdditionData = articleAdditionData;

                //article.Articletext = EncodeString(article.Articletext);
                //article.Asubject1 = int.Parse(articleAdditionData.Asubjects.Where(i => i.Selected == true).First().Value);
                //article_Asubject1
            }

            return false;
        }
Beispiel #2
0
        private void GetCurrentArticleData(Article article, ArticleAdditionData articleAdditionData)
        {
            HttpCookie titleCookie = HttpContext.Request.Cookies.Get("Title" + article.Id);
            HttpCookie articletextCookie = HttpContext.Request.Cookies.Get("Articletext" + article.Id);
            HttpCookie asubject1Cookie = HttpContext.Request.Cookies.Get("Asubject1" + article.Id);
            HttpCookie proglangsCookie = HttpContext.Request.Cookies.Get("selectedProglangs" + article.Id);

            if (titleCookie != null) article.Title = titleCookie.Value.Replace("(&lt)", "<").Replace("(&gt)", ">");
            if (articletextCookie != null) article.Articletext = articletextCookie.Value.Replace("(&lt)", "<").Replace("(&gt)", ">");

            if (asubject1Cookie != null)
            {
                int subjectId;
                if (int.TryParse(asubject1Cookie.Value, out subjectId))
                {
                    article.Asubject1 = subjectId;
                }
                else
                {
                    article.Asubject1 = article.Asubject1;
                }
                articleAdditionData.Asubjects = CreateSelectList("Выберите тему", "0", article.Asubject1.ToString());
            }
            else
            {
                articleAdditionData.Asubjects = CreateSelectList("Выберите тему", "0", article.Asubject1.ToString());
            }

            if (proglangsCookie != null && !String.IsNullOrWhiteSpace(proglangsCookie.Value))
            {
                List<string> proglangs_ = proglangsCookie.Value.Split(',').ToList();
                List<int> proglangs = proglangs_.Select(s => int.Parse(s)).ToList();
                article.Proglangs = db.Proglangs.Where(e => proglangs.Contains(e.Id)).ToList();
            }

            articleAdditionData.Proglangs = db.Proglangs.ToList();

            string userId = User.Identity.GetUserId();

            articleAdditionData.DraftArticleImags = db.DraftArticleImages.Where(e => e.Userid == userId)
                .Where(e => e.Article_Id == article.Id).ToList();
        }
Beispiel #3
0
        private Article CreateEdit(int? id, bool isNew)
        {
            Article article;

            if (isNew)
            {
                article = new Article()
                {
                    Id = (int)id
                };
            }
            else
            {
                DraftArticle draftArticleIsExists = db.DraftArticles.Where(i => i.Article_Id == id).FirstOrDefault();

                if (draftArticleIsExists == null)
                {
                    DraftArticle draftArticle = new DraftArticle()
                    {
                        Article_Id = (int)id
                    };
                    draftArticle = db.DraftArticles.Add(draftArticle);

                    db.SaveChanges();

                    ViewBag.IsDraftExists = false;
                }
                else
                {
                    ViewBag.IsDraftExists = true;
                }

                article = db.Articles.Find(id);

                if (article != null)
                {
                    StringBuilder sb = new StringBuilder(HttpUtility.HtmlDecode(article.Articletext));
                    article.Articletext = sb.ToString();
                }
            }

            ArticleAdditionData articleAdditionData = new ArticleAdditionData();
            GetCurrentArticleData(article, articleAdditionData);
            ViewBag.Asubject1 = articleAdditionData.Asubjects;
            ViewBag.articleAdditionData = articleAdditionData;

            return article;
        }