feat: sort groups price services by price first, then alphabetically

This commit is contained in:
Björn Fromme
2026-08-03 16:23:47 +02:00
parent 9d2aa11fdb
commit 0b21bb00cb
4 changed files with 55 additions and 5 deletions
@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace App\Controller\Admin\Accommodation;
use App\Entity\Groups\Accommodation;
use App\Entity\Groups\AdditionalService;
use App\Entity\Groups\BoardService;
use App\Form\Admin\Groups\AccommodationType;
use App\Service\CmsDataProvider;
use Doctrine\ORM\EntityManagerInterface;
@@ -52,10 +54,31 @@ class EditController extends AbstractController
return $this->redirectToRoute('app_admin_accommodation_edit', ['id' => $accommodation->getId()]);
}
$boardServices = $accommodation->getBoardServices()->toArray();
usort($boardServices, $this->compareServices(...));
$additionalServices = $accommodation->getAdditionalServices()->toArray();
usort($additionalServices, $this->compareServices(...));
return $this->render('admin/accommodation/edit.html.twig', [
'accommodation' => $accommodation,
'form' => $form,
'cmsData' => $cmsData,
'boardServices' => $boardServices,
'additionalServices' => $additionalServices,
]);
}
/**
* DateFrom ascending, then free (price 0) before paid, then alphabetically by label.
*/
private function compareServices(BoardService|AdditionalService $a, BoardService|AdditionalService $b): int
{
$aFree = 0 === $a->getPrice() ? 0 : 1;
$bFree = 0 === $b->getPrice() ? 0 : 1;
return $a->getDateFrom() <=> $b->getDateFrom()
?: $aFree <=> $bFree
?: strnatcasecmp($a->getLabel(), $b->getLabel());
}
}
@@ -156,6 +156,12 @@ class AccommodationBookingService
}
}
$boardServices = $this->sortServicesByPriceThenLabel($boardServices);
$ungrouped = $this->sortServicesByPriceThenLabel($ungrouped);
foreach ($grouped as $groupName => $groupServices) {
$grouped[$groupName] = $this->sortServicesByPriceThenLabel($groupServices);
}
return [
'boardServices' => $boardServices,
'additionalServices' => $additionalServices,
@@ -164,6 +170,27 @@ class AccommodationBookingService
];
}
/**
* Free services (price 0) first, then alphabetically; each group sorted alphabetically.
*
* @template T of BoardService|AdditionalService
*
* @param T[] $services
*
* @return T[]
*/
private function sortServicesByPriceThenLabel(array $services): array
{
usort($services, static function (BoardService|AdditionalService $a, BoardService|AdditionalService $b): int {
$aFree = 0 === $a->getPrice() ? 0 : 1;
$bFree = 0 === $b->getPrice() ? 0 : 1;
return $aFree <=> $bFree ?: strnatcasecmp($a->getLabel(), $b->getLabel());
});
return $services;
}
/**
* Determines whether the booking must be treated as non-binding and why.
*