Skip to content

Commit

Permalink
Merge pull request #1085 from Victoire/fix/sitemap
Browse files Browse the repository at this point in the history
Fix/sitemap
  • Loading branch information
lenybernard authored Aug 10, 2018
2 parents 6a457d8 + bc3c3f2 commit 34f80a4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Bundle/CoreBundle/Repository/ViewRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function findByViewReferences(array $viewReferences)
->setParameter('pageIds', $pageIds);

$pages = $qb->getQuery()->getResult();
$this->clearInstance();

foreach ($pages as $page) {
$pageId = $page->getId();
Expand Down Expand Up @@ -120,6 +121,7 @@ public function findOneByHomepage($locale = 'fr')
$query = $qb->getQuery();
$view = $query->getOneOrNullResult();
$view->translate($locale);
$this->clearInstance();

return $view;
}
Expand Down
6 changes: 5 additions & 1 deletion Bundle/PageBundle/Helper/PageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,15 @@ public function findPageByReference($viewReference)
]);
$page->setCurrentLocale($viewReference->getLocale());
} else { //VirtualBusinessPage
$page = $this->entityManager->getRepository('VictoireCoreBundle:View')
$page = $this->entityManager->getRepository(View::class)
->findOneBy([
'id' => $viewReference->getTemplateId(),
]);
$page->setCurrentLocale($viewReference->getLocale());
if ($entity = $this->findEntityByReference($viewReference)) {
if (method_exists($entity, 'setCurrentLocale')) {
$entity->setCurrentLocale($viewReference->getLocale());
}
if ($page instanceof BusinessTemplate) {
$page = $this->updatePageWithEntity($page, $entity);
}
Expand Down
2 changes: 1 addition & 1 deletion Bundle/SitemapBundle/Controller/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function xmlAction(Request $request)
}

return $this->render('VictoireSitemapBundle:Sitemap:sitemap.xml.twig', [
'pages' => json_decode($pages),
'pages' => json_decode($pages, true),
]);
}

Expand Down
38 changes: 26 additions & 12 deletions Bundle/SitemapBundle/Domain/Export/SitemapExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Doctrine\ORM\EntityManager;
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
use Victoire\Bundle\BusinessPageBundle\Entity\VirtualBusinessPage;
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
use Victoire\Bundle\PageBundle\Entity\BasePage;
use Victoire\Bundle\PageBundle\Helper\PageHelper;
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceRepository;
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
Expand Down Expand Up @@ -58,7 +60,10 @@ public function handle($locale)

/** @var ViewReference $tree */
$tree = $this->viewReferenceRepo->getOneReferenceByParameters(
['viewId' => $homepage->getId()],
[
'viewId' => $homepage->getId(),
'locale' => $locale,
],
true,
true
);
Expand All @@ -67,48 +72,57 @@ public function handle($locale)

$getChildrenIds = function (ViewReference $tree) use (&$getChildrenIds, $ids) {
foreach ($tree->getChildren() as $child) {
$ids[] = $child->getViewId();
$ids = array_merge($ids, $getChildrenIds($child));
if (null !== $child->getViewId()) {
$ids[] = $child->getViewId();
$ids = array_merge($ids, $getChildrenIds($child));
}
}

return $ids;
};
$ids = array_unique($getChildrenIds($tree));

$pages = $this->entityManager->getRepository('VictoirePageBundle:BasePage')
$pages = $this->entityManager->getRepository(BasePage::class)
->getAll(true)
->joinSeo()
->filterByIds($getChildrenIds($tree))
->joinTranslations($locale)
->filterByIds($ids)
->run();
/** @var BasePage $page */
foreach ($pages as $page) {
$page->translate($locale);
}

return array_merge($pages, $this->getBusinessPages($tree));
}

public function serialize($pages)
{
$data = [];
$sitemapEntries = [];

/** @var BasePage $page */
foreach ($pages as $page) {
// BusinessTemplate have no getUrl() method
if ($page instanceof BusinessTemplate) {
continue;
}

$seo = $page->getSeo();

$data[] = [
$sitemapEntry = [
'url' => $page->getUrl(),
'sitemapChangeFreq' => $seo === null ? 'monthly' : $seo->getSitemapChangeFreq(),
'sitemapPriority' => $seo === null ? 0.5 : $seo->getSitemapPriority(),
];

// This data is optional in sitemap, add it only if a publication date is available
// see https://www.sitemaps.org/protocol.html#xmlTagDefinitions
if (null !== $page->getPublishedAt() and $page->getPublishedAt() instanceof \DateTime) {
$data['publishedAt'] = $page->getPublishedAt()->format('c');
if (null !== $page->getPublishedAt() && $page->getPublishedAt() instanceof \DateTime) {
$sitemapEntry['publishedAt'] = $page->getPublishedAt()->format('Y-m-d');
}
$sitemapEntries[] = $sitemapEntry;
}

return json_encode($data);
return json_encode($sitemapEntries);
}

/**
Expand All @@ -125,7 +139,7 @@ private function getBusinessPages(ViewReference $tree, $businessPages = [])
{
foreach ($tree->getChildren() as $child) {
if ($child instanceof BusinessPageReference
&& $child->getViewNamespace() == 'Victoire\Bundle\BusinessPageBundle\Entity\VirtualBusinessPage'
&& $child->getViewNamespace() == VirtualBusinessPage::class
) {
/** @var WebViewInterface $businessPage */
$businessPage = $this->pageHelper->findPageByReference($child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% for page in pages %}
<url>
<loc>{{ url('victoire_core_page_show', {'url': page.url}) }}</loc>
{% if page.publishedAt %}<lastmod>{{ page.publishedAt }}</lastmod>{% endif %}
{% if page.publishedAt|default(false) %}<lastmod>{{ page.publishedAt }}</lastmod>{% endif %}
<changefreq>{{ page.sitemapChangeFreq }}</changefreq>
<priority>{{ page.sitemapPriority }}</priority>
</url>
Expand Down

0 comments on commit 34f80a4

Please sign in to comment.