<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller\admin;
use App\Form\Type\ChangePasswordType;
use App\Form\UserType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
use App\Entity\Category;
use App\Entity\User;
/**
* Controller used to manage current user.
*
* @author Romain Monteil <monteil.romain@gmail.com>
*/
#[Route('/coach')]
class CoachController extends AbstractController {
#[Route('/add-account', methods: ['GET', 'POST'], name: 'add_coach')]
public function addAccountByAdmin(Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response {
// $user = $this->getUser();
$em = $doctrine->getManager();
$categories = $em->getRepository(Category::class)->findAll();
if ($request->isMethod('post')) {
$coach = new User();
$coach->setFirstname($request->get('firstname'));
$coach->setLastname($request->get('lastname'));
$coach->setBio($request->get('bio'));
$coach->setPhone($request->get('phone'));
$coach->setEmail($request->get('email'));
$coach->setUsername($request->get('email'));
$coach->setAddress($request->get('address'));
$coach->setCountry($request->get('country'));
$coach->setPublished(1);
$coach->setPassword($passwordHasher->hashPassword($coach, '123456789'));
$em->persist($coach);
$em->flush();
if ($request->files->get('image')) {
$file = $request->files->get('image');
$coach->setImage("uploads/coaches/coach-" . $coach->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/coaches/coach-" . $coach->getId() . "/", $file->getClientOriginalName());
}
$em->persist($coach);
$em->flush();
$this->addFlash('success', 'Coach created successfully');
return $this->redirectToRoute('coaches_list');
}
return $this->render('admin/coach/addCoach.html.twig', [
'categories' => $categories,
]);
}
#[Route('/edit-account-{id}', methods: ['GET', 'POST'], name: 'edit_coach')]
public function editAccountByAdmin(Request $request, $id, ManagerRegistry $doctrine) {
// $user = $this->getUser();
$em = $doctrine->getManager();
$coach = $em->getRepository(User::class)->find($id);
$categories = $em->getRepository(Category::class)->findAll();
if ($request->isMethod('post')) {
$coach->setFirstname($request->get('firstname'));
$coach->setLastname($request->get('lastname'));
$coach->setBio($request->get('bio'));
$coach->setPhone($request->get('phone'));
$coach->setGender($request->get('gender'));
// $coach->setEmail($request->get('email'));
// $coach->setUsername($request->get('email'));
$coach->setAddress($request->get('address'));
$coach->setCountry($request->get('country'));
$coach->setPublished(1);
// $coach->setPassword($passwordHasher->hashPassword($coach, '123456789'));
if ($request->files->get('image')) {
$file = $request->files->get('image');
$coach->setImage("uploads/coaches/coach-" . $coach->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/coaches/coach-" . $coach->getId() . "/", $file->getClientOriginalName());
}
$em->persist($coach);
$em->flush();
$this->addFlash('success', 'Coach edited successfully');
return $this->redirectToRoute('coaches_list');
}
return $this->render('admin/coach/editCoach.html.twig', [
'categories' => $categories,
'coach' => $coach,
]);
}
#[Route('/coaches-list', methods: ['GET'], name: 'coaches_list')]
public function listCoaches(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$coaches = $em->getRepository(User::class)->findAll();
return $this->render('admin/coach/listCoaches.html.twig', [
'coaches' => $coaches,
]);
}
}