Skip to content

Commit

Permalink
Add ordering capability
Browse files Browse the repository at this point in the history
Implemented the ProvidesOrderClause interface to allow ordering of query results. Refactored the article renderer creation into a trait (CreateArticleRender) to reuse it in the page reference,.
  • Loading branch information
dmolineus committed Oct 18, 2024
1 parent 50df657 commit fde046c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
24 changes: 1 addition & 23 deletions src/Reference/ArticleReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

namespace Hofff\Contao\Content\Reference;

use Contao\ArticleModel;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\Model\Registry;
use Doctrine\DBAL\Connection;
use Hofff\Contao\Content\Renderer\ArticleRenderer;
use Hofff\Contao\Content\Renderer\Renderer;
use Hofff\Contao\Content\Renderer\Select;
use Hofff\Contao\Content\Util\ContaoUtil;
use Symfony\Contracts\Translation\TranslatorInterface;

final class ArticleReference extends RelatedReference implements CreatesRenderer, CreatesSelect
{
use ConfigureRenderer;
use CreateArticleRender;

public function __construct(
Connection $connection,
Expand All @@ -43,24 +39,6 @@ public function backendIcon(array $row): string
return $this->contaoUtil->isPublished((object) $reference) ? 'articles.svg' : 'articles_.svg';
}

/** {@inheritDoc} */
public function createRenderer(array $reference, array $config): Renderer
{
$model = Registry::getInstance()->fetch('tl_article', $reference['id']);
if (! $model instanceof ArticleModel) {
$model = new ArticleModel();
$model->setRow($reference);
}

$renderer = new ArticleRenderer($this->tokenChecker);
$renderer->setArticle($model);
$renderer->setRenderContainer($config['render_container']);

$this->configureRenderer($renderer, $config);

return $renderer;
}

/** {@inheritDoc} */
public function createSelect(array $config, int $index, string $column): Select
{
Expand Down
36 changes: 36 additions & 0 deletions src/Reference/CreateArticleRender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Hofff\Contao\Content\Reference;

use Contao\ArticleModel;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\Model\Registry;
use Hofff\Contao\Content\Renderer\ArticleRenderer;
use Hofff\Contao\Content\Renderer\Renderer;

trait CreateArticleRender
{
use ConfigureRenderer;

private readonly TokenChecker $tokenChecker;

/** {@inheritDoc} */
public function createRenderer(array $reference, array $config): Renderer
{
$model = Registry::getInstance()->fetch('tl_article', $reference['id']);
if (! $model instanceof ArticleModel) {
$model = new ArticleModel();
$model->setRow($reference);
}

$renderer = new ArticleRenderer($this->tokenChecker);
$renderer->setArticle($model);
$renderer->setRenderContainer($config['render_container']);

$this->configureRenderer($renderer, $config);

return $renderer;
}
}
15 changes: 11 additions & 4 deletions src/Reference/PageReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hofff\Contao\Content\Reference;

use Contao\Controller;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\StringUtil;
use Doctrine\DBAL\Connection;
use Hofff\Contao\Content\Renderer\Select;
Expand All @@ -15,11 +16,14 @@
use function array_merge;
use function array_values;

final class PageReference extends RelatedReference implements CreatesSelect
final class PageReference extends RelatedReference implements CreatesSelect, CreatesRenderer, ProvidesOrderClause
{
use CreateArticleRender;

public function __construct(
Connection $connection,
private readonly LanguageRelationDetector $langRelationDetector,
private readonly TokenChecker $tokenChecker,
) {
parent::__construct($connection);
}
Expand Down Expand Up @@ -80,11 +84,14 @@ public function createSelect(array $config, int $index, string $column): Select
article.pid = ?
$targetCondition
$sourceCondition
ORDER BY
article.sorting
SQL;

return new Select('article', $sql, $params);
return new Select('page', $sql, $params);
}

public function orderClause(): string
{
return 'hofff_content_index, sorting';
}

protected function labelColumn(): string
Expand Down
10 changes: 10 additions & 0 deletions src/Reference/ProvidesOrderClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Hofff\Contao\Content\Reference;

interface ProvidesOrderClause
{
public function orderClause(): string;
}
8 changes: 7 additions & 1 deletion src/Renderer/RendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Connection;
use Hofff\Contao\Content\Reference\CreatesRenderer;
use Hofff\Contao\Content\Reference\CreatesSelect;
use Hofff\Contao\Content\Reference\ProvidesOrderClause;
use Hofff\Contao\Content\Reference\ReferenceRegistry;

use function array_map;
Expand Down Expand Up @@ -57,7 +58,12 @@ public function createAll(array $configs, string $column): array
$queries = array_map(static fn (Select $select) => $select->sql, $selects);
$params = array_merge(...array_map(static fn (Select $select) => $select->params, $selects));
$sql = '(' . implode(') UNION ALL (', $queries) . ')';
$result = $this->connection->executeQuery($sql, $params);

if ($reference instanceof ProvidesOrderClause) {
$sql .= ' ORDER BY ' . $reference->orderClause();
}

$result = $this->connection->executeQuery($sql, $params);

while ($row = $result->fetchAssociative()) {
$i = $row['hofff_content_index'];
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<service id="Hofff\Contao\Content\Reference\PageReference">
<argument type="service" id="database_connection"/>
<argument type="service" id="Hofff\Contao\Content\Util\LanguageRelationDetector"/>
<argument type="service" id="contao.security.token_checker"/>
<tag name="Hofff\Contao\Content\Reference\Reference"/>
</service>

Expand Down

0 comments on commit fde046c

Please sign in to comment.