2014-06-08 09:51:06 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Web.Mvc;
|
|
|
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2014-06-09 16:42:57 +01:00
|
|
|
|
using Umbraco.Web.WebApi;
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
|
{
|
2014-06-09 13:18:35 +01:00
|
|
|
|
using System;
|
2014-06-08 22:39:01 +01:00
|
|
|
|
using System.Diagnostics;
|
2014-06-09 23:23:31 +01:00
|
|
|
|
using System.Diagnostics.Eventing.Reader;
|
|
|
|
|
|
using System.Drawing;
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
|
|
|
|
|
using global::umbraco;
|
2014-06-09 23:23:31 +01:00
|
|
|
|
using global::umbraco.cms.presentation;
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
|
|
|
|
|
using Umbraco.Web.Dynamics;
|
2014-06-26 11:24:57 +02:00
|
|
|
|
using Umbraco.Web.Models.TemplateQuery;
|
2014-06-09 23:23:31 +01:00
|
|
|
|
using System.Linq.Expressions;
|
2014-06-09 16:42:57 +01:00
|
|
|
|
|
2014-06-10 08:40:33 +01:00
|
|
|
|
using Umbraco.Web.Models;
|
|
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The API controller used for building content queries within the template
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PluginController("UmbracoApi")]
|
|
|
|
|
|
[DisableBrowserCache]
|
2014-06-09 16:42:57 +01:00
|
|
|
|
[JsonCamelCaseFormatter]
|
2014-06-08 09:51:06 -07:00
|
|
|
|
public class TemplateQueryController : UmbracoAuthorizedJsonController
|
|
|
|
|
|
{
|
|
|
|
|
|
public TemplateQueryController()
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
public TemplateQueryController(UmbracoContext umbracoContext)
|
|
|
|
|
|
:base(umbracoContext)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
2014-06-09 16:42:57 +01:00
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
private static readonly IEnumerable<OperathorTerm> _terms = new List<OperathorTerm>()
|
|
|
|
|
|
{
|
|
|
|
|
|
new OperathorTerm("is", Operathor.Equals, new [] {"string"}),
|
|
|
|
|
|
new OperathorTerm("is not", Operathor.NotEquals, new [] {"string"}),
|
|
|
|
|
|
new OperathorTerm("before", Operathor.LessThan, new [] {"datetime"}),
|
|
|
|
|
|
new OperathorTerm("before (including selected date)", Operathor.LessThanEqualTo, new [] {"datetime"}),
|
|
|
|
|
|
new OperathorTerm("after", Operathor.GreaterThan, new [] {"datetime"}),
|
|
|
|
|
|
new OperathorTerm("after (including selected date)", Operathor.GreaterThanEqualTo, new [] {"datetime"}),
|
|
|
|
|
|
new OperathorTerm("equals", Operathor.Equals, new [] {"int"}),
|
|
|
|
|
|
new OperathorTerm("does not equal", Operathor.NotEquals, new [] {"int"}),
|
|
|
|
|
|
new OperathorTerm("contains", Operathor.Contains, new [] {"string"}),
|
|
|
|
|
|
new OperathorTerm("does not contain", Operathor.NotContains, new [] {"string"}),
|
|
|
|
|
|
new OperathorTerm("greater than", Operathor.GreaterThan, new [] {"int"}),
|
|
|
|
|
|
new OperathorTerm("greater than or equal to", Operathor.GreaterThanEqualTo, new [] {"int"}),
|
|
|
|
|
|
new OperathorTerm("less than", Operathor.LessThan, new [] {"int"}),
|
|
|
|
|
|
new OperathorTerm("less than or equal to", Operathor.LessThanEqualTo, new [] {"int"})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2014-06-09 16:42:57 +01:00
|
|
|
|
private static readonly IEnumerable<PropertyModel> _properties = new List<PropertyModel>()
|
|
|
|
|
|
{
|
2014-06-10 16:43:28 +01:00
|
|
|
|
new PropertyModel() { Name = "Id", Alias = "Id", Type = "int" },
|
|
|
|
|
|
new PropertyModel() { Name = "Name", Alias = "Name", Type = "string" },
|
2014-06-09 09:14:26 -07:00
|
|
|
|
//new PropertyModel() { Name = "Url", Alias = "url", Type = "string" },
|
2014-06-10 16:43:28 +01:00
|
|
|
|
new PropertyModel() { Name = "Created Date", Alias = "CreateDate", Type = "datetime" },
|
|
|
|
|
|
new PropertyModel() { Name = "Last Updated Date", Alias = "UpdateDate", Type = "datetime" }
|
2014-06-08 10:20:34 -07:00
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
};
|
2014-06-09 16:42:57 +01:00
|
|
|
|
|
|
|
|
|
|
public QueryResultModel PostTemplateQuery(QueryModel model)
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
|
|
|
|
|
var umbraco = new UmbracoHelper(UmbracoContext);
|
|
|
|
|
|
|
|
|
|
|
|
var queryResult = new QueryResultModel();
|
|
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
2014-06-09 23:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
sb.Append("CurrentPage.Site()");
|
|
|
|
|
|
|
2014-06-08 22:39:01 +01:00
|
|
|
|
var timer = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
|
|
timer.Start();
|
|
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
var currentPage = umbraco.TypedContentAtRoot().FirstOrDefault();
|
2014-06-08 22:39:01 +01:00
|
|
|
|
timer.Stop();
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
|
|
|
|
|
var pointerNode = currentPage;
|
|
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
// adjust the "FROM"
|
2014-06-09 16:42:57 +01:00
|
|
|
|
if (model != null && model.Source.Id > 0)
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2014-06-09 16:42:57 +01:00
|
|
|
|
var targetNode = umbraco.TypedContent(model.Source.Id);
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-09 13:18:35 +01:00
|
|
|
|
var aliases = this.GetChildContentTypeAliases(targetNode, currentPage).Reverse();
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-09 13:18:35 +01:00
|
|
|
|
foreach (var contentTypeAlias in aliases)
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Start();
|
|
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
pointerNode = pointerNode.FirstChild(x => x.DocumentTypeAlias == contentTypeAlias);
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
if (pointerNode == null) break;
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
timer.Stop();
|
|
|
|
|
|
|
2014-06-09 13:18:35 +01:00
|
|
|
|
sb.AppendFormat(".FirstChild(\"{0}\")", contentTypeAlias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-09 23:23:31 +01:00
|
|
|
|
if (pointerNode == null || pointerNode.Id != model.Source.Id)
|
|
|
|
|
|
{
|
|
|
|
|
|
// we did not find the path
|
|
|
|
|
|
sb.Clear();
|
|
|
|
|
|
sb.AppendFormat("Umbraco.Content({0})", model.Source.Id);
|
|
|
|
|
|
pointerNode = targetNode;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TYPE to return if filtered by type
|
|
|
|
|
|
IEnumerable<IPublishedContent> contents;
|
2014-06-09 16:42:57 +01:00
|
|
|
|
if (model != null && string.IsNullOrEmpty(model.ContentType.Alias) == false)
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2014-06-08 22:39:01 +01:00
|
|
|
|
timer.Start();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-09 16:42:57 +01:00
|
|
|
|
contents = pointerNode.Children.OfTypes(new[] { model.ContentType.Alias });
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-08 22:39:01 +01:00
|
|
|
|
timer.Stop();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
// TODO change to .Children({0})
|
2014-06-09 16:42:57 +01:00
|
|
|
|
sb.AppendFormat(".Children(\"{0}\")", model.ContentType.Alias);
|
2014-06-08 09:51:06 -07:00
|
|
|
|
}
|
2014-06-08 22:39:01 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Start();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
contents = pointerNode.Children;
|
2014-06-08 22:39:01 +01:00
|
|
|
|
timer.Stop();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
sb.Append(".Children");
|
2014-06-08 22:39:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var clause = string.Empty;
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
|
|
|
|
|
// WHERE
|
2014-06-09 23:23:31 +01:00
|
|
|
|
var token = 0;
|
2014-06-10 14:05:57 +01:00
|
|
|
|
|
|
|
|
|
|
if (model != null)
|
2014-06-08 09:51:06 -07:00
|
|
|
|
{
|
2014-06-10 14:05:57 +01:00
|
|
|
|
model.Filters = model.Filters.Where(x => x.ConstraintValue != null);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var condition in model.Filters)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(string.IsNullOrEmpty( condition.ConstraintValue)) continue;
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
var operation = condition.BuildCondition(token);
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
clause = string.IsNullOrEmpty(clause) ? operation : string.Concat(new[] { clause, " && ", operation });
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
token++;
|
|
|
|
|
|
}
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
if (string.IsNullOrEmpty(clause) == false)
|
|
|
|
|
|
{
|
2014-06-09 23:23:31 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
timer.Start();
|
2014-06-09 23:23:31 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
//clause = "Visible && " + clause;
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
contents = contents.AsQueryable().Where(clause, model.Filters.Select(this.GetConstraintValue).ToArray());
|
|
|
|
|
|
// contents = contents.Where(clause, values.ToArray());
|
|
|
|
|
|
contents = contents.Where(x => x.IsVisible());
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
timer.Stop();
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
clause = string.Format("\"Visible && {0}\",{1}", clause,
|
|
|
|
|
|
string.Join(",", model.Filters.Select(x => x.Property.Type == "string" ?
|
|
|
|
|
|
string.Format("\"{0}\"", x.ConstraintValue) : x.ConstraintValue).ToArray()));
|
2014-06-08 22:39:01 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
sb.AppendFormat(".Where({0})", clause);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Start();
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
contents = contents.Where(x => x.IsVisible());
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
timer.Stop();
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
sb.Append(".Where(\"Visible\")");
|
2014-06-10 08:40:33 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
}
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
if (model.Sort != null && string.IsNullOrEmpty(model.Sort.Property.Alias) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Start();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
contents = this.SortByDefaultPropertyValue(contents, model.Sort);
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
timer.Stop();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
var direction = model.Sort.Direction == "ascending" ? string.Empty : " desc";
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 16:43:28 +01:00
|
|
|
|
sb.AppendFormat(".OrderBy(\"{0}{1}\")", model.Sort.Property.Alias, direction);
|
2014-06-10 14:05:57 +01:00
|
|
|
|
}
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
if (model.Take > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Start();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
contents = contents.Take(model.Take);
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
timer.Stop();
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-10 14:05:57 +01:00
|
|
|
|
sb.AppendFormat(".Take({0})", model.Take);
|
|
|
|
|
|
}
|
2014-06-09 13:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
queryResult.QueryExpression = sb.ToString();
|
2014-06-08 22:39:01 +01:00
|
|
|
|
queryResult.ExecutionTime = timer.ElapsedMilliseconds;
|
|
|
|
|
|
queryResult.ResultCount = contents.Count();
|
|
|
|
|
|
queryResult.SampleResults = contents.Take(20).Select(x => new TemplateQueryResult()
|
|
|
|
|
|
{
|
|
|
|
|
|
Icon = "icon-file",
|
|
|
|
|
|
Name = x.Name
|
|
|
|
|
|
});
|
2014-06-08 09:51:06 -07:00
|
|
|
|
|
2014-06-09 13:18:35 +01:00
|
|
|
|
|
2014-06-08 09:51:06 -07:00
|
|
|
|
return queryResult;
|
|
|
|
|
|
}
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "int" :
|
|
|
|
|
|
return int.Parse(condition.ConstraintValue);
|
|
|
|
|
|
case "datetime":
|
|
|
|
|
|
DateTime dt;
|
|
|
|
|
|
return DateTime.TryParse(condition.ConstraintValue, out dt) ? dt : DateTime.Today;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return condition.ConstraintValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IPublishedContent> SortByDefaultPropertyValue(IEnumerable<IPublishedContent> contents, SortExpression sortExpression)
|
2014-06-09 09:14:26 -07:00
|
|
|
|
{
|
2014-06-09 23:23:31 +01:00
|
|
|
|
switch (sortExpression.Property.Alias)
|
2014-06-09 09:14:26 -07:00
|
|
|
|
{
|
|
|
|
|
|
case "id" :
|
2014-06-09 23:23:31 +01:00
|
|
|
|
return sortExpression.Direction == "ascending"
|
|
|
|
|
|
? contents.OrderBy(x => x.Id)
|
|
|
|
|
|
: contents.OrderByDescending(x => x.Id);
|
2014-06-09 09:14:26 -07:00
|
|
|
|
case "createDate" :
|
2014-06-09 23:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
return sortExpression.Direction == "ascending"
|
|
|
|
|
|
? 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"
|
|
|
|
|
|
? 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"
|
|
|
|
|
|
? contents.OrderBy(x => x.Name)
|
|
|
|
|
|
: contents.OrderByDescending(x => x.Name);
|
2014-06-09 09:14:26 -07:00
|
|
|
|
default :
|
2014-06-09 23:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
return sortExpression.Direction == "ascending"
|
|
|
|
|
|
? contents.OrderBy(x => x.Name)
|
|
|
|
|
|
: contents.OrderByDescending(x => x.Name);
|
2014-06-09 09:14:26 -07:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-09 13:18:35 +01:00
|
|
|
|
private IEnumerable<string> GetChildContentTypeAliases(IPublishedContent targetNode, IPublishedContent current)
|
|
|
|
|
|
{
|
|
|
|
|
|
var aliases = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
if (targetNode.Id == current.Id) return aliases;
|
|
|
|
|
|
if (targetNode.Id != current.Id)
|
|
|
|
|
|
{
|
|
|
|
|
|
aliases.Add(targetNode.DocumentTypeAlias);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
aliases.AddRange(this.GetChildContentTypeAliases(targetNode.Parent, current));
|
|
|
|
|
|
|
|
|
|
|
|
return aliases;
|
|
|
|
|
|
}
|
2014-06-09 16:42:57 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a list of all content types
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public IEnumerable<ContentTypeModel> GetContentTypes()
|
|
|
|
|
|
{
|
2014-06-09 23:23:31 +01:00
|
|
|
|
var contentTypes =
|
2014-06-09 16:42:57 +01:00
|
|
|
|
ApplicationContext.Services.ContentTypeService.GetAllContentTypes()
|
2014-06-09 23:23:31 +01:00
|
|
|
|
.Select(x => new ContentTypeModel() { Alias = x.Alias, Name = x.Name })
|
|
|
|
|
|
.OrderBy(x => x.Name).ToList();
|
|
|
|
|
|
contentTypes.Insert(0, new ContentTypeModel() { Alias = string.Empty, Name = "Everything" });
|
|
|
|
|
|
|
|
|
|
|
|
return contentTypes;
|
2014-06-09 16:42:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a collection of allowed properties.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public IEnumerable<PropertyModel> GetAllowedProperties()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _properties.OrderBy(x => x.Name);
|
|
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
|
|
return _terms;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|