custom/plugins/fourtwosixitaliantranslations/src/FourtwosixItalianTranslations.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixItalianTranslations;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. class FourtwosixItalianTranslations extends Plugin
  13. {
  14.     const CSV_SEPARATOR "$";
  15.     public function activate(ActivateContext $activateContext): void
  16.     {
  17.         /** @var EntityRepository */
  18.         $languageRepository $this->container->get('language.repository');
  19.         $criteria = new Criteria();
  20.         $criteria->addFilter(new EqualsFilter('locale.code''it-IT'));
  21.         $criteria->addAssociation('locale.code');
  22.         $context Context::createDefaultContext();
  23.         $italianLanguage $languageRepository->searchIds($criteria$context)->getIds();
  24.         if($italianLanguage) {
  25.             
  26.             /** @var EntityRepository */
  27.             $mailTemplateTranslationRepository $this->container->get('mail_template.repository');
  28.             $criteria = new Criteria();
  29.             $criteria->addAssociation('translations');
  30.             $criteria->addAssociation('mailTemplateType');
  31.             $criteria->addFilter(new EqualsFilter('translations.languageId'$italianLanguage[0]));
  32.             $allItalianTranslations $mailTemplateTranslationRepository->search($criteria$context)->getElements();
  33.             $allAlreadTranslatedTechnicalNames = [];
  34.             foreach($allItalianTranslations as $translation) {
  35.                 $allAlreadTranslatedTechnicalNames[] = $translation->getMailTemplateType()->getTechnicalName();
  36.             }
  37.             $tranlsationsFile fopen(__DIR__ DIRECTORY_SEPARATOR "File" DIRECTORY_SEPARATOR "mail_template.csv""r");
  38.             $translationsToAdd = [];
  39.             while((($data fgetcsv($tranlsationsFile0self::CSV_SEPARATOR)) !== false)) {
  40.                 if(!in_array($data[0], $allAlreadTranslatedTechnicalNames)) {
  41.                     $translationsToAdd[$data[0]] = $data;
  42.                 }
  43.             }
  44.             /** @var Connection $connection */
  45.             $connection $this->container->get(\Doctrine\DBAL\Connection::class);
  46.             $sql "SELECT HEX(mail_template.id) as id, mail_template_type.technical_name FROM mail_template 
  47.                         INNER JOIN mail_template_type ON mail_template_type.id = mail_template.mail_template_type_id
  48.                         WHERE mail_template_type.technical_name IN (\"" implode('","'array_keys($translationsToAdd)) . "\")";
  49.             $result $connection->executeQuery($sql)->fetchAllAssociative();
  50.             $createTranslations = [];
  51.             foreach ($result as $newTranslation) {
  52.                 $newTranslationCSVRow $translationsToAdd[$newTranslation["technical_name"]];
  53.                 
  54.                 $createTranslations[] = [
  55.                     "languageId" => strtolower($italianLanguage[0]),
  56.                     "mailTemplateId" => strtolower($newTranslation['id']),
  57.                     "senderName" => $newTranslationCSVRow[1],
  58.                     "subject" => $newTranslationCSVRow[2],
  59.                     "contentHtml" => $newTranslationCSVRow[3],
  60.                     "contentPlain" => $newTranslationCSVRow[4],
  61.                     "description" => $newTranslationCSVRow[5],
  62.                 ];
  63.             }
  64.                 
  65.             /** @var EntityRepository */
  66.             $mailTemplateTranslationRepository $this->container->get('mail_template_translation.repository');
  67.             try {
  68.                 $mailTemplateTranslationRepository->create($createTranslations$context);
  69.                 //code...
  70.             } catch (\Shopware\Core\Framework\Validation\WriteConstraintViolationException $ex) {
  71.                 dump($ex->getViolations());
  72.             } 
  73.         }
  74.     }
  75.     public function update(UpdateContext $updateContext): void
  76.     {
  77.         $updateContext->setAutoMigrate(true);
  78.     }
  79.     public function uninstall(UninstallContext $uninstallContext): void
  80.     {
  81.         if ($uninstallContext->keepUserData()) {
  82.             return;
  83.         }
  84.         $uninstallContext->setAutoMigrate(true);
  85.     }
  86. }