custom/plugins/dkcLocations/src/Storefront/Controller/DkcLocationController.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DkcLocations\Storefront\Controller;
  3. use DkcLocations\Core\Content\Location\LocationEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Controller\StorefrontController;
  11. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @RouteScope(scopes={"storefront"})
  18.  */
  19. class DkcLocationController extends StorefrontController
  20. {
  21.     private EntityRepositoryInterface $locationRepository;
  22.     private EntityRepositoryInterface $employeeRepository;
  23.     private GenericPageLoaderInterface $genericPageLoader;
  24.     public function __construct(
  25.         EntityRepositoryInterface $locationRepository,
  26.         EntityRepositoryInterface $employeeRepository,
  27.         GenericPageLoaderInterface $genericPageLoader
  28.     ) {
  29.         $this->locationRepository $locationRepository;
  30.         $this->employeeRepository $employeeRepository;
  31.         $this->genericPageLoader $genericPageLoader;
  32.     }
  33.     /**
  34.      * @Route("/standorte/{slug}", name="frontend.dkc.location.detail", methods={"GET"}, defaults={"_routeScope"={"storefront"}})
  35.      */
  36.     public function detail(string $slugRequest $requestSalesChannelContext $salesChannelContext): Response
  37.     {
  38.         $salesChannelId $salesChannelContext->getSalesChannel()->getId();
  39.         // Load generic page (header, footer, navigation, breadcrumb)
  40.         $page $this->genericPageLoader->load($request$salesChannelContext);
  41.         // Load all active locations for this sales channel to find by slug
  42.         $criteria = new Criteria();
  43.         $criteria->addFilter(new EqualsFilter('active'true));
  44.         $criteria->addFilter(new EqualsFilter('locationSalesChannels.salesChannelId'$salesChannelId));
  45.         $criteria->addAssociation('country');
  46.         $locations $this->locationRepository->search($criteria$salesChannelContext->getContext());
  47.         /** @var LocationEntity|null $location */
  48.         $location null;
  49.         foreach ($locations as $loc) {
  50.             if (self::generateSlug($loc->getName()) === $slug) {
  51.                 $location $loc;
  52.                 break;
  53.             }
  54.         }
  55.         if (!$location) {
  56.             throw new NotFoundHttpException('Standort nicht gefunden.');
  57.         }
  58.         // Set meta title
  59.         $metaInformation $page->getMetaInformation();
  60.         if ($metaInformation) {
  61.             $metaInformation->setMetaTitle($location->getName() . ' | ' $salesChannelContext->getSalesChannel()->getName());
  62.         }
  63.         // Load employees for this location AND sales channel
  64.         $empCriteria = new Criteria();
  65.         $empCriteria->addFilter(new EqualsFilter('employeeLocations.locationId'$location->getId()));
  66.         $empCriteria->addFilter(new EqualsFilter('employeeSalesChannels.salesChannelId'$salesChannelId));
  67.         $empCriteria->addAssociation('employeeDepartments.department');
  68.         $empCriteria->addAssociation('image');
  69.         $empCriteria->addSorting(new FieldSorting('lastName'FieldSorting::ASCENDING));
  70.         $employees $this->employeeRepository->search($empCriteria$salesChannelContext->getContext());
  71.         // Group employees by department (employee can be in multiple departments)
  72.         $groupedEmployees = [];
  73.         foreach ($employees as $employee) {
  74.             $departments $employee->getEmployeeDepartments();
  75.             if ($departments === null || $departments->count() === 0) {
  76.                 // No department assigned
  77.                 $key '999_Allgemein';
  78.                 if (!isset($groupedEmployees[$key])) {
  79.                     $groupedEmployees[$key] = [
  80.                         'name' => 'Allgemein',
  81.                         'position' => 999,
  82.                         'employees' => [],
  83.                     ];
  84.                 }
  85.                 $groupedEmployees[$key]['employees'][] = $employee;
  86.             } else {
  87.                 foreach ($departments as $empDept) {
  88.                     $dept $empDept->getDepartment();
  89.                     $deptName $dept $dept->getName() : 'Allgemein';
  90.                     $deptPos $dept $dept->getPosition() : 999;
  91.                     $key $deptPos '_' $deptName;
  92.                     if (!isset($groupedEmployees[$key])) {
  93.                         $groupedEmployees[$key] = [
  94.                             'name' => $deptName,
  95.                             'position' => $deptPos,
  96.                             'employees' => [],
  97.                         ];
  98.                     }
  99.                     $groupedEmployees[$key]['employees'][] = $employee;
  100.                 }
  101.             }
  102.         }
  103.         ksort($groupedEmployees);
  104.         return $this->renderStorefront('@DkcLocations/storefront/page/location-detail.html.twig', [
  105.             'page' => $page,
  106.             'location' => $location,
  107.             'groupedEmployees' => $groupedEmployees,
  108.         ]);
  109.     }
  110.     public static function generateSlug(string $name): string
  111.     {
  112.         $slug mb_strtolower($name'UTF-8');
  113.         $slug str_replace(
  114.             ['ä''ö''ü''ß''Ä''Ö''Ü'],
  115.             ['ae''oe''ue''ss''ae''oe''ue'],
  116.             $slug
  117.         );
  118.         $slug preg_replace('/[^a-z0-9]+/''-'$slug);
  119.         $slug trim($slug'-');
  120.         return $slug;
  121.     }
  122. }