Completed
Pull Request — master (#6)
by Kirill
06:14
created

DashboardController::updateAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 15
cp 0
rs 9.0856
cc 2
eloc 15
nc 2
nop 0
crap 6
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Widget;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class DashboardController extends Controller
12
{
13
    /**
14
     * @Route("/dashboard", name="var_dashboard")
15
     */
16
    public function indexAction()
17
    {
18
        return $this->render('dashboard/index.html.twig', [
19
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
20
        ]);
21
    }
22
23
    /**
24
     * @Route("/dashboard/update.json", methods={"GET"}, name="dashboard_update")
25
     * @return Response
26
     */
27
    public function updateAction()
28
    {
29
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
30
31
        $widgets = $widgets->findAll();
32
33
        $list = [];
34
        /** @var Widget $widget */
35
        foreach ($widgets as $widget) {
36
            $list[$widget->getId()] = [
37
                'id'=>$widget->getId(),
38
                'lastchange'=>$widget->getVariable()->getLastupdate(),
39
                'value'=>$widget->getVariable()->getValue()
40
            ];
41
        }
42
        $response = new Response();
43
        $response->setContent(json_encode(array(
44
            'result' =>'ok',
45
            'widgets'=> $list,
46
        )));
47
        $response->headers->set('Content-Type', 'application/json');
48
        return $response;
49
    }
50
51
    /**
52
     * @Route("/dashboard/widgets.json", methods={"GET"}, name="dashboard_widgets")
53
     * @return Response
54
     */
55 1
    public function widgetsAction()
56
    {
57 1
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
58
59 1
        $widgets = $widgets->findAll();
60
61 1
        $list = [];
62
        /** @var Widget $widget */
63 1
        foreach ($widgets as $widget) {
64 1
            $list[$widget->getId()] = [
65 1
                'id'=>$widget->getId(),
66 1
                'x'=>$widget->getX(),
67 1
                'y'=>$widget->getY(),
68 1
                'width'=>$widget->getWidth(),
69 1
                'height'=>$widget->getHeight(),
70 1
                'name'=>$widget->getName(),
71 1
                'value'=>$widget->getVariable()->getValue()
72
            ];
73
        }
74 1
        $response = new Response();
75 1
        $response->setContent(json_encode(array(
76 1
            'result' =>'ok',
77 1
            'widgets'=> $list,
78
        )));
79 1
        $response->headers->set('Content-Type', 'application/json');
80 1
        return $response;
81
    }
82
    /**
83
     * @Route("/dashboard/widgets.json", methods={"POST"}, name="dashboard_widgets_save")
84
     * @param Request $request
85
     * @return Response
86
     */
87
    public function saveWidgetsAction(Request $request)
88
    {
89
90
        $widgets = $request->get('widgets');
91
92
        foreach ($widgets as $widget) {
93
            $w = $this->getDoctrine()->getRepository('AppBundle:Widget')->find($widget['id']);
94
            if (!$w) {
95
                continue;
96
            }
97
98
            $w->setWidth($widget['size_x']);
99
            $w->setHeight($widget['size_y']);
100
            $w->setX($widget['col']);
101
            $w->setY($widget['row']);
102
103
            $this->getDoctrine()->getManager()->persist($w);
104
        }
105
106
        $this->getDoctrine()->getManager()->flush();
107
108
        $response = new Response();
109
        $response->setContent(json_encode(array(
110
            'result' =>'ok',
111
        )));
112
        $response->headers->set('Content-Type', 'application/json');
113
        return $response;
114
    }
115
}
116