Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

100 lines
3.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\CreditHistoryInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SfAbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. abstract class AbstractController extends SfAbstractController
  9. {
  10. /**
  11. * @var \Doctrine\Persistence\ObjectManager
  12. * Entity manager
  13. */
  14. protected $em;
  15. public $class;
  16. public $type;
  17. public $template;
  18. public $routePrefix;
  19. public $repos;
  20. public function __construct(EntityManagerInterface $entityManager)
  21. {
  22. $this->em = $entityManager;
  23. $metadata = $this->em->getClassMetadata($this->class);
  24. $this->class = $metadata->getName();
  25. $this->repo = $this->em->getRepository($this->class);
  26. }
  27. public function indexAction(): Response
  28. {
  29. $repo = $this->em->getRepository($this->class);
  30. return $this->render('LcShopBundle:' . $this->template . ':index.html.twig', [
  31. 'entities' => $repo->findAll(),
  32. 'routePrefix'=> $this->routePrefix
  33. ]);
  34. }
  35. public function showAction($id): Response
  36. {
  37. $enity = $this->repo->find($id);
  38. return $this->render('LcShopBundle:'.$this->template.':show.html.twig', [
  39. 'entity' => $enity,
  40. 'routePrefix'=> $this->routePrefix
  41. ]);
  42. }
  43. public function editAction(Request $request, $id): Response
  44. {
  45. if ($id == 'new') {
  46. $entity = new $this->class;
  47. } else {
  48. $repo = $this->em->getRepository($this->class);
  49. $entity = $repo->find($id);
  50. }
  51. $form = $this->createForm($this->type, $entity);
  52. $form->handleRequest($request);
  53. if ($form->isSubmitted() && $form->isValid()) {
  54. $this->em->persist($entity);
  55. $this->getDoctrine()->getManager()->flush();
  56. return $this->redirectToRoute($this->routePrefix . '_index');
  57. }
  58. return $this->render('LcShopBundle:' . $this->template . ':edit.html.twig', [
  59. 'entity' => $entity,
  60. 'form' => $form->createView(),
  61. 'routePrefix'=> $this->routePrefix
  62. ]);
  63. }
  64. public function deleteAction(Request $request, $id): Response
  65. {
  66. $enity = $this->repo->find($id);
  67. if ($this->isCsrfTokenValid('delete'.$enity->getId(), $request->request->get('_token'))) {
  68. $entityManager = $this->getDoctrine()->getManager();
  69. $entityManager->remove($enity);
  70. $entityManager->flush();
  71. }
  72. return $this->redirectToRoute($this->routePrefix.'_index');
  73. }
  74. }