<?php
namespace App\Controller;
use App\Entity\Course;
use App\Repository\CourseRepository;
use App\Repository\CourseCategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\Criteria;
/**
* @Route("/courses")
*/
class CourseController extends AbstractController {
/**
* Course List.
* @Route(
* "/list/{page}/{sort}/{dir}",
* name="client_course_list",
* methods={"GET","POST"},
* requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
* defaults={"page"=1,"sort"="createdAt","dir"="desc"}
* )
*
* @return string
*/
public function list(
Request $request, $page, $sort, $dir,
\App\Service\SeoHelper $seoHelper,
CourseRepository $courseRepository
): Response {
$limit = $request->query->getInt('limit', 20);
$seoHelper->setInternalSeo('Courses', 'client_course_list');
$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->eq('isEnabled', true));
$criteria->orderBy([$sort => $dir]);
$entries = $courseRepository->matching($criteria);
$entries = $entries->slice(($page-1)*$limit, $limit);
$total = $courseRepository->countAll();
$pages = ceil($total/$limit);
return $this->render('client/courses/list.html.twig', [
'title' => 'Courses',
'listParams' => [],
'entries' => $entries,
'ordering' => [
'sort' => $sort,
'dir' => $dir
],
'meta' => [
'title' => 'Courses',
'type' => 'website',
],
'page' => [
'icon' => 'list',
'title' => 'Courses',
'subtitle' => 'View the list with the Courses',
'header' => 'Training Manager - Courses List'
],
'pagination' => [
'listURL' => 'client_course_list',
'totalSearch' => $total,
'total' => $total,
'limit' => $limit,
'totalPages' => $pages,
'page' => $page,
],
]);
}
/**
* Course Category List.
* @Route(
* "/category/{slug}/{page}/{sort}/{dir}",
* name="client_course_category_list",
* methods={"GET","POST"},
* requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
* defaults={"page"=1,"sort"="createdAt","dir"="desc"}
* )
*
* @return string
*/
public function category(
Request $request, $page, $sort, $dir, $slug,
\App\Service\SeoHelper $seoHelper,
CourseCategoryRepository $courseCategoryRepository
): Response {
$limit = $request->query->getInt('limit', 20);
$category = $courseCategoryRepository->findOneBy(["slug" => $slug, "isEnabled" => true], ["createdAt" => "Desc"]);
if (!$category) throw $this->createNotFoundException('The course category does not exist');
$seoHelper->setCourseCategorySeo($category);
$entries = $category->getCourseOrdering();
$total = $entries->count();
$entries = $entries->slice(($page-1)*$limit, $limit);
$pages = ceil($total/$limit);
return $this->render('client/courses/category.html.twig', [
'title' => $category->getTitle(),
'listParams' => [],
'entries' => $entries,
'showContent' => $category->getShowContent(),
'content' => $category->getContent(),
'ordering' => [
'sort' => $sort,
'dir' => $dir
],
'page' => [
'icon' => 'list',
'title' => 'Courses',
'subtitle' => 'View the list with the Courses',
'header' => 'Training Manager - Courses List',
],
'meta' => [
'title' => $category->getTitle(),
'type' => 'website',
'description' => $category->getMetaDescription(),
'keywords' => $category->getMetaKeywords()
],
'pagination' => [
'listURL' => 'client_course_list',
'totalSearch' => $total,
'total' => $total,
'limit' => $limit,
'totalPages' => $pages,
'page' => $page,
],
]);
}
/**
* @Route("/view/{slug}", name="client_course_view", methods={"GET"})
*/
public function show(
CourseRepository $courseRepository,
\App\Service\SeoHelper $seoHelper,
$slug
): Response {
$course = $courseRepository->findOneBy(['slug' => $slug, 'isEnabled' => true]);
if (!$course) throw $this->createNotFoundException('The course does not exist');
$seoHelper->setCourseSeo($course);
return $this->render('client/courses/show.html.twig', [
'title' => $course->getTitle(),
'page' => [
'icon' => 'star',
'title' => $course->getTitle()
],
'entry' => $course,
]);
}
}