Completed
Push — master ( b604fb...c00aa6 )
by Kirill
06:32
created

DashboardController::updateAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 0
cts 16
cp 0
rs 8.9713
cc 2
eloc 16
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
                'type'=>$widget->getType()
41
            ];
42
        }
43
        $response = new Response();
44
        $response->setContent(json_encode(array(
45
            'result' =>'ok',
46
            'widgets'=> $list,
47
        )));
48
        $response->headers->set('Content-Type', 'application/json');
49
        return $response;
50
    }
51
52
    /**
53
     * @Route("/dashboard/widgets.json", methods={"GET"}, name="dashboard_widgets")
54
     * @return Response
55
     */
56 1
    public function widgetsAction()
57
    {
58 1
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
59
60 1
        $widgets = $widgets->findAll();
61
62 1
        $list = [];
63
        /** @var Widget $widget */
64 1
        foreach ($widgets as $widget) {
65 1
            $list[$widget->getId()] = [
66 1
                'id'=>$widget->getId(),
67 1
                'x'=>$widget->getX(),
68 1
                'y'=>$widget->getY(),
69 1
                'width'=>$widget->getWidth(),
70 1
                'height'=>$widget->getHeight(),
71 1
                'name'=>$widget->getName(),
72 1
                'type'=>$widget->getType(),
73 1
                'value'=>$widget->getVariable()->getValue()
74
            ];
75
76 1
            if ($widget->getType() == 'chart') {
77 1
                $list[$widget->getId()]['history'] = $this->get('vars')->getDayHistory($widget->getVariable());
78
            }
79
        }
80 1
        $response = new Response();
81 1
        $response->setContent(json_encode(array(
82 1
            'result' =>'ok',
83 1
            'widgets'=> $list,
84
        )));
85 1
        $response->headers->set('Content-Type', 'application/json');
86 1
        return $response;
87
    }
88
    /**
89
     * @Route("/dashboard/widgets.json", methods={"POST"}, name="dashboard_widgets_save")
90
     * @param Request $request
91
     * @return Response
92
     */
93
    public function saveWidgetsAction(Request $request)
94
    {
95
96
        $widgets = $request->get('widgets');
97
98
        foreach ($widgets as $widget) {
99
            $w = $this->getDoctrine()->getRepository('AppBundle:Widget')->find($widget['id']);
100
            if (!$w) {
101
                continue;
102
            }
103
104
            $w->setWidth($widget['size_x']);
105
            $w->setHeight($widget['size_y']);
106
            $w->setX($widget['col']);
107
            $w->setY($widget['row']);
108
109
            $this->getDoctrine()->getManager()->persist($w);
110
        }
111
112
        $this->getDoctrine()->getManager()->flush();
113
114
        $response = new Response();
115
        $response->setContent(json_encode(array(
116
            'result' =>'ok',
117
        )));
118
        $response->headers->set('Content-Type', 'application/json');
119
        return $response;
120
    }
121
}
122