src/Command/ReportMissingSlugTranslationsCommand.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Command;
  3. use App\Entity\Language;
  4. use App\Entity\ProductLangParam;
  5. use App\Entity\CategoryLangParam;
  6. use App\Entity\FurnitureTypeLangParam;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. use Symfony\Component\Console\Helper\Table;
  12. class ReportMissingSlugTranslationsCommand extends ContainerAwareCommand
  13. {
  14.     protected function configure()
  15.     {
  16.         $this->setName('app:report:missing-slugs')
  17.             ->setDescription('Report missing or non-unique slugs and titles in products, categories, and furniture types');
  18.     }
  19.     protected function execute(InputInterface $inputOutputInterface $output)
  20.     {
  21.         $em $this->getContainer()->get('doctrine')->getManager();
  22.         $twig $this->getContainer()->get('twig');
  23.         $languageRepo $em->getRepository('App:Language');
  24.         $productLangParamRepo $em->getRepository('App:ProductLangParam');
  25.         $categoryLangParamRepo $em->getRepository('App:CategoryLangParam');
  26.         $furnitureLangParamRepo $em->getRepository('App:FurnitureTypeLangParam');
  27.         $languages $languageRepo->createQueryBuilder('l')->where('l.id != 1')->getQuery()->getResult();
  28.         $productReport = [];
  29.         $categoryReport = [];
  30.         $furnitureReport = [];
  31.         foreach ($languages as $language) {
  32.             // ----------- PRODUCTS -----------
  33.             $productLangParams $productLangParamRepo->createQueryBuilder('plp')
  34.                 ->join('plp.product''p')
  35.                 ->where('plp.language = :lang')
  36.                 ->andWhere('plp.visible = true')
  37.                 ->andWhere('p.deletedBy IS NULL')
  38.                 ->setParameter('lang'$language)
  39.                 ->getQuery()->getResult();
  40.             foreach ($productLangParams as $param) {
  41.                 $product $param->getProduct();
  42.                 $pl $product->translate('pl');
  43.                 $lang $product->translate($language->getLocale());
  44.                 $slugReason null;
  45.                 $titleReason null;
  46.                 if (!$lang || !$lang->getSlug()) {
  47.                     $slugReason 'Missing';
  48.                 } elseif ($lang->getSlug() === $pl->getSlug()) {
  49.                     $slugReason 'Not unique';
  50.                 }
  51.                 if (!$lang || !$lang->getMetaTitle()) {
  52.                     $titleReason 'Missing';
  53.                 } elseif ($lang->getMetaTitle() === $pl->getMetaTitle()) {
  54.                     $titleReason 'Not unique';
  55.                 }
  56.                 if ($slugReason || $titleReason) {
  57.                     $productReport[] = [
  58.                         'type' => 'Product',
  59.                         'id' => $product->getId(),
  60.                         'language' => $language->getLocale(),
  61.                         'name' => $pl->getName(),
  62.                         'pl_slug' => $pl->getSlug() ?: '---',
  63.                         'lang_slug' => $lang $lang->getSlug() : '---',
  64.                         'pl_title' => $pl->getMetaTitle() ?: '---',
  65.                         'lang_title' => $lang $lang->getMetaTitle() : '---',
  66.                         'slug_reason' => $slugReason ?: '',
  67.                         'title_reason' => $titleReason ?: '',
  68.                     ];
  69.                 }
  70.             }
  71.             // ----------- CATEGORIES -----------
  72.             $categoryLangParams $categoryLangParamRepo->createQueryBuilder('clp')
  73.                 ->join('clp.category''c')
  74.                 ->where('clp.language = :lang')
  75.                 ->andWhere('clp.visible = true')
  76.                 ->setParameter('lang'$language)
  77.                 ->getQuery()->getResult();
  78.             foreach ($categoryLangParams as $param) {
  79.                 $category $param->getCategory();
  80.                 if ($category->getDeletedBy()) continue;
  81.                 $pl $category->translate('pl');
  82.                 $lang $category->translate($language->getLocale());
  83.                 $plSlug $pl->getSlug();
  84.                 if (in_array($plSlug, ['premium''outlet''outlet-1''outlet-2'], true)) continue;
  85.                 $slugReason null;
  86.                 $titleReason null;
  87.                 if (!$lang || !$lang->getSlug()) {
  88.                     $slugReason 'Missing';
  89.                 } elseif ($lang->getSlug() === $pl->getSlug()) {
  90.                     $slugReason 'Not unique';
  91.                 }
  92.                 if (!$lang || !$lang->getMetaTitle()) {
  93.                     $titleReason 'Missing';
  94.                 } elseif ($lang->getMetaTitle() === $pl->getMetaTitle()) {
  95.                     $titleReason 'Not unique';
  96.                 }
  97.                 if ($slugReason || $titleReason) {
  98.                     $categoryReport[] = [
  99.                         'type' => 'Category',
  100.                         'id' => $category->getId(),
  101.                         'language' => $language->getLocale(),
  102.                         'name' => $pl->getName(),
  103.                         'pl_slug' => $plSlug ?: '---',
  104.                         'lang_slug' => $lang $lang->getSlug() : '---',
  105.                         'pl_title' => $pl->getMetaTitle() ?: '---',
  106.                         'lang_title' => $lang $lang->getMetaTitle() : '---',
  107.                         'slug_reason' => $slugReason ?: '',
  108.                         'title_reason' => $titleReason ?: '',
  109.                     ];
  110.                 }
  111.             }
  112.             // ----------- FURNITURE TYPES -----------
  113.             $furnitureLangParams $furnitureLangParamRepo->createQueryBuilder('flp')
  114.                 ->join('flp.furnitureType''f')
  115.                 ->where('flp.language = :lang')
  116.                 ->andWhere('flp.visible = true')
  117.                 ->setParameter('lang'$language)
  118.                 ->getQuery()->getResult();
  119.             foreach ($furnitureLangParams as $param) {
  120.                 $furniture $param->getFurnitureType();
  121.                 if ($furniture->getDeletedBy()) continue;
  122.                 $pl $furniture->translate('pl');
  123.                 $lang $furniture->translate($language->getLocale());
  124.                 $slugReason null;
  125.                 $titleReason null;
  126.                 if (!$lang || !$lang->getSlug()) {
  127.                     $slugReason 'Missing';
  128.                 } elseif ($lang->getSlug() === $pl->getSlug()) {
  129.                     $slugReason 'Not unique';
  130.                 }
  131.                 if (!$lang || !$lang->getMetaTitle()) {
  132.                     $titleReason 'Missing';
  133.                 } elseif ($lang->getMetaTitle() === $pl->getMetaTitle()) {
  134.                     $titleReason 'Not unique';
  135.                 }
  136.                 if ($slugReason || $titleReason) {
  137.                     $furnitureReport[] = [
  138.                         'type' => 'FurnitureType',
  139.                         'id' => $furniture->getId(),
  140.                         'language' => $language->getLocale(),
  141.                         'name' => $pl->getName(),
  142.                         'pl_slug' => $pl->getSlug() ?: '---',
  143.                         'lang_slug' => $lang $lang->getSlug() : '---',
  144.                         'pl_title' => $pl->getMetaTitle() ?: '---',
  145.                         'lang_title' => $lang $lang->getMetaTitle() : '---',
  146.                         'slug_reason' => $slugReason ?: '',
  147.                         'title_reason' => $titleReason ?: '',
  148.                     ];
  149.                 }
  150.             }
  151.         }
  152.         // -------- RENDER EMAIL --------
  153.         $htmlBody $twig->render('emails/missing_slug_report.html.twig', [
  154.             'productRows' => $productReport,
  155.             'categoryRows' => $categoryReport,
  156.             'furnitureRows' => $furnitureReport,
  157.         ]);
  158.         // -------- CSV --------
  159.         $csvPath getcwd() . '/missing-slugs-report.csv';
  160.         $fp fopen($csvPath'w');
  161.         fputcsv($fp, ['Type''ID''Lang''Name''PL Slug''Lang Slug''Slug Reason''PL Title''Lang Title''Title Reason']);
  162.         foreach (array_merge($productReport$categoryReport$furnitureReport) as $row) {
  163.             fputcsv($fp, [
  164.                 $row['type'], $row['id'], $row['language'], $row['name'],
  165.                 $row['pl_slug'], $row['lang_slug'], $row['slug_reason'],
  166.                 $row['pl_title'], $row['lang_title'], $row['title_reason']
  167.             ]);
  168.         }
  169.         fclose($fp);
  170.         $this->sendEmailViaGetResponse($htmlBody$csvPath);
  171.         $output->writeln('<info>Report generated and sent.</info>');
  172.     }
  173.     private function sendEmailViaGetResponse($htmlBody$csvPath)
  174.     {
  175.         $csvFile base64_encode(file_get_contents($csvPath));
  176.         $filename basename($csvPath);
  177.         $content = [
  178.             'fromField' => ['fromFieldId' => 'f'],
  179.             'subject' => 'Slug & Title Report :: Missing or Non-Unique Translations',
  180.             'content' => [
  181.                 'html' => $htmlBody,
  182.                 'plain' => strip_tags($htmlBody),
  183.             ],
  184.             'recipients' => [
  185.                 'to' => ['email' => 'm.krupicka@centrumkrzesel.pl']
  186.             ],
  187.             'attachments' => [
  188.                 [
  189.                     'content' => $csvFile,
  190.                     'mimeType' => 'text/csv',
  191.                     'fileName' => $filename
  192.                 ]
  193.             ]
  194.         ];
  195.         $body json_encode($content);
  196.         $ch curl_init();
  197.         curl_setopt($chCURLOPT_URL"https://api3.getresponse360.pl/v3/transactional-emails");
  198.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  199.         curl_setopt($chCURLOPT_POSTFIELDS$body);
  200.         curl_setopt($chCURLOPT_POST1);
  201.         curl_setopt($chCURLOPT_HTTPHEADER, [
  202.             "Content-Type: application/json",
  203.             "X-Auth-Token: api-key gs478s9uv59n5ekulmpmgn5p0uqpepbn",
  204.             "X-Domain: echairs.eu"
  205.         ]);
  206.         curl_exec($ch);
  207.         curl_close($ch);
  208.     }
  209. }