summaryrefslogtreecommitdiffstats
path: root/modules-available/roomplanner/page.inc.php
blob: b186944359087afba59682e71ba755f17d8d840c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

class Page_Roomplanner extends Page
{
    protected function doPreprocess()
    {
        User::load();

        if (!User::hasPermission('superadmin')) {
            Message::addError('main.no-permission');
            Util::redirect('?do=Main');
        }
    }

    protected function doRender()
    {

        $locationid = Request::get('locationid', null, 'integer');

        if ($locationid === null) { die('please specify locationid'); }


        $furniture      = $this->getFurniture($locationid);
        $subnetMachines = $this->getPotentialMachines($locationid);
        $machinesOnPlan = $this->getMachinesOnPlan($locationid);

        $action = Request::any('action', 'show', 'string');

        $roomConfig = array_merge($furniture, $machinesOnPlan);


        if ($action === 'show') {
            /* do nothing */
            Render::addTemplate('page', [
                'subnetMachines' => json_encode($subnetMachines),
                'locationid' => $locationid,
                'roomConfiguration' => json_encode($roomConfig, JSON_PRETTY_PRINT)]);
        } else if ($action === 'save') {
            /* save */
            $config = Request::post('serializedRoom', null, 'string');
            $config = json_decode($config, true);
            $this->saveRoomConfig($locationid, $config['furniture']);
            $this->saveComputerConfig($locationid, $config['computers'], $machinesOnPlan);
            Util::redirect("?do=roomplanner&locationid=$locationid&action=show");
        }

    }

    protected function doAjax()
    {
        $action = Request::get('action', null, 'string');

        if ($action === 'getmachines') {
            $query = Request::get('query', null, 'string');

            /* the query could be anything: UUID, IP or macaddr */
//			$result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname '
//				. ', MATCH (machineuuid, macaddr, clientip, hostname) AGAINST (:query) AS relevance '
//				. 'FROM machine '
//				. 'WHERE MATCH (machineuuid, macaddr, clientip, hostname) AGAINST (:query) '
//				. 'ORDER BY relevance DESC '
//				. 'LIMIT 5'
//				, ['query' => $query]);
//
            $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname '
                .'FROM machine '
                .'WHERE machineuuid LIKE :query '
                .' OR macaddr  	 LIKE :query '
                .' OR clientip    LIKE :query '
                .' OR hostname	 LIKE :query ', ['query' => "%$query%"]);

            $returnObject = ['machines' => []];

            while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
                $returnObject['machines'][] = $row;
            }
            echo json_encode($returnObject, JSON_PRETTY_PRINT);
        }
    }

    protected function saveComputerConfig($locationid, $computers, $oldComputers) {

        $oldUuids = [];
        /* collect all uuids  from the old computers */
        foreach($oldComputers['computers'] as $c) {
            $oldUuids[] = $c['muuid'];
        }

        $newUuids = [];
        foreach($computers as $computer) {
            $newUuids[] =  $computer['muuid'];

            $position = json_encode(['gridRow' => $computer['gridRow'],
                                     'gridCol' => $computer['gridCol'],
                                     'itemlook' => $computer['itemlook']], JSON_PRETTY_PRINT);

            Database::exec('UPDATE machine SET position = :position, locationid = :locationid WHERE machineuuid = :muuid',
            ['locationid' => $locationid, 'muuid' => $computer['muuid'], 'position' => $position]);
        }

        $toDelete = array_diff($oldUuids, $newUuids);

        foreach($toDelete as $d) {
            Database::exec("UPDATE machine SET position = '', locationid = NULL WHERE machineuuid = :uuid", ['uuid' => $d]);
        }
    }
    protected function saveRoomConfig($locationid, $furniture) {
        $obj = json_encode(['furniture' => $furniture], JSON_PRETTY_PRINT);
        Database::exec('INSERT INTO location_roomplan (locationid, roomplan) VALUES (:locationid, :roomplan) ON DUPLICATE KEY UPDATE roomplan=:roomplan',
            ['locationid' => $locationid,
             'roomplan' => $obj]);
    }

    protected function getFurniture($locationid) {
        $config = Database::queryFirst('SELECT roomplan FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $locationid]);
        if ($config == null) { return null; }
        $config = json_decode($config['roomplan'], true);
        return $config;
    }
    protected function getMachinesOnPlan($locationid) {
        $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname, position FROM machine WHERE locationid = :locationid',
            ['locationid' => $locationid]);
        $machines = [];
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $machine = [];
            $pos = json_decode($row['position'], true);

            $machine['muuid'] = $row['machineuuid'];
            $machine['ip']      = $row['clientip'];
            $machine['mac_address'] = $row['macaddr'];
            $machine['hostname'] = $row['hostname'];
            $machine['gridRow'] = (int) $pos['gridRow'];
            $machine['gridCol'] = (int) $pos['gridCol'];
            $machine['itemlook'] = $pos['itemlook'];
            $machine['data-width'] = 100;
            $machine['data-height'] = 100;
            $machines[] = $machine;
        }
        return ['computers' => $machines];
    }

    protected function getPotentialMachines($locationid)
    {
        $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname '
            .'FROM machine INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) '
            .'WHERE subnet.locationid = :locationid', ['locationid' => $locationid]);

        $machines = [];

        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $row['combined'] = implode(' ', array_values($row));
            $machines[] = $row;
        }

        return $machines;
    }
}