Files
kupshop/bundles/External/HannahBundle/Resources/script/ImportSellersContentScript.php
2025-08-02 16:30:27 +02:00

103 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace External\HannahBundle\Resources\script;
use KupShop\AdminBundle\Util\Script\Script;
use KupShop\ContentBundle\Util\Block;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Query\Operator;
class ImportSellersContentScript extends Script
{
protected static $name = '[OC] Import sellers content';
protected static $defaultParameters = [
'emailOnly' => true,
'file' => 'data/files/sellers.json',
];
protected function run(array $arguments)
{
$photosUrl = 'https://www.hannah.cz/data/photos/';
$blockUtil = ServiceContainer::getService(Block::class);
$data = json_decode(file_get_contents($arguments['file']), true);
foreach ($data as $seller) {
if (!($sellerId = $this->getSellerIdByExternalId((int) $seller['id_external']))) {
continue;
}
sqlQueryBuilder()
->update('sellers')
->directValues(['email' => $seller['email']])
->where(Operator::equals(['id' => $sellerId]))
->execute();
if ($arguments['emailOnly'] ?? false) {
continue;
}
// content
if ($blockId = $blockUtil->insertFirstBlock('sellers', $sellerId, $seller['content'])) {
sqlQueryBuilder()
->update('blocks')
->directValues(['json_content' => $seller['json_content']])
->where(Operator::equals(['id' => $blockId]))
->execute();
}
// photos
$photos = explode(';', $seller['photos'] ?: '');
foreach ($photos as $position => $photo) {
if ($photoId = $this->getDownloader()->importProductImage($photosUrl.$photo)) {
try {
sqlQueryBuilder()
->insert('photos_sellers_relation')
->directValues(
[
'id_photo' => $photoId,
'id_seller' => $sellerId,
'show_in_lead' => $position === 0 ? 'Y' : 'N',
'position' => $position,
]
)->execute();
} catch (\Exception $e) {
}
}
}
}
}
private function getDownloader(): \Downloader
{
static $downloader;
if (!$downloader) {
$downloader = (new \Downloader())
->setMethod('curl');
}
return $downloader;
}
private function getSellerIdByExternalId(int $externalId): ?int
{
$sellerId = sqlQueryBuilder()
->select('id')
->from('sellers')
->where(Operator::equals(['id_external' => $externalId]))
->execute()->fetchOne();
if (!$sellerId) {
return null;
}
return (int) $sellerId;
}
}
return ImportSellersContentScript::class;