Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Controller/FormSubmissionsController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FormBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
7
use Kunstmaan\AdminListBundle\AdminList\AdminList;
8
use Kunstmaan\AdminListBundle\AdminList\ExportList;
9
use Kunstmaan\FormBundle\AdminList\FormPageAdminListConfigurator;
10
use Kunstmaan\FormBundle\AdminList\FormSubmissionAdminListConfigurator;
11
use Kunstmaan\FormBundle\AdminList\FormSubmissionExportListConfigurator;
12
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
21
22
23
/**
24
 * The controller which will handle everything related with form pages and form submissions
25
 */
26
class FormSubmissionsController extends Controller
27
{
28
    /**
29
     * The index action will use an admin list to list all the form pages
30
     *
31
     * @Route("/", name="KunstmaanFormBundle_formsubmissions")
32
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
33
     *
34
     * @return array
35
     */
36 View Code Duplication
    public function indexAction(Request $request)
37
    {
38
        /* @var EntityManager $em */
39
        $em        = $this->getDoctrine()->getManager();
40
        $aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
41
42
        /* @var AdminList $adminList */
43
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
44
            new FormPageAdminListConfigurator($em, $aclHelper, PermissionMap::PERMISSION_VIEW),
45
            $em
46
        );
47
        $adminList->bindRequest($request);
48
49
        return array('adminlist' => $adminList);
50
    }
51
52
    /**
53
     * The list action will use an admin list to list all the form submissions related to the given $nodeTranslationId
54
     *
55
     * @param int $nodeTranslationId
56
     *
57
     * @Route("/list/{nodeTranslationId}", requirements={"nodeTranslationId" = "\d+"},
58
     *                                     name="KunstmaanFormBundle_formsubmissions_list")
59
     * @Method({"GET", "POST"})
60
     * @Template("@KunstmaanForm/FormSubmissions/list.html.twig")
61
     *
62
     * @return array
63
     */
64
    public function listAction(Request $request, $nodeTranslationId)
65
    {
66
        $em              = $this->getDoctrine()->getManager();
67
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
68
69
        /** @var AdminList $adminList */
70
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
71
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $this->getParameter('kunstmaan_form.deletable_formsubmissions')),
72
            $em
73
        );
74
        $adminList->bindRequest($request);
75
76
        return array('nodetranslation' => $nodeTranslation, 'adminlist' => $adminList);
77
    }
78
79
    /**
80
     * The edit action will be used to edit a given submission.
81
     *
82
     * @param int $nodeTranslationId The node translation id
83
     * @param int $submissionId      The submission id
84
     *
85
     * @Route("/list/{nodeTranslationId}/{submissionId}", requirements={"nodeTranslationId" = "\d+", "submissionId" =
86
     *                                                    "\d+"}, name="KunstmaanFormBundle_formsubmissions_list_edit")
87
     * @Method({"GET", "POST"})
88
     * @Template("@KunstmaanForm/FormSubmissions/edit.html.twig")
89
     *
90
     * @return array
91
     */
92
    public function editAction($nodeTranslationId, $submissionId)
93
    {
94
        $em = $this->getDoctrine()->getManager();
95
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
96
        $formSubmission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($submissionId);
97
        $request = $this->container->get('request_stack')->getCurrentRequest();
98
        $deletableFormsubmission = $this->getParameter('kunstmaan_form.deletable_formsubmissions');
99
100
        /** @var AdminList $adminList */
101
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
102
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation,$deletableFormsubmission),
103
            $em
104
        );
105
        $adminList->bindRequest($request);
106
107
        return [
108
            'nodetranslation' => $nodeTranslation,
109
            'formsubmission' => $formSubmission,
110
            'adminlist' => $adminList,
111
        ];
112
    }
113
114
    /**
115
     * Export as CSV of all the form submissions for the given $nodeTranslationId
116
     *
117
     * @param int $nodeTranslationId
118
     *
119
     * @Route("/export/{nodeTranslationId}.{_format}", requirements={"nodeTranslationId" = "\d+","_format" =
120
     *                                                 "csv|xlsx|ods"}, name="KunstmaanFormBundle_formsubmissions_export")
121
     * @Method({"GET"})
122
     *
123
     * @return Response
124
     */
125
    public function exportAction($nodeTranslationId, $_format)
126
    {
127
        $em = $this->getDoctrine()->getManager();
128
        /** @var NodeTranslation $nodeTranslation */
129
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
130
        $translator      = $this->get('translator');
131
132
        /** @var ExportList $exportList */
133
        $configurator = new FormSubmissionExportListConfigurator($em, $nodeTranslation, $translator);
0 ignored issues
show
$em of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManagerInterface>. It seems like you assume a child interface of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
134
        $exportList   = $this->get('kunstmaan_adminlist.factory')->createExportList($configurator);
135
136
        return $this->get('kunstmaan_adminlist.service.export')->getDownloadableResponse($exportList, $_format);
137
    }
138
139
    /**
140
     * @Route(
141
     *      "/{id}/delete",
142
     *      requirements={"id" = "\d+"},
143
     *      name="KunstmaanFormBundle_formsubmissions_delete"
144
     * )
145
     * @Method("POST")
146
     *
147
     * @param Request $request
148
     * @param int     $id
149
     *
150
     * @return RedirectResponse
151
     *
152
     * @throws AccessDeniedException
153
     */
154
    public function deleteAction(Request $request, $id)
155
    {
156
        $em = $this->getDoctrine()->getManager();
157
        $submission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($id);
158
159
        $node = $em->getRepository('KunstmaanNodeBundle:Node')->find($submission->getNode());
160
        $nt = $node->getNodeTranslation($request->getLocale());
161
162
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
163
164
        $url = $this->get('router')->generate(
165
            'KunstmaanFormBundle_formsubmissions_list',
166
            ['nodeTranslationId' => $nt->getId()]
167
        );
168
169
        $fields = $em->getRepository('KunstmaanFormBundle:FormSubmissionField')->findBy(['formSubmission' => $submission]);
170
171
        try {
172
            foreach ($fields as $field) {
173
                $em->remove($field);
174
            }
175
176
            $em->remove($submission);
177
            $em->flush();
178
179
            $this->addFlash(
180
                FlashTypes::SUCCESS,
181
                $this->get('translator')->trans('formsubmissions.delete.flash.success')
182
            );
183
        } catch (\Exception $e) {
184
            $this->get('logger')->error($e->getMessage());
185
            $this->addFlash(
186
                FlashTypes::ERROR,
187
                $this->get('translator')->trans('formsubmissions.delete.flash.error')
188
            );
189
        }
190
191
        $response = new RedirectResponse($url);
192
193
        return $response;
194
    }
195
}
196