104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\ZNZBundle\Resources\script;
|
|
|
|
use External\ZNZBundle\Util\ZNZUtil;
|
|
use KupShop\AdminBundle\Util\Script\Script;
|
|
use KupShop\CatalogBundle\Util\ReviewsUtil;
|
|
use KupShop\I18nBundle\Translations\ReviewsTranslation;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\KupShopBundle\Util\FileUtil;
|
|
use Query\Operator;
|
|
|
|
class ImportReviewsScript extends Script
|
|
{
|
|
protected static $name = '[ZNZ] Import reviews';
|
|
protected static $defaultParameters = [
|
|
'file' => 'data/files/reviews.csv',
|
|
];
|
|
|
|
protected function run(array $arguments)
|
|
{
|
|
$znzUtil = ServiceContainer::getService(ZNZUtil::class);
|
|
$reviewsTranslation = ServiceContainer::getService(ReviewsTranslation::class);
|
|
$languageContext = ServiceContainer::getService(LanguageContext::class);
|
|
|
|
$imported = 0;
|
|
foreach (FileUtil::loadCSV($arguments['file']) as $key => $item) {
|
|
if ($key <= 0) {
|
|
continue;
|
|
}
|
|
|
|
[$productId, $variationId] = $znzUtil->getProductByCode($item[4]);
|
|
|
|
if (!$productId) {
|
|
continue;
|
|
}
|
|
|
|
$review = $item[3];
|
|
$rating = $item[5];
|
|
$reviewStatus = match ($item[1]) {
|
|
'Approved' => ReviewsUtil::RANK_CONFIRMED,
|
|
'Pending' => ReviewsUtil::RANK_UNCONFIRMED,
|
|
'Not Approved' => ReviewsUtil::RANK_DECLINED,
|
|
default => ReviewsUtil::RANK_UNCONFIRMED,
|
|
};
|
|
$languageId = match ($item[7]) {
|
|
'fragranza_sk' => 'sk',
|
|
'parfumzentrum_de', 'pz_de' => 'de',
|
|
'casadelprofumo_it' => 'it',
|
|
'parfumetmoi_fr' => 'fr',
|
|
default => 'cs',
|
|
};
|
|
|
|
$reviewId = sqlQueryBuilder()
|
|
->select('id')
|
|
->from('reviews')
|
|
->where(Operator::equalsNullable([
|
|
'id_product' => $productId,
|
|
'id_product_variation' => $variationId,
|
|
'rating' => $rating,
|
|
'summary' => $review,
|
|
]))->execute()->fetchOne();
|
|
|
|
if (!$reviewId) {
|
|
$reviewId = sqlGetConnection()->transactional(function () use ($productId, $variationId, $rating, $reviewStatus, $review) {
|
|
sqlQueryBuilder()
|
|
->insert('reviews')
|
|
->directValues(
|
|
[
|
|
'id_product' => $productId,
|
|
'id_product_variation' => $variationId,
|
|
'rating' => $rating,
|
|
'figure' => $reviewStatus,
|
|
'summary' => $review,
|
|
]
|
|
)->execute();
|
|
|
|
return sqlInsertId();
|
|
});
|
|
} else {
|
|
sqlQueryBuilder()
|
|
->update('reviews')
|
|
->directValues(['figure' => $reviewStatus])
|
|
->where(Operator::equals(['id' => $reviewId]))
|
|
->execute();
|
|
}
|
|
if ($languageId != $languageContext->getDefaultId()) {
|
|
$reviewsTranslation->saveSingleObject($languageId, $reviewId, [
|
|
'summary' => $review,
|
|
]);
|
|
}
|
|
|
|
$imported++;
|
|
}
|
|
|
|
$this->log('Imported: '.$imported);
|
|
}
|
|
}
|
|
|
|
return ImportReviewsScript::class;
|