ClientController::homeAction()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.0534
cc 4
eloc 15
nc 4
nop 2
1
<?php
2
3
namespace ClientBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
9
use ClientBundle\Form\Type\ClientForm;
10
use ClientBundle\Form\Type\CategoriesForm;
11
use ClientBundle\Entity\Client;
12
use ClientBundle\Entity\Contact;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * Class ClientController
17
 */
18
class ClientController extends Controller
19
{
20
    /**
21
     * @param int $id
22
     * @return \Symfony\Component\HttpFoundation\Response
23
     */
24
    public function checkAction($id)
25
    {
26
        return new Response('test '.$id);
27
    }
28
29
    /**
30
     * @param int     $id
31
     * @param Request $request
32
     * @Method({"GET", "POST"})
33
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
34
     */
35
    public function showAction($id, Request $request)
36
    {
37
        if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
38
            throw $this->createAccessDeniedException();
39
        }
40
        $em = $this->getDoctrine()->getManager();
41
        $client = $em->find('ClientBundle:Client', $id);
42
43
        if (!$client) {
44
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
45
        } else {
46
            $form = $this->createForm(ClientForm::class, $client, ['id' => $client->getId()]);
47
            // перенести в вьюв
48
            if ($this->isGranted('ROLE_ADMIN')) {
49
                $form->add('delete', SubmitType::class);
50
            }
51
52
            $form->handleRequest($request);
53
54
            if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
55
                $client = $form->getData();
56
57
                if ($this->isGranted('ROLE_ADMIN') && $form->get('delete')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
                    return $this->redirectToRoute('client_del', ['id' => $client->getId()]);
59
                } elseif ($form->get('addcontact')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
                    $contact = new Contact();
61
                    $clientForm = $request->request->get('client_form');
62
63
                    $contactType = $em->find('ClientBundle:ContactType', $clientForm['newtypecontact']);
64
                    if (!$contactType) {
65
                        throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $clientForm['newtypecontact']));
66
                    } else {
67
                        $contact->setType($contactType);
68
                        $contact->setMean($clientForm['newmeancontact']);
69
                        $client->addContact($contact);
70
                        $em->persist($contact);
71
                    }
72
                } else {
73
                    $em->flush();
74
75
                    return $this->redirectToRoute('client_home');
76
                }
77
            }
78
79
            return $this->render('ClientBundle:Default:edit.html.twig', ['form' => $form->createView(), 'client' => $client]);
80
        }
81
    }
82
83
    /**
84
     * @Method("GET")
85
     * @return \Symfony\Component\HttpFoundation\Response
86
     */
87
    public function searchAction()
88
    {
89
         return $this->render('ClientBundle:Default:home.html.twig');
90
    }
91
92
    /**
93
     * @param Request $request
94
     * @param string  $ctg
95
     * @Method({"GET", "POST"})
96
     * @return \Symfony\Component\HttpFoundation\Response
97
     */
98
    public function homeAction(Request $request, $ctg = '')
99
    {
100
        $ctgRequest = $request->request->get('categories_form');
101
        if (null !== $ctgRequest && '' !== $ctgRequest['categories']) {
102
            $ctg = (int) $ctgRequest['categories'];
103
        }
104
        $em = $this->getDoctrine()->getManager();
105
        $categories = $em->getRepository('ClientBundle:Category')->findAll();
106
        $form = $this->createForm(CategoriesForm::class, $categories);
107
        $form->handleRequest($request);
108
        if (is_integer($ctg)) {
109
            $category = $em->getRepository('ClientBundle:Category')->find($ctg);
110
            $clients =  $category->getClients();
111
112
            return $this->render('ClientBundle:Default:home.html.twig', ['form' => $form->createView(), 'clients' => $clients]);
113
        } else {
114
            $clients = $em->getRepository('ClientBundle:Client')->findAll();
115
116
            return $this->render('ClientBundle:Default:home.html.twig', ['form' => $form->createView(), 'clients' => $clients]);
117
        }
118
    }
119
120
    /**
121
     * @param Request $request
122
     * @Method({"GET", "POST"})
123
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
124
     */
125
    public function addAction(Request $request)
126
    {
127
        $em = $this->getDoctrine()->getManager();
128
        $client = new Client();
129
        $form = $this->createForm(ClientForm::class, $client, array('id' => $client->getId()));
130
131
        $form->handleRequest($request);
132
133
        if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
134
            $client = $form->getData();
135
136
            if ($form->get('addcontact')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
                $contact = new Contact();
138
                $clientForm = $request->request->get('client_form');
139
                $contactType = $em->getRepository('ClientBundle:ContactType')->find($clientForm['newtypecontact']);
140
                if (!$contactType) {
141
                    throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $clientForm['newtypecontact']));
142
                } else {
143
                    $contact->setType($contactType);
144
                    $contact->setMean($clientForm['newmeancontact']);
145
                    $client->addContact($contact);
146
                    $em->persist($contact);
147
                    $em->persist($client);
148
                    $em->flush();
149
150
                    return $this->redirectToRoute('client_show', ['id' => $client->getId()]);
151
                }
152
            }
153
            $em->persist($client);
154
            $em->flush();
155
156
            return $this->redirectToRoute('client_home');
157
        }
158
159
        return $this->render('ClientBundle:Default:edit.html.twig', ['form' => $form->createView(), 'client' => $client]);
160
    }
161
162
    /**
163
     * @param int $id
164
     * @Method({"GET", "POST"})
165
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
166
     */
167
    public function delAction($id)
168
    {
169
        $em = $this->getDoctrine()->getManager();
170
        $client = $em->getRepository('ClientBundle:Client')->find($id);
171
        if (!$client) {
172
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
173
        } else {
174
            $em->remove($client);
175
            $em->flush();
176
        }
177
178
        return $this->redirectToRoute('client_home');
179
    }
180
}
181