private async Task <IDisplayResult> GroupedAsync(SiteSearch part, BuildPartDisplayContext context)
        {
            var request = _httpContextAccessor.HttpContext.Request;
            var term    = request.GetQueryString(FilterQueryStringParameter);
            var results = new List <SiteSearchGroupedResultsGroup>();

            if (!string.IsNullOrWhiteSpace(term))
            {
                var searchableTypes = GetSearchableContentTypes();
                var types           = part.ContentTypeSettings.Where(x => x.Included && !string.IsNullOrEmpty(x.Query)).ToArray();
                var parameters      = new Dictionary <string, object>
                {
                    { "filter", term },
                    { "from", 0 },
                    { "size", part.PageSize }
                };

                foreach (var type in types)
                {
                    var query = await _queryManager.GetQueryAsync(type.Query);

                    var displayName = searchableTypes.Where(x => x.Name == type.ContentType).SingleOrDefault()?.DisplayName ?? string.Empty;

                    if (!string.IsNullOrEmpty(displayName))
                    {
                        var contentItemQueryResults = await _queryManager.ExecuteQueryAsync(query, parameters) as ContentItemQueryResults;

                        results.Add(new SiteSearchGroupedResultsGroup
                        {
                            ContentType = displayName,
                            Items       = (ContentItem[])contentItemQueryResults?.Items,//  await _queryManager.ExecuteQueryAsync(query, parameters) as ContentItem[],
                            Settings    = type
                        });
                    }
                }
            }

            return(Initialize <SiteSearchGroupedViewModel>("SiteSearch_Grouped", model =>
            {
                model.EmptyResultsContent = part.EmptyResultsContent;
                model.Filter = term;
                model.FilterInputPlaceholder = part.FilterInputPlaceholder;
                model.ItemsDisplayType = part.ItemsDisplayType;
                model.Results = results;
                model.SubmitButtonLabel = part.SubmitButtonLabel;
            })
                   .Location("Detail", "Content:5"));
        }
Exemple #2
0
        public async Task <IActionResult> Query(
            string name,
            string parameters)
        {
            var query = await _queryManager.GetQueryAsync(name);

            if (query == null)
            {
                return(NotFound());
            }

            if (!await _authorizationService.AuthorizeAsync(User, Permissions.CreatePermissionForQuery(query.Name)))
            {
                // Intentionally not returning Unauthorized as it is not usable from APIs and would
                // expose the existence of a named query (not a concern per se).
                return(NotFound());
            }

            var queryParameters = parameters != null?
                                  JsonConvert.DeserializeObject <Dictionary <string, object> >(parameters)
                                      : new Dictionary <string, object>();

            var result = await _queryManager.ExecuteQueryAsync(query, queryParameters);

            return(new ObjectResult(result.Items));
        }
        public async Task BuildNavigationAsync(MenuItem menuItem, NavigationBuilder builder, IEnumerable <IAdminNodeNavigationBuilder> treeNodeBuilders)
        {
            _node = menuItem as QueryAdminNode;

            if (_node == null || !_node.Enabled || String.IsNullOrEmpty(_node.QueryId))
            {
                return;
            }

            //  _contentType = _contentDefinitionManager.GetTypeDefinition(_node.ContentType);
            //    var taxonomyContentItem = await _contentManager.GetAsync(_node.ContentType);

            //var taxonomyTree= await  _taxonomyServices.BuildTaxonomyTree( _node.ContentType);
            var query = await _queryManager.GetQueryAsync(_node.QueryId);

            if (query == null)
            {
                _logger.LogError("Can't find The content type '{ContentType}' for list admin node.", _node.QueryId);
                return;
            }
            var parameters  = new Dictionary <string, object>();
            var queryResult = await _queryManager.ExecuteQueryAsync(query, parameters);

            if (_node.AddQueryIdAsParent)
            {
//                if (query == null)
//                {
//                    _logger.LogError("Can't find The content type '{ContentType}' for list admin node.", _node.QueryId);
//                }

                await builder.AddAsync(new LocalizedString(query.Name, query.Name), async listTypeMenu =>
                {
                    AddPrefixToClasses(_node.IconForParentLink).ForEach(c => listTypeMenu.AddClass(c));
                    //todo
//                    listTypeMenu.Permission(ContentTypePermissions.CreateDynamicPermission(
//                        ContentTypePermissions.PermissionTemplates[Contents.Permissions.EditContent.Name], taxonomyContentItem));
                    await AddContentItemsAsync(listTypeMenu, queryResult, _node.LinkMode);
                });
            }
            else
            {
                await AddContentItemsAsync(builder, queryResult, _node.LinkMode);
            }

            // Add external children
            foreach (var childNode in _node.Items)
            {
                try
                {
                    var treeBuilder = treeNodeBuilders.Where(x => x.Name == childNode.GetType().Name).FirstOrDefault();
                    await treeBuilder.BuildNavigationAsync(childNode, builder, treeNodeBuilders);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "An exception occurred while building the '{MenuItem}' child Menu Item.", childNode.GetType().Name);
                }
            }
        }
Exemple #4
0
        public async Task <IActionResult> RunQuery(CypherQueryIndexOptions options, PagerParameters pagerParameters)
        {
            var query = await _queryManager.GetQueryAsync(options.QueryName);

            var cypherQuery = query as Models.CypherQuery;

            if (cypherQuery == null)
            {
                return(NotFound());
            }

            if (!await _authorizationService.AuthorizeAsync(User, OrchardCore.Queries.Permissions.CreatePermissionForQuery(cypherQuery.Name)))
            {
                return(NotFound());
            }

            var queryParameters = cypherQuery.Parameters != null
                ? JsonConvert.DeserializeObject <Dictionary <string, object> >(cypherQuery.Parameters)
                : new Dictionary <string, object>();

            var result = await _queryManager.ExecuteQueryAsync(query, queryParameters);

            //todo: handle ResultModelType null
            var mappedResults   = TransformResults(cypherQuery.ResultModelType !, result.Items);
            var filteredResults = string.IsNullOrWhiteSpace(options.Search)
                ? mappedResults
                : mappedResults.Where(w => w.Filter !.Contains(options.Search, System.StringComparison.OrdinalIgnoreCase)).ToList();

            var siteSettings = await _siteService.GetSiteSettingsAsync();

            var pager      = new Pager(pagerParameters, siteSettings.PageSize);
            var resultPage = filteredResults.Skip(pager.GetStartIndex()).Take(pager.PageSize).ToList();

            options.ItemViewModel = $"_ResultItem_{cypherQuery.ResultModelType}";
            var routeData = new RouteData();

            routeData.Values.Add("Options.Search", options.Search);
            routeData.Values.Add("Options.QueryName", options.QueryName);
            routeData.Values.Add("Options.ItemViewModel", options.ItemViewModel);

            //TODO: may need to create some Shape items here for the rendering
            var pagerShape = (await New.Pager(pager)).TotalItemCount(result.Items.Count()).RouteData(routeData);
            var viewModel  = new CypherQueryResultViewModel()
            {
                Results = (from a in resultPage select a as object).ToList(),
                Options = options,
                Pager   = pagerShape,
            };

            return(View(viewModel));
        }
        public async Task <XDocument> CreateFeedAsync(ContentItem contentItem, ActionContext actionContext)
        {
            var contentPart = contentItem.Get <ContentPart>(Constants.RssFeed.ContentType);
            var query       = await _queryManager.GetQueryAsync(contentPart?.Get <QueryField>(Constants.RssFeed.SourceFieldName)?.Value);

            var results = await _queryManager.ExecuteQueryAsync(query, new Dictionary <string, object>());

            var rss = new XElement("rss", new XAttribute("version", "2.0"), new XAttribute(XNamespace.Xmlns + "atom", "http://www.w3.org/2005/Atom"));

            rss.Add(CreateChannelMeta(contentItem));
            rss.Add(await CreateItemsAsync(contentItem, results.Items.Cast <ContentItem>(), actionContext));

            return(new XDocument(new XDeclaration("1.0", "utf-8", "yes"), rss));
        }
Exemple #6
0
        public override object BeforeMethod(string method)
        {
            var query = _queryManager.GetQueryAsync(method.ToString()).GetAwaiter().GetResult();

            if (query == null)
            {
                return(null);
            }

            var result = _queryManager.ExecuteQueryAsync(query, new Dictionary <string, object>()).GetAwaiter().GetResult();

            if (result is IEnumerable <JObject> )
            {
                // BUG: Somehow Liquid doesn't accept JObject collections directly unlike ContentItem.
                return(((IEnumerable <JObject>)result).Select(x => new JTokenDrop(x)).ToArray());
            }

            return(result);
        }
Exemple #7
0
        public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx)
        {
            var query = input.ToObjectValue() as Query;

            if (query == null)
            {
                return(NilValue.Instance);
            }

            var parameters = new Dictionary <string, object>();

            foreach (var name in arguments.Names)
            {
                parameters.Add(name, arguments[name].ToObjectValue());
            }

            var result = await _queryManager.ExecuteQueryAsync(query, parameters);

            return(FluidValue.Create(result.Items));
        }
Exemple #8
0
        public async Task <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx)
        {
            var query = await _queryManager.GetQueryAsync(input.ToStringValue());

            if (query == null)
            {
                return(null);
            }

            var parameters = new Dictionary <string, object>();

            foreach (var name in arguments.Names)
            {
                parameters.Add(name, arguments[name]);
            }

            var result = await _queryManager.ExecuteQueryAsync(query, parameters);

            return(FluidValue.Create(result));
        }
Exemple #9
0
        public async Task <IActionResult> Query(
            string name,
            string parameters)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ExecuteQueryApi))
            {
                return(Unauthorized());
            }

            var queryParameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(parameters ?? "");
            var query           = await _queryManager.GetQueryAsync(name);

            if (query == null)
            {
                return(NotFound());
            }

            var result = await _queryManager.ExecuteQueryAsync(query, queryParameters);

            return(new ObjectResult(result));
        }
Exemple #10
0
        //private async Task<PagerSlim> GetPagerAsync(IUpdateModel updater, ProjectionPart part)
        //{
        //    var settings = GetSettings(part);
        //    PagerSlimParameters pagerParameters = new PagerSlimParameters();
        //    await updater.TryUpdateModelAsync(pagerParameters);

        //    PagerSlim pager = new PagerSlim(pagerParameters, settings.PageSize);

        //    return pager;
        //}


        // [HttpPost, HttpGet]
        public async Task <object> QueryResult(string name, string parameters)
        {
            var query = await _queryManager.GetQueryAsync(name);

            if (query == null)
            {
                return(null);// NotFound();
            }

            //if (!await _authorizationService.AuthorizeAsync(User, Permissions.CreatePermissionForQuery(query.Name)))
            //{
            //    // Intentionally not returning Unauthorized as it is not usable from APIs and would
            //    // expose the existence of a named query (not a concern per se).
            //    return NotFound();
            //}

            var queryParameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(parameters ?? "");

            return(await _queryManager.ExecuteQueryAsync(query, queryParameters));

            //  return new ObjectResult(result);
        }
Exemple #11
0
        public void OnTemplateProcessing(TemplateContext context)
        {
            context.Filters.AddAsyncFilter("query", async(input, arguments, ctx) =>
            {
                var query = await _queryManager.GetQueryAsync(input.ToStringValue());

                if (query == null)
                {
                    return(null);
                }

                var parameters = new Dictionary <string, object>();

                foreach (var name in arguments.Names)
                {
                    parameters.Add(name, arguments[name]);
                }

                var result = await _queryManager.ExecuteQueryAsync(query, parameters);

                return(FluidValue.Create(result));
            });
        }