src/Controller/admin/CoachController.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Controller\admin;
  11. use App\Form\Type\ChangePasswordType;
  12. use App\Form\UserType;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Knp\Component\Pager\PaginatorInterface;
  22. use App\Entity\Category;
  23. use App\Entity\User;
  24. /**
  25.  * Controller used to manage current user.
  26.  *
  27.  * @author Romain Monteil <monteil.romain@gmail.com>
  28.  */
  29. #[Route('/coach')]
  30. class CoachController extends AbstractController {
  31.     #[Route('/add-account'methods: ['GET''POST'], name'add_coach')]
  32.     public function addAccountByAdmin(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response {
  33. //        $user = $this->getUser();
  34.         $em $doctrine->getManager();
  35.         $categories $em->getRepository(Category::class)->findAll();
  36.         if ($request->isMethod('post')) {
  37.             $coach = new User();
  38.             $coach->setFirstname($request->get('firstname'));
  39.             $coach->setLastname($request->get('lastname'));
  40.             $coach->setBio($request->get('bio'));
  41.             $coach->setPhone($request->get('phone'));
  42.             $coach->setEmail($request->get('email'));
  43.             $coach->setUsername($request->get('email'));
  44.             $coach->setAddress($request->get('address'));
  45.             $coach->setCountry($request->get('country'));
  46.             $coach->setPublished(1);
  47.             $coach->setPassword($passwordHasher->hashPassword($coach'123456789'));
  48.             $em->persist($coach);
  49.             $em->flush();
  50.             if ($request->files->get('image')) {
  51.                 $file $request->files->get('image');
  52.                 $coach->setImage("uploads/coaches/coach-" $coach->getId() . "/" $file->getClientOriginalName());
  53.                 $file->move("uploads/coaches/coach-" $coach->getId() . "/"$file->getClientOriginalName());
  54.             }
  55.             $em->persist($coach);
  56.             $em->flush();
  57.             $this->addFlash('success''Coach created successfully');
  58.             return $this->redirectToRoute('coaches_list');
  59.         }
  60.         return $this->render('admin/coach/addCoach.html.twig', [
  61.                     'categories' => $categories,
  62.         ]);
  63.     }
  64.     #[Route('/edit-account-{id}'methods: ['GET''POST'], name'edit_coach')]
  65.     public function editAccountByAdmin(Request $request$idManagerRegistry $doctrine) {
  66. //        $user = $this->getUser();
  67.         $em $doctrine->getManager();
  68.         $coach $em->getRepository(User::class)->find($id);
  69.         $categories $em->getRepository(Category::class)->findAll();
  70.         if ($request->isMethod('post')) {
  71.             $coach->setFirstname($request->get('firstname'));
  72.             $coach->setLastname($request->get('lastname'));
  73.             $coach->setBio($request->get('bio'));
  74.             $coach->setPhone($request->get('phone'));
  75.             $coach->setGender($request->get('gender'));
  76. //            $coach->setEmail($request->get('email'));
  77. //            $coach->setUsername($request->get('email'));
  78.             $coach->setAddress($request->get('address'));
  79.             $coach->setCountry($request->get('country'));
  80.             $coach->setPublished(1);
  81. //            $coach->setPassword($passwordHasher->hashPassword($coach, '123456789'));
  82.             if ($request->files->get('image')) {
  83.                 $file $request->files->get('image');
  84.                 $coach->setImage("uploads/coaches/coach-" $coach->getId() . "/" $file->getClientOriginalName());
  85.                 $file->move("uploads/coaches/coach-" $coach->getId() . "/"$file->getClientOriginalName());
  86.             }
  87.             $em->persist($coach);
  88.             $em->flush();
  89.             $this->addFlash('success''Coach edited successfully');
  90.             return $this->redirectToRoute('coaches_list');
  91.         }
  92.         return $this->render('admin/coach/editCoach.html.twig', [
  93.                     'categories' => $categories,
  94.                     'coach' => $coach,
  95.         ]);
  96.     }
  97.     #[Route('/coaches-list'methods: ['GET'], name'coaches_list')]
  98.     public function listCoaches(Request $requestManagerRegistry $doctrine) {
  99.         $em $doctrine->getManager();
  100.         $coaches $em->getRepository(User::class)->findAll();
  101.         return $this->render('admin/coach/listCoaches.html.twig', [
  102.                     'coaches' => $coaches,
  103.         ]);
  104.     }
  105. }