<?php
namespace App\Controller;
use App\Entity\Otpusk\Selection;
use App\Repository\Otpusk\SelectionCriteriaRepository;
use App\Services\PageGenerator;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class SelectionController extends AbstractController
{
private $em;
private $generator;
public function __construct(EntityManager $entityManager, PageGenerator $generator)
{
$this->em = $entityManager;
$this->generator = $generator;
}
/**
* @Route(
* "/select/{selectionSlug}/",
* name="app_selection",
* methods={"GET"},
* priority=3
* )
*/
public function selection(Request $request, $selectionSlug, SelectionCriteriaRepository $selectionCriteriaRepository){
$market = $request->getSession()->get('market');
$selection = $this->em->getRepository(Selection::class)->findOneBySlugAndOtherCriteria($market['id'], $selectionSlug);
if(is_null($selection)) throw new NotFoundHttpException('This page not found.');
$locale = $request->getLocale();
$tours = $this->generator->getSelectionPage($market['code'], $selection->getId(), $locale);
if (!$tours) $tours = [];
$criteries = $selectionCriteriaRepository->findBySelection($selection, true);
$criteriesIds = $this->getSortedCriteriesIds($criteries);
$tours = array_intersect_key($tours, $criteriesIds);
$this->sortArrayByKeysArray($tours, $criteriesIds);
$seo = $this->generator->getSelectionSeo($selection->translate($locale)->getTitle(), $locale);
$response = $this->render('selection/index.html.twig', [
'market' => $market,
'selection' => $selection,
'tours' => $tours,
'seo' => $seo,
'type' => 'selection_item',
]);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
return $response;
}
/**
* @Route(
* "/selection/sort/{entityClass}/{masOrdered}/",
* name="app_sort_criteria",
* methods={"POST"},
* priority=4
* )
*/
public function sortAction($entityClass, $masOrdered)
{
$masOrdered = explode(',', $masOrdered);
try {
$rc = new \ReflectionClass("App\\Entity\\Otpusk\\".$entityClass);
} catch (\ReflectionException $error) {
throw new \ReflectionException("The class name ". $entityClass ." cannot be reflected.");
}
foreach ($masOrdered as $key => $order) {
$item = $this->em->getRepository($rc->getName())->find($order);
if($item) {
$item->setPosition($key + 1);
$this->em->persist($item);
}
}
$this->em->flush();
return new JsonResponse([
'status' => true,
]);
}
public function getSortedCriteriesIds($data)
{
$ids = array();
foreach($data as $item) {
$ids[$item->getId()] = $item->getId();
}
return $ids;
}
public function sortArrayByKeysArray(array &$array, array $orderArray)
{
if (!empty($array) && !empty($orderArray)) {
$array = array_replace(array_flip($orderArray), $array);
}
}
}