<?php
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\TrainingVideoSection;
use App\Entity\TrainingVideos;
use App\Entity\TrainingVideoCategory;
use App\Form\Filters\TestimonialFilter;
use App\Form\CategoryForm;
use App\Form\CategorySectionForm;
use App\Form\TrainingVideoForm;
#[Route('/settings/manage-training-videos')]
class TrainingVideosController extends AbstractController
{
#[Route('/', methods: ['GET','POST'], name: 'manage_training_video_category')]
public function index(Request $request,EntityManagerInterface $entityManager): Response
{
$category = $entityManager->getRepository(\App\Entity\TrainingVideoCategory::class)->findAll();
return $this->render('manage-training-video/index.html.twig', [
'category' => $category,
]);
}
#[Route('/add', name: 'add_category', methods: ['GET', 'POST'])]
public function add(Request $request, EntityManagerInterface $entityManager){
$trainingVideoCategory = new TrainingVideoCategory();
$form = $this->createForm(CategoryForm::class, $trainingVideoCategory);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($trainingVideoCategory);
$entityManager->flush();
$this->addFlash('success', 'Created successfully.');
return $this->redirectToRoute('manage_training_video_category', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('manage-training-video/add.html.twig', [
'form' => $form,
'errors' => '',
]);
}
#[Route('/manage-category/{id}', name: 'manage_category', methods: ['GET', 'POST'])]
public function manageCategory($id, Request $request, EntityManagerInterface $entityManager, TrainingVideoCategory $trainingVideoCategory){
$trainingVideoSection = $entityManager->getRepository(\App\Entity\TrainingVideoSection::class)->findBy(array('categoryId'=>$id));
return $this->renderForm('manage-training-video/manage-category.html.twig', [
'trainingVideoCategory' => $trainingVideoCategory,
'trainingVideoSection'=> $trainingVideoSection
]);
}
#[Route('/{id}/edit', name: 'edit_category', methods: ['GET', 'POST'])]
/* #[Route('/edit/{id}', name: 'edit_category', methods: ['GET', 'POST'])] */
public function editCategory(Request $request, EntityManagerInterface $entityManager, TrainingVideoCategory $trainingVideoCategory): Response
{
$form = $this->createForm(CategoryForm::class, $trainingVideoCategory);
$form->handleRequest($request);
if($request->isMethod('POST')){
$params = $request->request->all();
$data = isset($params['category_form']['title']) ? $params['category_form']['title'] : '';
$trainingVideoCategory->setTitle($data);
$entityManager->persist($trainingVideoCategory);
$entityManager->flush();
$this->addFlash('success', 'Update successfully.');
return $this->redirectToRoute('manage_training_video_category', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('manage-training-video/_edit_form.html.twig', [
'form' => $form,
'trainingVideoCategory'=>$trainingVideoCategory
]);
}
#[Route('/delete-category/{id}', name: 'delete_category', methods: ['POST','GET'])]
public function delete(Request $request, EntityManagerInterface $entityManager, TrainingVideoCategory $trainingVideoCategory): Response
{
if ($this->isCsrfTokenValid('delete'.$trainingVideoCategory->getId(), $request->request->get('_token'))) {
if($request->isMethod('POST')){
$entityManager->remove($trainingVideoCategory);
$entityManager->flush();
$this->addFlash('error', 'Delete successfully.');
return $this->redirectToRoute('manage_training_video_category', [], Response::HTTP_SEE_OTHER);
}
}
return $this->renderForm('manage-training-video/_delete_form.html.twig', [
'trainingVideoCategory' => $trainingVideoCategory,
]);
}
#[Route('/{catId}/add-section', name: 'add_category_section', methods: ['GET', 'POST'])]
public function addSection($catId, Request $request, EntityManagerInterface $entityManager){
$trainingVideoSection = new TrainingVideoSection();
$form = $this->createForm(CategorySectionForm::class, $trainingVideoSection);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$trainingVideoSection->setCategoryId($catId);
$trainingVideoSection->setOrderBy(999);
$entityManager->persist($trainingVideoSection);
$entityManager->flush();
$this->addFlash('success', 'Created successfully.');
return $this->redirectToRoute('manage_category', ['id'=>$catId], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('manage-training-video/_section_form.html.twig', [
'form' => $form,
'cat_id'=>$catId,
]);
}
#[Route('/edit-section/{id}', name: 'edit_section', methods: ['GET', 'POST'])]
public function editSection($id,Request $request, EntityManagerInterface $entityManager,TrainingVideoSection $trainingVideoSection){
$categoryId =$trainingVideoSection->getCategoryId();
$form = $this->createForm(CategorySectionForm::class, $trainingVideoSection);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($trainingVideoSection);
$entityManager->flush();
$this->addFlash('success', 'Update successfully.');
return $this->redirectToRoute('manage_category', ['id'=>$categoryId], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('manage-training-video/_edit_section_form.html.twig', [
'form' => $form,
'sec_id'=>$id,
]);
}
#[Route('delete-section/{id}', name:'delete_section', methods:['GET','POST'])]
public function deleteSection(Request $request, EntityManagerInterface $entityManager,TrainingVideoSection $trainingVideoSection){
$categoryId =$trainingVideoSection->getCategoryId();
if ($this->isCsrfTokenValid('delete'.$trainingVideoSection->getId(), $request->request->get('_token'))) {
if($request->isMethod('POST')){
$entityManager->remove($trainingVideoSection);
$entityManager->flush();
$this->addFlash('error', 'Delete successfully.');
return $this->redirectToRoute('manage_category', ['id' =>$categoryId], Response::HTTP_SEE_OTHER);
}
}
return $this->renderForm('manage-training-video/_delete_section_form.html.twig', [
'trainingVideoSection' => $trainingVideoSection,
]);
}
#[Route('/{catId}/{secId}/add-video', name: 'add_video', methods: ['GET', 'POST'])]
public function addVideo($catId,$secId, Request $request, EntityManagerInterface $entityManager){
$trainingVideos = new TrainingVideos();
$form = $this->createForm(TrainingVideoForm::class, $trainingVideos);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$minute = $request->request->get('duration_minute');
$second = $request->request->get('duration_second');
$duration = $minute.':'.$second;
$trainingVideos->setCategoryId($catId);
$trainingVideos->setSectionId($secId);
$trainingVideos->setDuration($duration);
$trainingVideos->setOrderBy(999);
$entityManager->persist($trainingVideos);
$entityManager->flush();
$this->addFlash('success', 'Created successfully.');
return $this->redirectToRoute('manage_category', ['id'=>$catId ], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('manage-training-video/_video_form.html.twig', [
'form' => $form,
'catId'=>$catId,
'secId'=>$secId,
]);
}
#[Route('/edit-video/{id}', name: 'edit_video', methods: ['GET', 'POST'])]
public function editVideo($id, Request $request, EntityManagerInterface $entityManager, TrainingVideos $trainingVideos){
$catId = $trainingVideos->getCategoryId();
$form = $this->createForm(TrainingVideoForm::class, $trainingVideos);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($trainingVideos);
$entityManager->flush();
$this->addFlash('success', 'Update successfully.');
return $this->redirectToRoute('manage_category', ['id'=>$catId ], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('manage-training-video/_edit_video_form.html.twig', [
'form' => $form,
'id' => $id,
]);
}
#[Route('/delete-video/{cat_id}/{sec_id}/{id}', name:'delete_video', methods:['GET', 'POST'])]
public function deleteVideo($id,$cat_id,$sec_id, Request $request, EntityManagerInterface $entityManager, TrainingVideos $trainingVideos){
$category = $entityManager->getRepository(\App\Entity\TrainingVideoCategory::class)->findOneBy(array('id' => $cat_id));
if(!$category){
return new Response('No category found for id ' . $cat_id);
}
$section = $entityManager->getRepository(\App\Entity\TrainingVideoSection::class)->findOneBy(array('id' => $sec_id, 'categoryId' => $cat_id));
if(!$section){
return new Response('No section found for id ' . $sec_id);
}
$video = $entityManager->getRepository(\App\Entity\TrainingVideos::class)->findOneBy(array('categoryId' => $cat_id, 'sectionId' => $sec_id, 'id' => $id));
if(!$video){
return new Response('No video found for id ' . $id);
}
if ($this->isCsrfTokenValid('delete'.$trainingVideos->getId(), $request->request->get('_token'))) {
if($request->isMethod('POST')){
$entityManager->remove($video);
$entityManager->flush();
$this->addFlash('error', 'Delete successfully.');
return $this->redirectToRoute('manage_category', ['id' =>$cat_id], Response::HTTP_SEE_OTHER);
}
}
return $this->renderForm('manage-training-video/_delete_video_form.html.twig', [
'cat_id' => $cat_id,
'sec_id' => $sec_id,
'id' => $id
]);
}
}