1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\FormRequest; |
6
|
|
|
use AppBundle\Entity\DTO\Filter; |
7
|
|
|
use AppBundle\Form\DTO\FormRequestFilterType; |
8
|
|
|
use AppBundle\Form\FormRequestType; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FormRequestController. |
19
|
|
|
* |
20
|
|
|
* @Route("/form_request", name="form_requests") |
21
|
|
|
*/ |
22
|
|
|
class FormRequestController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @Route("", name="form_request_list") |
26
|
|
|
* @Template("@App/request_forms.html.twig") |
27
|
|
|
* |
28
|
|
|
* @param Request $request |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function listAction(Request $request) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$em = $this->getDoctrine()->getManager(); |
35
|
|
|
$filter = new Filter(); |
36
|
|
|
$filterForm = $this->createForm(FormRequestFilterType::class, $filter) |
37
|
|
|
->add('Search', SubmitType::class); |
38
|
|
|
|
39
|
|
|
$filterForm->handleRequest($request); |
40
|
|
|
$formRequests = $this->get('knp_paginator')->paginate( |
41
|
|
|
$em->getRepository(FormRequest::class)->selectRequestFormsByParams($filter), |
42
|
|
|
$request->query->getInt('page', 1), |
43
|
|
|
10 |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$approveForms = []; |
47
|
|
|
|
48
|
|
|
foreach ($formRequests as $formRequest) { |
49
|
|
|
if ($formRequest->getStatus() == 'pending') { |
50
|
|
|
$approveForms[$formRequest->getId()] = $this->createForm(FormRequestType::class, $formRequest, [ |
51
|
|
|
'method' => 'PUT', |
52
|
|
|
'action' => $this->generateUrl('form_approve', ['id' => $formRequest->getId()]), |
53
|
|
|
]) |
54
|
|
|
->createView(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return [ |
59
|
|
|
'formRequests' => $formRequests, |
60
|
|
|
'approveForms' => $approveForms, |
61
|
|
|
'filterForm' => $filterForm->createView(), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @Route("/form_request/approve", name="form_approve") |
67
|
|
|
* |
68
|
|
|
* @Method("PUT") |
69
|
|
|
* |
70
|
|
|
* @param Request $request |
71
|
|
|
* |
72
|
|
|
* @return Response|false |
73
|
|
|
*/ |
74
|
|
|
public function activationAction(Request $request) |
75
|
|
|
{ |
76
|
|
|
$status = $request->request->get('status'); |
77
|
|
|
$id = $request->request->get('id'); |
78
|
|
|
|
79
|
|
|
if (!$status || !$id) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$em = $this->getDoctrine()->getManager(); |
84
|
|
|
$formRequest = $em->getRepository(FormRequest::class)->find($id); |
85
|
|
|
if (!$formRequest) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($status !== 'approved' && $status !== 'rejected') { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$formRequest->setStatus($status); |
94
|
|
|
$em->flush(); |
95
|
|
|
|
96
|
|
|
$this->get('app.email_notification')->sendNotification( |
97
|
|
|
$formRequest->getUser()->getEmail(), |
98
|
|
|
'Form request action', |
99
|
|
|
'Hello, '.$formRequest->getUser()->getFirstName().'. |
100
|
|
|
Your form request was '.$formRequest->getStatus().'.' |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return new Response($status); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.