Exemplo n.º 1
0
        protected virtual LinkResolverData Parse(string linkExpression)
        {
            if (TokenizeExpression(linkExpression, out var type, out var path, out var query))
            {
                if (!string.IsNullOrWhiteSpace(type) && Enum.TryParse(type, true, out LinkType linkType))
                {
                    var result = new LinkResolverData {
                        Type = linkType, Expression = string.Concat(type, ":", path)
                    };

                    switch (linkType)
                    {
                    case LinkType.Product:
                    case LinkType.Category:
                    case LinkType.Manufacturer:
                    case LinkType.Topic:
                    case LinkType.BlogPost:
                    case LinkType.NewsItem:
                        if (int.TryParse(path, out var id))
                        {
                            // Reduce thrown exceptions in console
                            result.Value = id;
                        }
                        else
                        {
                            result.Value = path;
                        }

                        result.QueryString = query;
                        break;

                    case LinkType.Url:
                        result.Value = path + (query.HasValue() ? "?" + query : "");
                        break;

                    case LinkType.File:
                        result.Value       = path;
                        result.QueryString = query;
                        break;

                    default:
                        throw new SmartException("Unknown link builder type.");
                    }

                    return(result);
                }
            }

            return(new LinkResolverData {
                Type = LinkType.Url, Value = linkExpression.EmptyNull()
            });
        }
Exemplo n.º 2
0
        internal void GetEntityData <T>(LinkResolverData data, int languageId, Expression <Func <T, ResolverEntitySummary> > selector) where T : BaseEntity
        {
            ResolverEntitySummary summary = null;
            string systemName             = null;

            if (data.Value is string)
            {
                data.Id    = 0;
                systemName = (string)data.Value;
            }
            else
            {
                data.Id = (int)data.Value;
            }

            if (data.Type == LinkType.Topic)
            {
                var query = _services.DbContext.Set <Topic>()
                            .AsNoTracking()
                            .AsQueryable();

                query = string.IsNullOrEmpty(systemName)
                    ? query.Where(x => x.Id == data.Id)
                    : query.Where(x => x.SystemName == systemName);

                summary = query.Select(x => new ResolverEntitySummary
                {
                    Id              = x.Id,
                    Name            = x.SystemName,
                    Title           = x.Title,
                    ShortTitle      = x.ShortTitle,
                    Published       = x.IsPublished,
                    SubjectToAcl    = x.SubjectToAcl,
                    LimitedToStores = x.LimitedToStores
                })
                          .FirstOrDefault();
            }
            else
            {
                summary = _services.DbContext.Set <T>()
                          .AsNoTracking()
                          .Where(x => x.Id == data.Id)
                          .Select(selector)
                          .FirstOrDefault();
            }

            if (summary != null)
            {
                var entityName = data.Type.ToString();

                data.Id              = summary.Id != 0 ? summary.Id : data.Id;
                data.SubjectToAcl    = summary.SubjectToAcl;
                data.LimitedToStores = summary.LimitedToStores;
                data.PictureId       = summary.PictureId;
                data.Status          = summary.Deleted
                    ? LinkStatus.NotFound
                    : summary.Published ? LinkStatus.Ok : LinkStatus.Hidden;

                if (data.Type == LinkType.Topic)
                {
                    data.Label = GetLocalized(data.Id, entityName, "ShortTitle", languageId, null)
                                 ?? GetLocalized(data.Id, entityName, "Title", languageId, null)
                                 ?? summary.ShortTitle.NullEmpty()
                                 ?? summary.Title.NullEmpty()
                                 ?? summary.Name;
                }
                else
                {
                    data.Label = GetLocalized(data.Id, entityName, "Name", languageId, summary.Name);
                }

                data.Slug = _urlRecordService.GetActiveSlug(data.Id, entityName, languageId).NullEmpty() ?? _urlRecordService.GetActiveSlug(data.Id, entityName, 0);
                if (!string.IsNullOrEmpty(data.Slug))
                {
                    data.Link = _urlHelper.RouteUrl(entityName, new { SeName = data.Slug });
                }
            }
            else
            {
                data.Label  = systemName;
                data.Status = LinkStatus.NotFound;
            }
        }
Exemplo n.º 3
0
        internal void GetEntityData <T>(
            LinkResolverData data,
            int storeId,
            int languageId,
            Expression <Func <T, ResolverEntitySummary> > selector) where T : BaseEntity
        {
            ResolverEntitySummary summary = null;
            string systemName             = null;

            if (data.Value is string)
            {
                data.Id    = 0;
                systemName = (string)data.Value;
            }
            else
            {
                data.Id = (int)data.Value;
            }

            if (data.Type == LinkType.Topic)
            {
                Topic topic = null;

                if (string.IsNullOrEmpty(systemName))
                {
                    topic = _services.DbContext.Set <Topic>()
                            .AsNoTracking()
                            .FirstOrDefault(x => x.Id == data.Id);
                }
                else
                {
                    topic = _topicService.GetTopicBySystemName(systemName, storeId, false);
                    data.CheckLimitedToStores = false;
                }

                if (topic != null)
                {
                    summary = new ResolverEntitySummary
                    {
                        Id              = topic.Id,
                        Name            = topic.SystemName,
                        Title           = topic.Title,
                        ShortTitle      = topic.ShortTitle,
                        Published       = topic.IsPublished,
                        SubjectToAcl    = topic.SubjectToAcl,
                        LimitedToStores = topic.LimitedToStores
                    };
                }
            }
            else
            {
                summary = _services.DbContext.Set <T>()
                          .AsNoTracking()
                          .Where(x => x.Id == data.Id)
                          .Select(selector)
                          .FirstOrDefault();
            }

            if (summary != null)
            {
                var entityName = data.Type.ToString();

                data.Id              = summary.Id != 0 ? summary.Id : data.Id;
                data.SubjectToAcl    = summary.SubjectToAcl;
                data.LimitedToStores = summary.LimitedToStores;
                data.PictureId       = summary.PictureId;
                data.Status          = summary.Deleted
                    ? LinkStatus.NotFound
                    : summary.Published ? LinkStatus.Ok : LinkStatus.Hidden;

                switch (data.Type)
                {
                case LinkType.Topic:
                    data.Label = GetLocalized(data.Id, entityName, nameof(Topic.ShortTitle), languageId, null)
                                 ?? GetLocalized(data.Id, entityName, "Title", languageId, null)
                                 ?? summary.ShortTitle.NullEmpty()
                                 ?? summary.Title.NullEmpty()
                                 ?? summary.Name;
                    break;

                case LinkType.BlogPost:
                case LinkType.NewsItem:
                    data.Label = GetLocalized(data.Id, entityName, "Title", languageId, summary.Name);
                    break;

                default:
                    data.Label = GetLocalized(data.Id, entityName, "Name", languageId, summary.Name);
                    break;
                }

                data.Slug = _urlRecordService.GetActiveSlug(data.Id, entityName, languageId).NullEmpty() ?? _urlRecordService.GetActiveSlug(data.Id, entityName, 0);
                if (!string.IsNullOrEmpty(data.Slug))
                {
                    data.Link = _urlHelper.RouteUrl(entityName, new { SeName = data.Slug });
                }
            }
            else
            {
                data.Label  = systemName;
                data.Status = LinkStatus.NotFound;
            }
        }