Skip to content

Commit

Permalink
added filter support and replaced contao model query builder with Doc…
Browse files Browse the repository at this point in the history
…trineDBALQueryQueryBuilder instance
  • Loading branch information
Rico Kaltofen committed Apr 20, 2018
1 parent b28a6d4 commit 24a71b0
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ return PhpCsFixer\Config::create()
'header_comment' => ['header' => $header],
'ordered_imports' => true,
'ordered_class_elements' => true,
'php_unit_strict' => true,
'php_unit_strict' => false,
'phpdoc_order' => true,
'no_useless_return' => true,
'no_useless_else' => true,
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"heimrichhannot/contao-head-bundle": "^1.0",
"heimrichhannot/contao-utils-bundle": "^2.0.16",
"heimrichhannot/contao-status_messages": "^1.0",
"heimrichhannot/contao-filter-bundle": "^1.0-beta",
"heimrichhannot/contao-entity-filter-bundle": "^1.0",
"twig/extensions": "^1.5",
"urodoz/truncate-html": "^1.0"
Expand Down
46 changes: 46 additions & 0 deletions src/Choice/FilterChoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\ReaderBundle\Choice;

use HeimrichHannot\FilterBundle\Model\FilterConfigModel;
use HeimrichHannot\UtilsBundle\Choice\AbstractChoice;

class FilterChoice extends AbstractChoice
{
/**
* @return array
*/
protected function collect()
{
$choices = [];

$dataContainers = $this->getContext();

if (!is_array($dataContainers)) {
$dataContainers = [$dataContainers];
}

/**
* @var FilterConfigModel
*/
$adapter = $this->framework->getAdapter(FilterConfigModel::class);

if (!empty($dataContainers)) {
$filters = $adapter->findByDataContainers($dataContainers);
} else {
$filters = $adapter->findAll();
}

if (null !== $filters) {
$choices = $filters->fetchEach('title');
}

return $choices;
}
}
10 changes: 6 additions & 4 deletions src/Choice/ReaderItemTemplateChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ protected function collect()

$config = System::getContainer()->getParameter('huh.reader');

if (!isset($config['reader']['templates']['item'])) {
return $choices;
if (isset($config['reader']['templates']['item_prefixes'])) {
$choices = System::getContainer()->get('huh.utils.choice.twig_template')->setContext($config['reader']['templates']['item_prefixes'])->getCachedChoices();
}

foreach ($config['reader']['templates']['item'] as $template) {
$choices[$template['name']] = $template['template'];
if (isset($config['reader']['templates']['item'])) {
foreach ($config['reader']['templates']['item'] as $template) {
$choices[$template['name']] = $template['template'];
}
}

asort($choices);
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('item_prefixes')
->prototype('scalar')
->end()
->end()
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion src/Item/DefaultItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function parse(): string
$readerConfig = $this->_manager->getReaderConfig();

// add reader config element data
if (null !== ($readerConfigElements = $this->_manager->getReaderConfigElementRegistry()->findBy(['pid=?'], [$readerConfig->id]))) {
if (null !== ($readerConfigElements = $this->_manager->getReaderConfigElementRegistry()->findBy(['tl_reader_config_element.pid=?'], [$readerConfig->id]))) {
foreach ($readerConfigElements as $readerConfigElement) {
if (null === ($class = $this->_manager->getReaderConfigElementRegistry()->getElementClassByName($readerConfigElement->type))) {
continue;
Expand Down
96 changes: 69 additions & 27 deletions src/Manager/ReaderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Database;
use Contao\DataContainer;
use Contao\Model;
use Contao\StringUtil;
use Contao\System;
use Doctrine\DBAL\Query\QueryBuilder;
use HeimrichHannot\EntityFilterBundle\Backend\EntityFilter;
use HeimrichHannot\FilterBundle\Config\FilterConfig;
use HeimrichHannot\FilterBundle\Manager\FilterManager;
use HeimrichHannot\ReaderBundle\Backend\ReaderConfig;
use HeimrichHannot\ReaderBundle\Item\ItemInterface;
use HeimrichHannot\ReaderBundle\Model\ReaderConfigModel;
Expand All @@ -37,6 +41,16 @@ class ReaderManager implements ReaderManagerInterface
*/
protected $framework;

/**
* @var FilterManager
*/
protected $filterManager;

/**
* @var FilterConfig
*/
protected $filterConfig;

/**
* @var ReaderConfigModel
*/
Expand Down Expand Up @@ -104,6 +118,7 @@ class ReaderManager implements ReaderManagerInterface

public function __construct(
ContaoFrameworkInterface $framework,
FilterManager $filterManager,
EntityFilter $entityFilter,
ReaderConfigRegistry $readerConfigRegistry,
ReaderConfigElementRegistry $readerConfigElementRegistry,
Expand All @@ -115,6 +130,7 @@ public function __construct(
\Twig_Environment $twig
) {
$this->framework = $framework;
$this->filterManager = $filterManager;
$this->entityFilter = $entityFilter;
$this->readerConfigRegistry = $readerConfigRegistry;
$this->readerConfigElementRegistry = $readerConfigElementRegistry;
Expand All @@ -133,6 +149,7 @@ public function __construct(
public function retrieveItem(): ?ItemInterface
{
$readerConfig = $this->getReaderConfig();

$item = null;

switch ($readerConfig->itemRetrievalMode) {
Expand All @@ -150,15 +167,14 @@ public function retrieveItem(): ?ItemInterface

// hide unpublished items?
if (null !== $item && $readerConfig->hideUnpublishedItems) {
if (!$readerConfig->invertPublishedField && !$item->{$readerConfig->publishedField}
|| $readerConfig->invertPublishedField && $item->{$readerConfig->publishedField}
if (!$readerConfig->invertPublishedField && !$item[$readerConfig->publishedField]
|| $readerConfig->invertPublishedField && $item[$readerConfig->publishedField]
) {
return null;
}
}

$data = $item->row();
$this->dc = DC_Table_Utils::createFromModelData($data, $this->readerConfig->dataContainer);
$this->dc = DC_Table_Utils::createFromModelData($item, $this->readerConfig->dataContainer);

if (null !== ($itemClass = $this->getItemClassByName($this->readerConfig->item ?: 'default'))) {
$reflection = new \ReflectionClass($itemClass);
Expand All @@ -171,7 +187,7 @@ public function retrieveItem(): ?ItemInterface
throw new \Exception(sprintf('Item class %s must implement %s', $itemClass, \JsonSerializable::class));
}

$this->item = new $itemClass($this, $data);
$this->item = new $itemClass($this, $item);
}

return $this->item;
Expand Down Expand Up @@ -371,6 +387,33 @@ function (array $matches) use ($item) {
}
}

/**
* {@inheritdoc}
*/
public function getFilterConfig(): ?FilterConfig
{
// Caching
if (null !== $this->filterConfig && $this->filterConfig->getFilter()['id'] === $this->readerConfig->filter) {
return $this->filterConfig;
}

if ($this->readerConfig->filter > 0) {
$this->filterConfig = $this->filterManager->findById($this->readerConfig->filter);
}

return $this->filterConfig;
}

/**
* {@inheritdoc}
*/
public function getQueryBuilder(): QueryBuilder
{
$filterConfig = $this->getFilterConfig();

return null !== $filterConfig ? $filterConfig->getQueryBuilder() : System::getContainer()->get('huh.reader.query_builder');
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -533,26 +576,31 @@ protected function modifyPageTitle(string $pageTitle)
protected function retrieveItemByAutoItem()
{
$readerConfig = $this->readerConfig;
$queryBuilder = $this->getQueryBuilder()->select('*')->from($readerConfig->dataContainer)->setMaxResults(1);
$item = null;

if (Config::get('useAutoItem') && ($autoItem = Request::getGet('auto_item'))) {
$field = $readerConfig->itemRetrievalAutoItemField;

// try to find by a certain field (likely alias)
$item = $this->modelUtil->findOneModelInstanceBy(
$readerConfig->dataContainer,
[
$readerConfig->dataContainer.'.'.$field.'=?',
],
[
$autoItem,
]
);
/* @var Model $adapter */
$adapter = $this->framework->getAdapter(Model::class);
if (!($modelClass = $adapter->getClassFromTable($readerConfig->dataContainer))) {
return $item;
}

// fallback: ID
if (null === $item) {
$item = $this->modelUtil->findModelInstanceByPk($readerConfig->dataContainer, $autoItem);
/* @var Model $model */
if (null === ($model = $this->framework->getAdapter($modelClass))) {
return $item;
}

$orX = $queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($readerConfig->dataContainer.'.'.$field, ':autoItem'),
$queryBuilder->expr()->eq($readerConfig->dataContainer.'.'.$model->getPk(), ':autoItem')
);

$queryBuilder->where($orX);
$queryBuilder->setParameter(':autoItem', $autoItem);
$item = $queryBuilder->execute()->fetch();
}

return $item;
Expand All @@ -566,6 +614,7 @@ protected function retrieveItemByAutoItem()
protected function retrieveItemByFieldConditions()
{
$readerConfig = $this->readerConfig;
$queryBuilder = $this->getQueryBuilder()->select('*')->from($readerConfig->dataContainer)->setMaxResults(1);
$item = null;

$itemConditions = StringUtil::deserialize($readerConfig->itemRetrievalFieldConditions, true);
Expand All @@ -576,15 +625,8 @@ protected function retrieveItemByFieldConditions()
$readerConfig->dataContainer
);

$statement = $this->database->prepare(
"SELECT * FROM $readerConfig->dataContainer WHERE ($whereCondition)"
)->limit(1);

$result = call_user_func_array([$statement, 'execute'], $values);

if ($result->numRows > 0) {
$item = $this->modelUtil->findModelInstanceByPk($readerConfig->dataContainer, $result->id);
}
$queryBuilder->andWhere($whereCondition);
$item = $queryBuilder->execute()->fetch();
}

return $item;
Expand Down
16 changes: 16 additions & 0 deletions src/Manager/ReaderManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\DataContainer;
use Doctrine\DBAL\Query\QueryBuilder;
use HeimrichHannot\FilterBundle\Config\FilterConfig;
use HeimrichHannot\ReaderBundle\Item\ItemInterface;
use HeimrichHannot\ReaderBundle\Model\ReaderConfigModel;
use HeimrichHannot\ReaderBundle\Registry\ReaderConfigElementRegistry;
Expand Down Expand Up @@ -38,6 +40,20 @@ public function getModuleData(): array;
*/
public function getReaderConfig(): ReaderConfigModel;

/**
* Get the reader config.
*
* @return FilterConfig
*/
public function getFilterConfig(): ?FilterConfig;

/**
* Get the query builder.
*
* @return QueryBuilder
*/
public function getQueryBuilder(): QueryBuilder;

/**
* Set the current reader config model.
*
Expand Down
1 change: 1 addition & 0 deletions src/Module/ModuleReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct(ModuleModel $objModule, $strColumn = 'main')
$this->framework = System::getContainer()->get('contao.framework');
$this->translator = System::getContainer()->get('translator');
$this->readerConfigRegistry = System::getContainer()->get('huh.reader.reader-config-registry');
$this->readerConfig = $this->readerConfigRegistry->findByPk($objModule->readerConfig);

$this->manager = $this->getReaderManagerByName($this->readerConfig->manager ?: 'default');

Expand Down
32 changes: 32 additions & 0 deletions src/QueryBuilder/ReaderQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\ReaderBundle\QueryBuilder;

use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;

class ReaderQueryBuilder extends QueryBuilder
{
/**
* @var ContaoFrameworkInterface
*/
protected $framework;

/**
* @var array
*/
protected $contextualValues = [];

public function __construct(ContaoFrameworkInterface $framework, Connection $connection)
{
parent::__construct($connection);
$this->framework = $framework;
}
}
4 changes: 3 additions & 1 deletion src/Resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ huh:
- { name: list, class: HeimrichHannot\ReaderBundle\ConfigElementType\ListConfigElementType }
templates:
item:
- { name: default, template: "@HeimrichHannotContaoReader/reader_item_default.html.twig" }
- { name: default, template: "@HeimrichHannotContaoReader/reader_item_default.html.twig" }
item_prefixes:
- news_
Loading

0 comments on commit 24a71b0

Please sign in to comment.