public ActionResult Create()
 {
     PageCreateOrEditModel model = new PageCreateOrEditModel
         {
             AvailableCategories = GetDefaultSelectCategoriesList()
         };
     model.AvailableCategories.AddRange(GetAvailableCategoriesList());
     return View(model);
 }
 public ActionResult Create(PageCreateOrEditModel model)
 {
     if (ModelState.IsValid)
     {
         model.Url = model.Url.ReplaceWhiteSpacesWithHyphens().ToLower();
         if (!Context.Pages.Any(p => p.Url.ToLower() == model.Url.ToLower()))
         {
             Page newPage = CreatePageByModel(model);
             Context.Pages.Add(newPage);
             Context.SaveChanges();
             PageIndexHelper.AddOrUpdate(newPage);
             return RedirectToAction("AllPages");
         }
         ModelState.AddModelError("Url", Config.UrlErrorMessage);
     }
     model = RefillFailedPageModel(model);
     return View(model);
 }
 // TODO: refactor
 Object ICloneable.Clone()
 {
     PageCreateOrEditModel clone = new PageCreateOrEditModel
     {
         Id = this.Id,
         Url = this.Url,
         Title = this.Title,
         Description = this.Description,
         CategoryId = this.CategoryId,
         ContentsMarkup = this.ContentsMarkup,
         AvailableCategories = this.AvailableCategories,
         IsLargeSlideshowEnabled = this.IsLargeSlideshowEnabled,
         IsSmallSlideshowEnabled = this.IsSmallSlideshowEnabled,
         LargeSlideshowImages = this.LargeSlideshowImages,
         SmallSlideshowImages = this.SmallSlideshowImages,
         IsDisplayedInLatestPosts = this.IsDisplayedInLatestPosts,
         IsPublic = this.IsPublic
     };
     return clone;
 }
        private void UpdatePage(Page page, PageCreateOrEditModel model)
        {
            foreach (Image image in model.LargeSlideshowImages)
            {
                if (Context.Images.Local.All(i => i.Id != image.Id))
                {
                    Context.Images.Attach(image);
                }
            }
            foreach (Image image in model.SmallSlideshowImages)
            {
                if (Context.Images.Local.All(i => i.Id != image.Id))
                {
                    Context.Images.Attach(image);
                }
            }
            page.Title = model.Title;
            page.IsDisplayedInLatestPosts = model.IsDisplayedInLatestPosts;
            page.IsPublic = model.IsPublic;
            page.Description = model.Description;
            page.ContentsMarkup = model.ContentsMarkup;
            page.CreateDate = DateTime.Now;
            page.CategoryId = model.CategoryId > 0 ?
                model.CategoryId :
                default(int?);
            page.IsLargeSlideshowEnabled = model.IsLargeSlideshowEnabled;
            page.IsSmallSlideshowEnabled = model.IsSmallSlideshowEnabled;

            List<PageLargeSlideshowImage> largeSlideshowImages = page.
                PageLargeSlideshowImages.ToList();
            foreach (PageLargeSlideshowImage largeSlideshowImage in largeSlideshowImages)
            {
                Context.PageLargeSlideshowImages.Remove(largeSlideshowImage);
            }
            List<PageSmallSlideshowImage> smallSlideshowImages = page.
                PageSmallSlideshowImages.ToList();
            foreach (PageSmallSlideshowImage smallSlideshowImage in smallSlideshowImages)
            {
                Context.PageSmallSlideshowImages.Remove(smallSlideshowImage);
            }

            foreach (Image image in model.LargeSlideshowImages)
            {
                page.PageLargeSlideshowImages.Add(new PageLargeSlideshowImage
                    {
                        Image = Context.Images.
                            Local.Single(li => li.Id == image.Id),
                        Page = page
                    });
            }
            foreach (Image image in model.SmallSlideshowImages)
            {
                page.PageSmallSlideshowImages.Add(new PageSmallSlideshowImage
                    {
                        Image = Context.Images.
                            Local.Single(li => li.Id == image.Id),
                        Page = page
                    });
            }
            Context.SaveChanges();
        }
 private PageCreateOrEditModel RefillFailedPageModel(PageCreateOrEditModel failedModel)
 {
     PageCreateOrEditModel updatedModel = (PageCreateOrEditModel)((ICloneable) failedModel).Clone();
     if (failedModel.Id > default(int))
     {
         updatedModel.Url = Context.Pages.Single(p => p.Id == failedModel.Id).Url;
     }
     updatedModel.AvailableCategories = GetDefaultSelectCategoriesList();
     updatedModel.AvailableCategories.AddRange(GetAvailableCategoriesList());
     updatedModel.LargeSlideshowImages = updatedModel.LargeSlideshowImages.
         Select(pi => Context.Images.Single(i => i.Id == pi.Id)).
         ToList();
     updatedModel.SmallSlideshowImages = updatedModel.SmallSlideshowImages.
         Select(pi => Context.Images.Single(i => i.Id == pi.Id)).
         ToList();
     return updatedModel;
 }
        private Page CreatePageByModel(PageCreateOrEditModel model)
        {
            foreach (Image image in model.LargeSlideshowImages)
            {
                if (Context.Images.Local.All(i => i.Id != image.Id))
                {
                    Context.Images.Attach(image);
                }
            }
            foreach (Image image in model.SmallSlideshowImages)
            {
                if (Context.Images.Local.All(i => i.Id != image.Id))
                {
                    Context.Images.Attach(image);
                }
            }
            Page newPage = new Page
                    {
                        Title = model.Title,
                        Url = model.Url,
                        IsPublic = model.IsPublic,
                        IsDisplayedInLatestPosts = model.IsDisplayedInLatestPosts,
                        Description = model.Description,
                        ContentsMarkup = model.ContentsMarkup,
                        CreateDate = DateTime.Now,
                        CategoryId = model.CategoryId > 0 ? model.CategoryId : default(int?),
                        IsLargeSlideshowEnabled = model.IsLargeSlideshowEnabled,
                        IsSmallSlideshowEnabled = model.IsSmallSlideshowEnabled
                    };
            foreach (Image image in model.LargeSlideshowImages)
            {
                newPage.PageLargeSlideshowImages.Add(new PageLargeSlideshowImage
                    {
                        Image = Context.Images.
                            Local.Single(li => li.Id == image.Id),
                        Page = newPage
                    });
            }
            foreach (Image image in model.SmallSlideshowImages)
            {
                newPage.PageSmallSlideshowImages.Add(new PageSmallSlideshowImage
                    {
                        Image = Context.Images.
                            Local.Single(li => li.Id == image.Id),
                        Page = newPage
                    });
            }

            return newPage;
        }
 public ActionResult Edit(PageCreateOrEditModel model, String returnUrl)
 {
     Page page = Context.Pages.SingleOrDefault(x => x.Id == model.Id);
     if (page == null)
     {
         return RedirectToRoute("HttpError404");
     }
     if (ModelState.IsValid)
     {
         UpdatePage(page, model);
         PageIndexHelper.AddOrUpdate(page);
         if (Url.IsLocalUrl(returnUrl))
         {
             return Redirect(returnUrl);
         }
         return RedirectToAction("AllPages");
     }
     model = RefillFailedPageModel(model);
     return View(model);
 }