Carrotware.CMS.Mvc.UI.Admin.Models.SiteImportWordpressModel.ImportStuff C# (CSharp) Method

ImportStuff() public method

public ImportStuff ( ) : void
return void
        public void ImportStuff()
        {
            this.HasLoaded = false;
            this.Site = ContentImportExportUtils.GetSerializedWPExport(this.ImportID);

            SiteData.CurrentSite = null;

            SiteData site = SiteData.CurrentSite;

            this.Message = String.Empty;
            string sMsg = String.Empty;

            if (this.ImportSite || this.ImportPages || this.ImportPosts) {
                List<string> tags = site.GetTagList().Select(x => x.TagSlug.ToLowerInvariant()).ToList();
                List<string> cats = site.GetCategoryList().Select(x => x.CategorySlug.ToLowerInvariant()).ToList();

                this.Site.Tags.RemoveAll(x => tags.Contains(x.InfoKey.ToLowerInvariant()));
                this.Site.Categories.RemoveAll(x => cats.Contains(x.InfoKey.ToLowerInvariant()));

                sMsg += "<li>Imported Tags and Categories</li>";

                List<ContentTag> lstTag = (from l in this.Site.Tags.Distinct()
                                           select new ContentTag {
                                               ContentTagID = Guid.NewGuid(),
                                               IsPublic = true,
                                               SiteID = site.SiteID,
                                               TagSlug = l.InfoKey,
                                               TagText = l.InfoLabel
                                           }).Distinct().ToList();

                List<ContentCategory> lstCat = (from l in this.Site.Categories.Distinct()
                                                select new ContentCategory {
                                                    ContentCategoryID = Guid.NewGuid(),
                                                    IsPublic = true,
                                                    SiteID = site.SiteID,
                                                    CategorySlug = l.InfoKey,
                                                    CategoryText = l.InfoLabel
                                                }).Distinct().ToList();

                foreach (var v in lstTag) {
                    v.Save();
                }
                foreach (var v in lstCat) {
                    v.Save();
                }
            }
            SetMsg(sMsg);

            if (this.ImportSite) {
                sMsg += "<li>Updated Site Name</li>";
                site.SiteName = this.Site.SiteTitle;
                site.SiteTagline = this.Site.SiteDescription;
                site.Save();
            }
            SetMsg(sMsg);

            if (!this.MapUsers) {
                this.Site.Authors = new List<WordPressUser>();
            }

            //iterate author collection and find if in the system
            foreach (WordPressUser wpu in this.Site.Authors) {
                SecurityData sd = new SecurityData();

                ExtendedUserData usr = null;
                wpu.ImportUserID = Guid.Empty;

                //attempt to find the user in the userbase
                usr = ExtendedUserData.FindByEmail(wpu.Email);
                if (usr != null && usr.UserId != Guid.Empty) {
                    wpu.ImportUserID = usr.UserId;
                } else {
                    usr = ExtendedUserData.FindByUsername(wpu.Login);
                    if (usr != null && usr.UserId != Guid.Empty) {
                        wpu.ImportUserID = usr.UserId;
                    }
                }

                if (this.CreateUsers) {
                    if (wpu.ImportUserID == Guid.Empty) {
                        ApplicationUser user = new ApplicationUser { UserName = wpu.Login, Email = wpu.Email };
                        var result = sd.CreateApplicationUser(user, out usr);
                        if (result.Succeeded) {
                            usr = ExtendedUserData.FindByUsername(wpu.Login);
                        } else {
                            throw new Exception(String.Format("Could not create user: {0} ({1}) \r\n{2}", wpu.Login, wpu.Email, String.Join("\r\n", result.Errors)));
                        }
                        wpu.ImportUserID = usr.UserId;
                    }

                    if (wpu.ImportUserID != Guid.Empty) {
                        ExtendedUserData ud = new ExtendedUserData(wpu.ImportUserID);
                        if (!String.IsNullOrEmpty(wpu.FirstName) || !String.IsNullOrEmpty(wpu.LastName)) {
                            ud.FirstName = wpu.FirstName;
                            ud.LastName = wpu.LastName;
                            ud.Save();
                        }
                    }
                }
            }

            this.Site.Comments.ForEach(r => r.ImportRootID = Guid.Empty);

            using (ISiteNavHelper navHelper = SiteNavFactory.GetSiteNavHelper()) {
                if (this.ImportPages) {
                    sMsg += "<li>Imported Pages</li>";

                    int iOrder = 0;
                    SiteNav navHome = navHelper.FindHome(site.SiteID, false);
                    if (navHome != null) {
                        iOrder = 2;
                    }

                    foreach (var wpp in (from c in this.Site.Content
                                         where c.PostType == WordPressPost.WPPostType.Page
                                         orderby c.PostOrder, c.PostTitle
                                         select c).ToList()) {
                        GrabAttachments(wpp);
                        RepairBody(wpp);

                        ContentPage cp = ContentImportExportUtils.CreateWPContentPage(this.Site, wpp, site);
                        cp.SiteID = site.SiteID;
                        cp.ContentType = ContentPageType.PageType.ContentEntry;
                        cp.EditDate = SiteData.CurrentSite.Now;
                        cp.NavOrder = iOrder;
                        cp.TemplateFile = this.PageTemplate;

                        WordPressPost parent = (from c in this.Site.Content
                                                where c.PostType == WordPressPost.WPPostType.Page
                                                  && c.PostID == wpp.ParentPostID
                                                select c).FirstOrDefault();

                        SiteNav navParent = null;

                        SiteNav navData = navHelper.GetLatestVersion(site.SiteID, false, cp.FileName.ToLowerInvariant());
                        if (parent != null) {
                            navParent = navHelper.GetLatestVersion(site.SiteID, false, parent.ImportFileName.ToLowerInvariant());
                        }

                        //if URL exists already, make this become a new version in the current series
                        if (navData != null) {
                            cp.Root_ContentID = navData.Root_ContentID;
                            if (navData.NavOrder == 0) {
                                cp.NavOrder = 0;
                            }
                        }

                        if (navParent != null) {
                            cp.Parent_ContentID = navParent.Root_ContentID;
                        } else {
                            if (parent != null) {
                                cp.Parent_ContentID = parent.ImportRootID;
                            }
                        }
                        //preserve homepage
                        if (navHome != null && navHome.FileName.ToLowerInvariant() == cp.FileName.ToLowerInvariant()) {
                            cp.NavOrder = 0;
                        }

                        cp.RetireDate = CMSConfigHelper.CalcNearestFiveMinTime(cp.CreateDate).AddYears(200);
                        cp.GoLiveDate = CMSConfigHelper.CalcNearestFiveMinTime(cp.CreateDate).AddMinutes(-5);

                        //if URL exists already, make this become a new version in the current series
                        if (navData != null) {
                            cp.Root_ContentID = navData.Root_ContentID;
                            cp.RetireDate = navData.RetireDate;
                            cp.GoLiveDate = navData.GoLiveDate;
                        }

                        cp.SavePageEdit();

                        this.Site.Comments.Where(x => x.PostID == wpp.PostID).ToList().ForEach(r => r.ImportRootID = cp.Root_ContentID);

                        iOrder++;
                    }
                }

                if (this.ImportPosts) {
                    sMsg += "<li>Imported Posts</li>";

                    foreach (var wpp in (from c in this.Site.Content
                                         where c.PostType == WordPressPost.WPPostType.BlogPost
                                         orderby c.PostOrder
                                         select c).ToList()) {
                        GrabAttachments(wpp);
                        RepairBody(wpp);

                        ContentPage cp = ContentImportExportUtils.CreateWPContentPage(this.Site, wpp, site);
                        cp.SiteID = site.SiteID;
                        cp.Parent_ContentID = null;
                        cp.ContentType = ContentPageType.PageType.BlogEntry;
                        cp.EditDate = SiteData.CurrentSite.Now;
                        cp.NavOrder = SiteData.BlogSortOrderNumber;
                        cp.TemplateFile = this.PostTemplate;

                        SiteNav navData = navHelper.GetLatestVersion(site.SiteID, false, cp.FileName.ToLowerInvariant());

                        cp.RetireDate = CMSConfigHelper.CalcNearestFiveMinTime(cp.CreateDate).AddYears(200);
                        cp.GoLiveDate = CMSConfigHelper.CalcNearestFiveMinTime(cp.CreateDate).AddMinutes(-5);

                        //if URL exists already, make this become a new version in the current series
                        if (navData != null) {
                            cp.Root_ContentID = navData.Root_ContentID;
                            cp.RetireDate = navData.RetireDate;
                            cp.GoLiveDate = navData.GoLiveDate;
                        }

                        cp.SavePageEdit();

                        this.Site.Comments.Where(x => x.PostID == wpp.PostID).ToList().ForEach(r => r.ImportRootID = cp.Root_ContentID);
                    }

                    using (ContentPageHelper cph = new ContentPageHelper()) {
                        //cph.BulkBlogFileNameUpdateFromDate(site.SiteID);
                        cph.ResolveDuplicateBlogURLs(site.SiteID);
                        cph.FixBlogNavOrder(site.SiteID);
                    }
                }
            }
            SetMsg(sMsg);

            this.Site.Comments.RemoveAll(r => r.ImportRootID == Guid.Empty);

            if (this.Site.Comments.Any()) {
                sMsg += "<li>Imported Comments</li>";
            }

            foreach (WordPressComment wpc in this.Site.Comments) {
                int iCommentCount = -1;

                iCommentCount = PostComment.GetCommentCountByContent(site.SiteID, wpc.ImportRootID, wpc.CommentDateUTC, wpc.AuthorIP, wpc.CommentContent);
                if (iCommentCount < 1) {
                    iCommentCount = PostComment.GetCommentCountByContent(site.SiteID, wpc.ImportRootID, wpc.CommentDateUTC, wpc.AuthorIP);
                }

                if (iCommentCount < 1) {
                    PostComment pc = new PostComment();
                    pc.ContentCommentID = Guid.NewGuid();
                    pc.Root_ContentID = wpc.ImportRootID;
                    pc.CreateDate = site.ConvertUTCToSiteTime(wpc.CommentDateUTC);
                    pc.IsApproved = false;
                    pc.IsSpam = false;

                    pc.CommenterIP = wpc.AuthorIP;
                    pc.CommenterName = wpc.Author;
                    pc.CommenterEmail = wpc.AuthorEmail;
                    pc.PostCommentText = wpc.CommentContent;
                    pc.CommenterURL = wpc.AuthorURL;

                    if (wpc.Approved == "1") {
                        pc.IsApproved = true;
                    }
                    if (wpc.Approved.ToLowerInvariant() == "trash") {
                        pc.IsSpam = true;
                    }
                    if (wpc.Type.ToLowerInvariant() == "trackback" || wpc.Type.ToLowerInvariant() == "pingback") {
                        pc.CommenterEmail = wpc.Type;
                    }

                    pc.Save();
                }
            }
            SetMsg(sMsg);
        }

Usage Example

		public ActionResult SiteImportWP(SiteImportWordpressModel model) {
			ModelState.Clear();
			bool loaded = true;

			if (model.DownloadImages && String.IsNullOrEmpty(model.SelectedFolder)) {
				ModelState.AddModelError("SelectedFolder", "If download images is selected, you must select a target folder.");
			}

			loaded = ModelState.IsValid;

			if (ModelState.IsValid) {
				model.ImportStuff();

				if (!model.HasLoaded) {
					loaded = false;
					ModelState.AddModelError(String.Empty, "No Items Selected For Import");
				}
			}

			var newmodel = new SiteImportWordpressModel(model.ImportID);
			newmodel.Message = model.Message;
			newmodel.HasLoaded = loaded;

			newmodel.PageTemplate = model.PageTemplate;
			newmodel.PostTemplate = model.PostTemplate;
			newmodel.SelectedFolder = model.SelectedFolder;

			newmodel.ImportPages = model.ImportPages;
			newmodel.ImportPosts = model.ImportPosts;
			newmodel.ImportSite = model.ImportSite;

			newmodel.DownloadImages = model.DownloadImages;
			newmodel.FixHtmlBodies = model.FixHtmlBodies;
			newmodel.CreateUsers = model.CreateUsers;
			newmodel.MapUsers = model.MapUsers;

			Helper.HandleErrorDict(ModelState);

			return View(newmodel);
		}