diff --git a/src/Command/EntityFinderCommand.php b/src/Command/EntityFinderCommand.php index 0e9819dd..a3ff19d9 100644 --- a/src/Command/EntityFinderCommand.php +++ b/src/Command/EntityFinderCommand.php @@ -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; @@ -31,22 +35,21 @@ 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(); @@ -54,6 +57,8 @@ public function __construct(ContaoFramework $contaoFramework, EventDispatcherInt $this->eventDispatcher = $eventDispatcher; $this->connection = $connection; $this->entityFinderHelper = $entityFinderHelper; + $this->utils = $utils; + $this->finder = $finder; } protected function configure() @@ -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) { @@ -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; diff --git a/src/EntityFinder/Element.php b/src/EntityFinder/Element.php new file mode 100644 index 00000000..0cb79e90 --- /dev/null +++ b/src/EntityFinder/Element.php @@ -0,0 +1,48 @@ +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; + } +} \ No newline at end of file diff --git a/src/EntityFinder/Finder.php b/src/EntityFinder/Finder.php new file mode 100644 index 00000000..1eaff403 --- /dev/null +++ b/src/EntityFinder/Finder.php @@ -0,0 +1,76 @@ +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]; + } + ); + } +} \ No newline at end of file diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 69b3ae63..a71f1104 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -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: