예제 #1
0
        private string getNodeIdFromUdi(string media)
        {
            Current.Logger.Info(this.GetType(),"getNodeIdFromUdi(string media): value in media: "+media);
            var id = string.Empty;
            try
            {
                var udi = GuidUdi.Parse(media);
                //var typedContent = udi.ToPublishedContent();
                var umbracoContext = UmbracoContext.Current == null ? EnsureUmbracoContext() : UmbracoContext.Current;
                var umbracoHelper = new UmbracoHelper(umbracoContext, Current.Services);

                if (udi.ToString().ToLower().Contains("document"))
                {
                    var typedContent = umbracoHelper.Content(udi);
                    id = typedContent?.Id.ToString();
                }
                else if (udi.ToString().ToLower().Contains("media"))
                {
                    var typedContent = umbracoHelper.Media(udi);
                    id = typedContent?.Id.ToString();
                }
                

                
            }
            catch (Exception msg)
            {
                Current.Logger.Error(this.GetType(), "Error while processing getNodeIdFromUdi(string media): value in media: " + media, msg);
            }
            return id;

        }
예제 #2
0
        public IEnumerable <int> GetAndEnsureNodeIdsForTags(string currentNodeId, string tags, string containerId, string documentTypeAlias)
        {
            var udi     = GuidUdi.Parse(containerId);
            var entitiy = Services.EntityService.Get(udi.Guid);

            var cs = Services.ContentService;

            if (string.IsNullOrWhiteSpace(tags))
            {
                return(new List <int>());
            }

            // put posted tags in an array
            var postedTags = tags.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            // get the current node
            var node = cs.GetById(int.Parse(currentNodeId));

            // get all existing tag nodes in container
            var tagContainer = cs.GetById(entitiy.Id);
            var allTagNodes  = cs.GetPagedChildren(tagContainer.Id, 0, int.MaxValue, out var count).Where(x => x.ContentType.Alias == documentTypeAlias);

            bool hasNewTags = false;

            foreach (string postedTag in postedTags)
            {
                // get tag names which do not already exist in the tag container
                bool found = allTagNodes.Any(x => x.Name == postedTag);
                if (!found)
                {
                    // tag node doesnt exist so create new node
                    var dic = new Dictionary <string, object>()
                    {
                        { documentTypeAlias, postedTag }
                    };
                    var newTag = cs.CreateContent(postedTag, tagContainer.GetUdi(), documentTypeAlias);
                    cs.SaveAndPublish(newTag);
                    hasNewTags = true;
                }
            }

            // re-get container because new nodes might have been added.
            tagContainer = cs.GetById(entitiy.Id);
            if (hasNewTags)
            {
                // new tag so sort!
                cs.Sort(cs.GetPagedChildren(tagContainer.Id, 0, int.MaxValue, out var count1).OrderBy(x => x.Name));
            }

            // get all tag ids, and return
            var tagIds = cs.GetPagedChildren(tagContainer.Id, 0, int.MaxValue, out var count2).Where(x => postedTags.Contains(x.Name)).Select(x => x.Id);

            return(tagIds);
        }
예제 #3
0
        public string getImageURLFromUDI(string imageUDI)
        {
            try {
                var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

                var imageGuidUdi = GuidUdi.Parse(imageUDI);
                var imageNodeID  = ApplicationContext.Current.Services.EntityService.GetIdForKey(imageGuidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), imageGuidUdi.EntityType, true));
                var imageNode    = umbracoHelper.TypedMedia(imageNodeID.Result);

                return(imageNode.Url);
            }
            catch { return(""); }
        }
예제 #4
0
        public Exercise GetExercise(int id)
        {
            IContentService cs = Services.ContentService;

            var exercise = cs.GetById(id);

            Exercise e = new Exercise();

            e.Id    = exercise.Id;
            e.Name  = exercise.Name;
            e.Title = exercise.GetValue <string>("Title");
            if (exercise.GetValue <string>("ImageLinks") != null)
            {
                var imageString  = exercise.GetValue <string>("ImageLinks");
                var imageGuidUdi = GuidUdi.Parse(imageString);
                var imageNodeId  = ApplicationContext.Current.Services.EntityService.GetIdForKey(imageGuidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), imageGuidUdi.EntityType, true));
                var imageNode    = Umbraco.TypedMedia(imageNodeId.Result);
                e.ImageLinks = apiUrl + imageNode.Url;
            }
            else
            {
                e.ImageLinks = null;
            }

            e.Description = exercise.GetValue <string>("Description");
            e.Repetition  = exercise.GetValue <string>("Repetition");

            if (exercise.GetValue <string>("Breaks") != null)
            {
                e.Breaks = exercise.GetValue <string>("Breaks");
            }
            else
            {
                e.Breaks = null;
            }

            if (exercise.GetValue <string>("Focus") != null)
            {
                e.Focus = exercise.GetValue <string>("Focus");
            }
            else
            {
                e.Breaks = null;
            }

            e.ExerciseID    = exercise.GetValue <int>("ExerciseID");
            e.UserGroupID   = exercise.GetValue <int>("UserGroupID");
            e.ApiExerciseID = exercise.GetValue <string>("ApiExerciseID");

            return(e);
        }
        public void SetValue(IContentBase content, string alias, string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                content.SetValue(alias, value);
                return;
            }

            var relatedLinks = JsonConvert.DeserializeObject <JArray>(value);

            foreach (var relatedLink in relatedLinks)
            {
                //Get the value from the JSON object
                var isInternal = Convert.ToBoolean(relatedLink["isInternal"]);

                //We are only concerned about internal links
                if (!isInternal)
                {
                    continue;
                }

                var relatedLinkValue = relatedLink["link"].ToString();

                //Check if related links is stored as an int
                if (int.TryParse(relatedLinkValue, out var relatedLinkInt))
                {
                    //Update the JSON back to the int ids on this env
                    relatedLink["link"]     = relatedLinkInt;
                    relatedLink["internal"] = relatedLinkInt;
                }
                else
                {
                    //Get the UDI value in the JSON
                    var pickedUdi = GuidUdi.Parse(relatedLinkValue);

                    //Lets use entitiy sevice to get the int ID for this item on the new environment
                    //Get the Id corresponding to the Guid
                    //it *should* succeed when deploying, due to dependencies management
                    //nevertheless, assume it can fail, and then create an invalid localLink
                    var idAttempt = _entityService.GetIdForKey(pickedUdi.Guid, UmbracoObjectTypes.Document);

                    //Update the JSON back to the int ids on this env
                    relatedLink["link"]     = idAttempt.Success ? idAttempt.Result : 0;
                    relatedLink["internal"] = idAttempt.Success ? idAttempt.Result : 0;
                }
            }

            //Save the updated JSON with replaced UDIs for int IDs
            content.SetValue(alias, JsonConvert.SerializeObject(relatedLinks));
        }
예제 #6
0
        public IEnumerable <string> GetTags(int currentNodeId, string containerId, string documentTypeAlias)
        {
            //var udi = Udi.Parse(containerId);
            var udi     = GuidUdi.Parse(containerId);
            var entitiy = Services.EntityService.Get(udi.Guid);
            var cs      = Services.ContentService;
            /* Get the tags container */
            var tagContainer = cs.GetById(entitiy.Id);


            /* Compile a list of all tag pages that exist as children of the tags container */
            var tags = cs.GetPagedChildren(tagContainer.Id, 0, int.MaxValue, out var count).Where(x => x.ContentType.Alias == documentTypeAlias).Select(x => x.Name);

            return(tags);
        }
예제 #7
0
        public HttpResponseMessage GetByUdi(string udi)
        {
            var entity = Services.ContentService.GetById(GuidUdi.Parse(udi).Guid);

            if (entity != null)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, new
                {
                    id = entity.Id,
                    name = entity.Name,
                    icon = entity.ContentType.Icon,
                    url = Umbraco.Url(entity.Id),
                    published = entity.Published,
                    naviHide = entity.HasProperty("umbracoNaviHide") && entity.GetValue <bool>("umbracoNaviHide")
                }));
            }

            return(null);
        }
예제 #8
0
        public string getMediaUrl(string mediaValue)
        {
            if (mediaValue.Contains("umb://media") == false)
            {
                return(mediaValue);
            }

            var           mediaArray = mediaValue.Split(',');
            List <string> MediaList  = new List <string>();

            foreach (var item in mediaArray)
            {
                var imageGuidUdi = GuidUdi.Parse(item);
                // Get the ID of the node!
                var imageNodeId = ApplicationContext.Current.Services.EntityService.GetIdForKey(imageGuidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), imageGuidUdi.EntityType, true));

                var imageNode = Umbraco.TypedMedia(imageNodeId.Result);
                MediaList.Add(imageNode.Url());
            }

            return(string.Join(",", MediaList.ToArray()));
        }
예제 #9
0
        /// <inheritdoc />
        public IList <NexuRelationDisplayModel> GetUsedItemsFromList(IList <Udi> udis)
        {
            var nexuRelationDisplayModels = new List <NexuRelationDisplayModel>();

            if (udis?.Any() != true)
            {
                return(nexuRelationDisplayModels);
            }

            var relations = this.relationRepository.GetUsedItemsFromList(udis);

            if (!relations.Any())
            {
                return(nexuRelationDisplayModels);
            }

            foreach (var relation in relations.Distinct())
            {
                var guidUdi = GuidUdi.Parse(relation.Key);

                if (guidUdi.EntityType == Constants.UdiEntityType.Document)
                {
                    var content = this.contentService.GetById(guidUdi.Guid);

                    if (content != null)
                    {
                        var contentName = content.Name;
                        var published   = content.Published;

                        if (content.AvailableCultures.Any())
                        {
                            contentName = content.GetCultureName(relation.Value);
                            published   = content.IsCulturePublished(relation.Value);
                        }

                        nexuRelationDisplayModels.Add(new NexuRelationDisplayModel
                        {
                            Culture     = relation.Value,
                            Id          = content.Id,
                            IsPublished = published,
                            IsTrashed   = content.Trashed,
                            Key         = content.Key,
                            Name        = contentName
                        });
                    }
                }
                else if (guidUdi.EntityType == Constants.UdiEntityType.Media)
                {
                    var media = this.mediaService.GetById(guidUdi.Guid);

                    if (media != null)
                    {
                        if (nexuRelationDisplayModels.Any(x => x.Key == media.Key) == false)
                        {
                            nexuRelationDisplayModels.Add(new NexuRelationDisplayModel
                            {
                                Culture     = string.Empty,
                                Id          = media.Id,
                                IsPublished = false,
                                IsTrashed   = media.Trashed,
                                Key         = media.Key,
                                Name        = media.Name
                            });
                        }
                    }
                }
            }

            return(nexuRelationDisplayModels);
        }
예제 #10
0
        public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            if (source == null)
            {
                return(null);
            }

            var doc = new HtmlDocument {
                OptionOutputOriginalCase = true
            };

            doc.LoadHtml(source.ToString());

            // Get all img tags to replace the "src" url with Imgix url and cleanup the html.
            var imgNodes = doc.DocumentNode.SelectNodes("//img");

            if (imgNodes != null)
            {
                foreach (var imgNode in imgNodes)
                {
                    var src = imgNode.GetAttributeValue("src", null);
                    if (src == null)
                    {
                        continue;
                    }

                    var udiAttribute = imgNode.GetAttributeValue("data-udi", null);

                    if (udiAttribute.IsNullOrWhiteSpace())
                    {
                        continue;
                    }

                    var udi = Udi.Parse(udiAttribute);

                    var guidUdi = GuidUdi.Parse(udi.ToString());
                    var helper  = new UmbracoHelper(UmbracoContext.Current);
                    var image   = helper.TypedMedia(guidUdi.Guid);
                    if (image != null)
                    {
                        var imgixImageUrl = image.GetImgixImage().Url;

                        // Try get the potential image width and height attributes
                        var width  = imgNode.GetAttributeValue("width", null);
                        var height = imgNode.GetAttributeValue("height", null);
                        if (!width.IsNullOrWhiteSpace() && !height.IsNullOrWhiteSpace())
                        {
                            imgixImageUrl += $"?w={width}&h={height}";
                            // Clean up
                            imgNode.Attributes["width"].Remove();
                            imgNode.Attributes["height"].Remove();
                        }

                        // Replace the src url with Imgix url
                        imgNode.SetAttributeValue("src", imgixImageUrl);
                    }

                    // Set alternative text
                    var alt = imgNode.GetAttributeValue("alt", null);
                    if (!alt.IsNullOrWhiteSpace())
                    {
                        continue;
                    }

                    var imageAltText = image.GetPropertyValue <string>("alternativeText");
                    imgNode.SetAttributeValue("alt", imageAltText);

                    // Clean up
                    imgNode.Attributes["data-udi"].Remove();
                    if (!imgNode.GetAttributeValue("id", null).IsNullOrWhiteSpace())
                    {
                        imgNode.Attributes["id"].Remove();
                    }
                }
            }

            // Get all anchor tags to cleanup the html.
            var anchorNodes = doc.DocumentNode.SelectNodes("//a");

            if (anchorNodes != null)
            {
                foreach (var anchorNode in anchorNodes)
                {
                    // Clean up
                    if (!anchorNode.GetAttributeValue("data-udi", null).IsNullOrWhiteSpace())
                    {
                        anchorNode.Attributes["data-udi"].Remove();
                    }
                }
            }

            if (imgNodes != null || anchorNodes != null)
            {
                return(base.ConvertDataToSource(propertyType, doc.DocumentNode.OuterHtml, preview));
            }

            return(base.ConvertDataToSource(propertyType, source, preview));
        }