protected virtual IQueryOver<PagesView, PagesView> FilterQuery(IQueryOver<PagesView, PagesView> query,
PagesFilter request, Junction hasnotSeoDisjunction)
{
PageProperties alias = null;
if (!request.IncludeArchived)
{
query = query.Where(() => !alias.IsArchived);
}
if (request.OnlyMasterPages)
{
query = query.Where(() => alias.IsMasterPage);
}
else if (!request.IncludeMasterPages)
{
query = query.Where(() => !alias.IsMasterPage);
}
if (!string.IsNullOrWhiteSpace(request.SearchQuery))
{
var searchQuery = string.Format("%{0}%", request.SearchQuery);
query = query.Where(Restrictions.Disjunction()
.Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.Title), searchQuery))
.Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.PageUrl), searchQuery))
.Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.MetaTitle), searchQuery))
.Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.MetaDescription), searchQuery))
.Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.MetaKeywords), searchQuery)));
}
if (request.LanguageId.HasValue)
{
if (request.LanguageId.Value.HasDefaultValue())
{
query = query.Where(Restrictions.IsNull(Projections.Property(() => alias.Language.Id)));
}
else
{
query = query.Where(Restrictions.Eq(Projections.Property(() => alias.Language.Id), request.LanguageId.Value));
}
}
if (request.Tags != null)
{
foreach (var tagKeyValue in request.Tags)
{
var id = tagKeyValue.Key.ToGuidOrDefault();
query = query.WithSubquery.WhereExists(QueryOver.Of<PageTag>().Where(tag => tag.Tag.Id == id && tag.Page.Id == alias.Id).Select(tag => 1));
}
}
if (request.Categories != null)
{
var categories = request.Categories.Select(c => new Guid(c.Key)).Distinct().ToList();
foreach (var category in categories)
{
var childCategories = categoryService.GetChildCategoriesIds(category).ToArray();
query = query.WithSubquery.WhereExists(QueryOver.Of<PageCategory>().Where(cat => !cat.IsDeleted && cat.Page.Id == alias.Id).WhereRestrictionOn(cat => cat.Category.Id).IsIn(childCategories).Select(cat => 1));
}
}
if (request.Status.HasValue)
{
if (request.Status.Value == PageStatusFilterType.OnlyPublished)
{
query = query.Where(() => alias.Status == PageStatus.Published);
}
else if (request.Status.Value == PageStatusFilterType.OnlyUnpublished)
{
query = query.Where(() => alias.Status != PageStatus.Published);
}
else if (request.Status.Value == PageStatusFilterType.ContainingUnpublishedContents)
{
PageContent pageContentAlias = null;
Root.Models.Content contentAlias = null;
Root.Models.Content contentHistoryAlias = null;
var subQuery = QueryOver.Of(() => pageContentAlias)
.Inner.JoinAlias(() => pageContentAlias.Content, () => contentAlias, () => !contentAlias.IsDeleted)
.Left.JoinAlias(() => contentAlias.History, () => contentHistoryAlias, () => !contentHistoryAlias.IsDeleted)
.Where(() => pageContentAlias.Page.Id == alias.Id && !pageContentAlias.IsDeleted)
.Where(() => contentHistoryAlias.Status == ContentStatus.Draft || contentAlias.Status == ContentStatus.Draft)
.Select(p => 1);
query = query.WithSubquery.WhereExists(subQuery);
}
}
if (request.SeoStatus.HasValue)
{
if (request.SeoStatus.Value == SeoStatusFilterType.HasNotSeo)
{
query = query.Where(hasnotSeoDisjunction);
}
else
{
query = query.Where(Restrictions.Not(hasnotSeoDisjunction));
}
}
if (!string.IsNullOrWhiteSpace(request.Layout))
{
Guid id;
var length = request.Layout.Length - 2;
if (request.Layout.StartsWith("m-", StringComparison.Ordinal) && Guid.TryParse(request.Layout.Substring(2, length), out id))
{
query = query.Where(() => alias.MasterPage.Id == id);
}
if (request.Layout.StartsWith("l-", StringComparison.Ordinal) && Guid.TryParse(request.Layout.Substring(2, length), out id))
{
query = query.Where(() => alias.Layout.Id == id);
}
}
if (request.ContentId.HasValue)
{
Root.Models.Content contentAlias = null;
ChildContent childContentAlias = null;
HtmlContent htmlContentAlias = null;
PageContent pageContentAlias = null;
var htmlChildContentSubQuery =
QueryOver.Of(() => htmlContentAlias)
.JoinAlias(h => h.ChildContents, () => childContentAlias)
.Where(() => htmlContentAlias.Id == contentAlias.Id)
.And(() => childContentAlias.Child.Id == request.ContentId.Value)
.Select(pageContent => 1);
var pageContentSubQuery = QueryOver.Of(() => pageContentAlias)
.JoinAlias(() => pageContentAlias.Content, () => contentAlias)
.And(() => pageContentAlias.Page.Id == alias.Id)
.And(() => !contentAlias.IsDeleted)
.And(() => !pageContentAlias.IsDeleted)
.And(Restrictions.Or(
Restrictions.Where(() => contentAlias.Id == request.ContentId.Value),
Subqueries.WhereExists(htmlChildContentSubQuery)
))
.Select(pageContent => 1);
query = query.WithSubquery.WhereExists(pageContentSubQuery);
}
return query;
}
}