<?php
namespace App\Command;
use App\Entity\Language;
use App\Entity\ProductLangParam;
use App\Entity\CategoryLangParam;
use App\Entity\FurnitureTypeLangParam;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;
class ReportMissingSlugTranslationsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('app:report:missing-slugs')
->setDescription('Report missing or non-unique slugs and titles in products, categories, and furniture types');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$twig = $this->getContainer()->get('twig');
$languageRepo = $em->getRepository('App:Language');
$productLangParamRepo = $em->getRepository('App:ProductLangParam');
$categoryLangParamRepo = $em->getRepository('App:CategoryLangParam');
$furnitureLangParamRepo = $em->getRepository('App:FurnitureTypeLangParam');
$languages = $languageRepo->createQueryBuilder('l')->where('l.id != 1')->getQuery()->getResult();
$productReport = [];
$categoryReport = [];
$furnitureReport = [];
foreach ($languages as $language) {
// ----------- PRODUCTS -----------
$productLangParams = $productLangParamRepo->createQueryBuilder('plp')
->join('plp.product', 'p')
->where('plp.language = :lang')
->andWhere('plp.visible = true')
->andWhere('p.deletedBy IS NULL')
->setParameter('lang', $language)
->getQuery()->getResult();
foreach ($productLangParams as $param) {
$product = $param->getProduct();
$pl = $product->translate('pl');
$lang = $product->translate($language->getLocale());
$slugReason = null;
$titleReason = null;
if (!$lang || !$lang->getSlug()) {
$slugReason = 'Missing';
} elseif ($lang->getSlug() === $pl->getSlug()) {
$slugReason = 'Not unique';
}
if (!$lang || !$lang->getMetaTitle()) {
$titleReason = 'Missing';
} elseif ($lang->getMetaTitle() === $pl->getMetaTitle()) {
$titleReason = 'Not unique';
}
if ($slugReason || $titleReason) {
$productReport[] = [
'type' => 'Product',
'id' => $product->getId(),
'language' => $language->getLocale(),
'name' => $pl->getName(),
'pl_slug' => $pl->getSlug() ?: '---',
'lang_slug' => $lang ? $lang->getSlug() : '---',
'pl_title' => $pl->getMetaTitle() ?: '---',
'lang_title' => $lang ? $lang->getMetaTitle() : '---',
'slug_reason' => $slugReason ?: '',
'title_reason' => $titleReason ?: '',
];
}
}
// ----------- CATEGORIES -----------
$categoryLangParams = $categoryLangParamRepo->createQueryBuilder('clp')
->join('clp.category', 'c')
->where('clp.language = :lang')
->andWhere('clp.visible = true')
->setParameter('lang', $language)
->getQuery()->getResult();
foreach ($categoryLangParams as $param) {
$category = $param->getCategory();
if ($category->getDeletedBy()) continue;
$pl = $category->translate('pl');
$lang = $category->translate($language->getLocale());
$plSlug = $pl->getSlug();
if (in_array($plSlug, ['premium', 'outlet', 'outlet-1', 'outlet-2'], true)) continue;
$slugReason = null;
$titleReason = null;
if (!$lang || !$lang->getSlug()) {
$slugReason = 'Missing';
} elseif ($lang->getSlug() === $pl->getSlug()) {
$slugReason = 'Not unique';
}
if (!$lang || !$lang->getMetaTitle()) {
$titleReason = 'Missing';
} elseif ($lang->getMetaTitle() === $pl->getMetaTitle()) {
$titleReason = 'Not unique';
}
if ($slugReason || $titleReason) {
$categoryReport[] = [
'type' => 'Category',
'id' => $category->getId(),
'language' => $language->getLocale(),
'name' => $pl->getName(),
'pl_slug' => $plSlug ?: '---',
'lang_slug' => $lang ? $lang->getSlug() : '---',
'pl_title' => $pl->getMetaTitle() ?: '---',
'lang_title' => $lang ? $lang->getMetaTitle() : '---',
'slug_reason' => $slugReason ?: '',
'title_reason' => $titleReason ?: '',
];
}
}
// ----------- FURNITURE TYPES -----------
$furnitureLangParams = $furnitureLangParamRepo->createQueryBuilder('flp')
->join('flp.furnitureType', 'f')
->where('flp.language = :lang')
->andWhere('flp.visible = true')
->setParameter('lang', $language)
->getQuery()->getResult();
foreach ($furnitureLangParams as $param) {
$furniture = $param->getFurnitureType();
if ($furniture->getDeletedBy()) continue;
$pl = $furniture->translate('pl');
$lang = $furniture->translate($language->getLocale());
$slugReason = null;
$titleReason = null;
if (!$lang || !$lang->getSlug()) {
$slugReason = 'Missing';
} elseif ($lang->getSlug() === $pl->getSlug()) {
$slugReason = 'Not unique';
}
if (!$lang || !$lang->getMetaTitle()) {
$titleReason = 'Missing';
} elseif ($lang->getMetaTitle() === $pl->getMetaTitle()) {
$titleReason = 'Not unique';
}
if ($slugReason || $titleReason) {
$furnitureReport[] = [
'type' => 'FurnitureType',
'id' => $furniture->getId(),
'language' => $language->getLocale(),
'name' => $pl->getName(),
'pl_slug' => $pl->getSlug() ?: '---',
'lang_slug' => $lang ? $lang->getSlug() : '---',
'pl_title' => $pl->getMetaTitle() ?: '---',
'lang_title' => $lang ? $lang->getMetaTitle() : '---',
'slug_reason' => $slugReason ?: '',
'title_reason' => $titleReason ?: '',
];
}
}
}
// -------- RENDER EMAIL --------
$htmlBody = $twig->render('emails/missing_slug_report.html.twig', [
'productRows' => $productReport,
'categoryRows' => $categoryReport,
'furnitureRows' => $furnitureReport,
]);
// -------- CSV --------
$csvPath = getcwd() . '/missing-slugs-report.csv';
$fp = fopen($csvPath, 'w');
fputcsv($fp, ['Type', 'ID', 'Lang', 'Name', 'PL Slug', 'Lang Slug', 'Slug Reason', 'PL Title', 'Lang Title', 'Title Reason']);
foreach (array_merge($productReport, $categoryReport, $furnitureReport) as $row) {
fputcsv($fp, [
$row['type'], $row['id'], $row['language'], $row['name'],
$row['pl_slug'], $row['lang_slug'], $row['slug_reason'],
$row['pl_title'], $row['lang_title'], $row['title_reason']
]);
}
fclose($fp);
$this->sendEmailViaGetResponse($htmlBody, $csvPath);
$output->writeln('<info>Report generated and sent.</info>');
}
private function sendEmailViaGetResponse($htmlBody, $csvPath)
{
$csvFile = base64_encode(file_get_contents($csvPath));
$filename = basename($csvPath);
$content = [
'fromField' => ['fromFieldId' => 'f'],
'subject' => 'Slug & Title Report :: Missing or Non-Unique Translations',
'content' => [
'html' => $htmlBody,
'plain' => strip_tags($htmlBody),
],
'recipients' => [
'to' => ['email' => 'm.krupicka@centrumkrzesel.pl']
],
'attachments' => [
[
'content' => $csvFile,
'mimeType' => 'text/csv',
'fileName' => $filename
]
]
];
$body = json_encode($content);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api3.getresponse360.pl/v3/transactional-emails");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"X-Auth-Token: api-key gs478s9uv59n5ekulmpmgn5p0uqpepbn",
"X-Domain: echairs.eu"
]);
curl_exec($ch);
curl_close($ch);
}
}