DashboardController::saveWidgetsAction()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 17
cp 0
rs 8.8571
cc 3
eloc 19
nc 3
nop 1
crap 12
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(
19
            'dashboard/index.html.twig',
20
            [
21
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
22
            ]
23
        );
24
    }
25
26
    /**
27
     * @Route("/dashboard/update.json", methods={"GET"}, name="dashboard_update")
28
     * @return Response
29
     */
30 1
    public function updateAction()
31
    {
32 1
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
33
34 1
        $widgets = $widgets->findAll();
35
36 1
        $list = [];
37
        /**
38
 * @var Widget $widget
39
*/
40 1
        foreach ($widgets as $widget) {
41 1
            $list[$widget->getId()] = [
42 1
                'id'=>$widget->getId(),
43 1
                'lastchange'=>$widget->getVariable()->getLastupdate(),
44 1
                'value'=>$widget->getVariable()->getValue(),
45 1
                'type'=>$widget->getType()
46
            ];
47
        }
48 1
        $response = new Response();
49 1
        $response->setContent(
50
            json_encode(
51
                array(
52 1
                'result' =>'ok',
53 1
                'widgets'=> $list,
54
                )
55
            )
56
        );
57 1
        $response->headers->set('Content-Type', 'application/json');
58 1
        return $response;
59
    }
60
61
    /**
62
     * @Route("/dashboard/widgets.json", methods={"GET"}, name="dashboard_widgets")
63
     * @return Response
64
     */
65 1
    public function widgetsAction()
66
    {
67 1
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
68
69 1
        $widgets = $widgets->findAll();
70
71 1
        $list = [];
72
        /** @var Widget $widget */
73 1
        foreach ($widgets as $widget) {
74 1
            $list[$widget->getId()] = [
75 1
                'id'=>$widget->getId(),
76 1
                'x'=>$widget->getX(),
77 1
                'y'=>$widget->getY(),
78 1
                'width'=>$widget->getWidth(),
79 1
                'height'=>$widget->getHeight(),
80 1
                'name'=>$widget->getName(),
81 1
                'type'=>$widget->getType(),
82 1
                'value'=>$widget->getVariable()->getValue()
83
            ];
84
85 1
            if ($widget->getType() == 'chart') {
86
                $list[$widget->getId()]['history'] = $this->get('vars')->getDayHistory($widget->getVariable());
87
            }
88
        }
89 1
        $response = new Response();
90 1
        $response->setContent(
91
            json_encode(
92
                array(
93 1
                'result' =>'ok',
94 1
                'widgets'=> $list,
95
                )
96
            )
97
        );
98 1
        $response->headers->set('Content-Type', 'application/json');
99 1
        return $response;
100
    }
101
    /**
102
     * @Route("/dashboard/widgets.json", methods={"POST"}, name="dashboard_widgets_save")
103
     * @param Request $request
104
     * @return Response
105
     */
106
    public function saveWidgetsAction(Request $request)
107
    {
108
109
        $widgets = $request->get('widgets');
110
111
        foreach ($widgets as $widget) {
112
            $w = $this->getDoctrine()->getRepository('AppBundle:Widget')->find($widget['id']);
113
            if (!$w) {
114
                continue;
115
            }
116
117
            $w->setWidth($widget['size_x']);
118
            $w->setHeight($widget['size_y']);
119
            $w->setX($widget['col']);
120
            $w->setY($widget['row']);
121
122
            $this->getDoctrine()->getManager()->persist($w);
123
        }
124
125
        $this->getDoctrine()->getManager()->flush();
126
127
        $response = new Response();
128
        $response->setContent(
129
            json_encode(
130
                array(
131
                'result' =>'ok',
132
                )
133
            )
134
        );
135
        $response->headers->set('Content-Type', 'application/json');
136
        return $response;
137
    }
138
}
139