Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include VirtualBusinessPages into sitemap.xml #323

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Bundle/CoreBundle/Repository/ViewRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,32 @@ public function findOneByHomepage($locale = 'fr')

return $view;
}

/**
* Get PageSeo.
*
* @param string $method leftJoin|innerJoin
*
* @return ViewRepository
*/
public function joinSeo($method = 'leftJoin')
{
$this->getInstance()->$method('page.seo', 'seo')->addSelect('seo');

return $this;
}

/**
* Filter the query by the sitemap index (=visibility).
*
* @param array $ids
*
* @return ViewRepository
*/
public function filterByIds($ids)
{
$this->getInstance()->andWhere('page.id IN (:ids)')->setParameter('ids', $ids);

return $this;
}
}
74 changes: 61 additions & 13 deletions Bundle/SitemapBundle/Controller/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
use Victoire\Bundle\PageBundle\Entity\BasePage;
use Victoire\Bundle\PageBundle\Helper\PageHelper;
use Victoire\Bundle\SeoBundle\Entity\PageSeo;
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;

/**
* Victoire sitemap controller.
Expand All @@ -18,30 +23,73 @@
class SitemapController extends Controller
{
/**
* Change the sitemap priority for the given page.
* Get the whole list of published pages
* #1 get the _locale related homepage
* #2 parse recursively and extract every persisted pages ids
* #3 load these pages with seo (if exists)
* #4 parse recursively and extract every VirtualBusinessPages references
* #5 prepare VirtualBusinessPages.
*
* @Route(".{_format}", name="victoire_sitemap_xml", Requirements={"_format" = "xml"})
* @Template("VictoireSitemapBundle:Sitemap:sitemap.xml.twig")
*
* @return template
* @return Response
*/
public function xmlAction()
public function xmlAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$homepage = $em->getRepository('VictoirePageBundle:BasePage')
->findOneByHomepage($request->getLocale());

/** @var ViewReference $tree */
$tree = $this->get('victoire_view_reference.repository')->getOneReferenceByParameters(
['viewId' => $homepage->getId()],
true,
true
);

$ids = [$tree->getViewId()];

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

return $ids;
};

$pages = $em->getRepository('VictoirePageBundle:BasePage')
->getAll()
->getAll(true)
->joinSeo()
->filterByIds($getChildrenIds($tree))
->run();

$indexedPages = [];
foreach ($pages as $page) {
if (!$page->getSeo() || $page->getSeo()->isSitemapIndexed()) {
$indexedPages[] = $page;
/** @var PageHelper $pageHelper */
$pageHelper = $this->get('victoire_page.page_helper');
$entityManager = $this->getDoctrine()->getManager();

$getBusinessPages = function (ViewReference $tree) use (&$getBusinessPages, $pageHelper, $entityManager) {
$businessPages = [];
foreach ($tree->getChildren() as $child) {
if ($child instanceof BusinessPageReference
&& $child->getViewNamespace() == 'Victoire\Bundle\BusinessPageBundle\Entity\VirtualBusinessPage') {
$entity = $entityManager->getRepository($child->getEntityNamespace())->find($child->getEntityId());
/** @var WebViewInterface $businessPage */
$businessPage = $pageHelper->findPageByReference($child, $entity);
$businessPage->setReference($child);
$businessPages[] = $businessPage;
}
$businessPages = array_merge($businessPages, $getBusinessPages($child));
}
}

return [
'pages' => $indexedPages,
];
return $businessPages;
};

$pages = array_merge($pages, $getBusinessPages($tree));

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

/**
Expand Down