protected void MapToLinkModel(Link link, InternalLinkField field, SitecoreFieldConfiguration config)
        {
            var urlOptions = _urlOptionsResolver.CreateUrlOptions(config.UrlOptions);

            link.Url      = field.TargetItem == null ? string.Empty : LinkManager.GetItemUrl(field.TargetItem, urlOptions);
            link.Type     = LinkType.Internal;
            link.TargetId = field.TargetItem == null ? Guid.Empty : field.TargetItem.ID.Guid;
            link.Text     = field.TargetItem == null ? string.Empty : field.TargetItem.DisplayName;
        }
Esempio n. 2
0
        /// <summary>
        /// Sets up the data mapper for a particular property
        /// </summary>
        /// <param name="args">The args.</param>
        public override void Setup(DataMapperResolverArgs args)
        {
            var scConfig = args.PropertyConfiguration as SitecoreInfoConfiguration;

            if (scConfig == null)
            {
                throw new NullReferenceException("Configuration has not been set.");
            }

            ReadOnly = scConfig.Type != SitecoreInfoType.DisplayName && scConfig.Type != SitecoreInfoType.Name;


            switch (scConfig.Type)
            {
            case SitecoreInfoType.ContentPath:
                _getValue = item => item.Paths.ContentPath;
                break;

            case SitecoreInfoType.DisplayName:
                _getValue = item => item[Global.Fields.DisplayName];
                break;

            case SitecoreInfoType.FullPath:
                _getValue = item => item.Paths.FullPath;
                break;

            case SitecoreInfoType.Name:
                _getValue = item => item.Name;
                break;

            case SitecoreInfoType.Key:
                _getValue = item => item.Key;
                break;

            case SitecoreInfoType.MediaUrl:
                _getValue = item =>
                {
                    var mediaUrlOptions = _mediaUrlOptionsResolver.GetMediaUrlOptions(scConfig.MediaUrlOptions);
                    var media           = new MediaItem(item);
                    return(MediaManager.GetMediaUrl(media, mediaUrlOptions));
                };
                break;

            case SitecoreInfoType.Path:
                _getValue = item => item.Paths.Path;
                break;

            case SitecoreInfoType.TemplateId:
                _getValue = item =>
                {
                    if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(ID))
                    {
                        return(item.TemplateID);
                    }
                    return(item.TemplateID.Guid);
                };
                break;

            case SitecoreInfoType.TemplateName:
                _getValue = item => item.TemplateName;
                break;

            case SitecoreInfoType.Url:
                _getValue = item =>
                {
                    var urlOptions = _urlOptionsResolver.CreateUrlOptions(scConfig.UrlOptions);
                    if (UrlOptionsResolver.FlagCheck(scConfig.UrlOptions, SitecoreInfoUrlOptions.UseItemLanguage))
                    {
                        urlOptions.Language = item.Language;
                    }
                    else
                    {
                        urlOptions.Language = null;
                    }
                    return(LinkManager.GetItemUrl(item, urlOptions));
                };
                break;

            case SitecoreInfoType.Version:
                _getValue = item =>
                {
                    if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string))
                    {
                        return(item.Version.Number.ToString());
                    }
                    return(item.Version.Number);
                };
                break;

            case SitecoreInfoType.Language:
                _getValue = item =>
                {
                    if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string))
                    {
                        return(item.Language.Name);
                    }
                    return(item.Language);
                };
                break;

            case SitecoreInfoType.BaseTemplateIds:
                _getValue = item =>
                {
                    Template template = TemplateManager.GetTemplate(item.TemplateID, item.Database);
                    if (scConfig.PropertyInfo != null &&
                        scConfig.PropertyInfo.PropertyType == typeof(IEnumerable <ID>))
                    {
                        return(template.GetBaseTemplates().Select(x => x.ID));
                    }
                    return(template.GetBaseTemplates().Select(x => x.ID.Guid));
                };
                break;

            case SitecoreInfoType.ItemUri:
                _getValue = item => new ItemUri(item.ID, item.Language, item.Version, item.Database);
                break;

#if (SC81 || SC82 || SC90)
            case SitecoreInfoType.OriginalLanguage:
                _getValue = item => item.OriginalLanguage;
                break;

            case SitecoreInfoType.OriginatorId:
                _getValue = item => item.OriginatorId;
                break;
#endif
            default:
                throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type));
            }


            base.Setup(args);
        }
        /// <summary>
        /// Maps data from the CMS value to the .Net property value
        /// </summary>
        /// <param name="mappingContext">The mapping context.</param>
        /// <returns>System.Object.</returns>
        /// <exception cref="Glass.Mapper.MapperException">SitecoreInfoType {0} not supported.Formatted(scConfig.Type)</exception>
        public override object MapToProperty(AbstractDataMappingContext mappingContext)
        {
            var context = mappingContext as SitecoreDataMappingContext;

            if (context == null)
            {
                throw new NullReferenceException("Mapping Context has not been set.");
            }

            var item     = context.Item;
            var scConfig = Configuration as SitecoreInfoConfiguration;

            if (scConfig == null)
            {
                throw new NullReferenceException("Configuration has not been set.");
            }
            //TODO: move this to the config?
            switch (scConfig.Type)
            {
            case SitecoreInfoType.ContentPath:
                return(item.Paths.ContentPath);

            case SitecoreInfoType.DisplayName:
                return(item[Global.Fields.DisplayName]);

            case SitecoreInfoType.FullPath:
                return(item.Paths.FullPath);

            case SitecoreInfoType.Name:
                return(item.Name);

            case SitecoreInfoType.Key:
                return(item.Key);

            case SitecoreInfoType.MediaUrl:
                var mediaUrlOptions = _mediaUrlOptionsResolver.GetMediaUrlOptions(scConfig.MediaUrlOptions);
                var media           = new MediaItem(item);
                return(MediaManager.GetMediaUrl(media, mediaUrlOptions));

            case SitecoreInfoType.Path:
                return(item.Paths.Path);

            case SitecoreInfoType.TemplateId:
                if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(ID))
                {
                    return(item.TemplateID);
                }
                return(item.TemplateID.Guid);

            case SitecoreInfoType.TemplateName:
                return(item.TemplateName);

            case SitecoreInfoType.Url:
                var urlOptions = _urlOptionsResolver.CreateUrlOptions(scConfig.UrlOptions);
                urlOptions.Language = null;
                return(LinkManager.GetItemUrl(item, urlOptions));

            case SitecoreInfoType.Version:
                if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string))
                {
                    return(item.Version.Number.ToString());
                }
                return(item.Version.Number);

            case SitecoreInfoType.Language:
                if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string))
                {
                    return(item.Language.Name);
                }
                return(item.Language);

            case SitecoreInfoType.BaseTemplateIds:
                Template template = TemplateManager.GetTemplate(item.TemplateID, item.Database);
                if (scConfig.PropertyInfo != null &&
                    scConfig.PropertyInfo.PropertyType == typeof(IEnumerable <ID>))
                {
                    return(template.GetBaseTemplates().Select(x => x.ID));
                }
                return(template.GetBaseTemplates().Select(x => x.ID.Guid));

            case SitecoreInfoType.ItemUri:
                return(new ItemUri(item.ID, item.Language, item.Version, item.Database));

#if (SC81 || SC82)
            case SitecoreInfoType.OriginalLanguage:
                return(item.OriginalLanguage);

            case SitecoreInfoType.OriginatorId:
                return(item.OriginatorId);
#endif
            default:
                throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type));
            }
        }
        /// <summary>
        /// Gets the field.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.Object.</returns>
        public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
        {
            if (field == null || field.Value.Trim().IsNullOrEmpty())
            {
                return(null);
            }

            Link      link      = new Link();
            LinkField linkField = new LinkField(field);

            link.Anchor = linkField.Anchor;
            link.Class  = linkField.Class;
            link.Text   = linkField.Text;
            link.Title  = linkField.Title;
            link.Target = linkField.Target;
            link.Query  = linkField.QueryString;

            switch (linkField.LinkType)
            {
            case "anchor":
                link.Url  = linkField.Anchor;
                link.Type = LinkType.Anchor;
                break;

            case "external":
                link.Url  = linkField.Url;
                link.Type = LinkType.External;
                break;

            case "mailto":
                link.Url  = linkField.Url;
                link.Type = LinkType.MailTo;
                break;

            case "javascript":
                link.Url  = linkField.Url;
                link.Type = LinkType.JavaScript;
                break;

            case "media":
                if (linkField.TargetItem == null)
                {
                    link.Url = string.Empty;
                }
                else
                {
                    global::Sitecore.Data.Items.MediaItem media =
                        new global::Sitecore.Data.Items.MediaItem(linkField.TargetItem);
                    link.Url = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                }
                link.Type     = LinkType.Media;
                link.TargetId = linkField.TargetID.Guid;
                break;

            case "internal":
                var urlOptions = _urlOptionsResolver.CreateUrlOptions(config.UrlOptions);
                link.Url      = linkField.TargetItem == null ? string.Empty : LinkManager.GetItemUrl(linkField.TargetItem, urlOptions);
                link.Type     = LinkType.Internal;
                link.TargetId = linkField.TargetID.Guid;
                link.Text     = linkField.Text.IsNullOrEmpty() ? (linkField.TargetItem == null ? string.Empty : linkField.TargetItem.DisplayName) : linkField.Text;
                break;

            default:
                return(null);
            }



            return(link);
        }