sterntours/src/AppBundle/Entity/PageRepository.php
Kevin Adametz 754aa699a1 Startseiten-Prototyp unter Dev-Link
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-28 08:21:40 +00:00

307 lines
9 KiB
PHP

<?php
namespace AppBundle\Entity;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Doctrine\ORM\Query\Expr;
/**
* PageRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PageRepository extends NestedTreeRepository
{
/**
* @param Page $page
* @return Page[]|array
*
* @todo Optimize performance by adapting search algorithm's optimizations
*/
public function getChildrenWithTravelProgramsAndDates(Page $page)
{
$pages = $this->getChildrenQueryBuilder($page)
->leftJoin('node.travelProgram', 'tp')
->addSelect('tp')
->andWhere('tp.status > 0')
->andWhere('node.status > 0')
->orderBy('node.order')
->addOrderBy('tp.position')
->addOrderBy('node.title')
->getQuery()
->execute();
/** @var Page $childPage */
foreach ($pages as &$childPage)
{
if ($childPage->getTravelProgram())
{
$this->getEntityManager()->getRepository('AppBundle:TravelPeriod')->getTrueTravelPeriods(
$childPage->getTravelProgram());
}
}
return $pages;
}
public function findWithTravelProgramsOfCountry(TravelCountry $country)
{
return $this->createQueryBuilder('node')
->innerJoin('node.travelProgram', 'tp')
->innerJoin('tp.countries', 'c')
->where('c.id = '. $country->getId())
->andWhere('node.status = 1')
->andWhere('tp.status = 1')
->getQuery()
->execute()
;
}
/**
* @return Page[]
*/
public function findOffers()
{
$ret = [];
$countries = $this->getEntityManager()->getRepository('AppBundle:TravelCountry')->findAll();
foreach ($countries as $country)
{
$ret = array_merge($ret, $this->createQueryBuilder('node')
->innerJoin('node.travelProgram', 'tp')
->addSelect('tp')
->innerJoin('tp.countries', 'c')
->where('c.id = '. $country->getId())
->andWhere('node.status = 1')
->andWhere('tp.status = 1')
->orderBy('node.order')
->addOrderBy('tp.position')
->addOrderBy('node.title')
->setMaxResults(3)
->getQuery()
->execute()
);
}
shuffle($ret);
return $ret;
}
/**
* Curated offer selection for the start page test version.
*
* The existing findOffers() intentionally shuffles results. For the crisis
* communication start page we need a stable order with currently sellable
* destinations first.
*
* @param int $limit
* @return Page[]
*/
public function findHomepageOffers($limit = 12)
{
$pages = $this->createQueryBuilder('node')
->distinct()
->innerJoin('node.travelProgram', 'tp')
->addSelect('tp')
->innerJoin('tp.countries', 'c')
->addSelect('c')
->where('node.status = 1')
->andWhere('tp.status = 1')
->orderBy('node.order')
->addOrderBy('tp.position')
->addOrderBy('node.title')
->getQuery()
->execute();
$pages = array_values(array_filter($pages, function (Page $page) {
return $this->getHomepagePriority($page) <= 4;
}));
usort($pages, function (Page $a, Page $b) {
$priorityA = $this->getHomepagePriority($a);
$priorityB = $this->getHomepagePriority($b);
if ($priorityA !== $priorityB) {
return $priorityA - $priorityB;
}
$orderA = $a->getOrder() ?: 9999;
$orderB = $b->getOrder() ?: 9999;
if ($orderA !== $orderB) {
return $orderA - $orderB;
}
return strcasecmp($a->getTitle(), $b->getTitle());
});
return array_slice($pages, 0, $limit);
}
/**
* @return Page[]
*/
public function findHomepageCountryPages()
{
$pages = $this->findCountryPages();
usort($pages, function (Page $a, Page $b) {
$priorityA = $this->getHomepagePriority($a);
$priorityB = $this->getHomepagePriority($b);
if ($priorityA !== $priorityB) {
return $priorityA - $priorityB;
}
return strcasecmp($a->getTitle(), $b->getTitle());
});
return $pages;
}
private function getHomepagePriority(Page $page)
{
$haystack = $this->buildHomepageSortHaystack($page);
$priorities = [
0 => ['aegypt', 'agypt', 'egypt'],
1 => ['marokko'],
2 => ['usbek', 'uzbek'],
3 => ['oman'],
4 => ['jordan'],
6 => ['israel'],
7 => ['tuerkei', 'turkei'],
8 => ['iran'],
];
foreach ($priorities as $priority => $needles) {
foreach ($needles as $needle) {
if (strpos($haystack, $needle) !== false) {
return $priority;
}
}
}
return 99;
}
private function buildHomepageSortHaystack(Page $page)
{
$parts = [
$page->getSlug(),
$page->getTitle(),
$page->getTitleShort(),
$page->getRealUrlPath(),
];
if ($page->getCountry()) {
$parts[] = $page->getCountry()->getName();
$parts[] = $page->getCountry()->getSlug();
}
if ($page->getTravelProgram()) {
foreach ($page->getTravelProgram()->getCountries() as $country) {
$parts[] = $country->getName();
$parts[] = $country->getSlug();
}
}
return strtolower(implode(' ', array_filter($parts)));
}
public function findCountryPages()
{
return $this->createQueryBuilder('node')
->innerJoin('node.country', 'country')
->where('node.status > 0')
->andWhere('node.template = \'overview\'')
->andWhere('node.lvl = 0')
->orderBy('node.lft,node.title')
->getQuery()
->execute()
;
}
public function findTopCountryNavPages()
{
return $this->createQueryBuilder('node')
->innerJoin('node.country', 'country')
->leftJoin('node.children', 'childPage', Expr\Join::WITH, 'childPage.status > 0')
->addSelect('childPage')
->where('node.status > 0')
->andWhere('node.template = \'overview\'')
->andWhere('node.lvl = 0')
->andWhere('node.order > 0')
->orderBy('node.order,node.title, childPage.lft, childPage.title')
->getQuery()
->execute()
;
}
public function findFeedbacks($rootPageId)
{
$qb = $this->createQueryBuilder('node');
return $qb
->where($qb->expr()->eq('node.parent', $rootPageId))
->andWhere('node.showInNavi = 1')
->andWhere('node.status = 1')
->orderBy('node.order')
->getQuery()
->execute()
;
}
public function findParentsWithShowNav($rootPageId)
{
$qb = $this->createQueryBuilder('node');
$pages = $qb->innerJoin('node.travelProgram', 'tp')
->addSelect('tp')
->where($qb->expr()->eq('node.parent', $rootPageId))
->andWhere('node.showInNavi = 1')
->andWhere('node.status = 1')
->andWhere('tp.status > 0')
->orderBy('node.order')
->getQuery()
->execute()
;
foreach ($pages as &$childPage)
{
if ($childPage->getTravelProgram())
{
// var_dump($childPage->getTravelProgram()->getId());
// $this->getEntityManager()->getRepository('AppBundle:TravelPeriod')->getTrueTravelPeriods($childPage->getTravelProgram());
}
}
return $pages;
}
/**
* @param Page $page
*
* @return Page[]|\Doctrine\Common\Collections\Collection
*/
public function getSiblings(Page $page)
{
$parent = $page->getParent();
if (!$parent)
{
// On purpose, we don't treat root pages as if they were siblings
return [];
}
$siblings = $parent->getChildren();
foreach ($siblings as &$sibling)
{
$sibling->setParent($parent);
}
// Da diese Methode nur für die Navigation verwendet wird, kann man hier vorfiltern
$filteredSiblings = [];
foreach ($siblings as &$sibling)
{
if($sibling->getStatus() == 1 && $sibling->getShowInNavi() == 1)
{
$filteredSiblings[] = $sibling;
}
}
return $filteredSiblings;
}
}