Completed
Pull Request — master (#2)
by Andrew
03:06
created

SiteController::pdfEstateAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 25
ccs 0
cts 24
cp 0
rs 8.8571
cc 1
eloc 16
nc 1
nop 2
crap 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
     */
140
    public function showMenuItemAction(Request $request)
141
    {
142
        $em = $this->getDoctrine()->getManager();
143
        $menuitems = $em->getRepository('AppBundle:MenuItem')->findAll();
144
        return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems));
145
    }
146
147
    /**
148
     * @Route("/description_menu/{id}", name="show_description_menu_item")
149
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
150
     */
151
    public function showDescriptionMenuItem(Request $request, MenuItem $menuItem)
152
    {
153
        return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem));
154
    }
155
156
    /**
157
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites")
158
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
159
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
160
     */
161 View Code Duplication
    public function addEstateToFavoritesAction(Estate $estate, User $user, Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
162
    {
163
        $em = $this->getDoctrine()->getManager();
164
        if (!$user->hasEstate($estate)) {
165
            $user->addEstate($estate);
166
            $em->persist($user);
167
            $em->flush();
168
        }
169
170
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
171
    }
172
173
    /**
174
     * @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites")
175
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
176
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
177
     */
178 View Code Duplication
    public function deleteEstateFromFavoritesAction(Estate $estate, User $user, Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
179
    {
180
        $em = $this->getDoctrine()->getManager();
181
        if ($user->hasEstate($estate)) {
182
            $user->removeEstate($estate);
183
            $em->persist($user);
184
            $em->flush();
185
        }
186
187
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
188
    }
189
190
    /**
191
     * @Route("/pdf/{estate}", name = "pdf_estate")
192
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
193
     */
194
    public function pdfEstateAction(Estate $estate, Request $request)
195
    {
196
        $comment = new Comment();
197
        $commentForm = $this->createForm(CommentType::class, $comment, [
198
            'method' => 'POST',
199
        ])
200
            ->add('addComment', SubmitType::class, ['label' => 'common.save',
201
                'attr' => ['class' => 'btn btn-primary']
202
            ]);
203
        $html = $this->renderView('@App/site/pdf.html.twig', array(
204
            'estate' => $estate,
205
            'commentForm' => $commentForm->createView()
206
        ));
207
208
        return new Response(
209
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
210
                'images' => true,
211
            )),
212
            200,
213
            array(
214
                'Content-Type' => 'application/pdf',
215
                'Content-Disposition' => 'attachment; filename="file.pdf"'
216
            )
217
        );
218
    }
219
220
221
}