Skip to content

Commit

Permalink
EventSitemapProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
manuxi committed Mar 31, 2021
1 parent c7cd4ee commit 3bcb33a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Manuxi\SuluEventBundle\Repository;

use Doctrine\Common\Collections\Criteria;
use Manuxi\SuluEventBundle\Entity\Event;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
Expand All @@ -15,7 +16,6 @@
use Sulu\Component\SmartContent\Orm\DataProviderRepositoryInterface;
use Sulu\Component\SmartContent\Orm\DataProviderRepositoryTrait;


/**
* @method Event|null find($id, $lockMode = null, $lockVersion = null)
* @method Event|null findOneBy(array $criteria, array $orderBy = null)
Expand Down Expand Up @@ -83,6 +83,13 @@ public function findById(int $id, string $locale): ?Event
return $event;
}

public static function createEnabledCriteria(): Criteria
{
return Criteria::create()
->andWhere(Criteria::expr()->eq('enabled', true))
;
}

/**
* Returns filtered entities.
* When pagination is active the result count is pageSize + 1 to determine has next page.
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,11 @@
<argument type="service" id="sulu_event.repository.event"/>
<tag name="sulu_route.defaults_provider"/>
</service>

<service id="sulu_event.sitemap_provider" public="true" class="Manuxi\SuluEventBundle\Sitemap\EventSitemapProvider">
<argument type="service" id="sulu_event.repository.event"/>
<argument type="service" id="sulu_core.webspace.webspace_manager"/>
<tag name="sulu.sitemap.provider"/>
</service>
</services>
</container>
81 changes: 81 additions & 0 deletions src/Sitemap/EventSitemapProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Manuxi\SuluEventBundle\Sitemap;

use Manuxi\SuluEventBundle\Repository\EventRepository;
use Sulu\Bundle\WebsiteBundle\Sitemap\Sitemap;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapProviderInterface;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapUrl;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;

class EventSitemapProvider implements SitemapProviderInterface
{
private $eventRepository;
private $webspaceManager;
private $locales = [];

public function __construct(
EventRepository $eventRepository,
WebspaceManagerInterface $webspaceManager
) {
$this->eventRepository = $eventRepository;
$this->webspaceManager = $webspaceManager;
}

public function build($page, $scheme, $host)
{
$locale = $this->getLocaleByHost($host);

$result = [];
foreach ($this->findEvents(self::PAGE_SIZE, ($page - 1) * self::PAGE_SIZE) as $event) {
$event->setLocale($locale);
$result[] = new SitemapUrl(
$scheme . '://' . $host . $event->getRoutePath(),
$event->getLocale(),
$event->getLocale(),
$event->getChanged()
);
}

return $result;
}

public function createSitemap($scheme, $host)
{
return new Sitemap($this->getAlias(), $this->getMaxPage($scheme, $host));
}

public function getAlias()
{
return 'events';
}

/**
* @TODO: count method in repo
*/
public function getMaxPage($scheme, $host)
{
return ceil(count($this->findEvents()) / self::PAGE_SIZE);
}

private function getLocaleByHost($host) {
if(!\array_key_exists($host, $this->locales)) {
$portalInformation = $this->webspaceManager->getPortalInformations();
foreach ($portalInformation as $hostName => $portal) {
if($hostName === $host) {
$this->locales[$host] = $portal->getLocale();
}
}
}
return $this->locales[$host];
}

private function findEvents($limit = null, $offset = null)
{
$criteria = [
'enabled' => true,
];

return $this->eventRepository->findBy($criteria, [], $limit, $offset);
}
}

0 comments on commit 3bcb33a

Please sign in to comment.