Beispiel #1
0
        protected void Submit_Click(object sender, EventArgs e)
        {
            if (TargetBlog.SelectedValue == "")
            {
                return;
            }

            if (!WordPressFile.HasFile)
            {
                return;
            }

            var xml = XDocument.Load(WordPressFile.FileContent);

            _authors       = GetPostAuthorsFromXML(xml);
            _posts         = GetPostsFromXML(xml);
            _defaultAuthor = SFUserManager.GetUserByEmail(_defaultAuthorEmail);

            var importCount  = 0;
            var missingCount = 0;

            foreach (var post in _posts)
            {
                PostAuthor author = _authors.Where(a => a.Username == post.Creator).FirstOrDefault();

                string parentBlogTitle = "";

                if (_useParentBlogMap)
                {
                    parentBlogTitle = parentBlogMap[author.Email];
                    _targetBlogId   = SFBlogsManager.GetBlogs().Where(b => b.Title == parentBlogMap[author.Email]).SingleOrDefault().Id;
                }
                else
                {
                    parentBlogTitle = TargetBlog.SelectedItem.Text;
                    _targetBlogId   = Guid.Parse(TargetBlog.SelectedValue);
                }

                if (BlogExists(_targetBlogId))
                {
                    ImportPost(_targetBlogId, post);
                    importCount++;
                }
                else
                {
                    _importLog += "<br>Parent blog '" + parentBlogTitle + "' not found for post '" + post.Title + "'.<br>";
                    missingCount++;
                }

                lblResult.Text = String.Format("Import Complete. Posts Imported: {0}. Posts Missing: {1}", importCount, missingCount);
            }
        }
        private void ImportPost(Guid blogId, GenericBlogPost post)
        {
            Guid masterBlogPostId = Guid.NewGuid();

            BlogPost blogPost = SFBlogsManager.CreateBlogPost(masterBlogPostId);

            Blog blog = SFBlogsManager.GetBlogs().Where(b => b.Id == blogId).SingleOrDefault();

            blogPost.Parent = blog;

            masterBlogPostId = blogPost.Id;

            blogPost.Title           = post.Title;
            blogPost.DateCreated     = post.DateCreated;
            blogPost.PublicationDate = post.PublicationDate;
            blogPost.LastModified    = post.PublicationDate;

            string content = ParseContent(post.Content);

            content = ImportAndLinkContentImages(content, post.Title, blog.Title);

            blogPost.Content = content;

            blogPost.Summary       = post.Summary;
            blogPost.AllowComments = post.AllowComments;
            blogPost.UrlName       = post.PostName ?? Regex.Replace(post.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
            blogPost.Owner         = Guid.Empty;

            PostAuthor author = _authors.Where(a => a.Username == post.Creator).FirstOrDefault();

            if (author != null)
            {
                User user = SFUserManager.GetUserByEmail(author.Email);

                if (user != null)
                {
                    blogPost.Owner = user.Id;
                }
            }

            if (ddlCategoriesImportMode.SelectedIndex > 0)
            {
                if (post.Categories != null && post.Categories.Count > 0)
                {
                    foreach (string category in post.Categories)
                    {
                        HierarchicalTaxon taxon;

                        taxon = SFTaxonomyManager.GetTaxa <HierarchicalTaxon>().Where(t => t.Name == category).FirstOrDefault();

                        if (taxon == null && ddlCategoriesImportMode.SelectedIndex > 1)
                        {
                            taxon = CreateCategory(category);
                        }

                        if (taxon != null)
                        {
                            blogPost.Organizer.AddTaxa("Category", taxon.Id);
                        }
                    }
                }
            }

            if (ddlTagsImportMode.SelectedIndex > 0)
            {
                if (post.Tags != null && post.Tags.Count > 0)
                {
                    foreach (string tag in post.Tags)
                    {
                        FlatTaxon taxon;

                        taxon = SFTaxonomyManager.GetTaxa <FlatTaxon>().Where(t => t.Name == tag).FirstOrDefault();

                        if (taxon == null && ddlTagsImportMode.SelectedIndex > 1)
                        {
                            taxon = CreateTag(tag);
                        }

                        if (taxon != null)
                        {
                            blogPost.Organizer.AddTaxa("Tags", taxon.Id);
                        }
                    }
                }
            }

            if (post.AttachmentUrl != String.Empty)
            {
                string imageSrc = post.AttachmentUrl;

                string imgFile = Path.GetFileName(imageSrc);

                var sfImages = App.WorkWith().Images().Where(i => i.Status == ContentLifecycleStatus.Master).Get().ToList();

                var sfImg = sfImages.Where(i => Path.GetFileName(i.FilePath) == imgFile.ToLower()).FirstOrDefault();

                if (sfImg != null)
                {
                    blogPost.CreateRelation(sfImg, "Thumbnail");
                }
                else
                {
                    if (ImportImage(imageSrc, post.Title, blog.Title))
                    {
                        sfImg = App.WorkWith().Images().Where(i => i.Status == ContentLifecycleStatus.Master).Get().ToList().Where(i => Path.GetFileName(i.FilePath) == imgFile.ToLower()).FirstOrDefault();

                        blogPost.CreateRelation(sfImg, "Thumbnail");
                    }
                }
            }

            if (post.Meta != null)
            {
                if (post.Meta.ContainsKey("_yoast_wpseo_title"))
                {
                    blogPost.SetValue("SEOTitle", post.Meta["_yoast_wpseo_title"]);
                }

                if (post.Meta.ContainsKey("_yoast_wpseo_metadesc"))
                {
                    blogPost.SetValue("SEOMetaDescription", post.Meta["_yoast_wpseo_metadesc"]);
                }

                if (post.Meta.ContainsKey("_yoast_wpseo_focuskw"))
                {
                    blogPost.SetValue("SEOMetaKeywords", post.Meta["_yoast_wpseo_focuskw"]);
                }
            }

            SFBlogsManager.RecompileItemUrls(blogPost);
            SFBlogsManager.Lifecycle.PublishWithSpecificDate(blogPost, post.PublicationDate);
            SFBlogsManager.SaveChanges();

            var bag = new Dictionary <string, string>();

            bag.Add("ContentType", typeof(BlogPost).FullName);
            WorkflowManager.MessageWorkflow(masterBlogPostId, typeof(BlogPost), null, "Publish", false, bag);

            if (chkImportComments.Checked)
            {
                if (post.Comments != null && post.Comments.Count() > 0)
                {
                    foreach (var comment in post.Comments)
                    {
                        CreateBlogPostComment(masterBlogPostId, comment);
                    }
                }
            }

            var dbg = _importLog;
        }