WIP for better sortby content and taxonomy

This commit is contained in:
Néstor de Dios Fernández
2019-08-19 14:36:51 +02:00
parent b3c1ed0396
commit dd2ed29bb2
3 changed files with 61 additions and 19 deletions

View File

@@ -27,11 +27,12 @@ class ContentOverviewController extends TwigAwareController implements BackendZo
$page = (int) $request->query->get('page', 1);
$amountPerPage = $contentType->get('records_per_page', 10);
if ($request->query->get('sort') && $request->query->get('filter')) {
if ($request->query->get('sort') && $request->query->get('filter') && $request->query->get('taxonomy')) {
$sortBy = $request->query->get('sort');
$filter = $request->query->get('filter');
$taxonomy = $request->query->get('taxonomy');
$records = $contentRepository->findForListing($page, $amountPerPage, $contentType, false, $sortBy, $filter);
$records = $contentRepository->findForListing($page, $amountPerPage, $contentType, false, $sortBy, $filter, $taxonomy);
} elseif ($request->query->get('sort')) {
$sortBy = $request->query->get('sort');
$records = $contentRepository->findForListing($page, $amountPerPage, $contentType, false, $sortBy, '');

View File

@@ -34,7 +34,7 @@ class ContentRepository extends ServiceEntityRepository
return $this->createQueryBuilder('content');
}
public function findForListing(int $page, int $amountPerPage, ?ContentType $contentType = null, bool $onlyPublished = true, string $sortBy = '', string $filter = ''): Pagerfanta
public function findForListing(int $page, int $amountPerPage, ?ContentType $contentType = null, bool $onlyPublished = true, string $sortBy = '', string $filter = '', string $taxonomy = ''): Pagerfanta
{
$qb = $this->getQueryBuilder()
->addSelect('a')
@@ -55,12 +55,33 @@ class ContentRepository extends ServiceEntityRepository
->innerJoin('content.fields', 'f');
}
if($taxonomy) {
$qb->addSelect('t')
->innerJoin('content.taxonomies', 't')
->andWhere('slug', ':taxonomySlug')
->setParameter('taxonomySlug', $taxonomy);
}
if ($sortBy && \in_array($sortBy, $this->contentColumns, true)) {
$qb->orderBy('content.' . $sortBy);
} elseif (! empty($sortBy)) {
$qb->andWhere('f.name = :fieldname')
// First, create a querybuilder to get the fields that match the Query
$sortByQB = $this->getQueryBuilder()
->select('partial content.{id}');
$sortByQB->addSelect('f')
->innerJoin('content.fields', 'f')
->andWhere('f.name = :fieldname')
->setParameter('fieldname', $sortBy);
// These are the ID's of content we need.
$ids = array_column($sortByQB->getQuery()->getArrayResult(), 'id');
$qb->andWhere('content.id IN (:ids)')
->setParameter('ids', $ids)
->andWhere('f.name = :fieldname')
->setParameter('fieldname', $sortBy)
->orderBy('f.value');
->addOrderBy('f.value');
}
if ($filter) {
@@ -79,6 +100,7 @@ class ContentRepository extends ServiceEntityRepository
$qb->andWhere('content.id IN (:ids)')
->setParameter('ids', $ids);
}
dump($qb->getQuery()->getArrayResult());
return $this->createPaginator($qb->getQuery(), $page, $amountPerPage);
}

View File

@@ -47,8 +47,8 @@
'modifiedAt': 'Modified date', 'publishedAt': 'Published date', 'depublishedAt': 'Depublished date'
} %}
{% set sortBy = app.request.get('sort')|default() %}
{% set taxonomy = app.request.get('taxonomy')|default() %}
{% set filterValue = app.request.get('filter')|default() %}
<div class="card">
<div class="card-header">
{{ 'title.contentlisting'|trans }}
@@ -56,19 +56,38 @@
<div class="card-body">
<form>
<div class="form-group">
<p>
<strong>{{ 'listing.title_sortby'|trans }}</strong>:
<select class="form-control" name="sort">
<option value="" disabled {% if sortBy is empty %}selected{% endif %}>
{{ 'listing.option_select_item'|trans }}
</option>
{% for key, filterOption in filterOptions %}
<option value="{{ key }}" {% if sortBy == key %}selected{% endif %}>
{{ filterOption }}
</option>
{% endfor %}
</select>
</p>
{# Needs more work #}
{# <p>#}
{# <strong>{{ 'listing.title_sortby'|trans }}</strong>:#}
{# <select class="form-control" name="sort">#}
{# <option value="" disabled {% if sortBy is empty %}selected{% endif %}>#}
{# {{ 'listing.option_select_item'|trans }}#}
{# </option>#}
{# {% for key, filterOption in filterOptions %}#}
{# <option value="{{ key }}" {% if sortBy == key %}selected{% endif %}>#}
{# {{ filterOption }}#}
{# </option>#}
{# {% endfor %}#}
{# </select>#}
{# </p>#}
{# {% if contentType.get('taxonomy') %}#}
{# <p>#}
{# <strong>{{ 'listing.title_taxonomy'|trans }}</strong>:#}
{# <select class="form-control" name="taxonomy">#}
{# <option value="" disabled {% if sortBy is empty %}selected{% endif %}>#}
{# {{ 'listing.option_select_item'|trans }}#}
{# </option>#}
{# {% for taxonomy in contentType.get('taxonomy') %}#}
{# {% set taxonomyDefinition = config.get('taxonomies/' ~ taxonomy) %}#}
{# {% for key, taxonomyValue in taxonomyDefinition.options %}#}
{# <option value="{{ key }}" {% if taxonomy == key %}selected{% endif %}>#}
{# {{ taxonomyValue }}#}
{# </option>#}
{# {% endfor %}#}
{# {% endfor %}#}
{# </select>#}
{# </p>#}
{# {% endif %}#}
<p>
<strong>{{ 'listing.title_title'|trans }}</strong>:
<input class="form-control" type="text" name="filter" value="{{ filterValue }}"