/
ActorController.php
108 lines (92 loc) · 3.96 KB
/
ActorController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Doctrine\Common\Collections\ArrayCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Actor;
class ActorController extends Controller
{
/**
* @Route("/actors/{page}", name="actors_list", defaults={"page" = 1}, options={"expose"=true})
* @Template("actor/list.html.twig")
* @param Request $request
* @param integer $page
* @return mixed
*/
public function listAction(Request $request, $page)
{
if ($page < 1) {
$page = 1;
}
$motcle = $request->query->get('motcle');
$limit = $this->getParameter('app.max_actors_per_page');
$nbFilteredActors = $this->getActorManager()->getResultFilterCount($motcle);
$data = [
'actors' => $this->getActorManager()->getResultFilterPaginated($motcle, $limit, ($page - 1) * $limit),
'displayActorsFound' => true,
'pagination' => $this->getActorManager()->getPagination($request->query->all(), $page, 'actors_list', $limit, $nbFilteredActors),
'nbFilteredActors' => $nbFilteredActors
];
if ($request->isXmlHttpRequest()) {
return $this->get('templating')->renderResponse('actor/partials/actors.html.twig', $data);
}
return array_merge($data, ['form' => $this->getActorManager()->getActorSearchForm()->createView()]);
}
/**
* @Template("actor/partials/actors.html.twig", vars={"actors", "displayActorsFound"})
* @ParamConverter("actors", converter="project_collection_converter", options={"manager":"app.actor.manager", "orderBy":"birthday"})
*/
public function topAction(ArrayCollection $actors, $max = 5, $displayActorsFound = false)
{
}
/**
* @Route("/admin/actors/new", name="actor_new")
* @Route("/admin/actors/{id}/edit", name="actor_edit")
* @param Request $request
* @param Actor|null $actor
* @return array|RedirectResponse
* @Template("actor/edit.html.twig")
* @Security("has_role('ROLE_EDITOR')")
*/
public function newEditAction(Request $request, Actor $actor = null)
{
$entityToProcess = $this->getActorFormHandler()->processForm($actor);
if ($this->getActorFormHandler()->handleForm($this->getActorFormHandler()->getForm(), $entityToProcess, $request)) {
// we add flash messages to stick with context (new or edited object)
$this->addFlash('success', $this->getActorFormHandler()->getMessage());
return $this->redirectToRoute('actor_edit', array('id' => $entityToProcess->getId()));
}
return [
'form' => $this->getActorFormHandler()->createView(),
'actor' => $entityToProcess,
];
}
/**
* @Route("/admin/actors/{id}/delete", name="actor_delete")
* @param Actor $actor
* @ParamConverter("actor", class="AppBundle:Actor")
* @return RedirectResponse
*/
public function deleteAction(Actor $actor)
{
if (!$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
throw $this->createAccessDeniedException('You cannot access this page!');
}
$this->getActorManager()->remove($actor);
$this->addFlash('success', $this->get('translator')->trans('acteur.supprime', ['%actor%' => $actor]));
return new RedirectResponse($this->get('router')->generate('actors_list'));
}
public function getActorFormHandler()
{
return $this->get('app.actor.form.handler');
}
public function getActorManager()
{
return $this->get('app.actor.manager');
}
}