summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/page.inc.php
blob: f6b4b661b35be43375bcc3d82ee05bc57385f466 (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
<?php

class Page_LocationInfo extends Page
{

	private $action;

	/**
	 * Called before any page rendering happens - early hook to check parameters etc.
	 */
	protected function doPreprocess()
	{
		User::load();
		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main'); // does not return
		}
	}

	/**
	 * Menu etc. has already been generated, now it's time to generate page content.
	 */
	protected function doRender()
	{
		$getAction = Request::get('action');
		if (empty($getAction)) {
			Util::redirect('?do=locationinfo&action=infoscreen');
		}

		if ($getAction === 'infoscreen') {
			$this->getInfoScreenTable();
		}

		if($getAction == 'updateroomdb') {
			$this->updateInfoscreenDb();
			Util::redirect('?do=locationinfo&action=infoscreen');
		}

		if ($getAction === 'hide') {
			$roomId = Request::get('id');
			$hiddenValue = Request::get('value');
			$this->toggleHidden($roomId, $hiddenValue);
			Util::redirect('?do=locationinfo&action=infoscreen');
		}
	}

	protected function toggleHidden($id, $val) {
		Database::exec("UPDATE `locationinfo` SET hidden = $val WHERE locationid = $id");
	}

	protected function getInfoScreenTable() {

		$dbquery = Database::simpleQuery("SELECT * FROM `locationinfo`");

		$pcs = array();
		while($roominfo=$dbquery->fetch(PDO::FETCH_ASSOC)) {
			$data = array();
			$data['locationid'] = $roominfo['locationid'];
			$data['hidden'] = $roominfo['hidden'];

			$inUseCounter = 0;
			$totalPcCounter = 0;
			$data['computers'] = json_decode($roominfo['computers'], true);

			foreach ($data['computers'] as $value) {
				if ($value['inUse'] == 1) {
					$inUseCounter++;
				}
				$totalPcCounter++;
			}
			$data['inUse'] = $inUseCounter;
			$data['totalPcs'] = $totalPcCounter;
			$pcs[] = $data;
		}

		Render::addTemplate('location-info', array(
			'list' => array_values($pcs),
		));
	}

	protected function updateInfoscreenDb() {
		$dbquery = Database::simpleQuery("SELECT DISTINCT locationid FROM `machine` WHERE locationid IS NOT NULL");
		while($roominfo=$dbquery->fetch(PDO::FETCH_ASSOC)) {
			$this->updatePcInfos($roominfo['locationid']);
		}
	}

	/**
 	 * AJAX
 	 */
	protected function doAjax()
	{
		User::load();
		if (!User::isLoggedIn()) {
			die('Unauthorized');
		}
		$action = Request::any('action');
		if ($action === 'pcsubtable') {
			$id = Request::any('id');
			$this->ajaxShowLocation($id);

		}
	}

	private function ajaxShowLocation($id)
	{
		$dbquery = Database::simpleQuery("SELECT * FROM `locationinfo` WHERE locationid = $id");

		$data = array();
		while($roominfo=$dbquery->fetch(PDO::FETCH_ASSOC)) {
			$data = json_decode($roominfo['computers'], true);
		}

		echo Render::parse('pcsubtable', array(
			'list' => array_values($data),
		));
	}

}