Example #1
0
 private void AddPage(BootstrapContext db, string pageName, bool isBlogItem = true, bool isRssFeed = false)
 {
     if (db.Pages.Where(x => x.Name.ToLower().Equals(pageName)).FirstOrDefault() == null)
     {
         var permaLink = new Permalink()
         {
             Name = pageName,
             Type = null,
             ChangeEvent = new ChangeEvent() { CreatedByUserIdCode = "bha", CreatedOn = DateTime.Now, UpdatedByUserIdCode = "bha", UpdatedOn = DateTime.Now }
         };
         db.Permalinks.Add(permaLink);
         Page page = new Page()
         {
             Name = pageName,
             Title = pageName,
             Permalink = permaLink,
             IncludeInMenu = false,
             IsDraft = false,
             IsPublished = true,
             IsRssFeed = false,
             ChangeEvent = new ChangeEvent() { CreatedByUserIdCode = "bha", CreatedOn = DateTime.Now, UpdatedByUserIdCode = "bha", UpdatedOn = DateTime.Now }
         };
         PageContent pageContent = null;
         if (!isRssFeed && isBlogItem)
         {
             var contentData = new BlogItemContentData()
             {
                 Title = pageName,
                 Description = "",
                 Body = pageName,
                 Excerpt = "",
                 Translation = db.ContentTranslations.Where(x => x.Language.ToLower().Equals("fo")).FirstOrDefault()
             };
             db.BlogItemContentDatas.Add(contentData);
             var pageContentInfo = new PageContentInfo()
             {
                 HasRSS = true,
                 IsDraft = false,
                 IsPublished = true,
                 PublishedBy = "bha",
                 PublishedOn = DateTime.Now
             };
             db.PageContentInfos.Add(pageContentInfo);
             var blogItemContent = new BlogItemContent()
             {
                 PageContentInfo = pageContentInfo
             };
             blogItemContent.ContentData.Add(contentData);
             db.BlogItemContents.Add(blogItemContent);
             pageContent = new PageContent()
             {
                 ViewTemplate = isBlogItem ? db.ViewTemplates.Where(x => x.Name.ToLower().Equals("_BlogItem")).FirstOrDefault() : null,
                 Page = page,
                 PageContentType = db.PageContentTypes.Where(x => x.Name.Equals("BlogItem")).FirstOrDefault(),
                 ChangeEvent = new ChangeEvent() { CreatedByUserIdCode = "bha", CreatedOn = DateTime.Now, UpdatedByUserIdCode = "bha", UpdatedOn = DateTime.Now }
             };
             if (isBlogItem)
             {
                 pageContent.BlogItemContent = blogItemContent;
             }
             db.PageContents.Add(pageContent);
         }
         if (isRssFeed && !isBlogItem)
         {
             page.IsRssFeed = true;
             pageContent = new PageContent()
             {
                 Page = page,
                 PageContentType = db.PageContentTypes.Where(x => x.Name.Equals("BlogList")).FirstOrDefault(),
                 ChangeEvent = new ChangeEvent() { CreatedByUserIdCode = "bha", CreatedOn = DateTime.Now, UpdatedByUserIdCode = "bha", UpdatedOn = DateTime.Now }
             };
             db.PageContents.Add(pageContent);
         }
         //if (isBlogItem)
         //{
         //    page.Content.Add(pageContent);
         //}
         db.Pages.Add(page);
         db.SaveChanges();
     }
 }
Example #2
0
        public ActionResult Create(BlogItemContentCreateOrEditViewModel vmObj)
        {
            //var parent = db.Pages.Where(x => x.Id == vmObj.ParentId).FirstOrDefault();
            //if (parent == null)
            //{
            //    return new EmptyResult();
            //}
            //return CreateRelatedObjectUsingViewModel<Page, PageContent>(parent.Id, vmObj, (p) =>
            //{
            //    //Convert the ViewModel to DB Object (Model)
            //    var pageContent = new PageContent()
            //    {
            //        Index = vmObj.Index,
            //        //ViewName = vmObj.ViewName,
            //        ViewTemplateId = vmObj.ViewTemplateId,
            //        PageId = vmObj.ParentId,
            //        PageContentType = db.PageContentTypes.Where(x => x.Name.Equals("BlogList")).FirstOrDefault()
            //    };
            //    db.PageContents.Add(pageContent);
            //    return pageContent;
            //});


            var parent = db.Pages.Where(x => x.Id == vmObj.ParentId).FirstOrDefault();

            if (parent == null)
            {
                return(new EmptyResult());
            }
            return(CreateRelatedObjectUsingViewModel <Page, PageContent>(parent.Id, vmObj, (p) =>
            {
                //Convert the ViewModel to DB Object (Model)
                //var translation = db.ContentTranslations.Where(x => x.Id == vmObj.ContentTranslationId).FirstOrDefault();

                var blogItemContentData = new BlogItemContentData()
                {
                    Title = vmObj.Title,
                    Body = vmObj.Body,// vmObj.BodyHtml,
                    Excerpt = vmObj.Excerpt,
                    //Keywords = vmObj.Keywords,
                    Description = vmObj.Description,
                    TranslationId = db.BlogWebsiteSettings.FirstOrDefault().DefaultLanguageId
                                    //Translation = translation
                };
                db.BlogItemContentDatas.Add(blogItemContentData);
                var pageContentInfo = new PageContentInfo()
                {
                    HasRSS = vmObj.HasRSS,
                    IsDraft = vmObj.IsDraft,
                    IsPublished = vmObj.IsPublished,
                    PublishedBy = vmObj.PublishedBy,
                    PublishedOn = vmObj.PublishedOn,
                    ActiveFrom = vmObj.ActiveFrom,
                    ActiveTo = vmObj.ActiveTo
                };
                db.PageContentInfos.Add(pageContentInfo);
                var blogItemContent = new BlogItemContent()
                {
                    Id = vmObj.Id,
                    //Title = vmObj.Title,
                    //Body = vmObj.BodyHtml,
                    //Excerpt = vmObj.Excerpt,
                    ////Keywords = vmObj.Keywords,
                    //Description = vmObj.Description,
                    PageContentInfo = pageContentInfo
                };
                blogItemContent.ContentData.Add(blogItemContentData);
                db.BlogItemContents.Add(blogItemContent);
                var pageContent = new PageContent()
                {
                    BlogItemContent = blogItemContent,
                    Index = vmObj.Index,
                    //ViewName = vmObj.ViewName,
                    ViewTemplateId = vmObj.ViewTemplateId,
                    PageId = vmObj.ParentId,
                    PageContentType = db.PageContentTypes.Where(x => x.Name.Equals("BlogItem")).FirstOrDefault()
                };
                var index = 0;
                //var files = AddFiles(vmObj.Files);
                //foreach (var file in files)
                //{
                //    db.Files.Add(file);
                //    var imageInfo = new ImageInfo()
                //    {
                //        Description = "",
                //        Height = -1,
                //        Width = -1,
                //        ChangeEvent = CreateChangeEvent()
                //    };
                //    using (FileStream fs = new FileStream(Server.MapPath(Path.Combine("~/SimpleCMS/Data/Images", file.Name)), FileMode.Open, FileAccess.Read))
                //    {
                //        using (Image img = Image.FromStream(stream: fs,
                //                                            useEmbeddedColorManagement: false,
                //                                            validateImageData: false))
                //        {
                //            float width = img.PhysicalDimension.Width;
                //            float height = img.PhysicalDimension.Height;
                //            //float hresolution = img.HorizontalResolution;
                //            //float vresolution = img.VerticalResolution;
                //            imageInfo.Width = (int)width;
                //            imageInfo.Height = (int)height;
                //        }
                //        fs.Close();
                //    }
                //    db.Images.Add(imageInfo);
                //    var pageContentFile = new PageContentFile()
                //    {
                //        PageContent = pageContent,
                //        ImageInfo = imageInfo,
                //        Index = index,
                //        ChangeEvent = CreateChangeEvent()
                //    };
                //    db.PageContentFiles.Add(pageContentFile);
                //    pageContent.BlogItemContent.Files.Add(pageContentFile);
                //    index++;
                //}
                //db.PageContents.Add(pageContent);


                var images = base.SaveImages(vmObj.Files);
                if (images != null && images.Count > 0)
                {
                    foreach (var imageInfo in images)
                    {
                        var pageContentFile = new PageContentFile()
                        {
                            ImageInfo = imageInfo,
                            Index = index,
                            PageContent = pageContent,
                            ChangeEvent = CreateChangeEvent()
                        };
                        pageContent.BlogItemContent.Files.Add(pageContentFile);
                        db.PageContents.Add(pageContent);
                        //db.SaveChanges();
                        index++;
                    }
                }

                return pageContent;
            }));
        }