예제 #1
0
 public void saveArticleTags(string inputTags, int articleId)
 {
     string[] tagArray = inputTags.Split(',');
     Tag[]    tagList  = smokeFreeDB.Tag.ToArray();
     if (!String.IsNullOrEmpty(inputTags))
     {
         for (int i = 0; i < tagArray.Length; i++)
         {
             string      tmp        = tagArray[i];
             Tag         tag        = smokeFreeDB.Tag.Where(x => x.tagName == tmp).FirstOrDefault();
             ArticlesTag articleTag = new ArticlesTag();
             if (tag != null)
             {
                 // Tag is found
                 articleTag.tagID     = tag.tagID;
                 articleTag.articleID = articleId;
                 smokeFreeDB.ArticlesTag.Add(articleTag);
                 smokeFreeDB.SaveChanges();
             }
             else
             {
                 //Tag is not found in database
                 Tag newTag = new Tag();
                 newTag.tagName = tagArray[i];
                 smokeFreeDB.Tag.Add(newTag);
                 smokeFreeDB.SaveChanges();
                 smokeFreeDB.Entry(newTag).Reload();
                 articleTag.tagID     = newTag.tagID;
                 articleTag.articleID = articleId;
                 smokeFreeDB.ArticlesTag.Add(articleTag);
                 smokeFreeDB.SaveChanges();
             }
         }
     }
 }
        public ActionResult ArticleAdd(ArticleAddViewModel vm, List <HttpPostedFileBase> files, List <TagCheck> TagsListChecked)
        {
            if (vm.Article.ArticleId > 0)
            {
                //edycja newsa
                vm.Article.DateOfPublish = DateTime.Parse(Request.Form["DateOfPublish"]);
                vm.Article.AuthorId      = User.Identity.GetUserId();
                vm.Article.Text          = Server.HtmlEncode(vm.Article.Text);

                string tagLine = "";
                if (vm.TagsListChecked != null)
                {
                    foreach (var tagChecked in vm.TagsListChecked)
                    {
                        if (tagChecked.Value == true)
                        {
                            if (tagLine == "")
                            {
                                tagLine = tagChecked.Tag.Name;
                            }
                            else
                            {
                                tagLine = tagLine + "," + tagChecked.Tag.Name;
                            }
                        }
                    }
                }
                if (vm.Tags != null)
                {
                    vm.Tags = vm.Tags + "," + tagLine;
                }
                else
                {
                    vm.Tags = tagLine;
                }

                if (vm.Tags != null)
                {
                    var tags = vm.Tags.Split(',');
                    foreach (var tag in tags)
                    {
                        if (db.ArticlesTags.Where(n => n.Name == tag).Count() == 0)
                        {
                            db.ArticlesTags.Add(new ArticlesTag {
                                Name = tag
                            });
                            db.SaveChanges();
                        }
                        ArticlesTag artTag = db.ArticlesTags.Where(n => n.Name == tag).First();
                        if (artTag.Name != "" || artTag.Name != " ")
                        {
                            db.ArticlesTagsConnections.Add(new ArticlesTagsConnection {
                                ArticleId = vm.Article.ArticleId, ArticlesTagId = artTag.ArticlesTagId
                            });
                            db.SaveChanges();
                        }
                    }
                }
                db.Entry(vm.Article).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("ArticleAdd", new { confirmation = true }));
            }
            else
            {
                //dodawanie nowego newsa
                if (files != null && files.Count > 0) // != &&
                {
                    if (vm.Article.Title == null)
                    {
                        ModelState.AddModelError("", "Wpisz TYTUŁ.");
                    }
                    if (vm.Article.Text == null)
                    {
                        ModelState.AddModelError("", "Wpisz jakiś TEKST.");
                    }
                    if (vm.Article.Title == null || vm.Article.Text == null)
                    {
                        var articlesCategories = db.ArticlesCategories.ToList();
                        vm.ArticlesCategories = articlesCategories;
                        return(View(vm));
                    }
                    else
                    {
                        if (ModelState.IsValid)
                        {
                            //zapisywanie artykułu
                            vm.Article.DateOfPublish   = DateTime.Parse(Request.Form["DateOfPublish"]);
                            vm.Article.Visible         = vm.Article.Visible;
                            vm.Article.AuthorId        = User.Identity.GetUserId();
                            vm.Article.Text            = Server.HtmlEncode(vm.Article.Text);
                            vm.Article.IntroText       = Server.HtmlEncode(vm.Article.IntroText);
                            db.Entry(vm.Article).State = System.Data.Entity.EntityState.Added;
                            db.SaveChanges();
                            //////////////////////////////////

                            //zapisywanie tagów
                            string tagLine = "";
                            if (vm.TagsListChecked != null)
                            {
                                foreach (var tagChecked in vm.TagsListChecked)
                                {
                                    if (tagChecked.Value == true)
                                    {
                                        if (tagLine == "")
                                        {
                                            tagLine = tagChecked.Tag.Name;
                                        }
                                        else
                                        {
                                            tagLine = tagLine + "," + tagChecked.Tag.Name;
                                        }
                                    }
                                }
                            }
                            if (vm.Tags != null)
                            {
                                vm.Tags = vm.Tags + "," + tagLine;
                            }
                            else
                            {
                                vm.Tags = tagLine;
                            }

                            if (vm.Tags != null)
                            {
                                var tags = vm.Tags.Split(',');
                                foreach (var tag in tags)
                                {
                                    if (tag.Length > 0)
                                    {
                                        if (db.ArticlesTags.Where(n => n.Name == tag).Count() == 0)
                                        {
                                            db.ArticlesTags.Add(new ArticlesTag {
                                                Name = tag
                                            });
                                            db.SaveChanges();
                                        }
                                        ArticlesTag artTag = db.ArticlesTags.Where(n => n.Name == tag).First();
                                        db.ArticlesTagsConnections.Add(new ArticlesTagsConnection {
                                            ArticleId = vm.Article.ArticleId, ArticlesTagId = artTag.ArticlesTagId
                                        });
                                        db.SaveChanges();
                                    }
                                }
                            }
                            /////////////////////////////

                            //zapisywanie zdjec
                            var photo = new ArticlesPhoto();
                            int i     = 0;
                            foreach (var file in files)
                            {
                                var fileExt  = Path.GetExtension(file.FileName);
                                var filename = Guid.NewGuid() + fileExt;
                                var path     = Path.Combine(Server.MapPath(AppConfig.ArticlesImagesFolderRelated), filename);
                                file.SaveAs(path);

                                photo.ArticleId = vm.Article.ArticleId;
                                photo.FilePath  = filename;
                                photo.OrderNo   = i;
                                photo.Width     = 1300;
                                photo.Height    = 722;
                                db.ArticlesPhotos.Add(photo);
                                db.SaveChanges();
                                i++;
                            }
                            /////////////////////////////////////////////////////////////////////////////////////////

                            return(RedirectToAction("ArticleAdd", new { confirmation = true }));
                        }
                        else
                        {
                            var articleCategories = db.ArticlesCategories.ToList();
                            vm.ArticlesCategories = articleCategories;
                            return(View(vm));
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Nie wskazano pliku.");
                    var articleCategories = db.ArticlesCategories.ToList();
                    vm.ArticlesCategories = articleCategories;
                    return(View("ArticleAdd", vm));
                }
            }
        }
예제 #3
0
        public ActionResult Add(FormCollection collection)
        {
            string title_az = Request.Form["title_az"];
            string title_en = Request.Form["title_en"];

            string excerpt_az = Request.Form["excerpt_az"];
            string excerpt_en = Request.Form["excerpt_en"];

            string content_az = Request.Form["content_az"];
            string content_en = Request.Form["content_en"];

            string category_id = Request.Form["category_id"];
            string tags        = Request.Form["tags[]"];

            string status = Request.Form["status"];

            string title_img_id = Request.Form["title_img_id"];

            if (string.IsNullOrWhiteSpace(title_az) || string.IsNullOrWhiteSpace(title_en))
            {
                viewArticles.ErrorMsg = "Title is required field! ";
                return(View(model: viewArticles));
            }

            if (string.IsNullOrWhiteSpace(category_id))
            {
                viewArticles.ErrorMsg = "Category is required field!";
                return(View(model: viewArticles));
            }


            Article article = new Article
            {
                title_az   = title_az,
                title_en   = title_en,
                excerpt_az = excerpt_az,
                excerpt_en = excerpt_en,
                content_az = content_az,
                content_en = content_en,
                status     = Convert.ToByte(status)
            };

            if (!string.IsNullOrWhiteSpace(title_img_id))
            {
                article.title_img_id = Convert.ToInt32(title_img_id);
            }

            if (article.status == 1)
            {
                article.date = DateTime.Now;
            }

            db.Articles.Add(article);
            db.SaveChanges();

            ArticlesCategory articlesCategory = new ArticlesCategory
            {
                article_id  = article.id,
                category_id = Convert.ToInt32(category_id)
            };

            db.ArticlesCategories.Add(articlesCategory);

            if (string.IsNullOrWhiteSpace(tags))
            {
                List <string> tagsList = tags.Split(',').ToList();

                foreach (string tag in tagsList)
                {
                    ArticlesTag articlesTag = new ArticlesTag
                    {
                        article_id = article.id,
                        tag_id     = Convert.ToInt32(tag)
                    };

                    db.ArticlesTags.Add(articlesTag);
                }
            }

            db.SaveChanges();

            if (article.status == 0)
            {
                return(RedirectToAction("NonPublished"));
            }
            else
            {
                return(RedirectToAction("Published"));
            }
        }