Issues (3627)

Controller/Api/SubmissionApiController.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\FormBundle\Controller\Api;
13
14
use Mautic\ApiBundle\Controller\CommonApiController;
15
use Mautic\FormBundle\Entity\Form;
16
use Mautic\FormBundle\Entity\Submission;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
19
20
/**
21
 * Class SubmissionApiController.
22
 */
23
class SubmissionApiController extends CommonApiController
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function initialize(FilterControllerEvent $event)
29
    {
30
        $this->model            = $this->getModel('form.submission');
31
        $this->entityClass      = Submission::class;
32
        $this->entityNameOne    = 'submission';
33
        $this->entityNameMulti  = 'submissions';
34
        $this->permissionBase   = 'forms:form';
35
        $this->serializerGroups = ['submissionDetails', 'formList', 'ipAddressList', 'leadBasicList', 'pageList'];
36
37
        parent::initialize($event);
38
    }
39
40
    /**
41
     * Obtains a list of entities as defined by the API URL.
42
     *
43
     * @param int $formId
44
     *
45
     * @return Response
46
     */
47
    public function getEntitiesAction($formId = null)
48
    {
49
        $form = $this->getFormOrResponseWithError($formId);
50
51
        if ($form instanceof Response) {
0 ignored issues
show
$form is always a sub-type of Symfony\Component\HttpFoundation\Response.
Loading history...
52
            return $form;
53
        }
54
55
        $this->extraGetEntitiesArguments = array_merge(
56
            $this->extraGetEntitiesArguments,
57
            [
58
                'form'            => $form,
59
                'flatten_results' => true,
60
                'return_entities' => true,
61
            ]
62
        );
63
64
        return parent::getEntitiesAction();
65
    }
66
67
    /**
68
     * Obtains a list of entities for specific form and contact.
69
     *
70
     * @param int $formId
71
     * @param int $contactId
72
     *
73
     * @return Response
74
     */
75
    public function getEntitiesForContactAction($formId, $contactId)
76
    {
77
        $filter = [
78
            'filter' => [
79
                'where' => [
80
                    [
81
                        'col'  => 's.lead_id',
82
                        'expr' => 'eq',
83
                        'val'  => (int) $contactId,
84
                    ],
85
                ],
86
            ],
87
        ];
88
89
        $this->extraGetEntitiesArguments = array_merge($this->extraGetEntitiesArguments, $filter);
90
91
        return $this->getEntitiesAction($formId);
92
    }
93
94
    /**
95
     * Obtains a specific entity as defined by the API URL.
96
     *
97
     * @param int $id Entity ID
98
     *
99
     * @return Response
100
     */
101
    public function getEntityAction($formId = null, $submissionId = null)
102
    {
103
        $form = $this->getFormOrResponseWithError($formId);
104
105
        if ($form instanceof Response) {
0 ignored issues
show
$form is always a sub-type of Symfony\Component\HttpFoundation\Response.
Loading history...
106
            return $form;
107
        }
108
109
        return parent::getEntityAction($submissionId);
110
    }
111
112
    /**
113
     * Tries to fetch the form and returns Response if
114
     * - Form not found
115
     * - User doesn't have permission to view it.
116
     *
117
     * Returns Form on success
118
     *
119
     * @param int $formId
120
     *
121
     * @return Response|Form
122
     */
123
    protected function getFormOrResponseWithError($formId)
124
    {
125
        $formModel = $this->getModel('form');
126
        $form      = $formModel->getEntity($formId);
127
128
        if (!$form) {
129
            return $this->notFound();
130
        }
131
132
        if (!$this->checkEntityAccess($form)) {
133
            return $this->accessDenied();
134
        }
135
136
        return $form;
137
    }
138
}
139