Skip to content

Commit

Permalink
add new finder class, form support and generic child table support
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Nov 26, 2024
1 parent e11e15d commit 5805a91
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 17 deletions.
80 changes: 64 additions & 16 deletions src/Command/EntityFinderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\Database;
use Contao\DC_Table;
use Contao\FormModel;
use Contao\LayoutModel;
use Contao\ModuleModel;
use Contao\PageModel;
use Contao\ThemeModel;
use Doctrine\DBAL\Connection;
use HeimrichHannot\UtilsBundle\EntityFinder\EntityFinderHelper;
use HeimrichHannot\UtilsBundle\EntityFinder\Finder;
use HeimrichHannot\UtilsBundle\Event\ExtendEntityFinderEvent;
use HeimrichHannot\UtilsBundle\Util\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -31,29 +35,30 @@ class EntityFinderCommand extends Command
{
protected static $defaultName = 'huh:utils:entity_finder';

/** @var ContaoFramework */
private $contaoFramework;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var Connection
*/
private $connection;
/**
* @var EntityFinderHelper
*/
private $entityFinderHelper;

public function __construct(ContaoFramework $contaoFramework, EventDispatcherInterface $eventDispatcher, Connection $connection, EntityFinderHelper $entityFinderHelper)
private ContaoFramework $contaoFramework;
private EventDispatcherInterface $eventDispatcher;
private Connection $connection;
private EntityFinderHelper $entityFinderHelper;
private Utils $utils;
private Finder $finder;

public function __construct(
ContaoFramework $contaoFramework,
EventDispatcherInterface $eventDispatcher,
Connection $connection,
EntityFinderHelper $entityFinderHelper,
Utils $utils,
Finder $finder
)
{
parent::__construct();

$this->contaoFramework = $contaoFramework;
$this->eventDispatcher = $eventDispatcher;
$this->connection = $connection;
$this->entityFinderHelper = $entityFinderHelper;
$this->utils = $utils;
$this->finder = $finder;
}

protected function configure()
Expand Down Expand Up @@ -150,6 +155,20 @@ private function output(SymfonyStyle $io, array $tree, string $prepend = '', int

private function findEntity(string $table, $id, array &$parents, bool $onlyText = false): ?string
{
$element = $this->finder->find($table, $id);
if ($element) {
if ($onlyText) {
return $element->getDescription();
}
if (null === $element->getParents()) {
return null;
}
foreach ($element->getParents()($element->getTable(), $element->getId()) as $parent) {
$parents[] = ['table' => $parent['table'], 'id' => $parent['id']];
}
return null;
}

Controller::loadLanguageFile('default');

switch ($table) {
Expand Down Expand Up @@ -228,6 +247,35 @@ private function findEntity(string $table, $id, array &$parents, bool $onlyText
}

return 'Page not found: ID '.$id;

default:
Controller::loadDataContainer($table);
$dca = &$GLOBALS['TL_DCA'][$table];
if (!in_array($dca['config']['dataContainer'], ['Table', DC_Table::class])) {
return null;
}

if (isset($dca['config']['ptable'])) {
$model = $this->utils->model()->findModelInstanceByPk($table, $id);
if (!$model) {
return null;
}
$parents[] = ['table' => $dca['config']['ptable'], 'id' => $model->pid];

return 'Entity: '.$table.' (ID: '.$id.')';
}

if (isset($dca['config']['dynamicPtable']) && isset($dca['fields']['pid'])) {

$model = $this->utils->model()->findModelInstanceByPk($table, $id);
if (!$model) {
return null;
}
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
$parents[] = ['table' => $model->ptable, 'id' => $model->pid];

return 'Entity: '.$table.' (ID: '.$id.')';
}
}

return null;
Expand Down
48 changes: 48 additions & 0 deletions src/EntityFinder/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace HeimrichHannot\UtilsBundle\EntityFinder;

class Element
{
private int $id;
private string $table;
private ?string $description;
private ?\Closure $parents;

/**
* @param int $id
* @param string $table
* @param string|null $description
* @param \Closure(string $table, int $id):\Iterator|null $parents A closure that returns an iterator of parent elements
*/
public function __construct(int $id, string $table, string $description = null, \Closure $parents = null)
{
$this->id = $id;
$this->table = $table;
$this->description = $description;
$this->parents = $parents;
}

public function getId(): int
{
return $this->id;
}

public function getTable(): string
{
return $this->table;
}

public function getDescription(): ?string
{
return $this->description;
}

/**
* @return \Closure(string $table, int $id):\Iterator|null
*/
public function getParents(): ?\Closure
{
return $this->parents;
}
}
76 changes: 76 additions & 0 deletions src/EntityFinder/Finder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace HeimrichHannot\UtilsBundle\EntityFinder;

use Contao\ContentModel;
use Contao\FormFieldModel;
use Contao\FormModel;
use Contao\ModuleModel;

class Finder
{
private EntityFinderHelper $helper;

public function __construct(
EntityFinderHelper $helper
)
{
$this->helper = $helper;
}

public function find(string $table, int $id): ?Element
{
switch ($table) {
case FormModel::getTable():
return $this->form($id);
case FormFieldModel::getTable():
return $this->formField($id);
}

return null;
}

private function form(int $id): ?Element
{
$model = FormModel::findByPk($id);
if ($model === null) {
return null;
}

return new Element(
$model->id,
$model->getTable(),
'Form ' . $model->title. ' (ID: ' . $model->id . ')',
function() use ($model): \Iterator {
foreach (ModuleModel::findByForm($model->id) as $model) {
yield ['table' => $model::getTable(), 'id' => $model->id];
}
foreach (ContentModel::findByForm($model->id) as $model) {
yield ['table' => $model::getTable(), 'id' => $model->id];
}
foreach ($this->helper->findModulesByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
yield ['table' => $model::getTable(), 'id' => $model->id];
}
foreach ($this->helper->findContentElementByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
yield ['table' => $model::getTable(), 'id' => $model->id];
}
}
);
}

private function formField(int $id): ?Element
{
$model = FormFieldModel::findByPk($id);
if ($model === null) {
return null;
}
return new Element(
$model->id,
$model->getTable(),
'Form field ' . $model->name. ' (ID: ' . $model->id . ')',
function() use ($model): \Iterator {
yield ['table' => FormModel::getTable(), 'id' => $model->pid];
}
);
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:

HeimrichHannot\UtilsBundle\:
resource: '../../{Command,EntityFinder,Util}/*'
exclude: '../../Util/{Utils.php,DatabaseUtil/CreateWhereForSerializedBlobResult.php}'
exclude: '../../Util/{Utils.php,DatabaseUtil/CreateWhereForSerializedBlobResult.php,EntityFinder/Element.php}'
autowire: true
autoconfigure: true
bind:
Expand Down

0 comments on commit 5805a91

Please sign in to comment.