public ActionResult Edit(ArticleViewModel articleVM, FormCollection formValues, string command)
        {
            if (command == "Save & Preview")
            {
                if (ModelState.IsValid)
                {
                    if (Session["UserProfile"] != null)
                    {
                        List <TagItem> tags = null;

                        UserProfileItem userprofile = (UserProfileItem)Session["UserProfile"];
                        articleVM.ArticleItem.Userid = userprofile.ID;
                        string articleID = articleVM.ArticleItem.ID.ToString();
                        if (!string.IsNullOrEmpty(articleID))
                        {
                            if (!string.IsNullOrEmpty(articleVM.ArticleItem.Content))
                            {
                                articleVM.ArticleItem.Content_summary = StringHtmlExtensions.TruncateHtml(articleVM.ArticleItem.Content, CONTENT_SUMMARY_LENGTH, "....");
                            }

                            if (!string.IsNullOrEmpty(articleVM.ArticleItem.Title_English))
                            {
                                articleVM.ArticleItem.Title_SEO = articleVM.ArticleItem.Title_English.ToSeoUrl();
                            }


                            //update the article entry
                            DataAccessLayer datalayer = null;
                            datalayer = new DataAccessLayer();
                            if (datalayer.UpdateArticleEntry(articleVM.ArticleItem))
                            {
                                if (!string.IsNullOrEmpty(articleVM.ArticleItem.Tag_Article))
                                {
                                    //update tag table for this article
                                    tags = new List <TagItem>();
                                    var tagSplit = articleVM.ArticleItem.Tag_Article.Split(',');
                                    for (var i = 0; i < tagSplit.Length - 1; i++)
                                    {
                                        TagItem tagitem = new TagItem {
                                            id = i, name = tagSplit[i]
                                        };
                                        tags.Add(tagitem);
                                    }
                                    bool istagsupdated = datalayer.UpdateTagEntry(tags, articleVM.ArticleItem.ID);
                                    if (!istagsupdated)
                                    {
                                        //log error that tag updation failed
                                    }
                                }

                                return(RedirectToAction("ArticleOption", "Manage", new { articleid = articleID }));
                            }
                            else
                            {
                                //some error occured in updation - redirect to error page
                            }
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Authenticate", "Account"));
                    }
                }
            }
            else if (command == "Cancel")
            {
                return(RedirectToAction("Index", "Manage"));
            }
            return(View(articleVM));
        }
        public ActionResult EditPublished(ArticleViewModel articleVM, FormCollection formValues, string command)
        {
            DataAccessLayer     datalayer          = null;
            List <CategoryItem> article_categories = null;

            if (command == "Save & Preview")
            {
                if (ModelState.IsValid)
                {
                    if (Session["UserProfile"] != null)
                    {
                        List <TagItem> tags = null;

                        UserProfileItem userprofile = (UserProfileItem)Session["UserProfile"];
                        articleVM.ArticleItem.Userid = userprofile.ID;
                        string articleID = articleVM.ArticleItem.ID.ToString();
                        if (!string.IsNullOrEmpty(articleID))
                        {
                            if (!string.IsNullOrEmpty(articleVM.ArticleItem.Content))
                            {
                                articleVM.ArticleItem.Content_summary = StringHtmlExtensions.TruncateHtml(articleVM.ArticleItem.Content, CONTENT_SUMMARY_LENGTH, "....");
                            }

                            if (!string.IsNullOrEmpty(articleVM.ArticleItem.Title_English))
                            {
                                articleVM.ArticleItem.Title_SEO = articleVM.ArticleItem.Title_English.ToSeoUrl();
                            }


                            //update the article entry

                            datalayer = new DataAccessLayer();
                            if (datalayer.UpdateArticlePublished(articleVM.ArticleItem))
                            {
                                if (!string.IsNullOrEmpty(articleVM.ArticleItem.Tag_Article))
                                {
                                    //update tag table for this article
                                    tags = new List <TagItem>();
                                    var tagSplit = articleVM.ArticleItem.Tag_Article.Split(',');
                                    for (var i = 0; i < tagSplit.Length - 1; i++)
                                    {
                                        TagItem tagitem = new TagItem {
                                            id = i, name = tagSplit[i]
                                        };
                                        tags.Add(tagitem);
                                    }
                                    bool istagsupdated = datalayer.UpdateTagEntry(tags, articleVM.ArticleItem.ID);
                                    if (!istagsupdated)
                                    {
                                        //log error that tag updation failed
                                    }
                                }

                                return(RedirectToAction("ArticlePublishedOption", "Manage", new { articleid = articleID }));
                            }
                            else
                            {
                                //some error occured in updation - redirect to error page
                            }
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Authenticate", "Account"));
                    }
                }
            }
            else if (command == "Cancel")
            {
                return(RedirectToAction("Index", "Manage"));
            }
            //TODO: Lot of work need to be done here for error handling, tags etc
            datalayer            = new DataAccessLayer();
            article_categories   = datalayer.GetCategories();
            articleVM.Categories = article_categories;
            StringBuilder script = new StringBuilder();

            script.Append("<script type='text/javascript'>");
            script.Append("$('#" + articleVM.ArticleItem.Category.Id + "').prop('checked', true);");
            //script.Append("$('#error_alert').show();");
            script.Append("</script>");
            ViewBag.StartScript = script.ToString();

            return(View(articleVM));
        }
        public ActionResult CreateArticle(ArticleViewModel articleVM, FormCollection formValues, string command)
        {
            DataAccessLayer datalayer = null;
            int             articleID = 0;
            List <TagItem>  tags      = null;

            if (command == "Save & Preview")
            {
                if (ModelState.IsValid)
                {
                    //articleVM.ID = Guid.NewGuid();
                    //articleVM.Created_Date = DateTime.Now;
                    if (Session["UserProfile"] != null)
                    {
                        UserProfileItem userprofile = (UserProfileItem)Session["UserProfile"];
                        articleVM.ArticleItem.Userid = userprofile.ID;

                        if (!string.IsNullOrEmpty(articleVM.ArticleItem.Content))
                        {
                            articleVM.ArticleItem.Content_summary = StringHtmlExtensions.TruncateHtml(articleVM.ArticleItem.Content, 200, "....");
                        }
                        if (!string.IsNullOrEmpty(articleVM.ArticleItem.Title_English))
                        {
                            articleVM.ArticleItem.Title_SEO = articleVM.ArticleItem.Title_English.ToSeoUrl();
                        }



                        //TODO: check if content is empty - return VM

                        datalayer = new DataAccessLayer();
                        articleID = datalayer.CreateArticle(articleVM.ArticleItem);
                        if (articleID != 0)
                        {
                            if (!string.IsNullOrEmpty(articleVM.ArticleItem.Tag_Article))
                            {
                                //update tag table for this article
                                tags = new List <TagItem>();
                                var tagSplit = articleVM.ArticleItem.Tag_Article.Split(',');
                                for (var i = 0; i < tagSplit.Length - 1; i++)
                                {
                                    TagItem tagitem = new TagItem {
                                        id = i, name = tagSplit[i]
                                    };
                                    tags.Add(tagitem);
                                }
                                bool istagsupdated = datalayer.UpdateTagEntry(tags, articleID);
                                if (!istagsupdated)
                                {
                                    //log error that tag updation failed
                                }
                            }

                            return(RedirectToAction("ArticleOption", "Manage", new { articleid = articleID }));
                        }
                        else
                        {
                            //some error occured while saving the article
                            return(View(articleVM));
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Authenticate", "Account"));
                    }
                }
            }
            else if (command == "Cancel")
            {
                return(RedirectToAction("Index", "Manage"));
            }

            return(View(articleVM));
        }