<?php
/**
* Project Controller
*
* (c) Bertin van den Ham <b.vandenham@dappr.nl>
*/
namespace App\Controller;
use App\Entity\Project;
use Doctrine\Common\Collections\ArrayCollection;
use FontLib\TrueType\Collection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use VisualMedia\LisaBundle\Component\ManagerData;
use VisualMedia\ProjectBundle\Controller\ProjectController as BaseProjectController;
use VisualMedia\ProjectBundle\Manager\BaseProjectManager;
use VisualMedia\ProjectBundle\Provider\ProjectLayoutProvider;
/**
* Project Controller
*/
class ProjectController extends BaseProjectController
{
/**
* {@inheritDoc}
*/
public function viewBySlugAction(Request $request, string $slug = null): Response
{
$translator = $this->get('translator');
$layoutProvider = $this->get(ProjectLayoutProvider::class);
$manager = $this->get(BaseProjectManager::class);
if (null === $project = $manager->getFirst(new ManagerData(array($manager::OPTION_SLUG => $slug)))) {
throw new Exception($translator->trans('project.not_found', [], 'exception'), 404);
}
elseif (!$project->getPublished()) {
throw new Exception($translator->trans('project.gone', [], 'exception'), 410);
}
$previousProject = $nextProject = $firstProject = $lastProject = null;
if(null !== $projects = $manager->getIndex(new ManagerData(array(
$manager::OPTION_PUBLISHED => true
), array(
'id' => 'ASC'
)))) {
if(!empty($projects) && count($projects) > 1) {
// Find the index of the current, first and last project item in the array
$currentProjectIndex = $this->getCurrentProjectIndexFromArray($projects, $project);
$firstProjectIndex = array_key_first($projects);
$lastProjectIndex = array_key_last($projects);
// get first and last project
$firstProject = $projects[$firstProjectIndex];
$lastProject = $projects[$lastProjectIndex];
if($firstProject === $project) {
$next = $currentProjectIndex + 1;
$previousProject = $lastProject;
$nextProject = $projects[$next] ?? null;
} elseif($lastProject === $project) {
$prev = $currentProjectIndex - 1;
$previousProject = $projects[$prev] ?? null;
$nextProject = $firstProject;
} else {
$prev = $currentProjectIndex - 1;
$next = $currentProjectIndex + 1;
$previousProject = $projects[$prev];
$nextProject = $projects[$next];
}
}
}
return $this->render($project->getLayout(), array(
'browser_title' => $project->getBrowserTitle(),
'meta_description' => $project->getMetaDescription(),
'meta_keywords' => $project->getMetaKeywords(),
'indexable' => $project->getIndexable(),
'project' => $project,
'projects' => $projects,
'previous_project' => $previousProject,
'next_project' => $nextProject,
));
}
/**
* Get Current Project index From Array
*
* @param array $projects
* @param Project $project
*
* @return int|null
*/
protected function getCurrentProjectIndexFromArray(array $projects, Project $project): ?int
{
if(!empty($projects) && is_array($projects)) {
foreach($projects as $key => $item) {
if($item === $project) {
return $key;
}
}
}
return null;
}
}