<commit_before><?php

namespace Alpixel\Bundle\CMSBundle\Listener;

use Doctrine\ORM\EntityManager;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\SitemapListenerInterface;
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

class SitemapListener implements SitemapListenerInterface
{
    private $entityManager;
    private $router;
    private $defaultLocale;
    private $locales;
    private $baseUrl;
    private $contentTypes;

    public function __construct(RouterInterface $router, EntityManager $entityManager, $defaultLocale, $locales, $baseUrl, $contentTypes)
    {
        $this->entityManager = $entityManager;
        $this->router = $router;
        $this->defaultLocale = $defaultLocale;
        $this->locales = $locales;
        $this->baseUrl = $baseUrl;
        $this->contentTypes = $contentTypes;
    }

    public function populateSitemap(SitemapPopulateEvent $event)
    {
        $section = $event->getSection();

        if (is_null($section) || $section == 'cms') {
            $nodeRepository = $this->entityManager->getRepository('AlpixelCMSBundle:Node');
            $pages = $nodeRepository->findAllWithLocale($this->defaultLocale);

            foreach ($pages as $page) {
                $hasController = true;
                foreach ($this->contentTypes as $contentType) {
                    if (get_class($page) == $contentType['class'] && $contentType['controller'] === null) {
                        $hasController = false;
                    }
                }

                if ($hasController === false) {
                    continue;
                }

                $url = $this->router->generate('alpixel_cms', [
                    'slug'    => $page->getSlug(),
                    '_locale' => $page->getLocale(),
                ], UrlGeneratorInterface::ABSOLUTE_URL);

                $url = new UrlConcrete(
                    $url,
                    $page->getDateUpdated(),
                    UrlConcrete::CHANGEFREQ_MONTHLY,
                    .7
                );

                $urlLang = new GoogleMultilangUrlDecorator($url);
                foreach ($this->locales as $locale) {
                    if ($locale !== $this->defaultLocale) {
                        $translatedPage = $nodeRepository->findTranslation($page, $locale);

                        if ($translatedPage !== null) {
                            $url = $this->router->generate('alpixel_cms', [
                                'slug'    => $translatedPage->getSlug(),
                                '_locale' => $translatedPage->getLocale(),
                            ], UrlGeneratorInterface::ABSOLUTE_URL);

                            $urlLang->addLink($url, $locale);
                        }
                    }
                }

                $event->getGenerator()->addUrl(
                    $urlLang,
                    'cms'
                );
            }
        }
    }
}<commit_msg>updating the sitemap behaviour when dealing with locales<commit_after><?php

namespace Alpixel\Bundle\CMSBundle\Listener;

use Doctrine\ORM\EntityManager;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\SitemapListenerInterface;
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

class SitemapListener implements SitemapListenerInterface
{
    private $entityManager;
    private $router;
    private $defaultLocale;
    private $locales;
    private $baseUrl;
    private $contentTypes;

    public function __construct(
        RouterInterface $router,
        EntityManager $entityManager,
        $defaultLocale,
        $locales,
        $baseUrl,
        $contentTypes
    ) {
        $this->entityManager = $entityManager;
        $this->router = $router;
        $this->defaultLocale = $defaultLocale;
        $this->locales = $locales;
        $this->baseUrl = $baseUrl;
        $this->contentTypes = $contentTypes;
    }

    public function populateSitemap(SitemapPopulateEvent $event)
    {
        $section = $event->getSection();

        if (is_null($section) || $section == 'cms') {
            foreach ($this->locales as $locale) {
                $nodeRepository = $this->entityManager->getRepository('AlpixelCMSBundle:Node');
                $pages = $nodeRepository->findAllWithLocale($locale);

                foreach ($pages as $page) {
                    $hasController = true;
                    foreach ($this->contentTypes as $contentType) {
                        if (get_class($page) == $contentType['class'] && $contentType['controller'] === null) {
                            $hasController = false;
                        }
                    }

                    if ($hasController === false) {
                        continue;
                    }

                    $url = $this->router->generate(
                        'alpixel_cms',
                        [
                            'slug'    => $page->getSlug(),
                            '_locale' => $page->getLocale(),
                        ],
                        UrlGeneratorInterface::ABSOLUTE_URL
                    );

                    $url = new UrlConcrete(
                        $url,
                        $page->getDateUpdated(),
                        UrlConcrete::CHANGEFREQ_MONTHLY,
                        .7
                    );

                    $urlLang = new GoogleMultilangUrlDecorator($url);

                    $translations = $nodeRepository->findTranslations($page);
                    foreach ($translations as $translation) {
                        if ($locale !== $translation->getLocale()) {
                            $url = $this->router->generate(
                                'alpixel_cms',
                                [
                                    'slug'    => $translation->getSlug(),
                                    '_locale' => $translation->getLocale(),
                                ],
                                UrlGeneratorInterface::ABSOLUTE_URL
                            );
                            $urlLang->addLink($url, $translation->getLocale());
                        }
                    }

                    $event->getGenerator()->addUrl(
                        $urlLang,
                        'cms.'.$locale
                    );
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10891672807663964, 'fraction_numerical': 0.0002947678703021371, 'mean_word_length': 1.4985272459499264, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5421092', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1637, 'n_words': 274}