Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/EventListener/BackendEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
use Contao\System;
use ContaoCommunityAlliance\DcGeneral\Event\PostDuplicateModelEvent;
use ContaoCommunityAlliance\DcGeneral\Event\PostPasteModelEvent;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use MetaModels\DcGeneral\Data\Model;
use MetaModels\IFactory;

/**
* Handles event operations on tl_metamodel_dcasetting
Expand All @@ -50,13 +52,20 @@ class BackendEventListener
*/
private Connection $connection;

/**
* @var IFactory
*/
private IFactory $factory;

/**
* The ArticleContent constructor.
*
* @param Connection $connection The database connection.
*/
public function __construct(Connection $connection = null)
{
public function __construct(
IFactory $factory,
Connection $connection = null,
) {
if (null === $connection) {
// @codingStandardsIgnoreStart
@trigger_error(
Expand All @@ -68,6 +77,7 @@ public function __construct(Connection $connection = null)
assert($connection instanceof Connection);
}
$this->connection = $connection;
$this->factory = $factory;
}

/**
Expand Down Expand Up @@ -130,14 +140,33 @@ public function handlePostPasteModel(PostPasteModelEvent $event)
*/
private function duplicateContentEntries(string $strTable, int $intSourceId, int $intDestinationId): void
{
$metaModel = $this->factory->getMetaModel($strTable);
if ($metaModel === null) {
return;
}

$colNamesToCopy = [];
$attributes = $metaModel->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->get('type') === 'contentarticle') {
$colNamesToCopy[] = $attribute->getColName();
}
}

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

$objContent = $this->connection
->createQueryBuilder()
->select('*')
->from('tl_content', 't')
->where('t.pid=:id')
->andWhere('t.ptable=:ptable')
->andWhere('t.mm_slot IN (:slots)')
->setParameter('id', $intSourceId)
->setParameter('ptable', $strTable)
->setParameter('slots', $colNamesToCopy, ArrayParameterType::STRING)
->executeQuery();

while ($row = $objContent->fetchAssociative()) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ services:
MetaModels\AttributeContentArticleBundle\EventListener\BackendEventListener:
public: false
arguments:
- '@metamodels.factory'
- '@database_connection'
tags:
- name: kernel.event_listener
Expand Down