Completed
Pull Request — master (#2)
by Kate
03:14
created

SiteController::showCategoryAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 6
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
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites")
139
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
140
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
141
     */
142
    public function showMenuItemAction(Request $request)
143
    {
144
        $em = $this->getDoctrine()->getManager();
145
        $menuitems = $em->getRepository('AppBundle:MenuItem')->findAll();
146
        return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems));
147
    }
148
149
    /**
150
     * @Route("/description_menu/{id}", name="show_description_menu_item")
151
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
152
     */
153
    public function showDescriptionMenuItem(Request $request, MenuItem $menuItem)
154
    {
155
       return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem));
156
    }
157
}