<?php declare(strict_types=1);
namespace FourtwosixItalianTranslations;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class FourtwosixItalianTranslations extends Plugin
{
const CSV_SEPARATOR = "$";
public function activate(ActivateContext $activateContext): void
{
/** @var EntityRepository */
$languageRepository = $this->container->get('language.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('locale.code', 'it-IT'));
$criteria->addAssociation('locale.code');
$context = Context::createDefaultContext();
$italianLanguage = $languageRepository->searchIds($criteria, $context)->getIds();
if($italianLanguage) {
/** @var EntityRepository */
$mailTemplateTranslationRepository = $this->container->get('mail_template.repository');
$criteria = new Criteria();
$criteria->addAssociation('translations');
$criteria->addAssociation('mailTemplateType');
$criteria->addFilter(new EqualsFilter('translations.languageId', $italianLanguage[0]));
$allItalianTranslations = $mailTemplateTranslationRepository->search($criteria, $context)->getElements();
$allAlreadTranslatedTechnicalNames = [];
foreach($allItalianTranslations as $translation) {
$allAlreadTranslatedTechnicalNames[] = $translation->getMailTemplateType()->getTechnicalName();
}
$tranlsationsFile = fopen(__DIR__ . DIRECTORY_SEPARATOR . "File" . DIRECTORY_SEPARATOR . "mail_template.csv", "r");
$translationsToAdd = [];
while((($data = fgetcsv($tranlsationsFile, 0, self::CSV_SEPARATOR)) !== false)) {
if(!in_array($data[0], $allAlreadTranslatedTechnicalNames)) {
$translationsToAdd[$data[0]] = $data;
}
}
/** @var Connection $connection */
$connection = $this->container->get(\Doctrine\DBAL\Connection::class);
$sql = "SELECT HEX(mail_template.id) as id, mail_template_type.technical_name FROM mail_template
INNER JOIN mail_template_type ON mail_template_type.id = mail_template.mail_template_type_id
WHERE mail_template_type.technical_name IN (\"" . implode('","', array_keys($translationsToAdd)) . "\")";
$result = $connection->executeQuery($sql)->fetchAllAssociative();
$createTranslations = [];
foreach ($result as $newTranslation) {
$newTranslationCSVRow = $translationsToAdd[$newTranslation["technical_name"]];
$createTranslations[] = [
"languageId" => strtolower($italianLanguage[0]),
"mailTemplateId" => strtolower($newTranslation['id']),
"senderName" => $newTranslationCSVRow[1],
"subject" => $newTranslationCSVRow[2],
"contentHtml" => $newTranslationCSVRow[3],
"contentPlain" => $newTranslationCSVRow[4],
"description" => $newTranslationCSVRow[5],
];
}
/** @var EntityRepository */
$mailTemplateTranslationRepository = $this->container->get('mail_template_translation.repository');
try {
$mailTemplateTranslationRepository->create($createTranslations, $context);
//code...
} catch (\Shopware\Core\Framework\Validation\WriteConstraintViolationException $ex) {
dump($ex->getViolations());
}
}
}
public function update(UpdateContext $updateContext): void
{
$updateContext->setAutoMigrate(true);
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$uninstallContext->setAutoMigrate(true);
}
}