Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Automaticaly set imported element userModification #418

Merged
merged 4 commits into from
Aug 26, 2024
Merged
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
1 change: 1 addition & 0 deletions doc/04_Import_Execution_Details.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The actual processing is the same for both types and consists of following steps
- Update published state of element based on publish strategy.
- Process mappings and transformation pipelines for each mapping.
- Assign transformation result to data element based on data target definition.
- Assign userModification to data element if possible.

Cleanup jobs are simpler and consist of following steps:
- Look for existing data element based on loading strategy.
Expand Down
7 changes: 6 additions & 1 deletion src/DataSource/Interpreter/AbstractInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Pimcore\Log\ApplicationLogger;
use Pimcore\Log\FileObject;
use Pimcore\Model\Tool\TmpStore;
use Pimcore\Tool;
mattamon marked this conversation as resolved.
Show resolved Hide resolved
use Pimcore\Tool\Admin;
use Psr\Log\LoggerAwareTrait;

abstract class AbstractInterpreter implements InterpreterInterface
Expand Down Expand Up @@ -212,10 +214,13 @@ protected function processImportRow(array $data)
$createQueueItem = $this->deltaChecker->hasChanged($this->configName, $this->idDataIndex, $data);
}

// If there is no user logged in, we use the system user (ID 0) as userOwner
$userOwner = Admin::getCurrentUser()?->getId() ?? 0;

//create queue item
if ($createQueueItem) {
$this->logger->debug(sprintf('Adding item `%s` of `%s` to processing queue.', ($data[$this->idDataIndex] ?? null), $this->configName));
$this->queueService->addItemToQueue($this->configName, $this->executionType, ImportProcessingService::JOB_TYPE_PROCESS, json_encode($data));
$this->queueService->addItemToQueue($this->configName, $this->executionType, ImportProcessingService::JOB_TYPE_PROCESS, json_encode($data), $userOwner);
} else {
$message = sprintf("Import data of item `%s` of `%s` didn't change, not adding to queue.", ($data[$this->idDataIndex] ?? null), $this->configName);
$this->logger->debug($message);
Expand Down
4 changes: 2 additions & 2 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Pimcore\Bundle\DataImporterBundle;

use Pimcore\Bundle\DataImporterBundle\Migrations\Version20220304130000;
use Pimcore\Bundle\DataImporterBundle\Migrations\Version20240715160305;
use Pimcore\Extension\Bundle\Installer\SettingsStoreAwareInstaller;
use Pimcore\Model\User\Permission;

Expand Down Expand Up @@ -57,6 +57,6 @@ public function install(): void

public function getLastMigrationVersionClassName(): ?string
{
return Version20220304130000::class;
return Version20240715160305::class;
}
}
59 changes: 59 additions & 0 deletions src/Migrations/Version20240715160305.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\DataImporterBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Pimcore\Bundle\DataImporterBundle\Queue\QueueService;
use Pimcore\Migrations\BundleAwareMigration;

class Version20240715160305 extends BundleAwareMigration
{
public function up(Schema $schema): void
{
if ($schema->hasTable(QueueService::QUEUE_TABLE_NAME)) {
$queueTable = $schema->getTable(QueueService::QUEUE_TABLE_NAME);
$queueTable->addColumn('userOwner', 'integer', ['notnull' => true, 'default' => 0])->setUnsigned(true);
$queueTable->addIndex(['userOwner'], 'bundle_index_queue_executiontype_userOwner');
}
}

public function down(Schema $schema): void
{
if ($schema->hasTable(QueueService::QUEUE_TABLE_NAME)) {
$queueTable = $schema->getTable(QueueService::QUEUE_TABLE_NAME);
$queueTable->dropColumn('userOwner');
$queueTable->dropIndex('bundle_index_queue_executiontype_userOwner');
}
}

protected function getBundleName(): string
{
return 'PimcoreDataImporterBundle';
}
}
9 changes: 6 additions & 3 deletions src/Processing/ImportProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function processQueueItem(int $id)
if (empty($queueItem)) {
return;
}
$userOwner = $queueItem['userOwner'];

//get config
$configName = $queueItem['configName'];
Expand All @@ -144,7 +145,7 @@ public function processQueueItem(int $id)
//process element
if ($queueItem['jobType'] === self::JOB_TYPE_PROCESS) {
$data = json_decode($queueItem['data'], true);
$this->processElement($configName, $data, $resolver, $mapping);
$this->processElement($configName, $data, $resolver, $mapping, $userOwner);
} elseif ($queueItem['jobType'] === self::JOB_TYPE_CLEANUP) {
$this->cleanupElement($configName, $queueItem['data'], $resolver, $config['processingConfig']['cleanup'] ?? []);
} else {
Expand Down Expand Up @@ -179,7 +180,7 @@ private function flattenArray(array $arr): array
* @param Resolver $resolver
* @param MappingConfiguration[] $mapping
*/
protected function processElement(string $configName, array $importDataRow, Resolver $resolver, array $mapping)
protected function processElement(string $configName, array $importDataRow, Resolver $resolver, array $mapping, int $userOwner)
{
$element = null;
$importDataRowString = implode(', ', $this->flattenArray($importDataRow));
Expand Down Expand Up @@ -227,7 +228,9 @@ protected function processElement(string $configName, array $importDataRow, Reso
$event = new PreSaveEvent($configName, $importDataRow, $element);
$this->eventDispatcher->dispatch($event);

$element->save();
$element
->setUserModification($userOwner)
->save();

$event = new PostSaveEvent($configName, $importDataRow, $element);
$this->eventDispatcher->dispatch($event);
Expand Down
16 changes: 10 additions & 6 deletions src/Queue/QueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,26 @@ protected function getCurrentQueueTableOperationTime(): int
*
* @throws Exception
*/
public function addItemToQueue(string $configName, string $executionType, string $jobType, string $data): void
public function addItemToQueue(string $configName, string $executionType, string $jobType, string $data, int $userOwner = 0): void
{
$db = $this->getDb();
try {
$db->executeQuery(sprintf(
'INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE timestamp = VALUES(timestamp)',
self::QUEUE_TABLE_NAME,
implode(',', ['timestamp', 'configName', 'data', 'executionType', 'jobType']),
implode(',', ['timestamp', 'configName', 'data', 'executionType', 'jobType', 'userOwner']),
implode(',', [
$this->getCurrentQueueTableOperationTime(),
$db->quote($configName),
$db->quote($data),
$db->quote($executionType),
$db->quote($jobType)
$db->quote($jobType),
$userOwner
])
));
} catch (TableNotFoundException $exception) {
$this->createQueueTableIfNotExisting(function () use ($configName, $executionType, $jobType, $data) {
$this->addItemToQueue($configName, $executionType, $jobType, $data);
$this->createQueueTableIfNotExisting(function () use ($configName, $executionType, $jobType, $data, $userOwner) {
$this->addItemToQueue($configName, $executionType, $jobType, $data, $userOwner);
});
}
}
Expand All @@ -86,6 +87,8 @@ protected function createQueueTableIfNotExisting(\Closure $callable = null)
$this->getDb()->executeQuery(sprintf('CREATE TABLE IF NOT EXISTS %s (
id bigint AUTO_INCREMENT,
timestamp bigint NULL,
userOwner int unsigned NOT NULL DEFAULT 0,

configName varchar(80) NULL,
`data` TEXT null,
executionType varchar(20) NULL,
Expand All @@ -95,7 +98,8 @@ protected function createQueueTableIfNotExisting(\Closure $callable = null)
PRIMARY KEY (id),
KEY `bundle_index_queue_configName_index` (`configName`),
KEY `bundle_index_queue_executiontype_workerId` (`executionType`, `workerId`),
KEY `bundle_index_queue_configName_index_executionType` (`configName`, `executionType`))
KEY `bundle_index_queue_configName_index_executionType` (`configName`, `executionType`),
KEY `bundle_index_queue_executiontype_userOwner` (`userOwner`))
', self::QUEUE_TABLE_NAME));

if ($callable) {
Expand Down
Loading