-
Notifications
You must be signed in to change notification settings - Fork 5
/
Poll.php
336 lines (279 loc) · 10.6 KB
/
Poll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/*
* This file is part of the BaitPollBundle package.
*
* (c) BAIT s.r.o. <http://www.bait.sk/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bait\PollBundle;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Bait\PollBundle\FormFactory\PollFormFactoryInterface;
use Bait\PollBundle\Model\PollManagerInterface;
use Bait\PollBundle\Model\FieldManager;
use Bait\PollBundle\Model\FieldInterface;
use Bait\PollBundle\Model\AnswerManagerInterface;
use Bait\PollBundle\Model\AnswerGroupManagerInterface;
use Bait\PollBundle\Model\PollInterface;
use Bait\PollBundle\Model\SignedPollInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Class responsible for poll management.
*
* @author Ondrej Slintak <[email protected]>
*/
class Poll
{
/**
* @var Request
*/
protected $request;
/**
* @var EngineInterface
*/
protected $engine;
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var PollFormFactoryInterface
*/
protected $formFactory;
/**
* @var PollManagerInterface
*/
protected $pollManager;
/**
* @var AnswerManagerInterface
*/
protected $answerManager;
/**
* @var AnswerGroupManagerInterface
*/
protected $answerGroupManager;
/**
* @var string
*/
protected $template;
/**
* @var string
*/
protected $fieldClass;
/**
* @var string
*/
protected $pollClass;
/**
* @var string
*/
protected $cookiePrefix;
/**
* @var string
*/
protected $cookieDuration;
/**
* @var string
*/
protected $uploadDir;
/**
* @var string
*/
protected $theme;
/**
* @var boolean
*/
protected $isActive;
/**
* Constructs Poll service.
*
* @param Request $request Current request
* @param EngineInterface $engine Templating engine
* @param ObjectManager $objectManager Doctrine's object manager
* @param PollFormFactoryInterface $formFactory Poll form factory
* @param PollManagerInterface $pollManager Poll manager
* @param FieldManager $fieldManager Field manager
* @param AnswerManagerInterface $answerManager Answer manager
* @param AnswerGroupManagerInterface $answerGroupManager Answer group manager
*/
public function __construct(
Request $request,
EngineInterface $engine,
ObjectManager $objectManager,
PollFormFactoryInterface $formFactory,
PollManagerInterface $pollManager,
FieldManager $fieldManager,
AnswerManagerInterface $answerManager,
AnswerGroupManagerInterface $answerGroupManager,
$securityContext,
array $options
)
{
$this->request = $request;
$this->engine = $engine;
$this->objectManager = $objectManager;
$this->formFactory = $formFactory;
$this->pollManager = $pollManager;
$this->fieldManager = $fieldManager;
$this->answerManager = $answerManager;
$this->answerGroupManager = $answerGroupManager;
$this->securityContext = $securityContext;
list(
$this->fieldClass,
$this->pollClass,
$this->template,
$this->theme,
$this->cookiePrefix,
$this->cookieDuration,
$this->uploadDir
) = $options;
$this->isActive = true;
}
/**
* Creates form and validates it or saves data in case some data
* were already submitted.
*
* @param mixed $id Id of poll to be created
*
* @throws NotFoundHttpException
* @throws Exception
*/
public function create($id, Response &$response, $options = array())
{
$this->id = $id;
$this->poll = $this->pollManager->findOneById($id);
if (!$this->poll || $this->poll->getDeletedAt()) {
throw new NotFoundHttpException(
sprintf("Poll with id '%s' was not found.", $id)
);
}
if (!$this->poll->isActive()) {
$this->isActive = false;
return;
}
$this->form = $this->formFactory->create($id);
$formName = $this->form->getName();
if ($this->request->getMethod() === 'POST' && $this->request->request->has($formName) && !$this->answerGroupManager->hasAnswered($this->poll)) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$data = $this->form->getData();
$answers = array();
$answerGroup = $this->answerGroupManager->create($this->poll);
foreach ($data as $fieldId => $userAnswer) {
$fieldId = str_replace('field_', '', $fieldId);
$field = $this->objectManager->getReference($this->fieldClass, $fieldId);
if ($field->getType() === FieldInterface::TYPE_FILE) {
if ($field->isRequired()) {
$userAnswers = (array) $userAnswer->getClientOriginalName();
}
} else {
$userAnswers = (array) $userAnswer;
}
foreach ($userAnswers as $userAnswer) {
$answer = $this->answerManager->create($field, $userAnswer, $answerGroup);
$answers[] = $answer;
}
}
$response = new RedirectResponse($this->request->getUri());
$pollType = $this->poll->getType();
// Checks if poll type is proper one, defined in PollInterface
$fieldClassReflection = new \ReflectionClass($this->pollClass);
$fieldConstants = $fieldClassReflection->getConstants();
if (!in_array($pollType, $fieldConstants)) {
throw new \Exception(sprintf('"%s" is incorrect poll type.', $pollType));
}
// Checks what multi-answer prevention mechanisms should be triggered
// and error that might occur.
$doPersist = false;
if ($this->poll instanceof SignedPollInterface) {
$isAuthenticated = $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY');
if (in_array($pollType, array(PollInterface::POLL_TYPE_USER, PollInterface::POLL_TYPE_MIXED)) && $isAuthenticated) {
$user = $this->securityContext->getToken()->getUser();
$answerGroup->setAuthor($user);
$doPersist = true;
}
} else {
if (in_array($pollType, array(PollInterface::POLL_TYPE_USER, PollInterface::POLL_TYPE_MIXED))) {
throw new \Exception(sprintf('Poll type is "%s", but your Poll class (%s) doesn\'t implement Bait\PollBundle\Model\SignedPollInterface', $pollType, $this->pollClass));
}
}
if (in_array($pollType, array(PollInterface::POLL_TYPE_ANONYMOUS, PollInterface::POLL_TYPE_MIXED))) {
$cookieDuration = array_key_exists('cookieDuration', $options) ? (int) $options['cookieDuration'] : $this->cookieDuration;
$cookie = new Cookie(sprintf('%sanswered_%s', $this->cookiePrefix, $id), true, time() + $cookieDuration);
$response->headers->setCookie($cookie);
$doPersist = true;
}
// Checks if any upload occurred/should have occurred
// and handles it
if ($this->fieldManager->hasUploadFields($this->poll)) {
if ("" == $this->uploadDir) {
throw new \Exception("You should configure the bait_poll.upload_dir directive in your config.yml");
}
if (!is_writable($this->uploadDir . '/')) {
throw new \Exception(sprintf('"%s" is not a writable folder for uploads.', $this->uploadDir));
}
$answerGroup = $this->answerGroupManager->save($answerGroup);
$folder = $this->uploadDir . '/' . $this->poll->getId() . '/answergroups/' . $answerGroup->getId();
mkdir($folder, 0777, true);
foreach ($data as $fieldName => $field) {
if ($field instanceof UploadedFile) {
$field->move($folder, $fieldName . '.' . $field->guessExtension());
}
}
}
// If everything went ok, save all answers
if ($doPersist) {
$this->answerGroupManager->save($answerGroup);
$this->answerManager->save($answers);
}
}
}
}
/**
* Renders poll form into given template.
*
* @param string $template Path to poll template
*
* @return string
*/
public function render($template = null, $theme = null)
{
if (!$this->isActive) {
return null;
}
if (!$template) {
$template = $this->template;
}
if (!$theme) {
$theme = $this->theme;
}
$alreadyAnswered = $this->answerGroupManager->hasAnswered($this->poll);
$viewData = array(
'form' => $this->form->createView(),
'theme' => $theme,
'request' => $this->request,
'alreadyAnswered' => $alreadyAnswered,
'hasUploadFields' => $this->fieldManager->hasUploadFields($this->poll)
);
if ($this->poll->isAnswersVisible()) {
$fields = $this->poll->getFields();
$results = array();
foreach ($fields as $field) {
if ($field->isStandalone() && $field->hasChildren()) {
$children = $field->getChildren();
$answersCount = $this->answerManager->countVotesOf($children);
$results[$field->getTitle()] = $answersCount;
}
}
$viewData['results'] = $results;
}
return $this->engine->render($template, $viewData);
}
}