<?php
namespace App\Controller\Front;
use App\DTO\InstructionViewDTO;
use App\DTO\InstructionWithProgressDTO;
use App\Entity\Instruction;
use App\Entity\InstructionCategory;
use App\Entity\User;
use App\Repository\InstructionCategoryRepository;
use App\Repository\InstructionRepository;
use App\Repository\ProgressCheckListElementRepository;
use App\Repository\ProgressInstructionRepository;
use App\Repository\ProgressInstructionStepRepository;
use App\Service\ConfigService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class InstructionCatalogController extends AbstractController
{
use FrontTraitController;
private $em;
private $instructionRepository;
private $instructionCategoryRepository;
public function __construct(EntityManagerInterface $em,
InstructionRepository $instructionRepository,
InstructionCategoryRepository $instructionCategoryRepository)
{
$this->em = $em;
$this->instructionRepository = $instructionRepository;
$this->instructionCategoryRepository = $instructionCategoryRepository;
}
/**
* @Route("/", name="homepage", methods={"GET"})
*/
public function homepage(Request $request, string $reactSiteBaseUrl)
{
return $this->redirect($reactSiteBaseUrl);
}
/**
* @Route("/catalog", name="app_catalog", methods={"GET"})
*/
public function roots(Request $request, InstructionCategoryRepository $repository)
{
$locale = $request->cookies->get('locale', 'en');
/** @var InstructionCategory $root */
$root = $repository->findOneBy(['slug' => 'root-'.$locale]);
$subCategories = $repository->getChildrenWithActiveInstructions($root);
return $this->render('catalog/roots.html.twig', [
'rows' => $subCategories,
]);
}
/**
* @Route("/catalog/{slug}", name="app_catalog_category", methods={"GET"})
*/
public function category(InstructionCategory $instructionCategory,
InstructionRepository $instructionRepository,
InstructionCategoryRepository $repository)
{
$category = $repository->getJoinedActiveInstructions($instructionCategory);
$subCategories = $repository->getChildrenWithActiveInstructions($instructionCategory);
$instructions = [];
/** @var Instruction $instruction */
foreach ($instructionCategory->getInstructions() as $instruction) {
if ($instruction->visible()) {
$instructions[] = $instruction;
}
}
return $this->render('catalog/category.html.twig', [
'category' => $instructionCategory,
'instructions' => $instructions,
'subCategories' => $subCategories,
]);
}
/**
* @Route("/catalog/{categorySlug}/{instructionSlug}/view", name="app_catalog_instruction_view_readonly", methods={"GET"})
*/
public function viewReadonly(InstructionRepository $instructionRepository,
InstructionCategoryRepository $instructionCategoryRepository,
string $categorySlug, string $instructionSlug)
{
/** @var User $user */
$user = $this->getUser();
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED') OR
$this->isGranted('IS_AUTHENTICATED_FULLY ')
) {
return $this->redirectToRoute('app_catalog_instruction_view', [
'categorySlug' => $categorySlug,
'instructionSlug' => $instructionSlug,
]);
}
$instructionCategory = $instructionCategoryRepository->findOneBy(['slug' => $categorySlug]);
$instruction = $instructionRepository->findOneBy(['slug' => $instructionSlug]);
if (!isset($instructionCategory, $instruction)) {
throw new NotFoundHttpException();
}
return $this->render('catalog/instruction_readonly.html.twig', [
'category' => $instructionCategory,
'instruction' => $instruction,
]);
}
/**
* @Route("/catalog/{categorySlug}/{instructionSlug}", name="app_catalog_instruction_view", methods={"GET"})
*/
public function instruction(InstructionRepository $instructionRepository,
ConfigService $configService,
string $categorySlug, string $instructionSlug)
{
/** @var User $user */
$user = $this->getUser();
$instructionCategoryRepository = $this->em->getRepository(InstructionCategory::class);
$instructionCategory = $instructionCategoryRepository->findOneBy(['slug' => $categorySlug]);
$instruction = $instructionRepository->findOneBy(['slug' => $instructionSlug]);
if (!isset($instructionCategory, $instruction)) {
throw new NotFoundHttpException();
}
$root = $instructionCategoryRepository->findOneBy(['slug' => $instructionCategory->getSlug()]);
$arrayTree = $instructionCategoryRepository->children($root);
$showPaidServices = $configService->get('show-paid-services-on-website');
if ($user) {
$dto = new InstructionWithProgressDTO($instruction, $user, $this->em, $showPaidServices);
} else {
$dto = new InstructionViewDTO($instruction, $this->em);
}
return $this->render('catalog/instruction.html.twig', [
'category' => $instructionCategory,
'instruction' => $dto,
'rows' => $arrayTree,
'showPaidServices' => $showPaidServices,
]);
}
}