Skip to content

Commit

Permalink
Smart content provider: Added sorting (#81)
Browse files Browse the repository at this point in the history
* Implemented single author

* Code clean up

* Fixed tests

* Added PHPCR migration

* Corrected file name

* Implemented sorting in article data provider

* Code clean up

* Added author selection overlay

* Added note

* Added note

* Added property param types

* Moved function to correct place

* Removed empty line

* Changed type

* Added test

* Fixed comment

* Own testcase

* StyleCI clean up
  • Loading branch information
trickreich authored and wachterjohannes committed Feb 23, 2017
1 parent 88eb0f5 commit d1ed2e7
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 14 deletions.
70 changes: 60 additions & 10 deletions Content/ArticleDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ public function getConfiguration()
->enablePagination()
->enablePresentAs()
->setDeepLink('article/{locale}/edit:{id}/details')
->enableSorting(
[
['column' => 'published', 'title' => 'sulu_article.smart-content.published'],
['column' => 'authored', 'title' => 'sulu_article.smart-content.authored'],
['column' => 'created', 'title' => 'sulu_article.smart-content.created'],
['column' => 'title', 'title' => 'sulu_article.smart-content.title'],
['column' => 'author_full_name', 'title' => 'sulu_article.smart-content.author-full-name'],
]
)
->getConfiguration();
}

Expand All @@ -105,9 +114,7 @@ public function resolveDataItems(
$page = 1,
$pageSize = null
) {
if (array_key_exists('type', $propertyParameter) && null !== ($type = $propertyParameter['type']->getValue())) {
$filters['type'] = $type;
}
$filters['types'] = $this->getTypesProperty($propertyParameter);

$queryResult = $this->getSearchResult($filters, $limit, $page, $pageSize, $options['locale']);

Expand All @@ -133,9 +140,7 @@ public function resolveResourceItems(
$page = 1,
$pageSize = null
) {
if (array_key_exists('type', $propertyParameter) && null !== ($type = $propertyParameter['type']->getValue())) {
$filters['type'] = $type;
}
$filters['types'] = $this->getTypesProperty($propertyParameter);

$queryResult = $this->getSearchResult($filters, $limit, $page, $pageSize, $options['locale']);

Expand Down Expand Up @@ -196,6 +201,7 @@ private function hasNextPage(DocumentIterator $queryResult, $limit, $page, $page
private function getSearchResult(array $filters, $limit, $page, $pageSize, $locale)
{
$repository = $this->searchManager->getRepository($this->articleDocumentClass);
/** @var Search $search */
$search = $repository->createSearch();

$query = new BoolQuery();
Expand All @@ -215,9 +221,12 @@ private function getSearchResult(array $filters, $limit, $page, $pageSize, $loca
$search->addQuery(new TermQuery('locale', $locale));
}

if (array_key_exists('type', $filters)) {
$query->add(new TermQuery('type', $filters['type']));
++$queriesCount;
if (array_key_exists('types', $filters) && $filters['types']) {
$typesQuery = new BoolQuery();
foreach ($filters['types'] as $typeFilter) {
$typesQuery->add(new TermQuery('type', $typeFilter), BoolQuery::SHOULD);
}
$search->addQuery($typesQuery);
}

if (0 === $queriesCount) {
Expand All @@ -232,11 +241,52 @@ private function getSearchResult(array $filters, $limit, $page, $pageSize, $loca
$search->setSize($limit);
}

$search->addSort(new FieldSort('title'));
if (array_key_exists('sortBy', $filters) && is_array($filters['sortBy'])) {
$sortMethod = array_key_exists('sortMethod', $filters) ? $filters['sortMethod'] : 'asc';
$this->appendSortBy($filters['sortBy'], $sortMethod, $search);
}

return $repository->execute($search);
}

/**
* Returns array with all types defined in property parameter.
*
* @param array $propertyParameter
*
* @return array
*/
private function getTypesProperty($propertyParameter)
{
$filterTypes = [];

if (array_key_exists('types', $propertyParameter)
&& null !== ($types = explode(',', $propertyParameter['types']->getValue()))
) {
foreach ($types as $type) {
$filterTypes[] = $type;
}
}

return $filterTypes;
}

/**
* Extension point to append order.
*
* @param array $sortBy
* @param string $sortMethod
* @param Search $search
*
* @return array parameters for query
*/
private function appendSortBy($sortBy, $sortMethod, $search)
{
foreach ($sortBy as $column) {
$search->addSort(new FieldSort($column, $sortMethod));
}
}

/**
* Add the pagination to given query.
*
Expand Down
20 changes: 20 additions & 0 deletions Resources/translations/sulu/backend.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@
<source>sulu_article.form.settings.changelog.authored-only</source>
<target>Verfasst am %(authored)s</target>
</trans-unit>
<trans-unit id="25" resname="sulu_article.smart-content.published">
<source>sulu_article.smart-content.published</source>
<target>Veröffentlicht am (Datum)</target>
</trans-unit>
<trans-unit id="26" resname="sulu_article.smart-content.authored">
<source>sulu_article.smart-content.authored</source>
<target>Verfasst am (Datum)</target>
</trans-unit>
<trans-unit id="27" resname="sulu_article.smart-content.created">
<source>sulu_article.smart-content.created</source>
<target>Erstellt am (Datum)</target>
</trans-unit>
<trans-unit id="28" resname="sulu_article.smart-content.title">
<source>sulu_article.smart-content.title</source>
<target>Titel (Alphabet)</target>
</trans-unit>
<trans-unit id="29" resname="sulu_article.smart-content.author-full-name">
<source>sulu_article.smart-content.author-full-name</source>
<target>Autor (Alphabet)</target>
</trans-unit>
</body>
</file>
</xliff>
20 changes: 20 additions & 0 deletions Resources/translations/sulu/backend.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@
<source>sulu_article.form.settings.changelog.authored-only</source>
<target>Authored at %(authored)s</target>
</trans-unit>
<trans-unit id="25" resname="sulu_article.smart-content.published">
<source>sulu_article.smart-content.published</source>
<target>Published (date)</target>
</trans-unit>
<trans-unit id="26" resname="sulu_article.smart-content.authored">
<source>sulu_article.smart-content.authored</source>
<target>Authored (date)</target>
</trans-unit>
<trans-unit id="27" resname="sulu_article.smart-content.created">
<source>sulu_article.smart-content.created</source>
<target>Created (date)</target>
</trans-unit>
<trans-unit id="28" resname="sulu_article.smart-content.title">
<source>sulu_article.smart-content.title</source>
<target>Title (alphabetic)</target>
</trans-unit>
<trans-unit id="29" resname="sulu_article.smart-content.author-full-name">
<source>sulu_article.smart-content.author-full-name</source>
<target>Author (alphabetic)</target>
</trans-unit>
</body>
</file>
</xliff>
54 changes: 50 additions & 4 deletions Tests/Functional/Content/ArticleDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,67 @@ public function testResolveDataItems()

public function testResolveDataItemsTypeParam()
{
$this->createArticle();
$item = $this->createArticle('Test', 'simple');
$item1 = $this->createArticle();
$item2 = $this->createArticle('Test', 'simple');

/** @var DataProviderInterface $dataProvider */
$dataProvider = $this->getContainer()->get('sulu_article.content.data_provider');

// get all articles with type video
$result = $dataProvider->resolveDataItems(
[],
['type' => new PropertyParameter('type', 'video')],
['types' => new PropertyParameter('types', 'video')],
['locale' => 'de']
);

$this->assertInstanceOf(DataProviderResult::class, $result);
$this->assertCount(1, $result->getItems());
$this->assertEquals($item['id'], $result->getItems()[0]->getId());
$this->assertEquals($item2['id'], $result->getItems()[0]->getId());
}

public function testResolveDataItemsTypeParamMultiple()
{
$item1 = $this->createArticle();
$item2 = $this->createArticle('Test', 'simple');

/** @var DataProviderInterface $dataProvider */
$dataProvider = $this->getContainer()->get('sulu_article.content.data_provider');

// get all articles with type video or blog
$result = $dataProvider->resolveDataItems(
[],
['types' => new PropertyParameter('types', 'video,blog')],
['locale' => 'de']
);

$this->assertInstanceOf(DataProviderResult::class, $result);
$this->assertCount(2, $result->getItems());
$this->assertContains(
$item1['id'],
[$result->getItems()[0]->getId(), $result->getItems()[1]->getId()]
);
$this->assertContains(
$item2['id'],
[$result->getItems()[0]->getId(), $result->getItems()[1]->getId()]
);
}

public function testResolveDataItemsTypeParamWrong()
{
$item1 = $this->createArticle();
$item2 = $this->createArticle('Test', 'simple');

/** @var DataProviderInterface $dataProvider */
$dataProvider = $this->getContainer()->get('sulu_article.content.data_provider');

// get all articles with type other
$result = $dataProvider->resolveDataItems(
[],
['types' => new PropertyParameter('types', 'other')],
['locale' => 'de']
);
$this->assertInstanceOf(DataProviderResult::class, $result);
$this->assertCount(0, $result->getItems());
}

public function testResolveDataItemsPagination()
Expand Down

0 comments on commit d1ed2e7

Please sign in to comment.