Issues (3627)

app/bundles/FormBundle/Event/SubmissionEvent.php (1 issue)

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\Event;
13
14
use Mautic\CoreBundle\Event\CommonEvent;
15
use Mautic\FormBundle\Entity\Action;
16
use Mautic\FormBundle\Entity\Submission;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
class SubmissionEvent extends CommonEvent
21
{
22
    /**
23
     * Raw POST results.
24
     *
25
     * @var array
26
     */
27
    private $post = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $server = [];
33
34
    /**
35
     * Cleaned post results.
36
     *
37
     * @var array
38
     */
39
    private $results = [];
40
41
    /**
42
     * Form fields.
43
     *
44
     * @var array
45
     */
46
    private $fields = [];
47
48
    /**
49
     * Results converted to tokens.
50
     *
51
     * @var array
52
     */
53
    private $tokens = [];
54
55
    /**
56
     * Callback for post form submit.
57
     *
58
     * @var mixed
59
     */
60
    private $callbacks = [];
61
62
    /**
63
     * @var mixed
64
     */
65
    private $callbackResponses = [];
66
67
    /**
68
     * @var array
69
     */
70
    private $contactFieldMatches = [];
71
72
    /**
73
     * Array to hold information set by other actions that may be useful to subsequent actions.
74
     *
75
     * @var array
76
     */
77
    private $feedback = [];
78
79
    /**
80
     * @var Action
81
     */
82
    private $action;
83
84
    /**
85
     * @var string
86
     */
87
    private $context;
88
89
    /**
90
     * @var Request
91
     */
92
    private $request;
93
94
    /**
95
     * @var array|Response|null
96
     */
97
    private $postSubmitResponse;
98
99
    /**
100
     * SubmissionEvent constructor.
101
     *
102
     * @param $post
103
     * @param $server
104
     */
105
    public function __construct(Submission $submission, $post, $server, Request $request)
106
    {
107
        $this->entity  = $submission;
108
        $this->post    = $post;
109
        $this->server  = $server;
110
        $this->request = $request;
111
    }
112
113
    /**
114
     * Returns the Submission entity.
115
     *
116
     * @return Submission
117
     */
118
    public function getSubmission()
119
    {
120
        return $this->entity;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getPost()
127
    {
128
        return $this->post;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getServer()
135
    {
136
        return $this->server;
137
    }
138
139
    /**
140
     * @return Request
141
     */
142
    public function getRequest()
143
    {
144
        return $this->request;
145
    }
146
147
    /**
148
     * @return \Mautic\FormBundle\Entity\Form
149
     */
150
    public function getForm()
151
    {
152
        return $this->entity->getForm();
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getResults()
159
    {
160
        return $this->results;
161
    }
162
163
    /**
164
     * @param array $results
165
     *
166
     * @return SubmissionEvent
167
     */
168
    public function setResults($results)
169
    {
170
        $this->results = $results;
171
172
        return $this;
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    public function getFields()
179
    {
180
        return $this->fields;
181
    }
182
183
    /**
184
     * @param array $fields
185
     *
186
     * @return SubmissionEvent
187
     */
188
    public function setFields($fields)
189
    {
190
        $this->fields = $fields;
191
192
        return $this;
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function getTokens()
199
    {
200
        return $this->tokens;
201
    }
202
203
    /**
204
     * @param array $tokens
205
     *
206
     * @return SubmissionEvent
207
     */
208
    public function setTokens($tokens)
209
    {
210
        $this->tokens = $tokens;
211
212
        return $this;
213
    }
214
215
    /**
216
     * @return array
217
     */
218
    public function getContactFieldMatches()
219
    {
220
        return $this->contactFieldMatches;
221
    }
222
223
    /**
224
     * @param array $contactFieldMatches
225
     *
226
     * @return SubmissionEvent
227
     */
228
    public function setContactFieldMatches($contactFieldMatches)
229
    {
230
        $this->contactFieldMatches = $contactFieldMatches;
231
232
        return $this;
233
    }
234
235
    /**
236
     * @param $key
237
     * @param $feedback
238
     */
239
    public function setActionFeedback($key, $feedback)
240
    {
241
        $this->feedback[$key] = $feedback;
242
    }
243
244
    /**
245
     * Get feedback injected by another action.
246
     *
247
     * @param null $key
248
     *
249
     * @return array|bool|mixed
250
     */
251
    public function getActionFeedback($key = null)
252
    {
253
        if (null === $key) {
0 ignored issues
show
The condition null === $key is always true.
Loading history...
254
            return $this->feedback;
255
        } elseif (isset($this->feedback[$key])) {
256
            return $this->feedback[$key];
257
        }
258
259
        return false;
260
    }
261
262
    public function checkContext(string $context): bool
263
    {
264
        return $this->context === $context;
265
    }
266
267
    public function setContext(string $context): void
268
    {
269
        $this->context = $context;
270
    }
271
272
    public function setAction(?Action $action = null)
273
    {
274
        $this->action = $action;
275
        if (!is_null($action)) {
276
            $this->setContext($action->getType());
277
        }
278
    }
279
280
    public function getAction(): ?Action
281
    {
282
        return $this->action;
283
    }
284
285
    public function getActionConfig(): array
286
    {
287
        return $this->action ? $this->action->getProperties() : [];
288
    }
289
290
    /**
291
     * Set an post submit callback - include $callback['eventName' => '', 'anythingElse' ...].
292
     *
293
     * @param string $key
294
     */
295
    public function setPostSubmitCallback($key, array $callback)
296
    {
297
        if (!array_key_exists('eventName', $callback)) {
298
            throw new \InvalidArgumentException('eventName required');
299
        }
300
301
        $this->callbacks[$key] = $callback;
302
    }
303
304
    /**
305
     * @return mixed
306
     */
307
    public function getPostSubmitCallback($key = null)
308
    {
309
        return (null === $key) ? $this->callbacks : $this->callbacks[$key];
310
    }
311
312
    /**
313
     * @return int
314
     */
315
    public function hasPostSubmitCallbacks()
316
    {
317
        return count($this->callbacks) || count($this->callbackResponses);
318
    }
319
320
    /**
321
     * @return mixed
322
     */
323
    public function getPostSubmitCallbackResponse($key = null)
324
    {
325
        return (null === $key) ? $this->callbackResponses : $this->callbackResponses[$key];
326
    }
327
328
    /**
329
     * @param mixed $callbackResponse
330
     *
331
     * @return SubmissionEvent
332
     */
333
    public function setPostSubmitCallbackResponse($key, $callbackResponse)
334
    {
335
        $this->callbackResponses[$key] = $callbackResponse;
336
337
        return $this;
338
    }
339
340
    public function hasPostSubmitResponse(): bool
341
    {
342
        return null !== $this->postSubmitResponse;
343
    }
344
345
    public function getPostSubmitResponse()
346
    {
347
        return $this->postSubmitResponse;
348
    }
349
350
    public function setPostSubmitResponse($response): void
351
    {
352
        $this->postSubmitResponse = $response;
353
    }
354
}
355