1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\SurveyAnswer; |
6
|
|
|
use AppBundle\Entity\Survey; |
7
|
|
|
use AppBundle\Entity\SurveyType; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
class SurveyController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/surveys", name="surveys") |
18
|
|
|
* @Template("@App/surveys.html.twig") |
19
|
|
|
*/ |
20
|
|
|
public function surveysAction(Request $request) |
21
|
|
|
{ |
22
|
|
|
$em = $this->getDoctrine()->getManager(); |
23
|
|
|
$surveys = $em->getRepository(Survey::class)->findSurveyByStatus('submited'); |
24
|
|
|
$surveyTypes = $em->getRepository(SurveyType::class)->findAll(); |
25
|
|
|
$paginator = $this->get('knp_paginator'); |
26
|
|
|
$pagination = $paginator->paginate($surveys, $request->query->getInt('page', 1), 10); |
27
|
|
|
|
28
|
|
|
return [ |
29
|
|
|
'surveys' => $pagination, 'survey_types' => $surveyTypes, |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Survey $survey |
35
|
|
|
* @Route("/surveys/{id}", name="survey") |
36
|
|
|
* @Template("@App/survey.html.twig") |
37
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
38
|
|
|
*/ |
39
|
|
|
public function surveyAction(Survey $survey) |
40
|
|
|
{ |
41
|
|
|
$em = $this->getDoctrine()->getManager(); |
42
|
|
|
$answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); |
43
|
|
|
foreach ($answers as $answer) { |
44
|
|
|
$questions[] = $answer->getQuestion()->getId(); |
|
|
|
|
45
|
|
|
$contents[] = $answer->getContent(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
$questionAnswer = array_combine($questions, $contents); |
|
|
|
|
48
|
|
|
if (!$answers) { |
49
|
|
|
$questionAnswer = null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
'survey' => $survey, 'question_answers' => $questionAnswer, |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Request $request, SurveyType $surveyType |
|
|
|
|
59
|
|
|
* @Route("/survey/create/{survey_type}", name="survey_create") |
60
|
|
|
* @ParamConverter("surveyType", options={"mapping": {"survey_type": "name"}}) |
61
|
|
|
*/ |
62
|
|
|
public function surveyCreateAction(Request $request, SurveyType $surveyType) |
63
|
|
|
{ |
64
|
|
|
$survey = new Survey(); |
65
|
|
|
$survey->setType($surveyType); |
66
|
|
|
$form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); |
67
|
|
|
$form->handleRequest($request); |
68
|
|
|
|
69
|
|
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
70
|
|
|
$em = $this->getDoctrine()->getManager(); |
71
|
|
|
$em->persist($survey); |
72
|
|
|
$em->flush(); |
73
|
|
|
|
74
|
|
|
return $this->redirectToRoute('surveys'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->render('@App/surveyform.html.twig', array( |
78
|
|
|
'form' => $form->createView(), |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
84
|
|
|
* @Route("/survey/delete/{id}", name="survey_delete") |
85
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
86
|
|
|
*/ |
87
|
|
|
public function surveyDeleteAction(Request $request, Survey $survey) |
88
|
|
|
{ |
89
|
|
|
$em = $this->getDoctrine()->getManager(); |
90
|
|
|
$answers = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey); |
91
|
|
|
foreach ($answers as $answer) { |
92
|
|
|
$questions[] = $answer->getQuestion()->getId(); |
|
|
|
|
93
|
|
|
$contents[] = $answer->getContent(); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
$questionAnswer = array_combine($questions, $contents); |
|
|
|
|
96
|
|
|
if (!$answers) { |
97
|
|
|
$questionAnswer = null; |
98
|
|
|
} |
99
|
|
|
$form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey); |
100
|
|
|
$form->handleRequest($request); |
101
|
|
|
|
102
|
|
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
103
|
|
|
$em = $this->getDoctrine()->getManager(); |
104
|
|
|
$em->remove($survey); |
105
|
|
|
$em->flush(); |
106
|
|
|
|
107
|
|
|
return $this->redirectToRoute('surveys'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->render('@App/survey.html.twig', array( |
111
|
|
|
'form' => $form->createView(), 'survey' => $survey, 'question_answers' => $questionAnswer, |
112
|
|
|
)); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.