Ejemplo n.º 1
0
    /// <inheritdoc />
    public IEnumerable <IPublishedContent> GetContentByTag(string tag, string?group = null, string?culture = null)
    {
        IEnumerable <int> ids = _tagService.GetTaggedContentByTag(tag, group, culture)
                                .Select(x => x.EntityId);

        return(_contentQuery.Content(ids));
    }
Ejemplo n.º 2
0
        // Method One
        private string GetMediaFromPublishedContentQuery()
        {
            var willBeEMpty    = _publishedContentQuery.Content(2181)?.Url();
            var cheerleaderUrl = _publishedContentQuery.Media(2181)?.Url();

            //  _contentService.GetById(2182).?.Url();  HAS NO URL as it might not be published!!!

            return(cheerleaderUrl);
        }
        public vmBlock_DataLink Convert(Link link, UmbracoMappingContext context)
        {
            if (link != null)
            {
                if (link.Type == LinkType.External)
                {
                    return(new vmBlock_DataLink()
                    {
                        Title = link.Name,
                        Label = link.Name,
                        Href = link.Url,
                        IsExternalLink = true,
                        IsNewTab = link.Target == "_blank"
                    });
                }
                else
                {
                    var id = link.Udi as GuidUdi;
                    if (id != null)
                    {
                        var content = contentQuery.Content(id);

                        if (content != null)
                        {
                            var name = string.IsNullOrEmpty(link.Name) ? content.Name : link.Name;
                            var udi  = context.Model != null ? new GuidUdi("document", context.Model.Key) : null;

                            var d = new vmBlock_DataLink()
                            {
                                Title    = name,
                                Label    = name,
                                Href     = link.Url,
                                IsActive = link.Udi == udi,
                                IsNewTab = link.Target == "_blank"
                            };
                            return(d);
                        }

                        var media = contentQuery.Media(id);

                        if (media != null)
                        {
                            var name = string.IsNullOrEmpty(link.Name) ? media.Name : link.Name;

                            var d = new vmBlock_DataLink()
                            {
                                Title    = name,
                                Label    = name,
                                Href     = media.Url(),
                                IsNewTab = link.Target == "_blank"
                            };
                            return(d);
                        }
                    }
                }
            }
            return(null);
        }
        /// <summary>
        /// Returns the content id based on the configured ContentErrorPage section.
        /// </summary>
        internal static int?GetContentIdFromErrorPageConfig(
            ContentErrorPage errorPage,
            IEntityService entityService,
            IPublishedContentQuery publishedContentQuery,
            int?domainContentId)
        {
            if (errorPage.HasContentId)
            {
                return(errorPage.ContentId);
            }

            if (errorPage.HasContentKey)
            {
                // need to get the Id for the GUID
                // TODO: When we start storing GUIDs into the IPublishedContent, then we won't have to look this up
                // but until then we need to look it up in the db. For now we've implemented a cached service for
                // converting Int -> Guid and vice versa.
                Attempt <int> found = entityService.GetId(errorPage.ContentKey, UmbracoObjectTypes.Document);
                if (found)
                {
                    return(found.Result);
                }

                return(null);
            }

            if (errorPage.ContentXPath.IsNullOrWhiteSpace() == false)
            {
                try
                {
                    // we have an xpath statement to execute
                    var xpathResult = UmbracoXPathPathSyntaxParser.ParseXPathQuery(
                        xpathExpression: errorPage.ContentXPath,
                        nodeContextId: domainContentId,
                        getPath: nodeid =>
                    {
                        IEntitySlim ent = entityService.Get(nodeid);
                        return(ent.Path.Split(',').Reverse());
                    },
                        publishedContentExists: i => publishedContentQuery.Content(i) != null);

                    // now we'll try to execute the expression
                    IPublishedContent nodeResult = publishedContentQuery.ContentSingleAtXPath(xpathResult);
                    if (nodeResult != null)
                    {
                        return(nodeResult.Id);
                    }
                }
                catch (Exception ex)
                {
                    StaticApplicationLogging.Logger.LogError(ex, "Could not parse xpath expression: {ContentXPath}", errorPage.ContentXPath);
                    return(null);
                }
            }

            return(null);
        }
        /// <inheritdoc />
        public IPublishedContent GetPublishedContent(Guid contentId, CancellationToken cancellationToken)
        {
            var content = _publishedContent.Content(contentId);

            if (content is null)
            {
                throw new KeyNotFoundException($"Unable to get published content {contentId}. Content does not exist.");
            }

            return(content);
        }
Ejemplo n.º 6
0
        public IActionResult Get(Guid id, int level = 0)
        {
            var content    = _publishedContent.Content(id);
            var dictionary = new Dictionary <string, object>
            {
                { "addUrl", true }
            };

            if (level <= 0)
            {
                return(Ok(_contentResolver.Value.ResolveContent(content, dictionary)));
            }

            dictionary.Add("level", level);

            return(Ok(_contentResolver.Value.ResolveContent(content, dictionary)));
        }
Ejemplo n.º 7
0
        public CheeryQueryController(
            //IPublishedContentCache cache   /// causes an error
            UmbracoHelper helper,
            IPublishedContentQuery publishedContentQuery,
            IContentService contentService,
            IUmbracoContextFactory umbracoContextFactory)
        {
            _helper = helper;
            _publishedContentQuery = publishedContentQuery;
            _contentService        = contentService;
            _umbracoContextFactory = umbracoContextFactory;

            var page      = helper.Content(2141);
            var otherPage = publishedContentQuery.Content(2141);
            IPublishedContent anotherPage = null;

            using (var cref = umbracoContextFactory.EnsureUmbracoContext())
            {
                var cache = cref.UmbracoContext.Content;
                anotherPage = cache.GetById(2141);
            }
        }
Ejemplo n.º 8
0
        public PartialViewResult GetPreviewMarkup([FromForm] PreviewData data, [FromQuery] int pageId)
        {
            var page = default(IPublishedContent);

            // If the page is new, then the ID will be zero
            if (pageId > 0)
            {
                // Get page container node
                page = _contentQuery.Content(pageId);
                if (page == null)
                {
                    // If unpublished, then fake PublishedContent
                    page = new UnpublishedContent(pageId, _contentService, _contentTypeService, _dataTypeService, _propertyEditorCollection, _publishedContentTypeFactory);
                }
            }


            if (_umbracoContext.UmbracoContext.PublishedRequest == null)
            {
                var request = _router.CreateRequestAsync(new Uri(Request.GetDisplayUrl())).Result;
                request.SetPublishedContent(page);
                _umbracoContext.UmbracoContext.PublishedRequest = request.Build();
            }

            // Set the culture for the preview
            if (page != null && page.Cultures != null)
            {
                var currentCulture = string.IsNullOrWhiteSpace(data.Culture) ? page.GetCultureFromDomains() : data.Culture;
                if (currentCulture != null && page.Cultures.ContainsKey(currentCulture))
                {
                    var culture = new CultureInfo(page.Cultures[currentCulture].Culture);
                    // _umbracoContext.UmbracoContext.PublishedRequest.Culture = culture; // TODO: Not sure if this is needed?
                    System.Threading.Thread.CurrentThread.CurrentCulture   = culture;
                    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                    _umbracoContext.UmbracoContext.VariationContextAccessor.VariationContext = new VariationContext(culture.Name);
                }
            }

            // Get content node object
            var content = _dtgeHelper.ConvertValueToContent(data.Id, data.ContentTypeAlias, data.Value);

            // Construct preview model
            var model = new PreviewModel
            {
                Page            = page,
                Item            = content,
                EditorAlias     = data.EditorAlias,
                PreviewViewPath = data.PreviewViewPath,
                ViewPath        = data.ViewPath
            };


            // Render view

            var partialName = "~/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditorPreviewer.cshtml";

            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());

            viewData.Model = model;
            return(new PartialViewResult()
            {
                ViewName = partialName,
                ViewData = viewData
            });
        }
        private IEnumerable <IPublishedContent> PostTemplateValue(QueryModel model, StringBuilder queryExpression)
        {
            var indent = Environment.NewLine + "    ";

            // set the source
            IPublishedContent sourceDocument;

            if (model.Source != null && model.Source.Id > 0)
            {
                sourceDocument = _publishedContentQuery.Content(model.Source.Id);

                if (sourceDocument == null)
                {
                    queryExpression.AppendFormat("Umbraco.Content({0})", model.Source.Id);
                }
                else
                {
                    queryExpression.AppendFormat("Umbraco.Content(Guid.Parse(\"{0}\"))", sourceDocument.Key);
                }
            }
            else
            {
                sourceDocument = _publishedContentQuery.ContentAtRoot().FirstOrDefault();
                queryExpression.Append("Umbraco.ContentAtRoot().FirstOrDefault()");
            }

            // get children, optionally filtered by type
            IEnumerable <IPublishedContent> contents;

            queryExpression.Append(indent);
            if (model.ContentType != null && !model.ContentType.Alias.IsNullOrWhiteSpace())
            {
                contents = sourceDocument == null
                    ? Enumerable.Empty <IPublishedContent>()
                    : sourceDocument.ChildrenOfType(_variationContextAccessor, model.ContentType.Alias);

                queryExpression.AppendFormat(".ChildrenOfType(\"{0}\")", model.ContentType.Alias);
            }
            else
            {
                contents = sourceDocument == null
                    ? Enumerable.Empty <IPublishedContent>()
                    : sourceDocument.Children(_variationContextAccessor);

                queryExpression.Append(".Children()");
            }

            // apply filters
            foreach (var condition in model.Filters.Where(x => !x.ConstraintValue.IsNullOrWhiteSpace()))
            {
                //x is passed in as the parameter alias for the linq where statement clause
                var operation = condition.BuildCondition <IPublishedContent>("x");

                //for review - this uses a tonized query rather then the normal linq query.
                contents = contents.Where(operation.Compile());
                queryExpression.Append(indent);
                queryExpression.AppendFormat(".Where({0})", operation);
            }

            // always add IsVisible() to the query
            contents = contents.Where(x => x.IsVisible(_publishedValueFallback));
            queryExpression.Append(indent);
            queryExpression.Append(".Where(x => x.IsVisible())");

            // apply sort
            if (model.Sort != null && !model.Sort.Property.Alias.IsNullOrWhiteSpace())
            {
                contents = SortByDefaultPropertyValue(contents, model.Sort);

                queryExpression.Append(indent);
                queryExpression.AppendFormat(model.Sort.Direction == "ascending"
                    ? ".OrderBy(x => x.{0})"
                    : ".OrderByDescending(x => x.{0})"
                                             , model.Sort.Property.Alias);
            }

            // take
            if (model.Take > 0)
            {
                contents = contents.Take(model.Take);
                queryExpression.Append(indent);
                queryExpression.AppendFormat(".Take({0})", model.Take);
            }

            return(contents);
        }