Skip to content

Commit

Permalink
add inserttag finder helper, use helper in article search
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jan 9, 2024
1 parent 35808c0 commit fd0ef32
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Command/EntityFinderCommand.php
Expand Up @@ -170,6 +170,15 @@ private function findEntity(string $table, $id, array &$parents, bool $onlyText
if ($element) {
$parents[] = ['table' => PageModel::getTable(), 'id' => $element->pid];

if (!$onlyText) {
foreach ($this->entityFinderHelper->findModulesByInserttag('html', 'html', 'insert_article', $element->id) as $id) {
$parents[] = ['table' => ModuleModel::getTable(), 'id' => $id];
}
foreach ($this->entityFinderHelper->findContentElementByInserttag('html', 'html', 'insert_article', $element->id) as $id) {
$parents[] = ['table' => ContentModel::getTable(), 'id' => $id];
}
}

return 'Article: '.$element->title.' (ID: '.$element->id.')';
}

Expand Down
84 changes: 77 additions & 7 deletions src/EntityFinder/EntityFinderHelper.php
Expand Up @@ -8,20 +8,31 @@

namespace HeimrichHannot\UtilsBundle\EntityFinder;

use Contao\ContentModel;
use Contao\Database;
use Contao\Model\Collection;
use Contao\ModuleModel;
use Contao\StringUtil;
use Contao\Validator;
use Doctrine\DBAL\Connection;
use HeimrichHannot\UtilsBundle\Database\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Utils;

class EntityFinderHelper
{
/**
* @var DatabaseUtil
* @var Utils
*/
private $databaseUtil;
private $utils;
/**
* @var Connection
*/
private $connection;

public function __construct(DatabaseUtil $databaseUtil)
public function __construct(Utils $utils, Connection $connection)
{
$this->databaseUtil = $databaseUtil;
$this->utils = $utils;
$this->connection = $connection;
}

/**
Expand All @@ -35,10 +46,69 @@ public function __construct(DatabaseUtil $databaseUtil)
*/
public function findModulesByTypeAndSerializedValue(string $type, string $field, array $values): ?Collection
{
[$columns[], $values] = $this->databaseUtil->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
$columns[] = ModuleModel::getTable().'.type=?';
$values[] = $type;
$result = $this->utils->database()->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
$columns = [$result->createAndWhere(), ModuleModel::getTable().'.type=?'];
$values = [$result->values, $type];

return ModuleModel::findBy($columns, $values);
}

/**
* Find frontend modules by insert inserttags like insert_module oder insert_article.
*
* @param string $type The module type
* @param string $field The tl_module field
* @param string $inserttag The inserttag to search for, for example insert_module
* @param int $id The element id to search for, for example the module id (as used in {{insert_module::1}}, would be 1 in this case)
* @return array The found module ids
* @throws \Exception
*/
public function findModulesByInserttag(string $type, string $field, string $inserttag, int $id): array
{
if (!Validator::isAlias($field)) {
throw new \Exception('Invalid field name '.$field.'given.');
}
if (!Validator::isAlias($inserttag)) {
throw new \Exception('Invalid inserttag '.$inserttag.'given.');
}
$result = Database::getInstance()
->prepare("SELECT id FROM tl_module
WHERE type=?
AND (
$field LIKE '%{{".$inserttag."::".$id."}}%'
OR $field LIKE '%{{".$inserttag."::".$id."::%')")
->execute($type);

return $result->fetchEach('id');
}


/**
* Find content elements by insert inserttags like insert_module oder insert_article.
*
* @param string $type The element type
* @param string $field The tl_content field
* @param string $inserttag The inserttag to search for, for example insert_module
* @param int $id The element id to search for, for example the module id (as used in {{insert_module::1}}, would be 1 in this case)
* @return array The found content element ids
* @throws \Exception
*/
public function findContentElementByInserttag(string $type, string $field, string $inserttag, int $id): array
{
if (!Validator::isAlias($field)) {
throw new \Exception('Invalid field name '.$field.'given.');
}
if (!Validator::isAlias($inserttag)) {
throw new \Exception('Invalid inserttag '.$inserttag.'given.');
}
$result = Database::getInstance()
->prepare("SELECT id FROM tl_content
WHERE type=?
AND (
$field LIKE '%{{".$inserttag."::".$id."}}%'
OR $field LIKE '%{{".$inserttag."::".$id."::%')")
->execute($type);

return $result->fetchEach('id');
}
}

0 comments on commit fd0ef32

Please sign in to comment.