src/Controller/ProjectController.php line 84

Open in your IDE?
  1. <?php
  2. /**
  3.  * Project Controller
  4.  *
  5.  * (c) Bertin van den Ham <b.vandenham@dappr.nl>
  6.  */
  7. namespace App\Controller;
  8. use App\Entity\Project;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use FontLib\TrueType\Collection;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use VisualMedia\LisaBundle\Component\ManagerData;
  14. use VisualMedia\ProjectBundle\Controller\ProjectController as BaseProjectController;
  15. use VisualMedia\ProjectBundle\Manager\BaseProjectManager;
  16. use VisualMedia\ProjectBundle\Provider\ProjectLayoutProvider;
  17. /**
  18.  * Project Controller
  19.  */
  20. class ProjectController extends BaseProjectController
  21. {
  22.     /**
  23.      * {@inheritDoc}
  24.      */
  25.     public function viewBySlugAction(Request $requeststring $slug null): Response
  26.     {
  27.         $translator $this->get('translator');
  28.         $layoutProvider $this->get(ProjectLayoutProvider::class);
  29.         $manager $this->get(BaseProjectManager::class);
  30.         if (null === $project $manager->getFirst(new ManagerData(array($manager::OPTION_SLUG => $slug)))) {
  31.             throw new Exception($translator->trans('project.not_found', [], 'exception'), 404);
  32.         }
  33.         elseif (!$project->getPublished()) {
  34.             throw new Exception($translator->trans('project.gone', [], 'exception'), 410);
  35.         }
  36.         $previousProject $nextProject $firstProject $lastProject null;
  37.         if(null !== $projects $manager->getIndex(new ManagerData(array(
  38.             $manager::OPTION_PUBLISHED => true
  39.         ), array(
  40.             'id' => 'ASC'
  41.         )))) {
  42.             if(!empty($projects) && count($projects) > 1) {
  43.                 // Find the index of the current, first and last project item in the array
  44.                 $currentProjectIndex $this->getCurrentProjectIndexFromArray($projects$project);
  45.                 $firstProjectIndex array_key_first($projects);
  46.                 $lastProjectIndex array_key_last($projects);
  47.                 // get first and last project
  48.                 $firstProject $projects[$firstProjectIndex];
  49.                 $lastProject $projects[$lastProjectIndex];
  50.                 if($firstProject === $project) {
  51.                     $next $currentProjectIndex 1;
  52.                     $previousProject $lastProject;
  53.                     $nextProject $projects[$next] ?? null;
  54.                 } elseif($lastProject === $project) {
  55.                     $prev $currentProjectIndex 1;
  56.                     $previousProject $projects[$prev] ?? null;
  57.                     $nextProject $firstProject;
  58.                 } else {
  59.                     $prev $currentProjectIndex 1;
  60.                     $next $currentProjectIndex 1;
  61.                     $previousProject $projects[$prev];
  62.                     $nextProject $projects[$next];
  63.                 }
  64.             }
  65.         }
  66.         return $this->render($project->getLayout(), array(
  67.             'browser_title' => $project->getBrowserTitle(),
  68.             'meta_description' => $project->getMetaDescription(),
  69.             'meta_keywords' => $project->getMetaKeywords(),
  70.             'indexable' => $project->getIndexable(),
  71.             'project' => $project,
  72.             'projects' => $projects,
  73.             'previous_project' => $previousProject,
  74.             'next_project' => $nextProject,
  75.         ));
  76.     }
  77.     /**
  78.      * Get Current Project index From Array
  79.      *
  80.      * @param array $projects
  81.      * @param Project $project
  82.      *
  83.      * @return int|null
  84.      */
  85.     protected function getCurrentProjectIndexFromArray(array $projectsProject $project): ?int
  86.     {
  87.         if(!empty($projects) && is_array($projects)) {
  88.             foreach($projects as $key => $item) {
  89.                 if($item === $project) {
  90.                     return $key;
  91.                 }
  92.             }
  93.         }
  94.         return null;
  95.     }
  96. }