Skip to content

Commit

Permalink
see cl 1.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Patzer committed May 19, 2020
1 parent 74438ca commit 29d67d1
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.11.0] - 2020-05-19
- added new list config elements: `RelatedConfigElementType`, `TagsConfigElementType`

## [1.10.1] - 2020-05-15
- fixed image config element for svg files

Expand Down
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -37,9 +37,11 @@ Every reader config can have one or more reader config elements. These are desig

Currently available reader config element types:

Type | Description
------|------------
image | Configure the output of one or more image fields separately (image size, placeholder handling, ...)
Type | Description
--------------|------------
image | Configure the output of one or more image fields separately (image size, placeholder handling, ...)
tags | Output one or more tag fields based on [codefog/tags-bundle](https://github.com/codefog/tags-bundle).
related items | Output related items based on given tags (needs [heimrichhannot/contao-list-bundle](https://github.com/heimrichhannot/contao-list-bundle); needs [codefog/tags-bundle](https://github.com/codefog/tags-bundle)) or categories (needs [heimrichhannot/contao-categories-bundle](https://github.com/heimrichhannot/contao-categories-bundle)).

### Templates

Expand Down
99 changes: 99 additions & 0 deletions src/ConfigElementType/RelatedConfigElementType.php
@@ -0,0 +1,99 @@
<?php

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

namespace HeimrichHannot\ReaderBundle\ConfigElementType;

use Contao\Controller;
use Contao\Model;
use HeimrichHannot\ReaderBundle\DataContainer\ReaderConfigElementContainer;
use HeimrichHannot\ReaderBundle\Item\ItemInterface;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use HeimrichHannot\UtilsBundle\String\StringUtil;

class RelatedConfigElementType implements ReaderConfigElementTypeInterface
{
/**
* @var StringUtil
*/
private $stringUtil;
/**
* @var ModelUtil
*/
private $modelUtil;

public function __construct(StringUtil $stringUtil, ModelUtil $modelUtil)
{
$this->stringUtil = $stringUtil;
$this->modelUtil = $modelUtil;
}

/**
* Return the config element type alias.
*/
public static function getType(): string
{
return 'related';
}

/**
* Return the config element type palette.
*/
public function getPalette(): string
{
return '{config_legend},relatedExplanation,relatedListModule,relatedCriteriaExplanation,relatedCriteria;';
}

/**
* Update the item data.
*/
public function addToReaderItemData(ReaderConfigElementData $configElementData): void
{
$readerConfigElement = $configElementData->getReaderConfigElement();
$item = $configElementData->getItem();

$item->setFormattedValue(
$readerConfigElement->templateVariable ?: 'relatedItems',
$this->renderRelated($readerConfigElement, $item)
);

$configElementData->setItem($item);
}

protected function renderRelated(Model $configElement, ItemInterface $item): ?string
{
$GLOBALS['HUH_LIST_RELATED'] = [];

$this->applyTagsFilter($configElement, $item);

$result = Controller::getFrontendModule($configElement->relatedListModule);

unset($GLOBALS['HUH_LIST_RELATED']);

return $result;
}

protected function applyTagsFilter(Model $configElement, ItemInterface $item)
{
if (!class_exists('\Codefog\TagsBundle\CodefogTagsBundle') || !$configElement->tagsField) {
return;
}

$criteria = \Contao\StringUtil::deserialize($configElement->relatedCriteria, true);

if (empty($criteria)) {
return;
}

if (\in_array(ReaderConfigElementContainer::RELATED_CRITERIUM_TAGS, $criteria)) {
$GLOBALS['HUH_LIST_RELATED'][ReaderConfigElementContainer::RELATED_CRITERIUM_TAGS] = [
'field' => $configElement->tagsField,
'item' => $item,
];
}
}
}
150 changes: 150 additions & 0 deletions src/ConfigElementType/TagsConfigElementType.php
@@ -0,0 +1,150 @@
<?php

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

namespace HeimrichHannot\ReaderBundle\ConfigElementType;

use Contao\CoreBundle\Exception\RedirectResponseException;
use Contao\Database;
use Contao\System;
use HeimrichHannot\RequestBundle\Component\HttpFoundation\Request;
use HeimrichHannot\UtilsBundle\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use HeimrichHannot\UtilsBundle\String\StringUtil;
use HeimrichHannot\UtilsBundle\Url\UrlUtil;
use Twig\Environment;

class TagsConfigElementType implements ReaderConfigElementTypeInterface
{
/**
* @var Environment
*/
private $twig;
/**
* @var StringUtil
*/
private $stringUtil;
/**
* @var DcaUtil
*/
private $dcaUtil;
/**
* @var UrlUtil
*/
private $urlUtil;
/**
* @var Request
*/
private $request;
/**
* @var ModelUtil
*/
private $modelUtil;

public function __construct(Environment $twig, StringUtil $stringUtil, DcaUtil $dcaUtil, UrlUtil $urlUtil, Request $request, ModelUtil $modelUtil)
{
$this->twig = $twig;
$this->stringUtil = $stringUtil;
$this->dcaUtil = $dcaUtil;
$this->urlUtil = $urlUtil;
$this->request = $request;
$this->modelUtil = $modelUtil;
}

public function renderTags($configElement, $item): ?string
{
$table = $item->getDataContainer();

if (!$table || !isset($GLOBALS['TL_DCA'][$table]['fields'][$configElement->tagsField]['eval']['tagsManager']) || !$configElement->tagsField) {
return '';
}

$this->dcaUtil->loadDc($table);

$source = $GLOBALS['TL_DCA'][$table]['fields'][$configElement->tagsField]['eval']['tagsManager'];

$nonTlTable = $this->stringUtil->removeLeadingString('tl_', $item->getDataContainer());
$cfgTable = 'tl_cfg_tag_'.$nonTlTable;

$tags = [];

$tagRecords = Database::getInstance()->prepare("SELECT t.* FROM tl_cfg_tag t INNER JOIN $cfgTable t2 ON t.id = t2.cfg_tag_id".
" WHERE t2.{$nonTlTable}_id=? AND t.source=? ORDER BY t.name")->execute(
$item->getRawValue('id'),
$source
);

if ($tagRecords->numRows > 0) {
$tags = $tagRecords->fetchAllAssoc();
}

if ($configElement->tagsAddLink) {
$jumpTo = $this->urlUtil->getJumpToPageUrl($configElement->tagsJumpTo, false);

if (($tagId = $this->request->getGet('huh_cfg_tag')) && $jumpTo) {
if (null !== ($filterConfigElement = $this->modelUtil->findModelInstanceByPk('tl_filter_config_element', $configElement->tagsFilterConfigElement))) {
$sessionKey = System::getContainer()->get('huh.filter.manager')->findById($configElement->tagsFilter)->getSessionKey();

$sessionData = System::getContainer()->get('huh.filter.session')->getData($sessionKey);

$sessionData = \is_array($sessionData) ? $sessionData : [];

$sessionData[$filterConfigElement->name] = $tagId;

System::getContainer()->get('huh.filter.session')->setData($sessionKey, $sessionData);

throw new RedirectResponseException('/'.ltrim($jumpTo, '/'));
}
}

foreach ($tags as &$tag) {
$tag['url'] = $this->urlUtil->addQueryString('huh_cfg_tag='.$tag['id']);
}
}

$data = [
'configElement' => $configElement,
'item' => $item,
];

$data['tags'] = $tags;

return $this->twig->render(System::getContainer()->get('huh.utils.template')->getTemplate($configElement->tagsTemplate), $data);
}

/**
* Return the config element type alias.
*/
public static function getType(): string
{
return 'tags';
}

/**
* Return the config element type palette.
*/
public function getPalette(): string
{
return '{config_legend},tagsField,tagsAddLink,tagsTemplate;';
}

/**
* Update the item data.
*/
public function addToReaderItemData(ReaderConfigElementData $configElementData): void
{
$readerConfigElement = $configElementData->getReaderConfigElement();
$item = $configElementData->getItem();

$item->setFormattedValue(
$readerConfigElement->templateVariable ?: 'tags',
$this->renderTags($readerConfigElement, $item)
);

$configElementData->setItem($item);
}
}
37 changes: 37 additions & 0 deletions src/DataContainer/ReaderConfigElementContainer.php
Expand Up @@ -8,12 +8,17 @@

namespace HeimrichHannot\ReaderBundle\DataContainer;

use Contao\StringUtil;
use HeimrichHannot\ReaderBundle\ConfigElementType\RelatedConfigElementType;
use HeimrichHannot\ReaderBundle\Model\ReaderConfigElementModel;
use HeimrichHannot\ReaderBundle\Registry\ReaderConfigElementRegistry;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ReaderConfigElementContainer
{
const RELATED_CRITERIUM_TAGS = 'tags';
const RELATED_CRITERIUM_CATEGORIES = 'categories';

/**
* @var ReaderConfigElementRegistry
*/
Expand All @@ -32,13 +37,33 @@ public function __construct(ReaderConfigElementRegistry $configElementTypeRegist
$this->container = $container;
}

public function getRelatedCriteriaAsOptions()
{
$options = [];

if (class_exists('\Codefog\TagsBundle\CodefogTagsBundle')) {
$options[] = static::RELATED_CRITERIUM_TAGS;
}

// TODO
// if (class_exists('\HeimrichHannot\CategoriesBundle\CategoriesBundle')) {
// $options[] = static::RELATED_CRITERIUM_CATEGORIES;
// }

return $options;
}

/**
* Update dca palettes with config element types palettes.
*
* @param $dc
*/
public function onLoadCallback($dc)
{
if (null === ($readerConfigElement = $this->container->get('huh.utils.model')->findModelInstanceByPk('tl_reader_config_element', $dc->id))) {
return;
}

$configElementTypes = $this->configElementTypeRegistry->getReaderConfigElementTypes();

if (empty($configElementTypes)) {
Expand All @@ -49,6 +74,18 @@ public function onLoadCallback($dc)
$palette = '{title_type_legend},title,type,templateVariable;'.$listConfigElementType->getPalette();
$GLOBALS['TL_DCA'][ReaderConfigElementModel::getTable()]['palettes'][$listConfigElementType::getType()] = $palette;
}

// related
if ($readerConfigElement->type === RelatedConfigElementType::getType()) {
$criteria = StringUtil::deserialize($readerConfigElement->relatedCriteria, true);

if (\in_array(static::RELATED_CRITERIUM_TAGS, $criteria)) {
$GLOBALS['TL_DCA']['tl_list_config_element']['palettes'][RelatedConfigElementType::getType()] = str_replace(
'relatedCriteria;', 'relatedCriteria,tagsField;',
$GLOBALS['TL_DCA']['tl_list_config_element']['palettes'][RelatedConfigElementType::getType()]
);
}
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/config/services.yml
Expand Up @@ -140,4 +140,6 @@ services:
HeimrichHannot\ReaderBundle\ConfigElementType\RedirectionConfigElementType: ~
HeimrichHannot\ReaderBundle\ConfigElementType\DeleteConfigElementType: ~
HeimrichHannot\ReaderBundle\ConfigElementType\NavigationConfigElementType: ~
HeimrichHannot\ReaderBundle\ConfigElementType\SubmissionFormConfigElementType: ~
HeimrichHannot\ReaderBundle\ConfigElementType\SubmissionFormConfigElementType: ~
HeimrichHannot\ReaderBundle\ConfigElementType\RelatedConfigElementType: ~
HeimrichHannot\ReaderBundle\ConfigElementType\TagsConfigElementType: ~

0 comments on commit 29d67d1

Please sign in to comment.