Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dennis Patzer
committed
May 19, 2020
1 parent
74438ca
commit 29d67d1
Showing
9 changed files
with
490 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.