src/Controller/UserController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Knp\Snappy\Pdf;
  5. use App\Form\UserFormType;
  6. use App\Repository\UserRepository;
  7. use App\Form\Type\RegistrationType;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use App\Service\SchoolYearService;
  17. use App\Repository\AttributionRepository;
  18. use App\Repository\MainTeacherRepository;
  19. /**
  20. * User controller.
  21. *
  22. * @Route("/admin/users")
  23. */
  24. class UserController extends AbstractController
  25. {
  26. private $em;
  27. private SchoolYearService $schoolYearService;
  28. private AttributionRepository $attRepo;
  29. private MainTeacherRepository $mainTeacherRepo;
  30. private UserRepository $repo;
  31. public function __construct( UserRepository $repo,MainTeacherRepository $mainTeacherRepo,AttributionRepository $attRepo,SchoolYearService $schoolYearService,EntityManagerInterface $em)
  32. {
  33. $this->em = $em;
  34. $this->schoolYearService = $schoolYearService;
  35. $this->attRepo = $attRepo;
  36. $this->mainTeacherRepo = $mainTeacherRepo;
  37. $this->repo = $repo;
  38. }
  39. /**
  40. * Lists all Programme entities.
  41. *
  42. * @Route("/", name="admin_users")
  43. * @Method("GET")
  44. * @Template()
  45. */
  46. public function indexAction()
  47. {
  48. $users = $this->repo->findAll();
  49. return $this->render('user/list.html.twig', compact("users"));
  50. }
  51. /**
  52. * Lists all Programme entities.
  53. *
  54. * @Route("/print/", name="admin_teacher_list")
  55. * @Method("GET")
  56. * @Template()
  57. */
  58. public function listAction(Pdf $pdf)
  59. {
  60. $year = $this->schoolYearService->sessionYearById();
  61. $users = $this->repo->findAllOfCurrentYear($year);
  62. $html = $this->renderView('user/teachers.html.twig', array(
  63. 'year' => $year,
  64. 'users' => $users,
  65. ));
  66. return new Response(
  67. $pdf->getOutputFromHtml($html),
  68. 200,
  69. array(
  70. 'Content-Type' => 'application/pdf',
  71. 'Content-Disposition' => 'inline; filename="teacher_list.pdf"'
  72. )
  73. );
  74. }
  75. /**
  76. * @Route("/create",name="admin_users_new", methods={"GET","POST"})
  77. */
  78. public function create(Request $request): Response
  79. {
  80. $user = new User();
  81. $form = $this->createForm(UserType::class, $user);
  82. $form->handleRequest($request);
  83. if ($form->isSubmitted() && $form->isValid()) {
  84. $this->em->persist($user);
  85. $this->em->flush();
  86. $this->addFlash('success', 'User succesfully created');
  87. return $this->redirectToRoute('admin_users');
  88. }
  89. return $this->render(
  90. 'user/new.html.twig',
  91. ['form' => $form->createView()]
  92. );
  93. }
  94. /**
  95. * Finds and displays a User entity.
  96. *
  97. * @Route("/{id}/show", name="app_users_show", requirements={"id"="\d+"})
  98. * @Method("GET")
  99. * @Template()
  100. */
  101. public function appShowAction(User $user)
  102. {
  103. return $this->render('user/app_show.html.twig', compact("user"));
  104. }
  105. /**
  106. * Finds and displays a User entity.
  107. *
  108. * @Route("/{id}/show", name="admin_users_show", requirements={"id"="\d+"})
  109. * @Method("GET")
  110. * @Template()
  111. */
  112. public function showAction(User $user)
  113. {
  114. $mainTeacher = $this->mainTeacherRepo->findOneBy(array("teacher"=> $user, "schoolYear"=> $this->schoolYearService->sessionYearById()));
  115. return $this->render('account/show.html.twig', compact("user", "mainTeacher"));
  116. }
  117. /**
  118. * Creates a new User entity.
  119. *
  120. * @Route("/create", name="admin_users_create")
  121. * @Method("POST")
  122. * @Template("AppBundle:User:new.html.twig")
  123. */
  124. public function createAction(Request $request)
  125. {
  126. $user = new User();
  127. $form = $this->createForm(new UserType(), $user);
  128. if ($form->handleRequest($request)->isValid()) {
  129. $em = $this->getDoctrine()->getManager();
  130. $user->setAvatar($_POST['avatar']);
  131. $em->persist($user);
  132. $em->flush();
  133. return $this->redirect($this->generateUrl('admin_users_show', array('id' => $user->getId())));
  134. }
  135. return array(
  136. 'user' => $user,
  137. 'form' => $form->createView(),
  138. );
  139. }
  140. /**
  141. * Displays a form to an existing User entity.
  142. *
  143. * @Route("/{id}/pdf", name="admin_users_pdf", requirements={"id"="\d+"})
  144. * @Method("GET")
  145. * @Template()
  146. */
  147. public function presentAction(User $user)
  148. {
  149. $em = $this->getDoctrine()->getManager();
  150. $year = $em->getRepository('AppBundle:SchoolYear')->findOneBy(array("activated" => true));
  151. return $this->render('user/present.html.twig', array(
  152. 'user' => $user,
  153. 'year' => $year,
  154. ));
  155. }
  156. /**
  157. * Displays a form to edit an existing Programme entity.
  158. *
  159. * @Route("/{id}/edit", name="admin_users_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  160. * @Template()
  161. */
  162. public function edit(Request $request, User $user): Response
  163. {
  164. $form = $this->createForm(UserFormType::class, $user, [
  165. 'method' => 'PUT'
  166. ]);
  167. $form->handleRequest($request);
  168. if ($form->isSubmitted() && $form->isValid()) {
  169. $this->em->flush();
  170. $this->addFlash('success', 'User succesfully updated');
  171. return $this->redirectToRoute('admin_users');
  172. }
  173. return $this->render('user/edit.html.twig', [
  174. 'user' => $user,
  175. 'form' => $form->createView()
  176. ]);
  177. }
  178. /**
  179. * Displays a form to edit an existing Programme entity.
  180. *
  181. * @Route("/{id}/toggle", name="admin_users_toggle", requirements={"id"="\d+"}, methods={"GET","PUT"})
  182. * @Template()
  183. */
  184. public function toggleIsVerified(Request $request, User $user)
  185. {
  186. $user->toggleIsVerified();
  187. $entityManager = $this->getDoctrine()->getManager();
  188. $entityManager->persist($user);
  189. $entityManager->flush();
  190. // dd($user);
  191. return $this->redirectToRoute('admin_users');
  192. }
  193. /**
  194. * Deletes a Programme entity.
  195. *
  196. * @Route("/{id}/delete", name="admin_users_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  197. */
  198. public function delete(User $user, Request $request): Response
  199. {
  200. if ($this->isCsrfTokenValid('users_deletion' . $user->getId(), $request->request->get('crsf_token'))) {
  201. $this->em->remove($user);
  202. $this->em->flush();
  203. $this->addFlash('info', 'User succesfully deleted');
  204. }
  205. return $this->redirectToRoute('admin_users');
  206. }
  207. }