2018-07-27 13:01:54 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
2021-09-14 22:13:39 +02:00
|
|
|
|
using System.Globalization;
|
2014-06-08 09:51:06 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2021-02-15 13:14:07 +01:00
|
|
|
|
using Umbraco.Cms.Core;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.TemplateQuery;
|
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2021-02-10 11:11:18 +01:00
|
|
|
|
using Umbraco.Cms.Web.BackOffice.Filters;
|
2021-02-10 11:42:04 +01:00
|
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2021-02-10 11:11:18 +01:00
|
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The API controller used for building content queries within the template
|
|
|
|
|
|
/// </summary>
|
2020-08-04 12:27:21 +10:00
|
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2014-06-09 16:42:57 +01:00
|
|
|
|
[JsonCamelCaseFormatter]
|
2014-06-08 09:51:06 -07:00
|
|
|
|
public class TemplateQueryController : UmbracoAuthorizedJsonController
|
|
|
|
|
|
{
|
2019-12-19 10:43:00 +01:00
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private readonly IPublishedContentQuery _publishedContentQuery;
|
2020-06-22 10:08:08 +02:00
|
|
|
|
private readonly ILocalizedTextService _localizedTextService;
|
|
|
|
|
|
private readonly IPublishedValueFallback _publishedValueFallback;
|
|
|
|
|
|
private readonly IContentTypeService _contentTypeService;
|
2019-12-19 10:43:00 +01:00
|
|
|
|
|
|
|
|
|
|
public TemplateQueryController(
|
2020-01-07 13:08:21 +01:00
|
|
|
|
IVariationContextAccessor variationContextAccessor,
|
2020-06-22 10:08:08 +02:00
|
|
|
|
IPublishedContentQuery publishedContentQuery,
|
|
|
|
|
|
ILocalizedTextService localizedTextService,
|
|
|
|
|
|
IPublishedValueFallback publishedValueFallback,
|
|
|
|
|
|
IContentTypeService contentTypeService)
|
2019-12-19 10:43:00 +01:00
|
|
|
|
{
|
2020-06-22 10:08:08 +02:00
|
|
|
|
_variationContextAccessor = variationContextAccessor ??
|
|
|
|
|
|
throw new ArgumentNullException(nameof(variationContextAccessor));
|
|
|
|
|
|
_publishedContentQuery =
|
|
|
|
|
|
publishedContentQuery ?? throw new ArgumentNullException(nameof(publishedContentQuery));
|
|
|
|
|
|
_localizedTextService =
|
|
|
|
|
|
localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
|
|
|
|
|
|
_publishedValueFallback =
|
|
|
|
|
|
publishedValueFallback ?? throw new ArgumentNullException(nameof(publishedValueFallback));
|
|
|
|
|
|
_contentTypeService = contentTypeService ?? throw new ArgumentNullException(nameof(contentTypeService));
|
2019-12-19 10:43:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
private IEnumerable<OperatorTerm> Terms => new List<OperatorTerm>
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
2021-07-05 20:58:04 +02:00
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","is"), Operator.Equals, new [] {"string"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","isNot"), Operator.NotEquals, new [] {"string"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","before"), Operator.LessThan, new [] {"datetime"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","beforeIncDate"), Operator.LessThanEqualTo, new [] {"datetime"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","after"), Operator.GreaterThan, new [] {"datetime"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","afterIncDate"), Operator.GreaterThanEqualTo, new [] {"datetime"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","equals"), Operator.Equals, new [] {"int"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","doesNotEqual"), Operator.NotEquals, new [] {"int"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","contains"), Operator.Contains, new [] {"string"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","doesNotContain"), Operator.NotContains, new [] {"string"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","greaterThan"), Operator.GreaterThan, new [] {"int"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","greaterThanEqual"), Operator.GreaterThanEqualTo, new [] {"int"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","lessThan"), Operator.LessThan, new [] {"int"}),
|
|
|
|
|
|
new OperatorTerm(_localizedTextService.Localize("template","lessThanEqual"), Operator.LessThanEqualTo, new [] {"int"})
|
2019-01-21 18:50:27 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<PropertyModel> Properties => new List<PropertyModel>
|
2014-06-09 16:42:57 +01:00
|
|
|
|
{
|
2021-07-05 20:58:04 +02:00
|
|
|
|
new PropertyModel { Name = _localizedTextService.Localize("template","id"), Alias = "Id", Type = "int" },
|
|
|
|
|
|
new PropertyModel { Name = _localizedTextService.Localize("template","name"), Alias = "Name", Type = "string" },
|
|
|
|
|
|
new PropertyModel { Name = _localizedTextService.Localize("template","createdDate"), Alias = "CreateDate", Type = "datetime" },
|
|
|
|
|
|
new PropertyModel { Name = _localizedTextService.Localize("template","lastUpdatedDate"), Alias = "UpdateDate", Type = "datetime" }
|
2019-01-21 18:50:27 +01:00
|
|
|
|
};
|
2014-06-09 16:42:57 +01:00
|
|
|
|
|
|
|
|
|
|
public QueryResultModel PostTemplateQuery(QueryModel model)
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2019-01-21 18:50:27 +01:00
|
|
|
|
var queryExpression = new StringBuilder();
|
2022-03-31 15:57:23 +02:00
|
|
|
|
IEnumerable<IPublishedContent>? contents;
|
2019-01-17 13:33:44 +01:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
if (model == null)
|
|
|
|
|
|
{
|
2022-03-31 15:57:23 +02:00
|
|
|
|
contents = _publishedContentQuery.ContentAtRoot().FirstOrDefault()?.Children(_variationContextAccessor);
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append("Umbraco.ContentAtRoot().FirstOrDefault().Children()");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
contents = PostTemplateValue(model, queryExpression);
|
|
|
|
|
|
}
|
2017-05-30 10:50:09 +02:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
// timing should be fairly correct, due to the fact that all the linq statements are yield returned.
|
|
|
|
|
|
var timer = new Stopwatch();
|
|
|
|
|
|
timer.Start();
|
2022-03-31 15:57:23 +02:00
|
|
|
|
var results = contents?.ToList() ?? new List<IPublishedContent>();
|
2019-01-21 18:50:27 +01:00
|
|
|
|
timer.Stop();
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
return new QueryResultModel
|
|
|
|
|
|
{
|
|
|
|
|
|
QueryExpression = queryExpression.ToString(),
|
|
|
|
|
|
ResultCount = results.Count,
|
|
|
|
|
|
ExecutionTime = timer.ElapsedMilliseconds,
|
2020-08-26 08:05:15 +02:00
|
|
|
|
SampleResults = results.Take(20).Select(x => new TemplateQueryResult
|
|
|
|
|
|
{
|
|
|
|
|
|
Icon = "icon-document",
|
|
|
|
|
|
Name = x.Name
|
|
|
|
|
|
})
|
2019-01-21 18:50:27 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2022-03-31 15:57:23 +02:00
|
|
|
|
private IEnumerable<IPublishedContent>? PostTemplateValue(QueryModel model, StringBuilder queryExpression)
|
2019-01-21 18:50:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
var indent = Environment.NewLine + " ";
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
// set the source
|
2022-03-31 15:57:23 +02:00
|
|
|
|
IPublishedContent? sourceDocument;
|
2019-01-21 18:50:27 +01:00
|
|
|
|
if (model.Source != null && model.Source.Id > 0)
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
sourceDocument = _publishedContentQuery.Content(model.Source.Id);
|
2019-01-17 13:33:44 +01:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
if (sourceDocument == null)
|
|
|
|
|
|
queryExpression.AppendFormat("Umbraco.Content({0})", model.Source.Id);
|
|
|
|
|
|
else
|
|
|
|
|
|
queryExpression.AppendFormat("Umbraco.Content(Guid.Parse(\"{0}\"))", sourceDocument.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
sourceDocument = _publishedContentQuery.ContentAtRoot().FirstOrDefault();
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append("Umbraco.ContentAtRoot().FirstOrDefault()");
|
2014-06-08 09:51:06 -07:00
|
|
|
|
}
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
// get children, optionally filtered by type
|
2022-03-31 15:57:23 +02:00
|
|
|
|
IEnumerable<IPublishedContent>? contents;
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append(indent);
|
|
|
|
|
|
if (model.ContentType != null && !model.ContentType.Alias.IsNullOrWhiteSpace())
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2019-01-21 18:50:27 +01:00
|
|
|
|
contents = sourceDocument == null
|
|
|
|
|
|
? Enumerable.Empty<IPublishedContent>()
|
2020-06-22 10:08:08 +02:00
|
|
|
|
: sourceDocument.ChildrenOfType(_variationContextAccessor, model.ContentType.Alias);
|
2019-03-20 13:29:39 +01:00
|
|
|
|
queryExpression.AppendFormat(".ChildrenOfType(\"{0}\")", model.ContentType.Alias);
|
2014-06-08 09:51:06 -07:00
|
|
|
|
}
|
2014-06-08 22:39:01 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-01-21 18:50:27 +01:00
|
|
|
|
contents = sourceDocument == null
|
|
|
|
|
|
? Enumerable.Empty<IPublishedContent>()
|
2019-12-19 10:43:00 +01:00
|
|
|
|
: sourceDocument.Children(_variationContextAccessor);
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append(".Children()");
|
2014-06-08 22:39:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-31 15:57:23 +02:00
|
|
|
|
if (model.Filters is not null)
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2022-03-31 15:57:23 +02:00
|
|
|
|
// 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");
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2019-01-26 10:52:19 -05:00
|
|
|
|
//for review - this uses a tonized query rather then the normal linq query.
|
2022-03-31 15:57:23 +02:00
|
|
|
|
contents = contents?.Where(operation.Compile());
|
|
|
|
|
|
queryExpression.Append(indent);
|
|
|
|
|
|
queryExpression.AppendFormat(".Where({0})", operation);
|
|
|
|
|
|
}
|
2019-01-21 18:50:27 +01:00
|
|
|
|
}
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
// always add IsVisible() to the query
|
2022-03-31 15:57:23 +02:00
|
|
|
|
contents = contents?.Where(x => x.IsVisible(_publishedValueFallback));
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append(indent);
|
|
|
|
|
|
queryExpression.Append(".Where(x => x.IsVisible())");
|
2018-07-27 13:01:54 +01:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
// apply sort
|
2022-03-31 15:57:23 +02:00
|
|
|
|
if (model.Sort != null && (!model.Sort.Property?.Alias.IsNullOrWhiteSpace() ?? false))
|
2019-01-21 18:50:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
contents = SortByDefaultPropertyValue(contents, model.Sort);
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append(indent);
|
|
|
|
|
|
queryExpression.AppendFormat(model.Sort.Direction == "ascending"
|
|
|
|
|
|
? ".OrderBy(x => x.{0})"
|
|
|
|
|
|
: ".OrderByDescending(x => x.{0})"
|
2022-04-01 14:35:18 +02:00
|
|
|
|
, model.Sort?.Property?.Alias);
|
2014-06-09 13:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
// take
|
|
|
|
|
|
if (model.Take > 0)
|
2017-05-30 10:50:09 +02:00
|
|
|
|
{
|
2022-03-31 15:57:23 +02:00
|
|
|
|
contents = contents?.Take(model.Take);
|
2019-01-21 18:50:27 +01:00
|
|
|
|
queryExpression.Append(indent);
|
|
|
|
|
|
queryExpression.AppendFormat(".Take({0})", model.Take);
|
|
|
|
|
|
}
|
2019-01-17 13:33:44 +01:00
|
|
|
|
|
2019-01-21 18:50:27 +01:00
|
|
|
|
return contents;
|
2014-06-08 09:51:06 -07:00
|
|
|
|
}
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
private object GetConstraintValue(QueryCondition condition)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (condition.Property.Type)
|
|
|
|
|
|
{
|
2017-05-30 10:50:09 +02:00
|
|
|
|
case "int":
|
2021-09-14 22:13:39 +02:00
|
|
|
|
return int.Parse(condition.ConstraintValue, CultureInfo.InvariantCulture);
|
2014-06-09 23:23:31 +01:00
|
|
|
|
case "datetime":
|
|
|
|
|
|
DateTime dt;
|
|
|
|
|
|
return DateTime.TryParse(condition.ConstraintValue, out dt) ? dt : DateTime.Today;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return condition.ConstraintValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-31 15:57:23 +02:00
|
|
|
|
private IEnumerable<IPublishedContent>? SortByDefaultPropertyValue(IEnumerable<IPublishedContent>? contents, SortExpression sortExpression)
|
2014-06-09 09:14:26 -07:00
|
|
|
|
{
|
2022-03-31 15:57:23 +02:00
|
|
|
|
switch (sortExpression.Property?.Alias)
|
2014-06-09 09:14:26 -07:00
|
|
|
|
{
|
2017-05-30 10:50:09 +02:00
|
|
|
|
case "id":
|
2014-06-09 23:23:31 +01:00
|
|
|
|
return sortExpression.Direction == "ascending"
|
2022-03-31 15:57:23 +02:00
|
|
|
|
? contents?.OrderBy(x => x.Id)
|
|
|
|
|
|
: contents?.OrderByDescending(x => x.Id);
|
2017-05-30 10:50:09 +02:00
|
|
|
|
case "createDate":
|
2014-06-09 23:23:31 +01:00
|
|
|
|
return sortExpression.Direction == "ascending"
|
2022-03-31 15:57:23 +02:00
|
|
|
|
? contents?.OrderBy(x => x.CreateDate)
|
|
|
|
|
|
: contents?.OrderByDescending(x => x.CreateDate);
|
2014-06-09 09:14:26 -07:00
|
|
|
|
case "publishDate":
|
2014-06-09 23:23:31 +01:00
|
|
|
|
return sortExpression.Direction == "ascending"
|
2022-03-31 15:57:23 +02:00
|
|
|
|
? contents?.OrderBy(x => x.UpdateDate)
|
|
|
|
|
|
: contents?.OrderByDescending(x => x.UpdateDate);
|
2014-06-09 09:14:26 -07:00
|
|
|
|
case "name":
|
2014-06-09 23:23:31 +01:00
|
|
|
|
return sortExpression.Direction == "ascending"
|
2022-03-31 15:57:23 +02:00
|
|
|
|
? contents?.OrderBy(x => x.Name)
|
|
|
|
|
|
: contents?.OrderByDescending(x => x.Name);
|
2017-05-30 10:50:09 +02:00
|
|
|
|
default:
|
2014-06-09 23:23:31 +01:00
|
|
|
|
return sortExpression.Direction == "ascending"
|
2022-03-31 15:57:23 +02:00
|
|
|
|
? contents?.OrderBy(x => x.Name)
|
|
|
|
|
|
: contents?.OrderByDescending(x => x.Name);
|
2014-06-09 09:14:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
2014-06-09 16:42:57 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a list of all content types
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public IEnumerable<ContentTypeModel> GetContentTypes()
|
|
|
|
|
|
{
|
2020-06-22 10:08:08 +02:00
|
|
|
|
var contentTypes = _contentTypeService.GetAll()
|
2022-03-31 15:57:23 +02:00
|
|
|
|
.Select(x => new ContentTypeModel { Alias = x.Alias, Name = _localizedTextService.Localize("template", "contentOfType", tokens: new string[] { x.Name ?? string.Empty }) })
|
2016-09-01 19:06:08 +02:00
|
|
|
|
.OrderBy(x => x.Name).ToList();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
2021-07-05 20:58:04 +02:00
|
|
|
|
contentTypes.Insert(0, new ContentTypeModel { Alias = string.Empty, Name = _localizedTextService.Localize("template", "allContent") });
|
2014-06-09 23:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
return contentTypes;
|
2014-06-09 16:42:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a collection of allowed properties.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public IEnumerable<PropertyModel> GetAllowedProperties()
|
|
|
|
|
|
{
|
2014-10-23 18:52:11 +10:00
|
|
|
|
return Properties.OrderBy(x => x.Name);
|
2014-06-09 16:42:57 +01:00
|
|
|
|
}
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a collection of constraint conditions that can be used in the query
|
|
|
|
|
|
/// </summary>
|
2014-06-09 16:42:57 +01:00
|
|
|
|
public IEnumerable<object> GetFilterConditions()
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2014-10-23 18:52:11 +10:00
|
|
|
|
return Terms;
|
2014-06-08 09:51:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|