<?php
namespace App\Controller;
use App\Dto\SearchFormDto;
use App\Entity\Applicant;
use App\Entity\Attribute;
use App\Entity\City;
use App\Entity\Course;
use App\Entity\CourseRegistrationRequest;
use App\Entity\CourseReview;
use App\Entity\CourseTopic;
use App\Entity\Organizer;
use App\Form\CourseFilterType;
use App\Form\CourseRegistrationRequestType;
use App\Form\CourseReviewType;
use App\Form\CourseType;
use App\Repository\CourseRepository;
use App\Util\ApplicantUtil;
use App\Util\AttributeUtil;
use App\Util\CourseUtil;
use App\Util\FoxentryUtil;
use App\Util\OrganizerUtil;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/kurzy/tema")
*/
class CourseTopicController extends AbstractController
{
/**
* @var OrganizerUtil
*/
private $organizerUtil;
/**
* @var ApplicantUtil
*/
private $applicantUtil;
/**
* @var CourseUtil
*/
private $courseUtil;
/**
* @var AttributeUtil
*/
private $attributeUtil;
public function __construct(OrganizerUtil $organizerUtil, ApplicantUtil $applicantUtil, CourseUtil $courseUtil, AttributeUtil $attributeUtil)
{
$this->courseUtil = $courseUtil;
$this->attributeUtil = $attributeUtil;
}
/**
* @Route("/{slug}", name="course_topic_index", methods={"GET","POST"})
*/
public function index(string $slug, Request $request, PaginatorInterface $paginator, FoxentryUtil $foxentryUtil): Response
{
$topic = $this->courseUtil->getCourseTopicBySlug($slug);
if (!$topic instanceof CourseTopic) {
throw $this->createNotFoundException('Topic not found');
}
$form = $this->createForm(CourseFilterType::class, null, ['topic' => $topic]);
$form->handleRequest($request);
/** @var SearchFormDto $filterData */
$filterData = $form->getData();
if (!$filterData instanceof SearchFormDto) {
$filterData = new SearchFormDto();
}
$locationsResult = [];
if ($form->isSubmitted() && !empty($filterData->getLocation())) {
$locationsResult = $foxentryUtil->getSearchResults($filterData->getLocation());
if (is_array($locationsResult) && count($locationsResult) > 1) {
$filterData->setLocation(null);
}
}
if ($locationsResult instanceof City) {
$locationsResult = [];
}
$filterData->setTopics([$topic]);
$submenuGroups = $this->attributeUtil->getSubmenuGroupsByTopic($topic);
$pagination = $paginator->paginate(
$this->courseUtil->searchCourses($filterData), /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20
/*limit per page*/
);
return $this->render('courseTopic/index.html.twig', [
'topic' => $topic,
'submenuGroups' => $submenuGroups,
'form' => $form->createView(),
'showMap' => $form->isSubmitted(),
'pagination' => $pagination,
'locations' => $locationsResult
]);
}
/**
* @Route("/{topicSlug}/{attributeSlug}", name="course_topic_group_index", methods={"GET","POST"})
*/
public function attributeGroup(string $topicSlug, string $attributeSlug, Request $request, PaginatorInterface $paginator, FoxentryUtil $foxentryUtil): Response
{
$topic = $this->courseUtil->getCourseTopicBySlug($topicSlug);
if (!$topic instanceof CourseTopic) {
throw $this->createNotFoundException('Topic not found');
}
$attribute = $this->attributeUtil->getAttributeByIdentifier($attributeSlug);
if (!$attribute instanceof Attribute) {
throw $this->createNotFoundException('Attribute not found');
}
$form = $this->createForm(CourseFilterType::class, null);
$form->handleRequest($request);
/** @var SearchFormDto $filterData */
$filterData = $form->getData();
if (!$filterData instanceof SearchFormDto) {
$filterData = new SearchFormDto();
}
$locationsResult = [];
if ($form->isSubmitted() && !empty($filterData->getLocation())) {
$locationsResult = $foxentryUtil->getSearchResults($filterData->getLocation());
if (is_array($locationsResult) && count($locationsResult) > 1) {
$filterData->setLocation(null);
}
}
if ($locationsResult instanceof City) {
$locationsResult = [];
}
$filterData->setTopics([$topic]);
$filterData->setAttributes([$attribute]);
$submenuGroups = $this->attributeUtil->getSubmenuGroupsByTopic($topic);
$pagination = $paginator->paginate(
$this->courseUtil->searchCourses($filterData), /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20
/*limit per page*/
);
return $this->render('courseTopic/index.html.twig', [
'topic' => $topic,
'attribute' => $attribute,
'submenuGroups' => $submenuGroups,
'form' => $form->createView(),
'pagination' => $pagination,
'showMap' => true,
'courses' => $this->courseUtil->searchCourses($filterData),
'locations' => $locationsResult
]);
}
}