src/Controller/CourseController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Course;
  4. use App\Repository\CourseRepository;
  5. use App\Repository\CourseCategoryRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Doctrine\Common\Collections\Criteria;
  11. /**
  12. * @Route("/courses")
  13. */
  14. class CourseController extends AbstractController {
  15.     /**
  16.      * Course List.
  17.      * @Route(
  18.      *      "/list/{page}/{sort}/{dir}",
  19.      *      name="client_course_list",
  20.      *      methods={"GET","POST"},
  21.      *      requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
  22.      *      defaults={"page"=1,"sort"="createdAt","dir"="desc"}
  23.      *  )
  24.      *
  25.      * @return string
  26.      */
  27.     public function list(
  28.         Request $request$page$sort$dir,
  29.         \App\Service\SeoHelper $seoHelper,
  30.         CourseRepository $courseRepository
  31.     ): Response {
  32.         $limit $request->query->getInt('limit'20);
  33.         $seoHelper->setInternalSeo('Courses''client_course_list');
  34.         $criteria Criteria::create();
  35.         $criteria->andWhere(Criteria::expr()->eq('isEnabled'true));
  36.         $criteria->orderBy([$sort => $dir]);
  37.         $entries $courseRepository->matching($criteria);
  38.         $entries $entries->slice(($page-1)*$limit$limit);
  39.         $total $courseRepository->countAll();
  40.         $pages ceil($total/$limit);
  41.         return $this->render('client/courses/list.html.twig', [
  42.             'title' => 'Courses',
  43.             'listParams' => [],
  44.             'entries' => $entries,
  45.             'ordering' => [
  46.                 'sort' => $sort,
  47.                 'dir' => $dir
  48.             ],
  49.             'meta' => [
  50.                 'title' => 'Courses',
  51.                 'type' => 'website',
  52.             ],
  53.             'page' => [
  54.                 'icon' => 'list',
  55.                 'title' => 'Courses',
  56.                 'subtitle' => 'View the list with the Courses',
  57.                 'header' => 'Training Manager - Courses List'
  58.             ],
  59.             'pagination' => [
  60.                 'listURL' => 'client_course_list',
  61.                 'totalSearch' => $total,
  62.                 'total' => $total,
  63.                 'limit' => $limit,
  64.                 'totalPages' => $pages,
  65.                 'page' => $page,
  66.             ],
  67.         ]);
  68.     }
  69.     /**
  70.      * Course Category List.
  71.      * @Route(
  72.      *      "/category/{slug}/{page}/{sort}/{dir}",
  73.      *      name="client_course_category_list",
  74.      *      methods={"GET","POST"},
  75.      *      requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
  76.      *      defaults={"page"=1,"sort"="createdAt","dir"="desc"}
  77.      *  )
  78.      *
  79.      * @return string
  80.      */
  81.     public function category(
  82.         Request $request$page$sort$dir$slug,
  83.         \App\Service\SeoHelper $seoHelper,
  84.         CourseCategoryRepository $courseCategoryRepository
  85.     ): Response {
  86.         $limit $request->query->getInt('limit'20);
  87.         $category $courseCategoryRepository->findOneBy(["slug" => $slug"isEnabled" => true], ["createdAt" => "Desc"]);
  88.         if (!$category) throw $this->createNotFoundException('The course category does not exist');
  89.         $seoHelper->setCourseCategorySeo($category);
  90.         $entries $category->getCourseOrdering();
  91.         $total $entries->count();
  92.         $entries $entries->slice(($page-1)*$limit$limit);
  93.         $pages ceil($total/$limit);
  94.         return $this->render('client/courses/category.html.twig', [
  95.             'title' => $category->getTitle(),
  96.             'listParams' => [],
  97.             'entries' => $entries,
  98.             'showContent' => $category->getShowContent(),
  99.             'content' => $category->getContent(),
  100.             'ordering' => [
  101.                 'sort' => $sort,
  102.                 'dir' => $dir
  103.             ],
  104.             'page' => [
  105.                 'icon' => 'list',
  106.                 'title' => 'Courses',
  107.                 'subtitle' => 'View the list with the Courses',
  108.                 'header' => 'Training Manager - Courses List',
  109.             ],
  110.             'meta' => [
  111.                 'title' => $category->getTitle(),
  112.                 'type' => 'website',
  113.                 'description' => $category->getMetaDescription(),
  114.                 'keywords' => $category->getMetaKeywords()
  115.             ],
  116.             'pagination' => [
  117.                 'listURL' => 'client_course_list',
  118.                 'totalSearch' => $total,
  119.                 'total' => $total,
  120.                 'limit' => $limit,
  121.                 'totalPages' => $pages,
  122.                 'page' => $page,
  123.             ],
  124.         ]);
  125.     }
  126.     /**
  127.      * @Route("/view/{slug}", name="client_course_view", methods={"GET"})
  128.      */
  129.     public function show(
  130.         CourseRepository $courseRepository,
  131.         \App\Service\SeoHelper $seoHelper,
  132.         $slug
  133.     ): Response {
  134.         $course $courseRepository->findOneBy(['slug' => $slug'isEnabled' => true]);
  135.         if (!$course) throw $this->createNotFoundException('The course does not exist');
  136.         $seoHelper->setCourseSeo($course);
  137.         return $this->render('client/courses/show.html.twig', [
  138.             'title' => $course->getTitle(),
  139.             'page' => [
  140.                 'icon' => 'star',
  141.                 'title' => $course->getTitle()
  142.             ],
  143.             'entry' => $course,
  144.         ]);
  145.     }
  146. }