Skip to content

Commit

Permalink
Fixed not working sorting with ES >= 5.0 (#245)
Browse files Browse the repository at this point in the history
* Fixed not working sorting with ES >= 5.0

* Removed not needed import

* Use of correct init

* FIxed StyleCI

* Fixed test

* Code clean up
  • Loading branch information
trickreich authored and wachterjohannes committed Aug 16, 2017
1 parent 4ae8eeb commit 1506a97
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 14 deletions.
70 changes: 57 additions & 13 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
use Sulu\Bundle\ArticleBundle\ListBuilder\ElasticSearchFieldDescriptor;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Component\Content\Form\Exception\InvalidFormException;
use Sulu\Component\Content\Mapper\ContentMapperInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\Rest\Exception\MissingParameterException;
use Sulu\Component\Rest\Exception\RestException;
use Sulu\Component\Rest\ListBuilder\FieldDescriptor;
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
use Sulu\Component\Rest\RequestParametersTrait;
use Sulu\Component\Rest\RestController;
Expand All @@ -54,25 +54,44 @@ class ArticleController extends RestController implements ClassResourceInterface
/**
* Create field-descriptor array.
*
* @return FieldDescriptor[]
* @return ElasticSearchFieldDescriptor[]
*/
private function getFieldDescriptors()
{
return [
'uuid' => new FieldDescriptor('uuid', 'public.id', true),
'typeTranslation' => new FieldDescriptor(
'uuid' => new ElasticSearchFieldDescriptor('id', null, 'public.id', false, false, 'string', '', '', false),
'typeTranslation' => new ElasticSearchFieldDescriptor(
'typeTranslation',
'typeTranslation.raw',
'sulu_article.list.type',
!$this->getParameter('sulu_article.display_tab_all'),
false
),
'title' => new FieldDescriptor('title', 'public.title', false, true),
'creatorFullName' => new FieldDescriptor('creatorFullName', 'sulu_article.list.creator', true, false),
'changerFullName' => new FieldDescriptor('changerFullName', 'sulu_article.list.changer', false, false),
'authorFullName' => new FieldDescriptor('authorFullName', 'sulu_article.author', false, false),
'created' => new FieldDescriptor('created', 'public.created', true, false, 'datetime'),
'changed' => new FieldDescriptor('changed', 'public.changed', false, false, 'datetime'),
'authored' => new FieldDescriptor('authored', 'sulu_article.authored', false, false, 'date'),
'title' => new ElasticSearchFieldDescriptor('title', 'title.raw', 'public.title', false, true),
'creatorFullName' => new ElasticSearchFieldDescriptor(
'creatorFullName',
'creatorFullName.raw',
'sulu_article.list.creator',
true,
false
),
'changerFullName' => new ElasticSearchFieldDescriptor(
'changerFullName',
'changerFullName.raw',
'sulu_article.list.changer',
false,
false
),
'authorFullName' => new ElasticSearchFieldDescriptor(
'authorFullName',
'authorFullName.raw',
'sulu_article.author',
false,
false
),
'created' => new ElasticSearchFieldDescriptor('created', null, 'public.created', true, false, 'datetime'),
'changed' => new ElasticSearchFieldDescriptor('changed', null, 'public.changed', false, false, 'datetime'),
'authored' => new ElasticSearchFieldDescriptor('authored', null, 'sulu_article.authored', false, false, 'date'),
];
}

Expand Down Expand Up @@ -146,9 +165,11 @@ public function cgetAction(Request $request)

$count = $repository->count($search);

if (null !== $restHelper->getSortColumn()) {
if (null !== $restHelper->getSortColumn() &&
$sortField = $this->getSortFieldName($restHelper->getSortColumn())
) {
$search->addSort(
new FieldSort($this->uncamelize($restHelper->getSortColumn()), $restHelper->getSortOrder())
new FieldSort($sortField, $restHelper->getSortOrder())
);
}

Expand Down Expand Up @@ -345,11 +366,13 @@ public function postTriggerAction($uuid, Request $request)
$this->getDocumentManager()->flush();

$data = $this->getDocumentManager()->find($uuid, $locale);

break;
case 'remove-draft':
$data = $this->getDocumentManager()->find($uuid, $locale);
$this->getDocumentManager()->removeDraft($data, $locale);
$this->getDocumentManager()->flush();

break;
case 'copy-locale':
$destLocales = $this->getRequestParameter($request, 'dest', true);
Expand All @@ -366,6 +389,7 @@ public function postTriggerAction($uuid, Request $request)
$this->getMapper()->copyLanguage($uuid, $userId, null, $locale, $destLocales);

$data = $this->getDocumentManager()->find($uuid, $locale);

break;
case 'copy':
/** @var ArticleDocument $document */
Expand All @@ -374,13 +398,15 @@ public function postTriggerAction($uuid, Request $request)
$this->getDocumentManager()->flush();

$data = $this->getDocumentManager()->find($copiedPath, $locale);

break;
case 'order':
$this->orderPages($this->getRequestParameter($request, 'pages', true), $locale);
$this->getDocumentManager()->flush();
$this->getDocumentManager()->clear();

$data = $this->getDocumentManager()->find($uuid, $locale);

break;
default:
throw new RestException('Unrecognized action: ' . $action);
Expand Down Expand Up @@ -494,10 +520,28 @@ private function handleActionParameter($actionParameter, $document, $locale)
switch ($actionParameter) {
case 'publish':
$this->getDocumentManager()->publish($document, $locale);

break;
}
}

/**
* @param string $sortBy
*
* @return null|string
*/
private function getSortFieldName($sortBy)
{
$sortBy = $this->uncamelize($sortBy);
$fieldDescriptors = $this->getFieldDescriptors();

if (array_key_exists($sortBy, $fieldDescriptors)) {
return $fieldDescriptors[$sortBy]->getSortField();
}

return null;
}

/**
* Converts camel case string into normalized string with underscore.
*
Expand Down
63 changes: 63 additions & 0 deletions ListBuilder/ElasticSearchFieldDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\ListBuilder;

use Sulu\Component\Rest\ListBuilder\FieldDescriptor;

/**
* Extends the default FieldDescriptor with the property sort field to configure it.
* Default is the name.
*/
class ElasticSearchFieldDescriptor extends FieldDescriptor
{
/**
* @var string
*/
private $sortField;

public function __construct(
$name,
$sortField = null,
$translation = null,
$disabled = false,
$default = false,
$type = '',
$width = '',
$minWidth = '',
$sortable = true,
$editable = false,
$cssClass = ''
) {
$this->sortField = $sortField ? $sortField : $name;

parent::__construct(
$name,
$translation,
$disabled,
$default,
$type,
$width,
$minWidth,
$sortable,
$editable,
$cssClass
);
}

/**
* @return string
*/
public function getSortField()
{
return $this->sortField;
}
}
2 changes: 1 addition & 1 deletion Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public function testCGetSort()
$this->flush();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/articles?locale=de&sortBy=title.raw&sortOrder=desc&type=blog');
$client->request('GET', '/api/articles?locale=de&sortBy=title&sortOrder=desc&type=blog');

$this->assertHttpStatusCode(200, $client->getResponse());

Expand Down

0 comments on commit 1506a97

Please sign in to comment.