public override void SetValue(global::Sitecore.Data.Items.Item item, object value, ISitecoreService service)
        {

            Link link = value as Link;

            var itemField = base.GetField(item);

            if (itemField == null) return;

            LinkField field = new LinkField(itemField);
            if (link == null || link.Type == LinkType.NotSet)
            {
                field.Clear();
                return;
            }


            switch (link.Type)
            {
                case LinkType.Internal:
                    field.LinkType = "internal";
                    if (field.TargetID.Guid != link.TargetId)
                    {
                        if (link.TargetId == Guid.Empty)
                        {
                            ItemLink iLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, field.TargetItem.Database.Name, field.TargetID, field.TargetItem.Paths.FullPath);
                            field.RemoveLink(iLink);
                        }
                        else
                        {
                            ID newId = new ID(link.TargetId);
                            Item target = item.Database.GetItem(newId);
                            if (target != null)
                            {
                                field.TargetID = newId;
                                ItemLink nLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                                field.UpdateLink(nLink);
                                field.Url = LinkManager.GetItemUrl(target);
                            }
                            else throw new MapperException("No item with ID {0}. Can not update Link field".Formatted(newId));
                        }

                    }
                    break;
                case LinkType.Media:
                    field.LinkType = "media";
                    if (field.TargetID.Guid != link.TargetId)
                    {
                        if (link.TargetId == Guid.Empty)
                        {
                            ItemLink iLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, field.TargetItem.Database.Name, field.TargetID, field.TargetItem.Paths.FullPath);
                            field.RemoveLink(iLink);
                        }
                        else
                        {
                            ID newId = new ID(link.TargetId);
                            Item target = item.Database.GetItem(newId);
                            global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(target);

                            if (target != null)
                            {
                                field.TargetID = newId;
                                ItemLink nLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                                field.UpdateLink(nLink);
                                field.Url = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                            }
                            else throw new MapperException("No item with ID {0}. Can not update Link field".Formatted(newId));
                        }

                    }
                    break;
                case LinkType.External:
                    field.LinkType = "external";
                    field.Url = link.Url;
                    break;
                case LinkType.Anchor:
                    field.LinkType = "anchor";
                    field.Url = link.Anchor;
                    break;
                case LinkType.MailTo:
                    field.LinkType = "mailto";
                    field.Url = link.Url;
                    break;
                case LinkType.JavaScript:
                    field.LinkType = "javascript";
                    field.Url = link.Url;
                    break;


            }



            if (!link.Anchor.IsNullOrEmpty())
                field.Anchor = link.Anchor;
            if (!link.Class.IsNullOrEmpty())
                field.Class = link.Class;
            if (!link.Text.IsNullOrEmpty())
                field.Text = link.Text;
            if (!link.Title.IsNullOrEmpty())
                field.Title = link.Title;
            if (!link.Query.IsNullOrEmpty())
                field.QueryString = link.Query;
            if (!link.Target.IsNullOrEmpty())
                field.Target = link.Target;


        }
示例#2
0
        public override void SetValue(global::Sitecore.Data.Items.Item item, object value, ISitecoreService service)
        {
            Link link = value as Link;

            var itemField = base.GetField(item);

            if (itemField == null)
            {
                return;
            }

            LinkField field = new LinkField(itemField);

            if (link == null || link.Type == LinkType.NotSet)
            {
                field.Clear();
                return;
            }


            switch (link.Type)
            {
            case LinkType.Internal:
                field.LinkType = "internal";
                if (field.TargetID.Guid != link.TargetId)
                {
                    if (link.TargetId == Guid.Empty)
                    {
                        ItemLink iLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, field.TargetItem.Database.Name, field.TargetID, field.TargetItem.Paths.FullPath);
                        field.RemoveLink(iLink);
                    }
                    else
                    {
                        ID   newId  = new ID(link.TargetId);
                        Item target = item.Database.GetItem(newId);
                        if (target != null)
                        {
                            field.TargetID = newId;
                            ItemLink nLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                            field.UpdateLink(nLink);
                            field.Url = LinkManager.GetItemUrl(target);
                        }
                        else
                        {
                            throw new MapperException("No item with ID {0}. Can not update Link field".Formatted(newId));
                        }
                    }
                }
                break;

            case LinkType.Media:
                field.LinkType = "media";
                if (field.TargetID.Guid != link.TargetId)
                {
                    if (link.TargetId == Guid.Empty)
                    {
                        ItemLink iLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, field.TargetItem.Database.Name, field.TargetID, field.TargetItem.Paths.FullPath);
                        field.RemoveLink(iLink);
                    }
                    else
                    {
                        ID   newId  = new ID(link.TargetId);
                        Item target = item.Database.GetItem(newId);
                        global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(target);

                        if (target != null)
                        {
                            field.TargetID = newId;
                            ItemLink nLink = new ItemLink(item.Database.Name, item.ID, field.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                            field.UpdateLink(nLink);
                            field.Url = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                        }
                        else
                        {
                            throw new MapperException("No item with ID {0}. Can not update Link field".Formatted(newId));
                        }
                    }
                }
                break;

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

            case LinkType.Anchor:
                field.LinkType = "anchor";
                field.Url      = link.Anchor;
                break;

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

            case LinkType.JavaScript:
                field.LinkType = "javascript";
                field.Url      = link.Url;
                break;
            }



            if (!link.Anchor.IsNullOrEmpty())
            {
                field.Anchor = link.Anchor;
            }
            if (!link.Class.IsNullOrEmpty())
            {
                field.Class = link.Class;
            }
            if (!link.Text.IsNullOrEmpty())
            {
                field.Text = link.Text;
            }
            if (!link.Title.IsNullOrEmpty())
            {
                field.Title = link.Title;
            }
            if (!link.Query.IsNullOrEmpty())
            {
                field.QueryString = link.Query;
            }
            if (!link.Target.IsNullOrEmpty())
            {
                field.Target = link.Target;
            }
        }
        protected void MapToLinkField(Link link, LinkField linkField, SitecoreFieldConfiguration config)
        {
            var item = linkField.InnerField.Item;

            switch (link.Type)
            {
            case LinkType.Internal:
                linkField.LinkType = "internal";
                if (linkField.TargetID.Guid != link.TargetId)
                {
                    if (link.TargetId == Guid.Empty)
                    {
                        ItemLink iLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, linkField.TargetItem.Database.Name, linkField.TargetID, linkField.TargetItem.Paths.FullPath);
                        linkField.RemoveLink(iLink);
                    }
                    else
                    {
                        ID   newId  = new ID(link.TargetId);
                        Item target = item.Database.GetItem(newId);
                        if (target != null)
                        {
                            linkField.TargetID = newId;
                            ItemLink nLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                            linkField.UpdateLink(nLink);
                            linkField.Url = LinkManager.GetItemUrl(target);
                        }
                        else
                        {
                            throw new MapperException("No item with ID {0}. Can not update Link linkField".Formatted(newId));
                        }
                    }
                }
                break;

            case LinkType.Media:
                linkField.LinkType = "media";
                if (linkField.TargetID.Guid != link.TargetId)
                {
                    if (link.TargetId == Guid.Empty)
                    {
                        ItemLink iLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, linkField.TargetItem.Database.Name, linkField.TargetID, linkField.TargetItem.Paths.FullPath);
                        linkField.RemoveLink(iLink);
                    }
                    else
                    {
                        ID   newId  = new ID(link.TargetId);
                        Item target = item.Database.GetItem(newId);

                        if (target != null)
                        {
                            global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(target);

                            linkField.TargetID = newId;
                            ItemLink nLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                            linkField.UpdateLink(nLink);
                            var mediaUrl = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                            linkField.Url = mediaUrl;
                        }
                        else
                        {
                            throw new MapperException("No item with ID {0}. Can not update Link linkField".Formatted(newId));
                        }
                    }
                }
                break;

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

            case LinkType.Anchor:
                linkField.LinkType = "anchor";
                linkField.Url      = link.Anchor;
                break;

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

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



            if (!link.Anchor.IsNullOrEmpty())
            {
                linkField.Anchor = link.Anchor;
            }
            if (!link.Class.IsNullOrEmpty())
            {
                linkField.Class = link.Class;
            }
            if (!link.Text.IsNullOrEmpty())
            {
                linkField.Text = link.Text;
            }
            if (!link.Title.IsNullOrEmpty())
            {
                linkField.Title = link.Title;
            }
            if (!link.Query.IsNullOrEmpty())
            {
                linkField.QueryString = HttpUtility.UrlEncode(link.Query);
            }
            if (!link.Target.IsNullOrEmpty())
            {
                linkField.Target = link.Target;
            }
        }
        /// <summary>
        /// Sets the field.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="value">The value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <exception cref="Glass.Mapper.MapperException">
        /// No item with ID {0}. Can not update Link linkField.Formatted(newId)
        /// or
        /// No item with ID {0}. Can not update Link linkField.Formatted(newId)
        /// </exception>
        public override void SetField(Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
        {
            Link link = value as Link;
            

            if (field == null) return;

            var item = field.Item;

            LinkField linkField = new LinkField(field);
            if (link == null || link.Type == LinkType.NotSet)
            {
                linkField.Clear();
                return;
            }


            switch (link.Type)
            {
                case LinkType.Internal:
                    linkField.LinkType = "internal";
                    if (linkField.TargetID.Guid != link.TargetId)
                    {
                        if (link.TargetId == Guid.Empty)
                        {
                            ItemLink iLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, linkField.TargetItem.Database.Name, linkField.TargetID, linkField.TargetItem.Paths.FullPath);
                            linkField.RemoveLink(iLink);
                        }
                        else
                        {
                            ID newId = new ID(link.TargetId);
                            Item target = item.Database.GetItem(newId);
                            if (target != null)
                            {
                                linkField.TargetID = newId;
                                ItemLink nLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                                linkField.UpdateLink(nLink);
                                linkField.Url = LinkManager.GetItemUrl(target);
                            }
                            else throw new MapperException("No item with ID {0}. Can not update Link linkField".Formatted(newId));
                        }

                    }
                    break;
                case LinkType.Media:
                    linkField.LinkType = "media";
                    if (linkField.TargetID.Guid != link.TargetId)
                    {
                        if (link.TargetId == Guid.Empty)
                        {
                            ItemLink iLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, linkField.TargetItem.Database.Name, linkField.TargetID, linkField.TargetItem.Paths.FullPath);
                            linkField.RemoveLink(iLink);
                        }
                        else
                        {
                            ID newId = new ID(link.TargetId);
                            Item target = item.Database.GetItem(newId);

                            if (target != null)
                            {
                                global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(target);

                                linkField.TargetID = newId;
                                ItemLink nLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                                linkField.UpdateLink(nLink);
                                var mediaUrl = global::Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);
                                linkField.Url = mediaUrl;
                            }
                            else throw new MapperException("No item with ID {0}. Can not update Link linkField".Formatted(newId));
                        }

                    }
                    break;
                case LinkType.External:
                    linkField.LinkType = "external";
                    linkField.Url = link.Url;
                    break;
                case LinkType.Anchor:
                    linkField.LinkType = "anchor";
                    linkField.Url = link.Anchor;
                    break;
                case LinkType.MailTo:
                    linkField.LinkType = "mailto";
                    linkField.Url = link.Url;
                    break;
                case LinkType.JavaScript:
                    linkField.LinkType = "javascript";
                    linkField.Url = link.Url;
                    break;


            }



            if (!link.Anchor.IsNullOrEmpty())
                linkField.Anchor = link.Anchor;
            if (!link.Class.IsNullOrEmpty())
                linkField.Class = link.Class;
            if (!link.Text.IsNullOrEmpty())
                linkField.Text = link.Text;
            if (!link.Title.IsNullOrEmpty())
                linkField.Title = link.Title;
            if (!link.Query.IsNullOrEmpty())
                linkField.QueryString = HttpUtility.UrlEncode(link.Query);
            if (!link.Target.IsNullOrEmpty())
                linkField.Target = link.Target;
        }