public ModelResult <Post> AddPost(Blog blog, PostInputForImport postInput) { ModelResult <Post> results = addPost(postInput, () => postInput.ToPost(blog)); if (results.IsValid) { Post newPost = results.Item; pluginEngine.ExecuteAll("PostAddedFromImport", new { context, post = new PostReadOnly(newPost, urlHelper.AbsolutePath(urlHelper.Post(newPost))) }); //TODO: (erikpo) Move this into a module addCreatorSubscription(newPost); } return(results); }
public virtual OxiteViewModel ImportSave(Blog blog, string slugPattern) { if (blog == null) { return(null); } ValidationStateDictionary validationState = new ValidationStateDictionary(); XmlTextReader reader = null; bool modifiedSite = false; try { reader = new XmlTextReader(Request.Files[0].InputStream); BlogMLBlog blogMLBlog = BlogMLSerializer.Deserialize(reader); Language language = languageService.GetLanguage(context.Site.LanguageDefault); BlogInputForImport blogInput = new BlogInputForImport(blogMLBlog.SubTitle, blogMLBlog.SubTitle, blogMLBlog.DateCreated); ModelResult <Blog> results = blogService.EditBlog(blog, blogInput); if (!results.IsValid) { ModelState.AddModelErrors(results.ValidationState); return(Import(blog)); } if (!context.Site.HasMultipleBlogs) { Site site = siteService.GetSite(); site.DisplayName = blog.DisplayName; site.Description = blog.Description; siteService.EditSite(site, out validationState); if (!validationState.IsValid) { throw new Exception(); } modifiedSite = true; } postService.RemoveAll(blog); foreach (BlogMLPost blogMLPost in blogMLBlog.Posts) { if (string.IsNullOrEmpty(blogMLPost.Title) || string.IsNullOrEmpty(blogMLPost.Content.Text)) { continue; } PostInputForImport postInput = blogMLPost.ToImportPostInput(blogMLBlog, context.Site.CommentingDisabled | blog.CommentingDisabled, slugPattern, blogMLPost.Approved ? EntityState.Normal : EntityState.PendingApproval, context.User.Cast <User>()); ModelResult <Post> addPostResults = postService.AddPost(blog, postInput); if (!addPostResults.IsValid) { ModelState.AddModelErrors(addPostResults.ValidationState); return(Import(blog)); } foreach (BlogMLComment blogMLComment in blogMLPost.Comments) { CommentInputForImport commentInput = blogMLComment.ToImportCommentInput(blogMLBlog, context.User.Cast <User>(), language); ModelResult <PostComment> addCommentResults = commentService.AddComment(addPostResults.Item, commentInput); if (!addCommentResults.IsValid) { ModelState.AddModelErrors(addCommentResults.ValidationState); return(Import(blog)); } } } } catch (Exception ex) { ModelState.AddModelErrors(validationState); if (!string.IsNullOrEmpty(ex.Message)) { ModelState.AddModelError("ModelName", ex); } return(Import(blog)); } finally { if (reader != null) { reader.Close(); } } if (modifiedSite) { OxiteApplication.Load(ControllerContext.HttpContext); } return(new OxiteViewModel { Container = blog }); }