<?php
namespace App\Controller;
use App\Dto\SearchFormDto;
use App\Entity\Applicant;
use App\Entity\Attribute;
use App\Entity\AttributeGroup;
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/skupina")
*/
class AttributeGroupController 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="attribute_group_index", methods={"GET","POST"})
*/
public function index(string $slug, Request $request, FoxentryUtil $foxentryUtil, PaginatorInterface $paginator): Response
{
$attributeGroup = $this->attributeUtil->getAttributeGroupByIdentifier($slug);
if (!$attributeGroup instanceof AttributeGroup) {
throw $this->createNotFoundException('Group 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->setAttributes($attributeGroup->getAttributes()->toArray());
$submenuGroups = $this->attributeUtil->getSubmenuByAttributeGroup($attributeGroup);
$pagination = $paginator->paginate(
$this->courseUtil->searchCourses($filterData), /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20
/*limit per page*/
);
return $this->render('attributeGroup/index.html.twig', [
'attributeGroup' => $attributeGroup,
'submenuGroups' => $submenuGroups,
'form' => $form->createView(),
'showMap'=>$form->isSubmitted(),
'courses' => $pagination,
'locations' => $locationsResult
]);
}
/**
* @Route("/{slug}/{attributeSlug}", name="attribute_group_attr_index", methods={"GET","POST"})
*/
public function attributeGroupSub(string $slug, string $attributeSlug, Request $request, FoxentryUtil $foxentryUtil): Response
{
$attributeGroup = $this->attributeUtil->getAttributeGroupByIdentifier($slug);
if (!$attributeGroup instanceof AttributeGroup) {
throw $this->createNotFoundException('Group 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->setAttributes([$attribute]);
$submenuGroups = $this->attributeUtil->getSubmenuByAttributeGroup($attributeGroup);
return $this->render('attributeGroup/index.html.twig', [
'attributeGroup' => $attributeGroup,
'attribute' => $attribute,
'submenuGroups' => $submenuGroups,
'form' => $form->createView(),
'courses' => $this->courseUtil->searchCourses($filterData),
'locations' => $locationsResult
]);
}
}