|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: kate |
|
5
|
|
|
* Date: 17.02.16 |
|
6
|
|
|
* Time: 9:30 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AppBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use AppBundle\Entity\Comment; |
|
12
|
|
|
use AppBundle\Entity\MenuItem; |
|
13
|
|
|
use AppBundle\Entity\Estate; |
|
14
|
|
|
use AppBundle\Entity\User; |
|
15
|
|
|
use AppBundle\Entity\Category; |
|
16
|
|
|
use AppBundle\Form\CommentType; |
|
17
|
|
|
use AppBundle\Form\SearchType; |
|
18
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class SiteController extends Controller |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @Route("/", name="homepage") |
|
30
|
|
|
*/ |
|
31
|
|
|
public function indexAction(Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
34
|
|
|
$estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles(); |
|
35
|
|
|
$breadcrumbs = $this->get("white_october_breadcrumbs"); |
|
36
|
|
|
$breadcrumbs->addItem("Homepage"); |
|
37
|
|
|
return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @Route("/menu", name="menu") |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function menuAction(Request $request) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
46
|
|
|
$categoryEntity = $em->getRepository('AppBundle\Entity\Category'); |
|
47
|
|
|
$categories = $categoryEntity->childrenHierarchy(); |
|
48
|
|
|
return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @Route("/show_category/{slug}", name="show_category") |
|
53
|
|
|
* @ParamConverter("category", class="AppBundle\Entity\Category", options={"mapping": {"slug": "title"}}) |
|
54
|
|
|
*/ |
|
55
|
|
|
public function showCategoryAction(Request $request, Category $category) |
|
56
|
|
|
{ |
|
57
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
58
|
|
|
$estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($category->getTitle()); |
|
59
|
|
|
$breadcrumbs = $this->get("white_october_breadcrumbs"); |
|
60
|
|
|
$node = $category; |
|
61
|
|
|
while ($node) { |
|
62
|
|
|
$breadcrumbs->prependItem($node->getTitle()); |
|
63
|
|
|
$node = $node->getParent(); |
|
64
|
|
|
} |
|
65
|
|
|
$breadcrumbs->prependItem("Homepage"); |
|
66
|
|
|
|
|
67
|
|
|
return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @Route("/show_estate/{slug}", name="show_estate") |
|
72
|
|
|
*/ |
|
73
|
|
|
public function showEstateAction(Request $request, $slug) |
|
74
|
|
|
{ |
|
75
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
76
|
|
|
$estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug); |
|
77
|
|
|
$breadcrumbs = $this->get("white_october_breadcrumbs"); |
|
78
|
|
|
$node = $estate->getCategory(); |
|
79
|
|
|
$breadcrumbs->prependItem($estate->getTitle()); |
|
80
|
|
|
while ($node) { |
|
81
|
|
|
$breadcrumbs->prependItem($node->getTitle()); |
|
82
|
|
|
$node = $node->getParent(); |
|
83
|
|
|
} |
|
84
|
|
|
$breadcrumbs->prependItem("Homepage"); |
|
85
|
|
|
$comment = new Comment(); |
|
86
|
|
|
$commentForm = $this->createForm(CommentType::class, $comment, [ |
|
87
|
|
|
'method' => 'POST', |
|
88
|
|
|
]) |
|
89
|
|
|
->add('addComment', SubmitType::class, ['label' => 'common.save', |
|
90
|
|
|
'attr' => ['class' => 'btn btn-primary'] |
|
91
|
|
|
]); |
|
92
|
|
|
$commentForm->handleRequest($request); |
|
93
|
|
|
if ($commentForm->isSubmitted() && $commentForm->isValid()) { |
|
94
|
|
|
$comment->setEstate($estate); |
|
95
|
|
|
if ($this->getUser()->hasRole('ROLE_ADMIN')) { |
|
96
|
|
|
$comment->setEnabled(true); |
|
97
|
|
|
} else { |
|
98
|
|
|
$comment->setEnabled(false); |
|
99
|
|
|
} |
|
100
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
101
|
|
|
$em->persist($comment); |
|
102
|
|
|
$em->flush(); |
|
103
|
|
|
$this->get('session')->getFlashBag()->add('success', 'site.flush_comment'); |
|
104
|
|
|
return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug())); |
|
105
|
|
|
} |
|
106
|
|
|
return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate, |
|
107
|
|
|
'commentForm' => $commentForm->createView())); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @Route("/search", name="site_search") |
|
112
|
|
|
* */ |
|
113
|
|
|
public function searchAction(Request $request) |
|
114
|
|
|
{ |
|
115
|
|
|
$finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories(); |
|
116
|
|
|
$searchForm = $this->createForm(SearchType::class, null, array( |
|
117
|
|
|
'action' => $this->generateUrl('site_search'), |
|
118
|
|
|
'categories_choices' => $finalCategories |
|
119
|
|
|
)) |
|
120
|
|
|
->add('search', SubmitType::class, ['label' => 'Искать', |
|
121
|
|
|
'attr' => ['class' => 'btn btn-default'] |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
$searchForm->handleRequest($request); |
|
125
|
|
|
if ($searchForm->isValid() && $searchForm->isSubmitted()) { |
|
126
|
|
|
$estates = $this->get('app.search')->searchEstate($searchForm->getData()); |
|
127
|
|
|
// return new Response(); |
|
|
|
|
|
|
128
|
|
|
return $this->render('AppBundle:site:index.html.twig', array('estates' => $estates)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->render('@App/site/search.html.twig', array( |
|
132
|
|
|
'form' => $searchForm->createView(), |
|
133
|
|
|
)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @Route(name="show_menu_item") |
|
138
|
|
|
*/ |
|
139
|
|
|
public function showMenuItemAction(Request $request) |
|
140
|
|
|
{ |
|
141
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
142
|
|
|
$menuitems = $em->getRepository('AppBundle:MenuItem')->findAll(); |
|
143
|
|
|
return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @Route("/description_menu/{id}", name="show_description_menu_item") |
|
148
|
|
|
* @ParamConverter("MenuItem", options={"mapping": {"id": "id"}}) |
|
149
|
|
|
*/ |
|
150
|
|
|
public function showDescriptionMenuItem(Request $request, MenuItem $menuItem) |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites") |
|
157
|
|
|
* @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}}) |
|
158
|
|
|
* @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}}) |
|
159
|
|
|
*/ |
|
160
|
|
View Code Duplication |
public function addEstateToFavoritesAction(Estate $estate, User $user, Request $request) |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
163
|
|
|
if (!$user->hasEstate($estate)) { |
|
164
|
|
|
$user->addEstate($estate); |
|
165
|
|
|
$em->persist($user); |
|
166
|
|
|
$em->flush(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug())); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites") |
|
174
|
|
|
* @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}}) |
|
175
|
|
|
* @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}}) |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function deleteEstateFromFavoritesAction(Estate $estate, User $user, Request $request) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
180
|
|
|
if ($user->hasEstate($estate)) { |
|
181
|
|
|
$user->removeEstate($estate); |
|
182
|
|
|
$em->persist($user); |
|
183
|
|
|
$em->flush(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug())); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @Route("/pdf/{estate}", name = "pdf_estate") |
|
191
|
|
|
* @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}}) |
|
192
|
|
|
*/ |
|
193
|
|
|
public function pdfEstateAction(Estate $estate, Request $request) |
|
194
|
|
|
{ |
|
195
|
|
|
$comment = new Comment(); |
|
196
|
|
|
$commentForm = $this->createForm(CommentType::class, $comment, [ |
|
197
|
|
|
'method' => 'POST', |
|
198
|
|
|
]) |
|
199
|
|
|
->add('addComment', SubmitType::class, ['label' => 'common.save', |
|
200
|
|
|
'attr' => ['class' => 'btn btn-primary'] |
|
201
|
|
|
]); |
|
202
|
|
|
$html = $this->renderView('@App/site/pdf.html.twig', array( |
|
203
|
|
|
'estate' => $estate, |
|
204
|
|
|
'commentForm' => $commentForm->createView() |
|
205
|
|
|
)); |
|
206
|
|
|
|
|
207
|
|
|
return new Response( |
|
208
|
|
|
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, array( |
|
209
|
|
|
'images' => true, |
|
210
|
|
|
)), |
|
211
|
|
|
200, |
|
212
|
|
|
array( |
|
213
|
|
|
'Content-Type' => 'application/pdf', |
|
214
|
|
|
'Content-Disposition' => 'attachment; filename="file.pdf"' |
|
215
|
|
|
) |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.