Exemplo n.º 1
0
        public virtual async Task <IActionResult> Edit(PostEditViewModel model)
        {
            var project = await ProjectService.GetCurrentProjectSettings();

            if (project == null)
            {
                Log.LogInformation("redirecting to index because project settings not found");

                return(RedirectToRoute(BlogRoutes.BlogIndexRouteName));
            }

            var canEdit = await User.CanEditPages(project.Id, AuthorizationService);

            if (!canEdit)
            {
                Log.LogInformation("redirecting to index because user is not allowed to edit");
                return(RedirectToRoute(BlogRoutes.BlogIndexRouteName));
            }

            if (!ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.Id))
                {
                    ViewData["Title"] = StringLocalizer["New Post"];
                }
                else
                {
                    ViewData["Title"] = string.Format(CultureInfo.CurrentUICulture, StringLocalizer["Edit - {0}"], model.Title);
                }
                model.ProjectId      = project.Id;
                model.TeasersEnabled = project.TeaserMode != TeaserMode.Off;

                return(View(model));
            }

            var categories = new List <string>();

            if (!string.IsNullOrEmpty(model.Categories))
            {
                if (ContentOptions.ForceLowerCaseCategories)
                {
                    categories = model.Categories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim().ToLower())
                                 .Where(x =>
                                        !string.IsNullOrWhiteSpace(x) &&
                                        x != ","
                                        )
                                 .Distinct()
                                 .ToList();
                }
                else
                {
                    categories = model.Categories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim())
                                 .Where(x =>
                                        !string.IsNullOrWhiteSpace(x) &&
                                        x != ","
                                        )
                                 .Distinct()
                                 .ToList();
                }
            }

            IPost post = null;

            if (!string.IsNullOrEmpty(model.Id))
            {
                post = await BlogService.GetPost(model.Id);
            }

            var    isNew = false;
            bool   slugAvailable;
            string slug;

            if (post != null)
            {
                post.Title           = model.Title;
                post.MetaDescription = model.MetaDescription;
                post.Content         = model.Content;
                post.Categories      = categories;
                if (model.Slug != post.Slug)
                {
                    // remove any bad chars
                    model.Slug    = ContentUtils.CreateSlug(model.Slug);
                    slugAvailable = await BlogService.SlugIsAvailable(project.Id, model.Slug);

                    if (slugAvailable)
                    {
                        post.Slug = model.Slug;
                    }
                    else
                    {
                        //log.LogWarning($"slug {model.Slug} was requested but not changed because it is already in use");
                        this.AlertDanger(StringLocalizer["The post slug was not changed because the requested slug is already in use."], true);
                    }
                }
            }
            else
            {
                isNew = true;
                if (!string.IsNullOrEmpty(model.Slug))
                {
                    // remove any bad chars
                    model.Slug    = ContentUtils.CreateSlug(model.Slug);
                    slug          = model.Slug;
                    slugAvailable = await BlogService.SlugIsAvailable(project.Id, slug);

                    if (!slugAvailable)
                    {
                        slug = ContentUtils.CreateSlug(model.Title);
                    }
                }
                else
                {
                    slug = ContentUtils.CreateSlug(model.Title);
                }

                slugAvailable = await BlogService.SlugIsAvailable(project.Id, slug);

                if (!slugAvailable)
                {
                    //log.LogInformation("returning 409 because slug already in use");
                    ModelState.AddModelError("postediterror", StringLocalizer["slug is already in use."]);

                    return(View(model));
                }

                post = new Post()
                {
                    BlogId          = project.Id,
                    Author          = await AuthorNameResolver.GetAuthorName(User),
                    Title           = model.Title,
                    MetaDescription = model.MetaDescription,
                    Content         = model.Content,
                    Slug            = slug
                    , Categories    = categories.ToList()
                };
            }
            if (!string.IsNullOrEmpty(model.Author))
            {
                post.Author = model.Author;
            }

            post.IsPublished    = model.IsPublished;
            post.CorrelationKey = model.CorrelationKey;
            post.ImageUrl       = model.ImageUrl;
            post.ThumbnailUrl   = model.ThumbnailUrl;
            post.IsFeatured     = model.IsFeatured;
            post.ContentType    = model.ContentType;

            post.TeaserOverride = model.TeaserOverride;
            post.SuppressTeaser = model.SuppressTeaser;

            if (!string.IsNullOrEmpty(model.PubDate))
            {
                var localTime = DateTime.Parse(model.PubDate);
                post.PubDate = TimeZoneHelper.ConvertToUtc(localTime, project.TimeZoneId);
            }

            if (isNew)
            {
                await BlogService.Create(post);
            }
            else
            {
                await BlogService.Update(post);
            }

            if (project.IncludePubDateInPostUrls)
            {
                return(RedirectToRoute(BlogRoutes.PostWithDateRouteName,
                                       new
                {
                    year = post.PubDate.Year,
                    month = post.PubDate.Month.ToString("00"),
                    day = post.PubDate.Day.ToString("00"),
                    slug = post.Slug
                }));
            }
            else
            {
                return(RedirectToRoute(BlogRoutes.PostWithoutDateRouteName,
                                       new { slug = post.Slug }));
            }
        }
Exemplo n.º 2
0
        public virtual async Task <IActionResult> Edit(PageEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.Id))
                {
                    ViewData["Title"] = StringLocalizer["New Page"];
                }
                else
                {
                    ViewData["Title"] = string.Format(CultureInfo.CurrentUICulture, StringLocalizer["Edit - {0}"], model.Title);
                }
                return(View(model));
            }

            var project = await ProjectService.GetCurrentProjectSettings();

            if (project == null)
            {
                Log.LogInformation("redirecting to index because project settings not found");

                return(RedirectToRoute(PageRoutes.PageRouteName));
            }

            var canEdit = await User.CanEditPages(project.Id, AuthorizationService);

            if (!canEdit)
            {
                Log.LogInformation("redirecting to index because user is not allowed to edit");
                return(RedirectToRoute(PageRoutes.PageRouteName));
            }

            IPage page = null;

            if (!string.IsNullOrEmpty(model.Id))
            {
                page = await PageService.GetPage(model.Id);
            }

            var    needToClearCache = false;
            var    isNew            = false;
            string slug             = string.Empty;;
            bool   slugIsAvailable  = false;

            if (page != null)
            {
                if ((!string.IsNullOrEmpty(page.ViewRoles)))
                {
                    if (!User.IsInRoles(page.ViewRoles))
                    {
                        Log.LogWarning($"page {page.Title} is protected by roles that user is not in so redirecting");
                        return(RedirectToRoute(PageRoutes.PageRouteName));
                    }
                }

                if (page.Title != model.Title)
                {
                    needToClearCache = true;
                }
                page.Title           = model.Title;
                page.MetaDescription = model.MetaDescription;
                page.Content         = model.Content;
                if (page.IsPublished != model.IsPublished)
                {
                    needToClearCache = true;
                }
                if (page.PageOrder != model.PageOrder)
                {
                    needToClearCache = true;
                }
                if (!string.IsNullOrEmpty(model.Slug))
                {
                    // remove any bad characters
                    model.Slug = ContentUtils.CreateSlug(model.Slug);
                    if (model.Slug != page.Slug)
                    {
                        slugIsAvailable = await PageService.SlugIsAvailable(model.Slug);

                        if (slugIsAvailable)
                        {
                            page.Slug        = model.Slug;
                            needToClearCache = true;
                        }
                        else
                        {
                            this.AlertDanger(StringLocalizer["The page slug was not changed because the requested slug is already in use."], true);
                        }
                    }
                }
            }
            else
            {
                isNew            = true;
                needToClearCache = true;
                if (!string.IsNullOrEmpty(model.Slug))
                {
                    // remove any bad chars
                    model.Slug      = ContentUtils.CreateSlug(model.Slug);
                    slugIsAvailable = await PageService.SlugIsAvailable(model.Slug);

                    if (slugIsAvailable)
                    {
                        slug = model.Slug;
                    }
                }

                if (string.IsNullOrEmpty(slug))
                {
                    slug = ContentUtils.CreateSlug(model.Title);
                }

                slugIsAvailable = await PageService.SlugIsAvailable(slug);

                if (!slugIsAvailable)
                {
                    model.DisqusShortname = project.DisqusShortName;
                    //log.LogInformation("returning 409 because slug already in use");
                    ModelState.AddModelError("pageediterror", StringLocalizer["slug is already in use."]);

                    return(View(model));
                }

                page = new Page()
                {
                    ProjectId       = project.Id,
                    Author          = await AuthorNameResolver.GetAuthorName(User),
                    Title           = model.Title,
                    MetaDescription = model.MetaDescription,
                    Content         = model.Content,
                    Slug            = slug,
                    ParentId        = "0"

                                      //,Categories = categories.ToList()
                };
            }


            if (!string.IsNullOrEmpty(model.ParentSlug))
            {
                var parentPage = await PageService.GetPageBySlug(model.ParentSlug);

                if (parentPage != null)
                {
                    if (parentPage.Id != page.ParentId)
                    {
                        page.ParentId    = parentPage.Id;
                        page.ParentSlug  = parentPage.Slug;
                        needToClearCache = true;
                    }
                }
            }
            else
            {
                // empty means root level
                page.ParentSlug = string.Empty;
                page.ParentId   = "0";
            }
            if (page.ViewRoles != model.ViewRoles)
            {
                needToClearCache = true;
            }
            page.ViewRoles      = model.ViewRoles;
            page.CorrelationKey = model.CorrelationKey;

            page.PageOrder     = model.PageOrder;
            page.IsPublished   = model.IsPublished;
            page.ShowHeading   = model.ShowHeading;
            page.ShowMenu      = model.ShowMenu;
            page.MenuOnly      = model.MenuOnly;
            page.DisableEditor = model.DisableEditor;
            page.ShowComments  = model.ShowComments;
            if (page.MenuFilters != model.MenuFilters)
            {
                needToClearCache = true;
            }
            page.MenuFilters = model.MenuFilters;
            if (page.ExternalUrl != model.ExternalUrl)
            {
                needToClearCache = true;
            }
            page.ExternalUrl = model.ExternalUrl;
            page.ContentType = model.ContentType;

            if (!string.IsNullOrEmpty(model.Author))
            {
                page.Author = model.Author;
            }

            if (!string.IsNullOrEmpty(model.PubDate))
            {
                var localTime = DateTime.Parse(model.PubDate);
                var pubDate   = TimeZoneHelper.ConvertToUtc(localTime, project.TimeZoneId);
                if (pubDate.Date != page.PubDate.Date)
                {
                    needToClearCache = true;
                }
                page.PubDate = pubDate;
            }
            if (page.ProjectId != project.Id)
            {
                page.ProjectId = project.Id;
            }

            if (isNew)
            {
                await PageService.Create(page, model.IsPublished);

                this.AlertSuccess(StringLocalizer["The page was created successfully."], true);
            }
            else
            {
                await PageService.Update(page, model.IsPublished);

                this.AlertSuccess(StringLocalizer["The page was updated successfully."], true);
            }


            if (needToClearCache)
            {
                PageService.ClearNavigationCache();
            }



            if (page.Slug == project.DefaultPageSlug)
            {
                return(RedirectToRoute(PageRoutes.PageRouteName, new { slug = "" }));
            }

            if (!string.IsNullOrEmpty(page.ExternalUrl))
            {
                this.AlertWarning(StringLocalizer["Note that since this page has an override url, the menu item will link to the url so the page is used only as a means to add a link in the menu, the content is not used."], true);
                return(RedirectToRoute(PageRoutes.PageEditRouteName, new { slug = page.Slug }));
            }

            //var url = Url.RouteUrl(pageRoutes.PageRouteName, new { slug = page.Slug });
            return(RedirectToRoute(PageRoutes.PageRouteName, new { slug = page.Slug }));
            //return Content(url);
        }
Exemplo n.º 3
0
        public virtual async Task <IActionResult> Edit(string slug = "", string type = "")
        {
            var projectSettings = await ProjectService.GetCurrentProjectSettings();

            if (projectSettings == null)
            {
                Log.LogInformation("redirecting to index because project settings not found");
                return(RedirectToRoute(BlogRoutes.BlogIndexRouteName));
            }

            var canEdit = await User.CanEditPages(projectSettings.Id, AuthorizationService);

            if (!canEdit)
            {
                Log.LogInformation("redirecting to index because user cannot edit");
                return(RedirectToRoute(BlogRoutes.BlogIndexRouteName));
            }

            if (slug == "none")
            {
                slug = string.Empty;
            }

            var model = new PostEditViewModel
            {
                ProjectId      = projectSettings.Id,
                TeasersEnabled = projectSettings.TeaserMode != TeaserMode.Off
            };

            PostResult postResult = null;

            if (!string.IsNullOrEmpty(slug))
            {
                postResult = await BlogService.GetPostBySlug(slug);
            }
            if (postResult == null || postResult.Post == null)
            {
                ViewData["Title"] = StringLocalizer["New Post"];
                model.Author      = await AuthorNameResolver.GetAuthorName(User);

                model.IsPublished    = true;
                model.PubDate        = TimeZoneHelper.ConvertToLocalTime(DateTime.UtcNow, projectSettings.TimeZoneId).ToString();
                model.CurrentPostUrl = Url.RouteUrl(BlogRoutes.BlogIndexRouteName);
                model.ContentType    = projectSettings.DefaultContentType;
                if (ContentOptions.AllowMarkdown && !string.IsNullOrWhiteSpace(type) && type == "markdown")
                {
                    model.ContentType = "markdown";
                }
                if (!string.IsNullOrWhiteSpace(type) && type == "html")
                {
                    model.ContentType = "html";
                }
            }
            else
            {
                ViewData["Title"]     = string.Format(CultureInfo.CurrentUICulture, StringLocalizer["Edit - {0}"], postResult.Post.Title);
                model.Author          = postResult.Post.Author;
                model.Content         = postResult.Post.Content;
                model.Id              = postResult.Post.Id;
                model.CorrelationKey  = postResult.Post.CorrelationKey;
                model.IsPublished     = postResult.Post.IsPublished;
                model.MetaDescription = postResult.Post.MetaDescription;
                model.PubDate         = TimeZoneHelper.ConvertToLocalTime(postResult.Post.PubDate, projectSettings.TimeZoneId).ToString();
                model.Slug            = postResult.Post.Slug;
                model.Title           = postResult.Post.Title;
                model.CurrentPostUrl  = await BlogService.ResolvePostUrl(postResult.Post).ConfigureAwait(false);

                model.DeletePostRouteName = BlogRoutes.PostDeleteRouteName;
                model.Categories          = string.Join(",", postResult.Post.Categories);
                model.ImageUrl            = postResult.Post.ImageUrl;
                model.ThumbnailUrl        = postResult.Post.ThumbnailUrl;
                model.IsFeatured          = postResult.Post.IsFeatured;
                model.ContentType         = postResult.Post.ContentType;
                model.TeaserOverride      = postResult.Post.TeaserOverride;
                model.SuppressTeaser      = postResult.Post.SuppressTeaser;
            }


            return(View(model));
        }
Exemplo n.º 4
0
        public virtual async Task <IActionResult> Edit(
            string slug       = "",
            string parentSlug = "",
            string type       = ""
            )
        {
            var projectSettings = await ProjectService.GetCurrentProjectSettings();

            if (projectSettings == null)
            {
                Log.LogInformation("redirecting to index because project settings not found");
                return(RedirectToRoute(PageRoutes.PageRouteName));
            }

            var canEdit = await User.CanEditPages(projectSettings.Id, AuthorizationService);

            if (!canEdit)
            {
                Log.LogInformation("redirecting to index because user cannot edit");
                return(RedirectToRoute(PageRoutes.PageRouteName));
            }

            if (slug == "none")
            {
                slug = string.Empty;
            }

            var model = new PageEditViewModel
            {
                ProjectId       = projectSettings.Id,
                DisqusShortname = projectSettings.DisqusShortName
            };

            IPage page = null;

            if (!string.IsNullOrEmpty(slug))
            {
                page = await PageService.GetPageBySlug(slug);
            }
            if (page == null)
            {
                ViewData["Title"] = StringLocalizer["New Page"];
                model.Slug        = slug;
                model.ParentSlug  = parentSlug;
                model.PageOrder   = await PageService.GetNextChildPageOrder(parentSlug);

                model.ContentType = projectSettings.DefaultContentType;
                if (EditOptions.AllowMarkdown && !string.IsNullOrWhiteSpace(type) && type == "markdown")
                {
                    model.ContentType = "markdown";
                }
                if (!string.IsNullOrWhiteSpace(type) && type == "html")
                {
                    model.ContentType = "html";
                }

                var rootList = await PageService.GetRootPages().ConfigureAwait(false);

                if (rootList.Count == 0)
                {
                    var rootPagePath = Url.RouteUrl(PageRoutes.PageRouteName);
                    // expected if home page doesn't exist yet
                    if (slug == "home" && rootPagePath == "/home")
                    {
                        model.Title = StringLocalizer["Home"];
                    }
                }
                model.Author = await AuthorNameResolver.GetAuthorName(User);

                model.PubDate = TimeZoneHelper.ConvertToLocalTime(DateTime.UtcNow, projectSettings.TimeZoneId).ToString();
            }
            else // page not null
            {
                // if the page is protected by view roles return 404 if user is not in an allowed role
                if ((!string.IsNullOrEmpty(page.ViewRoles)))
                {
                    if (!User.IsInRoles(page.ViewRoles))
                    {
                        Log.LogWarning($"page {page.Title} is protected by roles that user is not in so returning 404");
                        return(NotFound());
                    }
                }

                ViewData["Title"]     = string.Format(CultureInfo.CurrentUICulture, StringLocalizer["Edit - {0}"], page.Title);
                model.Author          = page.Author;
                model.Content         = page.Content;
                model.Id              = page.Id;
                model.CorrelationKey  = page.CorrelationKey;
                model.IsPublished     = page.IsPublished;
                model.ShowMenu        = page.ShowMenu;
                model.MenuOnly        = page.MenuOnly;
                model.MetaDescription = page.MetaDescription;
                model.PageOrder       = page.PageOrder;
                model.ParentId        = page.ParentId;
                model.ParentSlug      = page.ParentSlug;
                model.PubDate         = TimeZoneHelper.ConvertToLocalTime(page.PubDate, projectSettings.TimeZoneId).ToString();
                model.ShowHeading     = page.ShowHeading;
                model.Slug            = page.Slug;
                model.ExternalUrl     = page.ExternalUrl;
                model.Title           = page.Title;
                model.MenuFilters     = page.MenuFilters;
                model.ViewRoles       = page.ViewRoles;
                model.ShowComments    = page.ShowComments;
                model.DisableEditor   = page.DisableEditor;
                model.ContentType     = page.ContentType;
            }


            return(View(model));
        }