-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupController.php
More file actions
executable file
·107 lines (90 loc) · 3.25 KB
/
Copy pathGroupController.php
File metadata and controls
executable file
·107 lines (90 loc) · 3.25 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
<?php
declare(strict_types=1);
namespace KejawenLab\Semart\Surat\Controller\Admin;
use KejawenLab\Semart\Surat\Entity\Group;
use KejawenLab\Semart\Surat\Pagination\Paginator;
use KejawenLab\Semart\Surat\Request\RequestHandler;
use KejawenLab\Semart\Surat\Security\Authorization\Permission;
use KejawenLab\Semart\Surat\Security\Service\GroupService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/groups")
*
* @Permission(menu="GROUP")
*
* @author Muhamad Surya Iksanudin <surya.iksanudin@gmail.com>
*/
class GroupController extends AdminController
{
/**
* @Route("/", methods={"GET"}, name="groups_index", options={"expose"=true})
*
* @Permission(actions=Permission::VIEW)
*/
public function index(Request $request, Paginator $paginator)
{
$groups = $paginator->paginate(Group::class, (int) $request->query->get('p', 1));
if ($request->isXmlHttpRequest()) {
$table = $this->renderView('group/table-content.html.twig', ['groups' => $groups]);
$pagination = $this->renderView('group/pagination.html.twig', ['groups' => $groups]);
return new JsonResponse([
'table' => $table,
'pagination' => $pagination,
]);
}
return $this->render('group/index.html.twig', ['title' => 'Grup', 'groups' => $groups]);
}
/**
* @Route("/{id}", methods={"GET"}, name="groups_detail", options={"expose"=true})
*
* @Permission(actions=Permission::VIEW)
*/
public function find(string $id, GroupService $service, SerializerInterface $serializer)
{
$group = $service->get($id);
if (!$group) {
throw new NotFoundHttpException();
}
return new JsonResponse($serializer->serialize($group, 'json', ['groups' => ['read']]));
}
/**
* @Route("/save", methods={"POST"}, name="groups_save", options={"expose"=true})
*
* @Permission(actions={Permission::ADD, Permission::EDIT})
*/
public function save(Request $request, GroupService $service, RequestHandler $requestHandler)
{
$primary = $request->get('id');
if ($primary) {
$group = $service->get($primary);
} else {
$group = new Group();
}
if (!$group) {
throw new NotFoundHttpException();
}
$requestHandler->handle($request, $group);
if (!$requestHandler->isValid()) {
return new JsonResponse(['status' => 'KO', 'errors' => $requestHandler->getErrors()]);
}
$this->commit($group);
return new JsonResponse(['status' => 'OK']);
}
/**
* @Route("/{id}/delete", methods={"POST"}, name="groups_remove", options={"expose"=true})
*
* @Permission(actions=Permission::DELETE)
*/
public function delete(string $id, GroupService $service)
{
if (!$group = $service->get($id)) {
throw new NotFoundHttpException();
}
$this->remove($group);
return new JsonResponse(['status' => 'OK']);
}
}