addOption('apply', null, InputOption::VALUE_NONE, 'Write changes (default: dry run)');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
Bootstrap::initializeBackendAuthentication();
$apply = (bool)$input->getOption('apply');
$pool = GeneralUtility::makeInstance(ConnectionPool::class);
$conn = $pool->getConnectionForTable(self::TABLE);
$output->writeln(($apply ? 'APPLY' : 'DRY-RUN'));
$datamap = [];
$index = 0;
foreach (self::PRODUCTS as [$title, $slug, $siblingSlug, $teaser]) {
$existing = $conn->fetchAssociative(
'SELECT uid FROM ' . self::TABLE . ' WHERE deleted = 0 AND (slug = ? OR title = ?)', [$slug, $title]
);
if ($existing) {
$output->writeln(sprintf(' = "%s" exists (#%d) - skipped', $title, (int)$existing['uid']));
continue;
}
$sibling = $conn->fetchAssociative(
'SELECT uid, pid FROM ' . self::TABLE . ' WHERE deleted = 0 AND slug = ?', [$siblingSlug]
);
if (!$sibling) {
$output->writeln(sprintf(' ! sibling %s not found - "%s" skipped', $siblingSlug, $title));
continue;
}
$categories = $pool->getConnectionForTable('sys_category_record_mm')->fetchFirstColumn(
"SELECT uid_local FROM sys_category_record_mm
WHERE tablenames = ? AND fieldname = 'categories' AND uid_foreign = ? ORDER BY sorting_foreign",
[self::TABLE, (int)$sibling['uid']]
);
$datamap[self::TABLE]['NEWPROD' . (++$index)] = [
'pid' => (int)$sibling['pid'],
'title' => $title,
'slug' => $slug,
'teaser' => $teaser,
'categories' => implode(',', array_map('intval', $categories)),
];
$output->writeln(sprintf(' + "%s" (slug %s, teaser draft) - pid %d + %d categorie(s) from sibling #%d',
$title, $slug, (int)$sibling['pid'], count($categories), (int)$sibling['uid']));
}
if (!$apply) {
$output->writeln("\nDRY RUN - nothing written. Add --apply, then re-run vitec:import-product-images.");
return Command::SUCCESS;
}
if ($datamap === []) {
$output->writeln('Nothing to create.');
return Command::SUCCESS;
}
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($datamap, []);
$dataHandler->process_datamap();
if ($dataHandler->errorLog !== []) {
$output->writeln('DataHandler: ' . implode(' | ', $dataHandler->errorLog) . '');
return Command::FAILURE;
}
foreach ($dataHandler->substNEWwithIDs as $newUid) {
$output->writeln(' created #' . (int)$newUid);
}
$output->writeln('Done. Now run vitec:import-product-images (dry-run, then --apply) to attach the assets; replace the teaser drafts once the agency texts arrive.');
return Command::SUCCESS;
}
}