Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Controller/FormSubmissionsController.php (1 issue)

Labels
Severity

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 Symfony\Component\Routing\Annotation\Route;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
20
21
/**
22
 * The controller which will handle everything related with form pages and form submissions
23
 */
24
class FormSubmissionsController extends Controller
25
{
26
    /**
27
     * The index action will use an admin list to list all the form pages
28
     *
29
     * @Route("/", name="KunstmaanFormBundle_formsubmissions")
30
     * @Template("@KunstmaanAdminList/Default/list.html.twig")
31
     *
32
     * @return array
33
     */
34
    public function indexAction(Request $request)
35
    {
36
        /* @var EntityManager $em */
37
        $em = $this->getDoctrine()->getManager();
38
        $aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
39
40
        /* @var AdminList $adminList */
41
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
42
            new FormPageAdminListConfigurator($em, $aclHelper, PermissionMap::PERMISSION_VIEW)
43
        );
44
        $adminList->bindRequest($request);
45
46
        return array('adminlist' => $adminList);
47
    }
48
49
    /**
50
     * The list action will use an admin list to list all the form submissions related to the given $nodeTranslationId
51
     *
52
     * @param int $nodeTranslationId
53
     *
54
     * @Route("/list/{nodeTranslationId}", requirements={"nodeTranslationId" = "\d+"},
55
     *                                     name="KunstmaanFormBundle_formsubmissions_list", methods={"GET", "POST"})
56
     * @Template("@KunstmaanForm/FormSubmissions/list.html.twig")
57
     *
58
     * @return array
59
     */
60
    public function listAction(Request $request, $nodeTranslationId)
61
    {
62
        $em = $this->getDoctrine()->getManager();
63
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
64
65
        /** @var AdminList $adminList */
66
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
67
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $this->getParameter('kunstmaan_form.deletable_formsubmissions'))
68
        );
69
        $adminList->bindRequest($request);
70
71
        return array('nodetranslation' => $nodeTranslation, 'adminlist' => $adminList);
72
    }
73
74
    /**
75
     * The edit action will be used to edit a given submission.
76
     *
77
     * @param int $nodeTranslationId The node translation id
78
     * @param int $submissionId      The submission id
79
     *
80
     * @Route("/list/{nodeTranslationId}/{submissionId}", requirements={"nodeTranslationId" = "\d+", "submissionId" =
81
     *                                                    "\d+"}, name="KunstmaanFormBundle_formsubmissions_list_edit", methods={"GET", "POST"})
82
     * @Template("@KunstmaanForm/FormSubmissions/edit.html.twig")
83
     *
84
     * @return array
85
     */
86
    public function editAction($nodeTranslationId, $submissionId)
87
    {
88
        $em = $this->getDoctrine()->getManager();
89
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
90
        $formSubmission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($submissionId);
91
        $request = $this->container->get('request_stack')->getCurrentRequest();
92
        $deletableFormsubmission = $this->getParameter('kunstmaan_form.deletable_formsubmissions');
93
94
        /** @var AdminList $adminList */
95
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
96
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $deletableFormsubmission)
97
        );
98
        $adminList->bindRequest($request);
99
100
        return [
101
            'nodetranslation' => $nodeTranslation,
102
            'formsubmission' => $formSubmission,
103
            'adminlist' => $adminList,
104
        ];
105
    }
106
107
    /**
108
     * Export as CSV of all the form submissions for the given $nodeTranslationId
109
     *
110
     * @param int $nodeTranslationId
111
     *
112
     * @Route("/export/{nodeTranslationId}.{_format}", requirements={"nodeTranslationId" = "\d+","_format" =
113
     *                                                 "csv|xlsx|ods"}, name="KunstmaanFormBundle_formsubmissions_export", methods={"GET"})
114
     *
115
     * @return Response
116
     */
117
    public function exportAction($nodeTranslationId, $_format)
118
    {
119
        $em = $this->getDoctrine()->getManager();
120
        /** @var NodeTranslation $nodeTranslation */
121
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
122
        $translator = $this->get('translator');
123
124
        /** @var ExportList $exportList */
125
        $configurator = new FormSubmissionExportListConfigurator($em, $nodeTranslation, $translator);
126
        $exportList = $this->get('kunstmaan_adminlist.factory')->createExportList($configurator);
127
128
        return $this->get('kunstmaan_adminlist.service.export')->getDownloadableResponse($exportList, $_format);
129
    }
130
131
    /**
132
     * @Route(
133
     *      "/{id}/delete",
134
     *      requirements={"id" = "\d+"},
135
     *      name="KunstmaanFormBundle_formsubmissions_delete",
136
     *      methods={"POST"}
137
     * )
138
     *
139
     * @param Request $request
140
     * @param int     $id
141
     *
142
     * @return RedirectResponse
143
     *
144
     * @throws AccessDeniedException
145
     */
146
    public function deleteAction(Request $request, $id)
147
    {
148
        $em = $this->getDoctrine()->getManager();
149
        $submission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($id);
150
151
        $node = $em->getRepository('KunstmaanNodeBundle:Node')->find($submission->getNode());
152
        $nt = $node->getNodeTranslation($request->getLocale());
153
154
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
155
156
        $url = $this->get('router')->generate(
157
            'KunstmaanFormBundle_formsubmissions_list',
158
            ['nodeTranslationId' => $nt->getId()]
159
        );
160
161
        $fields = $em->getRepository('KunstmaanFormBundle:FormSubmissionField')->findBy(['formSubmission' => $submission]);
162
163
        try {
164
            foreach ($fields as $field) {
165
                $em->remove($field);
166
            }
167
168
            $em->remove($submission);
0 ignored issues
show
It seems like $submission defined by $em->getRepository('Kuns...Submission')->find($id) on line 149 can also be of type null; however, Doctrine\Persistence\ObjectManager::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
169
            $em->flush();
170
171
            $this->addFlash(
172
                FlashTypes::SUCCESS,
173
                $this->get('translator')->trans('formsubmissions.delete.flash.success')
174
            );
175
        } catch (\Exception $e) {
176
            $this->get('logger')->error($e->getMessage());
177
            $this->addFlash(
178
                FlashTypes::DANGER,
179
                $this->get('translator')->trans('formsubmissions.delete.flash.error')
180
            );
181
        }
182
183
        return new RedirectResponse($url);
184
    }
185
}
186