src/Controller/TrainingVideosController.php line 215

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Entity\TrainingVideoSection;
  9. use App\Entity\TrainingVideos;
  10. use App\Entity\TrainingVideoCategory;
  11. use App\Form\Filters\TestimonialFilter;
  12. use App\Form\CategoryForm;
  13. use App\Form\CategorySectionForm;
  14. use App\Form\TrainingVideoForm;
  15. #[Route('/settings/manage-training-videos')]
  16. class TrainingVideosController extends AbstractController
  17. {
  18.     #[Route('/'methods: ['GET','POST'], name'manage_training_video_category')]
  19.     public function index(Request $request,EntityManagerInterface $entityManager): Response
  20.     {
  21.         $category $entityManager->getRepository(\App\Entity\TrainingVideoCategory::class)->findAll();
  22.         return $this->render('manage-training-video/index.html.twig', [
  23.             'category' => $category,
  24.         ]);
  25.     }
  26.     #[Route('/add'name'add_category'methods: ['GET''POST'])]
  27.     public function add(Request $requestEntityManagerInterface $entityManager){
  28.         $trainingVideoCategory = new TrainingVideoCategory();
  29.         $form $this->createForm(CategoryForm::class, $trainingVideoCategory);
  30.         $form->handleRequest($request);
  31.         if ($form->isSubmitted() && $form->isValid()) {
  32.             $entityManager->persist($trainingVideoCategory);
  33.             $entityManager->flush();
  34.             $this->addFlash('success''Created successfully.');
  35.             return $this->redirectToRoute('manage_training_video_category', [], Response::HTTP_SEE_OTHER);
  36.         }
  37.         return $this->renderForm('manage-training-video/add.html.twig', [
  38.             'form' => $form,
  39.             'errors' => '',
  40.         ]);
  41.     }
  42.     #[Route('/manage-category/{id}'name'manage_category'methods: ['GET''POST'])]
  43.     public function manageCategory($idRequest $requestEntityManagerInterface $entityManagerTrainingVideoCategory $trainingVideoCategory){
  44.        
  45.         $trainingVideoSection $entityManager->getRepository(\App\Entity\TrainingVideoSection::class)->findBy(array('categoryId'=>$id));
  46.         return $this->renderForm('manage-training-video/manage-category.html.twig', [
  47.             'trainingVideoCategory' => $trainingVideoCategory,
  48.             'trainingVideoSection'=> $trainingVideoSection
  49.         ]);
  50.     }
  51.     #[Route('/{id}/edit'name'edit_category'methods: ['GET''POST'])]
  52.     /* #[Route('/edit/{id}', name: 'edit_category', methods: ['GET', 'POST'])] */
  53.     public function editCategory(Request $requestEntityManagerInterface $entityManagerTrainingVideoCategory $trainingVideoCategory): Response
  54.     {       
  55.         $form $this->createForm(CategoryForm::class, $trainingVideoCategory);
  56.         $form->handleRequest($request);
  57.         if($request->isMethod('POST')){
  58.             $params $request->request->all();
  59.            $data =  isset($params['category_form']['title']) ? $params['category_form']['title'] : '';
  60.             
  61.             $trainingVideoCategory->setTitle($data);
  62.             $entityManager->persist($trainingVideoCategory);
  63.             $entityManager->flush();
  64.             $this->addFlash('success''Update successfully.');
  65.             return $this->redirectToRoute('manage_training_video_category', [], Response::HTTP_SEE_OTHER);
  66.         }
  67.         return $this->renderForm('manage-training-video/_edit_form.html.twig', [
  68.             'form' => $form,
  69.             'trainingVideoCategory'=>$trainingVideoCategory
  70.         ]);
  71.     }
  72.     #[Route('/delete-category/{id}'name'delete_category'methods: ['POST','GET'])]
  73.     public function delete(Request $requestEntityManagerInterface $entityManager,  TrainingVideoCategory $trainingVideoCategory): Response
  74.     {
  75.         if ($this->isCsrfTokenValid('delete'.$trainingVideoCategory->getId(), $request->request->get('_token'))) {
  76.             
  77.             if($request->isMethod('POST')){
  78.                 $entityManager->remove($trainingVideoCategory);
  79.                 $entityManager->flush();
  80.                 $this->addFlash('error''Delete successfully.');
  81.                 return $this->redirectToRoute('manage_training_video_category', [], Response::HTTP_SEE_OTHER);
  82.             }
  83.         }
  84.         return  $this->renderForm('manage-training-video/_delete_form.html.twig', [
  85.             'trainingVideoCategory' => $trainingVideoCategory,
  86.         ]);
  87.     }
  88.     #[Route('/{catId}/add-section'name'add_category_section'methods: ['GET''POST'])]
  89.     public function addSection($catIdRequest $requestEntityManagerInterface $entityManager){
  90.         $trainingVideoSection = new TrainingVideoSection();
  91.         $form $this->createForm(CategorySectionForm::class, $trainingVideoSection);
  92.         $form->handleRequest($request);
  93.         
  94.             if ($form->isSubmitted() && $form->isValid()) {
  95.                 $trainingVideoSection->setCategoryId($catId);
  96.                 $trainingVideoSection->setOrderBy(999);
  97.                 $entityManager->persist($trainingVideoSection);
  98.                 $entityManager->flush();
  99.                 $this->addFlash('success''Created successfully.');
  100.                 return $this->redirectToRoute('manage_category', ['id'=>$catId], Response::HTTP_SEE_OTHER);
  101.             }
  102.         return $this->renderForm('manage-training-video/_section_form.html.twig', [
  103.             'form' => $form,
  104.             'cat_id'=>$catId,
  105.         ]);
  106.     }
  107.     #[Route('/edit-section/{id}'name'edit_section'methods: ['GET''POST'])]
  108.     public function editSection($id,Request $requestEntityManagerInterface $entityManager,TrainingVideoSection $trainingVideoSection){
  109.         
  110.         $categoryId =$trainingVideoSection->getCategoryId();
  111.         $form $this->createForm(CategorySectionForm::class, $trainingVideoSection);
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted() && $form->isValid()) {
  114.             $entityManager->persist($trainingVideoSection);
  115.             $entityManager->flush();
  116.             $this->addFlash('success''Update successfully.');
  117.             return $this->redirectToRoute('manage_category', ['id'=>$categoryId], Response::HTTP_SEE_OTHER);
  118.         }
  119.         return $this->renderForm('manage-training-video/_edit_section_form.html.twig', [
  120.             'form' => $form,
  121.             'sec_id'=>$id,
  122.         ]);
  123.     }
  124.     #[Route('delete-section/{id}'name:'delete_section'methods:['GET','POST'])]
  125.     public function deleteSection(Request $requestEntityManagerInterface $entityManager,TrainingVideoSection $trainingVideoSection){
  126.         $categoryId =$trainingVideoSection->getCategoryId();
  127.         if ($this->isCsrfTokenValid('delete'.$trainingVideoSection->getId(), $request->request->get('_token'))) {
  128.             if($request->isMethod('POST')){
  129.                 $entityManager->remove($trainingVideoSection);
  130.                 $entityManager->flush();
  131.                 $this->addFlash('error''Delete successfully.');
  132.                 return $this->redirectToRoute('manage_category', ['id' =>$categoryId], Response::HTTP_SEE_OTHER);       
  133.             }
  134.         }
  135.         return  $this->renderForm('manage-training-video/_delete_section_form.html.twig', [
  136.             'trainingVideoSection' => $trainingVideoSection,
  137.         ]);
  138.     }
  139.     #[Route('/{catId}/{secId}/add-video'name'add_video'methods: ['GET''POST'])]
  140.     public function addVideo($catId,$secIdRequest $requestEntityManagerInterface $entityManager){
  141.         $trainingVideos = new TrainingVideos();
  142.         $form $this->createForm(TrainingVideoForm::class, $trainingVideos);
  143.         $form->handleRequest($request);
  144.         if ($form->isSubmitted() && $form->isValid()) {
  145.             $minute $request->request->get('duration_minute');
  146.             $second $request->request->get('duration_second');
  147.             $duration $minute.':'.$second;
  148.             $trainingVideos->setCategoryId($catId);
  149.             $trainingVideos->setSectionId($secId);
  150.             $trainingVideos->setDuration($duration);
  151.             $trainingVideos->setOrderBy(999);
  152.             $entityManager->persist($trainingVideos);
  153.             $entityManager->flush();
  154.             $this->addFlash('success''Created successfully.');
  155.             return $this->redirectToRoute('manage_category', ['id'=>$catId ], Response::HTTP_SEE_OTHER);
  156.         }
  157.         return $this->renderForm('manage-training-video/_video_form.html.twig', [
  158.             'form' => $form,
  159.             'catId'=>$catId,
  160.             'secId'=>$secId,
  161.         ]);
  162.     }
  163.     #[Route('/edit-video/{id}'name'edit_video'methods: ['GET''POST'])]
  164.     public function editVideo($idRequest $requestEntityManagerInterface $entityManagerTrainingVideos $trainingVideos){
  165.         $catId $trainingVideos->getCategoryId();
  166.         $form $this->createForm(TrainingVideoForm::class, $trainingVideos);
  167.         $form->handleRequest($request);
  168.         if ($form->isSubmitted() && $form->isValid()) {
  169.             $entityManager->persist($trainingVideos);
  170.             $entityManager->flush();
  171.             $this->addFlash('success''Update successfully.');
  172.             return $this->redirectToRoute('manage_category', ['id'=>$catId ], Response::HTTP_SEE_OTHER);
  173.         }
  174.         return $this->renderForm('manage-training-video/_edit_video_form.html.twig', [
  175.             'form' => $form,
  176.             'id' => $id,
  177.         ]);
  178.     }
  179.     #[Route('/delete-video/{cat_id}/{sec_id}/{id}'name:'delete_video'methods:['GET''POST'])]
  180.     public function deleteVideo($id,$cat_id,$sec_idRequest $requestEntityManagerInterface $entityManagerTrainingVideos $trainingVideos){
  181.         $category $entityManager->getRepository(\App\Entity\TrainingVideoCategory::class)->findOneBy(array('id' => $cat_id));
  182.         if(!$category){
  183.            return new Response('No category found for id ' $cat_id);
  184.         }
  185.         
  186.         $section $entityManager->getRepository(\App\Entity\TrainingVideoSection::class)->findOneBy(array('id' => $sec_id'categoryId' => $cat_id));
  187.         if(!$section){
  188.            return new Response('No section found for id ' $sec_id);
  189.         }
  190.         
  191.         $video $entityManager->getRepository(\App\Entity\TrainingVideos::class)->findOneBy(array('categoryId' => $cat_id'sectionId' => $sec_id'id' => $id));
  192.         if(!$video){
  193.            return new Response('No video found for id ' $id);
  194.         }
  195.         if ($this->isCsrfTokenValid('delete'.$trainingVideos->getId(), $request->request->get('_token'))) {
  196.             
  197.             if($request->isMethod('POST')){
  198.                 $entityManager->remove($video);
  199.                 $entityManager->flush();
  200.                 $this->addFlash('error''Delete successfully.');
  201.                 return $this->redirectToRoute('manage_category', ['id' =>$cat_id], Response::HTTP_SEE_OTHER);
  202.             }
  203.         }
  204.         return  $this->renderForm('manage-training-video/_delete_video_form.html.twig', [
  205.                 'cat_id' => $cat_id,
  206.                 'sec_id' => $sec_id,
  207.                 'id' => $id
  208.         ]);
  209.     }
  210. }