<?php
namespace App\Controller;
use App\Entity\CareerPath;
use App\Repository\CareerPathRepository;
use App\Repository\CareerPathCategoryRepository;
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("/career-paths")
*/
class CareerPathController extends AbstractController {
/**
* CareerPath List.
* @Route(
* "/list/{page}/{sort}/{dir}",
* name="client_career_path_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,
CareerPathRepository $careerPathRepository
): Response {
$limit = $request->query->getInt('limit', 20);
$seoHelper->setInternalSeo('CareerPaths', 'client_career_path_list');
$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->eq('isEnabled', true));
$criteria->orderBy([$sort => $dir]);
//$totalCriteria->andWhere(Criteria::expr()->eq('isDeceased', false));
$entries = $careerPathRepository->matching($criteria);
$entries = $entries->slice(($page-1)*$limit, $limit);
$total = $careerPathRepository->countAll();
$pages = ceil($total/$limit);
return $this->render('client/career-paths/list.html.twig', [
'title' => 'Career Paths',
'listParams' => [],
'entries' => $entries,
'ordering' => [
'sort' => $sort,
'dir' => $dir
],
'meta' => [
'title' => 'CareerPaths',
'type' => 'website',
],
'page' => [
'icon' => 'list',
'title' => 'CareerPaths',
'subtitle' => 'View the list with the CareerPaths',
'header' => 'Training Manager - CareerPaths List'
],
'pagination' => [
'listURL' => 'client_career_path_list',
'totalSearch' => $total,
'total' => $total,
'limit' => $limit,
'totalPages' => $pages,
'page' => $page,
],
]);
}
/**
* CareerPath Category List.
* @Route(
* "/category/{slug}/{page}/{sort}/{dir}",
* name="client_career_path_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,
CareerPathCategoryRepository $careerPathCategoryRepository
): Response {
$limit = $request->query->getInt('limit', 20);
$category = $careerPathCategoryRepository->findOneBy(["slug" => $slug, "isEnabled" => true], ["createdAt" => "Desc"]);
if (!$category) throw $this->createNotFoundException('The career path category does not exist');
$seoHelper->setCareerPathCategorySeo($category);
$entries = $category->getCareerPathOrdering();
$total = $entries->count();
$entries = $entries->slice(($page-1)*$limit, $limit);
$pages = ceil($total/$limit);
return $this->render('client/career-paths/category.html.twig', [
'title' => $category->getTitle(),
'listParams' => [],
'entries' => $entries,
'ordering' => [
'sort' => $sort,
'dir' => $dir
],
'page' => [
'icon' => 'list',
'title' => 'Career Paths',
'subtitle' => 'View the list with the Career Paths',
'header' => 'Training Manager - Career Paths List',
],
'meta' => [
'title' => $category->getTitle(),
'type' => 'website',
'description' => $category->getMetaDescription(),
'keywords' => $category->getMetaKeywords()
],
'pagination' => [
'listURL' => 'client_career_path_list',
'totalSearch' => $total,
'total' => $total,
'limit' => $limit,
'totalPages' => $pages,
'page' => $page,
],
]);
}
/**
* @Route("assignCareerPath", name="client_assign_career_path", methods={"POST"})
*/
public function assignCareerPath(
Request $request,
\App\Repository\CareerPathRepository $careerPathRepository,
\Symfony\Contracts\Translation\TranslatorInterface $translator,
\Doctrine\Persistence\ManagerRegistry $managerRegistry
): Response {
$returnURL = null;
$careerPath = $careerPathRepository->find($request->request->get('careerPath'));
if ($careerPath && $this->isCsrfTokenValid('addcareerpath', $request->request->get('_csrf_token'))) {
$entityManager = $managerRegistry->getManager();
$userCareerPath = new \App\Entity\UserCareerPath();
$userCareerPath->setUser($this->getUser());
$userCareerPath->setCareerPath($careerPath);
$entityManager->persist($userCareerPath);
$entityManager->flush();
$this->addFlash(
'success',
$translator->trans('Career Path was added to your Paths successfully!')
);
} else {
$this->addFlash(
'danger',
$translator->trans('Invalid Token!')
);
}
return $this->redirectToRoute('client_career_path_view', ['slug' => $careerPath->getSlug()]);
}
/**
* @Route("/view/{slug}", name="client_career_path_view", methods={"GET"})
*/
public function show(
CareerPathRepository $careerPathRepository,
\App\Repository\UserCareerPathRepository $userCareerPathRepository,
\App\Service\SeoHelper $seoHelper,
$slug
): Response {
$careerPath = $careerPathRepository->findOneBy(['slug' => $slug, 'isEnabled' => true]);
if (!$careerPath) throw $this->createNotFoundException('The career path not exist');
$courses = $careerPath->getCoursesOrdering();
$userActive = $userCareerPathRepository->findOneBy(['user' => $this->getUser(), 'careerPath' => $careerPath]);
$seoHelper->setCareerPathSeo($careerPath);
return $this->render('client/career-paths/show.html.twig', [
'title' => $careerPath->getTitle(),
'page' => [
'icon' => 'star',
'title' => $careerPath->getTitle()
],
'userActive' => $userActive?true:false,
'entry' => $careerPath,
'courses' => $courses
]);
}
}