-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSequenceController.php
More file actions
109 lines (96 loc) · 3.8 KB
/
SequenceController.php
File metadata and controls
109 lines (96 loc) · 3.8 KB
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
109
<?php
namespace UJM\ExoBundle\Controller\Sequence;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as EXT;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use UJM\ExoBundle\Entity\Sequence\Sequence;
/**
* Description of SequenceController
*
*/
class SequenceController extends Controller {
/**
* display a sequence
* @Route("/get/{id}", requirements={"id" = "\d+"}, name="ujm_sequence_open")
* @Method("GET")
* @ParamConverter("Sequence", class="UJMExoBundle:Sequence\Sequence")
*/
public function openAction(Sequence $resource) {
if (false === $this->container->get('security.context')->isGranted('OPEN', $resource->getResourceNode())) {
throw new AccessDeniedException();
}
return $this->render('UJMExoBundle:Sequence:view.html.twig', array(
'_resource' => $resource
)
);
}
/**
* administrate an exercise player
* @Route("/edit/{id}", requirements={"id" = "\d+"}, name="ujm_sequence_administrate")
* @Method("GET")
* @ParamConverter("Sequence", class="UJMExoBundle:Sequence\Sequence")
*/
public function administrateAction(Sequence $resource) {
if (false === $this->container->get('security.context')->isGranted('ADMINISTRATE', $resource->getResourceNode())) {
throw new AccessDeniedException();
}
$steps = $this->get('ujm_exo_bundle.manager.steps')->getSteps($resource);
return $this->render('UJMExoBundle:Sequence:edit.html.twig', array(
'_resource' => $resource,
'steps' => $steps
)
);
}
/**
* update an exercise player
* @Route("/update/{id}", requirements={"id" = "\d+"}, name="ujm_sequence_update", options = {"expose" = true})
* @Method("PUT")
* @ParamConverter("Sequence", class="UJMExoBundle:Sequence\Sequence")
*
*/
public function updateAction(Sequence $resource) {
if (false === $this->container->get('security.context')->isGranted('EDIT', $resource->getResourceNode())) {
throw new AccessDeniedException();
}
$params = array(
'method' => 'PUT',
'csrf_protection' => false,
);
// Create form
$form = $this->container->get('form.factory')->create('sequence_type', $resource, $params);
$request = $this->container->get('request');
$form->submit($request);
$response = array();
if ($form->isValid()) {
$resource = $form->getData();
$updated = $this->get('ujm_exo_bundle.manager.sequence')->update($resource);
$response['status'] = 'success';
$response['messages'] = array();
$response['data'] = $updated;
} else {
$errors = $this->getFormErrors($form);
$response['status'] = 'error';
$response['messages'] = $errors;
$response['data'] = null;
}
return new JsonResponse($response);
}
private function getFormErrors(FormInterface $form) {
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$errors[$key] = $error->getMessage();
}
// Get errors from children
foreach ($form->all() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getFormErrors($child);
}
}
return $errors;
}
}