1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Listener; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Presta\SitemapBundle\Event\SitemapPopulateEvent; |
7
|
|
|
use Presta\SitemapBundle\Service\SitemapListenerInterface; |
8
|
|
|
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator; |
9
|
|
|
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete; |
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11
|
|
|
use Symfony\Component\Routing\RouterInterface; |
12
|
|
|
|
13
|
|
|
class SitemapListener implements SitemapListenerInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
private $entityManager; |
16
|
|
|
private $router; |
17
|
|
|
private $defaultLocale; |
18
|
|
|
private $locales; |
19
|
|
|
private $baseUrl; |
20
|
|
|
private $contentTypes; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
RouterInterface $router, |
24
|
|
|
EntityManager $entityManager, |
|
|
|
|
25
|
|
|
$defaultLocale, |
26
|
|
|
$locales, |
27
|
|
|
$baseUrl, |
28
|
|
|
$contentTypes |
29
|
|
|
) { |
30
|
|
|
$this->entityManager = $entityManager; |
31
|
|
|
$this->router = $router; |
32
|
|
|
$this->defaultLocale = $defaultLocale; |
33
|
|
|
$this->locales = $locales; |
34
|
|
|
$this->baseUrl = $baseUrl; |
35
|
|
|
$this->contentTypes = $contentTypes; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function populateSitemap(SitemapPopulateEvent $event) |
39
|
|
|
{ |
40
|
|
|
$section = $event->getSection(); |
41
|
|
|
|
42
|
|
|
if (is_null($section) || $section == 'cms') { |
43
|
|
|
foreach ($this->locales as $locale) { |
44
|
|
|
$nodeRepository = $this->entityManager->getRepository('AlpixelCMSBundle:Node'); |
45
|
|
|
$pages = $nodeRepository->findAllWithLocale($locale); |
|
|
|
|
46
|
|
|
|
47
|
|
|
foreach ($pages as $page) { |
48
|
|
|
$hasController = true; |
49
|
|
|
foreach ($this->contentTypes as $contentType) { |
50
|
|
|
if (get_class($page) == $contentType['class'] && $contentType['controller'] === null) { |
51
|
|
|
$hasController = false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($hasController === false) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$url = $this->router->generate( |
60
|
|
|
'alpixel_cms', |
61
|
|
|
[ |
62
|
|
|
'slug' => $page->getSlug(), |
63
|
|
|
'_locale' => $page->getLocale(), |
64
|
|
|
], |
65
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$url = new UrlConcrete( |
69
|
|
|
$url, |
70
|
|
|
$page->getDateUpdated(), |
71
|
|
|
UrlConcrete::CHANGEFREQ_MONTHLY, |
72
|
|
|
.7 |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$urlLang = new GoogleMultilangUrlDecorator($url); |
76
|
|
|
|
77
|
|
|
$translations = $nodeRepository->findTranslations($page); |
78
|
|
|
foreach ($translations as $translation) { |
79
|
|
|
if ($locale !== $translation->getLocale()) { |
80
|
|
|
$url = $this->router->generate( |
81
|
|
|
'alpixel_cms', |
82
|
|
|
[ |
83
|
|
|
'slug' => $translation->getSlug(), |
84
|
|
|
'_locale' => $translation->getLocale(), |
85
|
|
|
], |
86
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
87
|
|
|
); |
88
|
|
|
$urlLang->addLink($url, $translation->getLocale()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$event->getGenerator()->addUrl( |
|
|
|
|
93
|
|
|
$urlLang, |
94
|
|
|
'cms.'.$locale |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.