Esempio n. 1
0
        protected override DriverResult Editor(AutoroutePart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var settings = part.TypePartDefinition.Settings.GetModel <AutorouteSettings>();

            // if the content type has no pattern for autoroute, then use a default one
            if (!settings.Patterns.Any())
            {
                settings.AllowCustomPattern        = true;
                settings.AutomaticAdjustmentOnEdit = false;
                settings.DefaultPatternIndex       = 0;
                settings.Patterns = new List <RoutePattern> {
                    new RoutePattern {
                        Name = "Title", Description = "my-title", Pattern = "{Content.Slug}"
                    }
                };

                _notifier.Warning(T("No route patterns are currently defined for this Content Type. If you don't set one in the settings, a default one will be used."));
            }

            var viewModel = new AutoroutePartEditViewModel {
                CurrentUrl = part.DisplayAlias,
                Settings   = settings
            };

            // retrieve home page
            var homepage           = _aliasService.Get(string.Empty);
            var displayRouteValues = _contentManager.GetItemMetadata(part).DisplayRouteValues;

            if (homepage.Match(displayRouteValues))
            {
                viewModel.PromoteToHomePage = true;
            }

            if (settings.PerItemConfiguration)
            {
                // if enabled, the list of all available patterns is displayed, and the user can
                // select which one to use

                // todo: later
            }

            var previous = part.DisplayAlias;

            if (updater != null && updater.TryUpdateModel(viewModel, Prefix, null, null))
            {
                // remove any trailing slash in the permalink
                while (!string.IsNullOrEmpty(viewModel.CurrentUrl) && viewModel.CurrentUrl.StartsWith("/"))
                {
                    viewModel.CurrentUrl = viewModel.CurrentUrl.Substring(1);
                }

                part.DisplayAlias = viewModel.CurrentUrl;

                // reset the alias if we need to force regeneration, and the user didn't provide a custom one
                if (settings.AutomaticAdjustmentOnEdit && previous == part.DisplayAlias)
                {
                    part.DisplayAlias = string.Empty;
                }

                if (!_autorouteService.IsPathValid(part.DisplayAlias))
                {
                    updater.AddModelError("CurrentUrl", T("Please do not use any of the following characters in your permalink: \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\", \", \"<\", \">\", \"\\\", \"|\", \"%\", \".\". No spaces are allowed (please use dashes or underscores instead)."));
                }

                // if CurrentUrl is set, the handler won't try to create an alias for it
                // but instead keep the value

                // if home page is requested, use "/" to have the handler create a homepage alias
                if (viewModel.PromoteToHomePage)
                {
                    part.DisplayAlias = "/";
                }
            }

            var usr = _orchardServices.WorkContext.CurrentUser;
            var r   = usr.As <UserRolesPart>();

            if (r.Roles.Any(p => p == "vendedor"))
            {
                viewModel.FixedPart  = string.Concat("tiendas/", usr.UserName, "/");
                viewModel.CurrentUrl = viewModel.CurrentUrl.Replace(viewModel.FixedPart, "");
            }



            return(ContentShape("Parts_Autoroute_Edit",
                                () => shapeHelper.EditorTemplate(TemplateName: "Parts.Autoroute.Edit", Model: viewModel, Prefix: Prefix)));
        }
Esempio n. 2
0
        protected override DriverResult Editor(AutoroutePart part, IUpdateModel updater, dynamic shapeHelper)
        {
            var settings    = part.TypePartDefinition.Settings.GetModel <AutorouteSettings>();
            var itemCulture = _cultureManager.GetSiteCulture();

            // If we are editing an existing content item, check to see if we are an ILocalizableAspect so we can use its culture for alias generation.
            if (part.Record.Id != 0)
            {
                var localizableAspect = part.As <ILocalizableAspect>();

                if (localizableAspect != null)
                {
                    itemCulture = localizableAspect.Culture;
                }
            }

            if (settings.UseCulturePattern)
            {
                // Hack: if the LocalizedPart is attached to the content item, it will submit the following form value,
                // which we use to determine what culture to use for alias generation.
                var context = _httpContextAccessor.Current();
                if (!String.IsNullOrEmpty(context.Request.Form["Localization.SelectedCulture"]))
                {
                    itemCulture = context.Request.Form["Localization.SelectedCulture"].ToString();
                }
            }

            // We update the settings assuming that when
            // pattern culture = null or "" it means culture = default website culture
            // for patterns that we migrated.
            foreach (var pattern in settings.Patterns.Where(x => String.IsNullOrWhiteSpace(x.Culture)))
            {
                pattern.Culture = _cultureManager.GetSiteCulture();;
            }

            // We do the same for default patterns.
            foreach (var pattern in settings.DefaultPatterns.Where(x => String.IsNullOrWhiteSpace(x.Culture)))
            {
                pattern.Culture = _cultureManager.GetSiteCulture();
            }

            // If the content type has no pattern for autoroute, then use a default one.
            if (!settings.Patterns.Any(x => String.Equals(x.Culture, itemCulture, StringComparison.OrdinalIgnoreCase)))
            {
                settings.Patterns = new List <RoutePattern> {
                    new RoutePattern {
                        Name = "Title", Description = "my-title", Pattern = "{Content.Slug}", Culture = itemCulture
                    }
                };
            }

            // If the content type has no defaultPattern for autoroute, then use a default one.
            if (!settings.DefaultPatterns.Any(x => String.Equals(x.Culture, itemCulture, StringComparison.OrdinalIgnoreCase)))
            {
                // If we are in the default culture, check the old setting.
                if (String.Equals(itemCulture, _cultureManager.GetSiteCulture(), StringComparison.OrdinalIgnoreCase))
                {
                    if (!String.IsNullOrWhiteSpace(settings.DefaultPatternIndex))
                    {
                        var patternIndex = settings.DefaultPatternIndex;
                        settings.DefaultPatterns.Add(new DefaultPattern {
                            PatternIndex = patternIndex, Culture = itemCulture
                        });
                    }
                    else
                    {
                        settings.DefaultPatterns.Add(new DefaultPattern {
                            PatternIndex = "0", Culture = itemCulture
                        });
                    }
                }
                else
                {
                    settings.DefaultPatterns.Add(new DefaultPattern {
                        PatternIndex = "0", Culture = itemCulture
                    });
                }
            }

            var viewModel = new AutoroutePartEditViewModel {
                CurrentUrl     = part.DisplayAlias,
                Settings       = settings,
                CurrentCulture = itemCulture
            };

            // Retrieve home page.
            var homePageId = _homeAliasService.GetHomePageId(VersionOptions.Latest);
            var isHomePage = part.Id == homePageId;

            viewModel.IsHomePage        = isHomePage;
            viewModel.PromoteToHomePage = part.PromoteToHomePage;

            if (settings.PerItemConfiguration)
            {
                // If enabled, the list of all available patterns is displayed, and the user can select which one to use.
                // todo: later
            }

            var previous = part.DisplayAlias;

            if (updater != null && updater.TryUpdateModel(viewModel, Prefix, null, null))
            {
                // Remove any leading slash in the permalink.
                if (viewModel.CurrentUrl != null)
                {
                    viewModel.CurrentUrl = viewModel.CurrentUrl.TrimStart('/');
                }

                part.DisplayAlias = viewModel.CurrentUrl;

                // Reset the alias if we need to force regeneration, and the user didn't provide a custom one.
                if (settings.AutomaticAdjustmentOnEdit && previous == part.DisplayAlias)
                {
                    part.DisplayAlias = String.Empty;
                }

                if (!_autorouteService.IsPathValid(part.DisplayAlias))
                {
                    updater.AddModelError("CurrentUrl", T("Please do not use any of the following characters in your permalink: \":\", \"?\", \"#\", \"[\", \"]\", \"@\", \"!\", \"$\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \";\", \"=\", \", \"<\", \">\", \"\\\", \"|\", \"%\", \".\". No spaces are allowed (please use dashes or underscores instead)."));
                }

                if (part.DisplayAlias != null && part.DisplayAlias.Length > 1850)
                {
                    updater.AddModelError("CurrentUrl", T("Your permalink is too long. The permalink can only be up to 1,850 characters."));
                }

                // Mark the content item to be the homepage. Once this content isp ublished, the home alias will be updated to point to this content item.
                part.PromoteToHomePage = viewModel.PromoteToHomePage;
            }

            return(ContentShape("Parts_Autoroute_Edit",
                                () => shapeHelper.EditorTemplate(TemplateName: "Parts.Autoroute.Edit", Model: viewModel, Prefix: Prefix)));
        }