public ActionResult EditArticle(int articleID)
        {
            var article = articleRepository.GetById(articleID);

            if (article == null)
            {
                return(RedirectToHomeWithError("Article not found!"));
            }

            var newspaper = article.Newspaper;

            if (newspaper == null)
            {
                return(NewspaperDoesNotExistRedirect());
            }

            var entity = SessionHelper.CurrentEntity;
            var rights = newspaperService.GetNewspaperRights(newspaper, entity, SessionHelper.LoggedCitizen);

            if (rights.HasFlag(NewspaperRightsEnum.CanWriteArticles) == false)
            {
                return(RedirectToHomeWithError("You cannot write articles!"));
            }

            var vm = new WriteArticleViewModel(newspaper, article, newspaperService);

            return(View(vm));
        }
        public ActionResult WriteArticle(WriteArticleViewModel vm, int newspaperID)
        {
            var newspaper = newspaperRepository.GetById(newspaperID);

            if (newspaper == null)
            {
                return(NewspaperDoesNotExistRedirect());
            }

            var entity = SessionHelper.CurrentEntity;
            var rights = newspaperService.GetNewspaperRights(newspaper, entity, SessionHelper.LoggedCitizen);

            if (rights.HasFlag(NewspaperRightsEnum.CanWriteArticles) == false)
            {
                return(RedirectToHomeWithError("You cannot write articles!"));
            }

            if (ModelState.IsValid)
            {
                var article = newspaperService.CreateArticle(newspaper, SessionHelper.CurrentEntity, vm.Title, vm.Content, vm.Price, vm.PayOnlyContent, vm.ShortDescription, vm.ArticleImage, vm.Publish == true);
                if (article.IsError)
                {
                    return(RedirectBackWithError(article));
                }
                return(RedirectToAction("ReadArticle", new { articleID = article.ReturnValue.ID }));
            }

            vm.Info = new NewspaperInfoViewModel(newspaper, newspaperService);
            return(View(vm));
        }
        public ActionResult EditArticle(WriteArticleViewModel vm, int articleID)
        {
            var article = articleRepository.GetById(articleID);

            if (article == null)
            {
                return(RedirectToHomeWithError("Article not found!"));
            }

            var newspaper = article.Newspaper;

            if (newspaper == null)
            {
                return(NewspaperDoesNotExistRedirect());
            }

            var entity = SessionHelper.CurrentEntity;
            var rights = newspaperService.GetNewspaperRights(newspaper, entity, SessionHelper.LoggedCitizen);

            if (rights.HasFlag(NewspaperRightsEnum.CanWriteArticles) == false)
            {
                return(RedirectToHomeWithError("You cannot write articles!"));
            }

            if (ModelState.IsValid)
            {
                article.Content          = vm.Content;
                article.PayOnlyContent   = vm.PayOnlyContent ?? "";
                article.ShortDescription = vm.ShortDescription;
                article.Price            = (decimal?)vm.Price;
                if (vm.EnablePayOnly == false)
                {
                    article.Price = null;
                }

                article.Title = vm.Title;
                if (vm.ArticleImage != null)
                {
                    var result = uploadService.UploadImage(vm.ArticleImage, WebServices.enums.UploadLocationEnum.Articles);
                    if (result.IsError)
                    {
                        AddError(result as MethodResult);
                    }
                    else
                    {
                        article.ImgURL = result;
                    }
                }
                article.Published = (vm.Publish == true);

                articleRepository.SaveChanges();

                return(RedirectToAction("ReadArticle", new { articleID = article.ID }));
            }

            vm.Info = new NewspaperInfoViewModel(newspaper, newspaperService);
            return(View(vm));
        }
        public ActionResult HandleWriteArticle(WriteArticleViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(CurrentUmbracoPage());
            }

            if (!IsValidContentType(vm.ArticleImage.ContentType))
            {
                ModelState.AddModelError("Error", "Only JPG, JPEG, PNG & GIF files are allowed.");
                return(CurrentUmbracoPage());
            }

            try
            {
                //TODO: Add related articles! Important
                //What you need to take in order to fulfill it
                var newsArticles    = Umbraco.ContentAtRoot().DescendantsOrSelfOfType("newsArticles").FirstOrDefault();
                var createdArticles = Umbraco.ContentAtRoot().DescendantsOrSelfOfType("newsArticle");

                if (newsArticles != null)
                {
                    var newsArticle = Services.ContentService.Create(vm.Title, newsArticles.Id, "newsArticle");

                    var folderContent = Services.MediaService.GetById(folderContentId);

                    var attachedMedia = Services.MediaService.CreateMediaWithIdentity(vm.ArticleImage.FileName, folderContent.Id, "file");
                    attachedMedia.SetValue(contentTypeBaseServiceProvider, "umbracoFile", vm.ArticleImage.FileName, vm.ArticleImage.InputStream);
                    Services.MediaService.Save(attachedMedia);

                    newsArticle.SetValue("author", vm.Auhtor);
                    newsArticle.SetValue("postDate", vm.PostDate);
                    newsArticle.SetValue("leadIn", vm.LeadIn);
                    newsArticle.SetValue("articleImage", attachedMedia.GetUdi());
                    newsArticle.SetValue("articleContent", vm.ArticleContent);

                    var tagsSplited = vm.Tags
                                      .Split(new[] { ' ', '#', ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                    if (tagsSplited != null)
                    {
                        newsArticle.AssignTags("newsCategories", tagsSplited, true);
                    }

                    var relatedArticles = new List <string>();

                    foreach (var article in createdArticles)
                    {
                        var property = article.Properties.SingleOrDefault(x => x.Alias == "newsCategories").Value() as IEnumerable <string>;

                        foreach (var prop in property)
                        {
                            if (tagsSplited.Contains(prop))
                            {
                                relatedArticles.Add($"umb://document/{article.Key.ToString()}");
                            }
                        }
                    }
                    newsArticle.SetValue("articleRelatedContent", string.Join(",", relatedArticles.Take(3)));
                    Services.ContentService.SaveAndPublish(newsArticle);
                }

                return(RedirectToCurrentUmbracoPage());
            }
            catch (Exception ex)
            {
                Logger.Error <WriteArticleController>("There was an error in the article submission", ex.Message);
                ModelState.AddModelError("Error", "Sorry there was a problem noting your details. Would you please try again later?");
            }

            return(CurrentUmbracoPage());
        }
        public ActionResult RenderWriteArticle()
        {
            var vm = new WriteArticleViewModel();

            return(PartialView(PARTIAL_VIEW_PATH + "WriteArticle.cshtml", vm));
        }