summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/page.inc.php
blob: d507c4c3bbff1adc54b0b17c56883d87df200949 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
<?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
		}

		$this->action = Request::post('action');
		if ($this->action === 'updateOpeningTimeExpert') {
			$this->updateOpeningTimeExpert();
		} elseif ($this->action === 'updateOpeningTimeEasy') {
			$this->updateOpeningTimeEasy();
		} elseif ($this->action === 'updateConfig') {
			$this->updateConfig();
		} elseif ($this->action === 'updateServer') {
			$this->updateServer();
		} elseif ($this->action === 'deleteServer') {
			$this->deleteServer();
		} elseif ($this->action === 'checkConnection') {
			$this->checkConnection();
		} elseif ($this->action === 'updateServerSettings') {
			$this->updateServerSettings();
		}
	}

	/**
	 * 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', 0, 'int');
			$hiddenValue = Request::get('value', 0, 'int');
			$this->toggleHidden($roomId, $hiddenValue);
			Util::redirect('?do=locationinfo&action=infoscreen#row' . $roomId);
		}

	}

	/**
	 * Updates the server in the db.
	 */
	private function updateServer()
	{
		$id = Request::post('id', 0, 'int');
		if ($id == 0) {
			Database::exec("INSERT INTO `setting_location_info` (servername, serverurl, servertype) VALUES (:name, :url, :type)",
				array('name' => Request::post('name', '', 'string'),
					'url' => Request::post('url', '', 'string'),
					'type' => Request::post('type', '', 'string')));
		} else {
			Database::exec("INSERT INTO `setting_location_info` (serverid, servername, servertype, serverurl) VALUES (:id, :name, :type, :url)
			ON DUPLICATE KEY UPDATE servername=:name, serverurl=:url, servertype=:type",
				array('id' => $id,
					'name' => Request::post('name', '', 'string'),
					'url' => Request::post('url', '', 'string'),
					'type' => Request::post('type', '', 'string')));
		}

		$this->checkConnection();
	}

	/**
	 * Deletes the server from the db.
	 */
	private function deleteServer()
	{
		$id = Request::post('id', 0, 'int');
		Database::exec("DELETE FROM `setting_location_info` WHERE serverid=:id", array('id' => $id));
	}

	/**
	 * Updated the config in the db.
	 */
	private function updateConfig()
	{
		$result = array();

		$locationid = Request::post('id', 0, 'int');
		$result['language'] = Request::post('language');
		$result['mode'] = Request::post('mode', 1, 'int');
		$result['vertical'] = Request::post('vertical', false, 'bool');
		$result['eco'] = Request::post('eco', false, 'bool');
		$result['scaledaysauto'] = Request::post('autoscale', false, 'bool');
		$result['daystoshow'] = Request::post('daystoshow', 7, 'int');
		$result['rotation'] = Request::post('rotation', 0, 'int');
		$result['scale'] = Request::post('scale', 50, 'int');
		$result['switchtime'] = Request::post('switchtime', 20, 'int');
		$result['calupdate'] = Request::post('calupdate', 30, 'int');
		$result['roomupdate'] = Request::post('roomupdate', 30, 'int');
		$result['configupdate'] = Request::post('configupdate', 180, 'int');
		$serverid = Request::post('serverid', 0, 'int');
		$serverroomid = Request::post('serverroomid', '', 'string');

		Database::exec("INSERT INTO `location_info` (locationid, serverid, serverroomid, config, lastcalendarupdate) VALUES (:id, :serverid, :serverroomid, :config, :lastcalendarupdate)
		 ON DUPLICATE KEY UPDATE config=:config, serverid=:serverid, serverroomid=:serverroomid, lastcalendarupdate=:lastcalendarupdate",
			array('id' => $locationid,
				'config' => json_encode($result, true),
				'serverid' => $serverid,
				'serverroomid' => $serverroomid,
				'lastcalendarupdate' => 0));

		Message::addSuccess('config-saved');
		Util::redirect('?do=locationinfo');
	}

	/**
	 * Updates the server settings in the db.
	 */
	private function updateServerSettings()
	{
		$serverid = Request::post('id', -1, 'int');
		$servername = Request::post('name', 'unnamed', 'string');
		$serverurl = Request::post('url', '', 'string');
		$servertype = Request::post('type', '', 'string');

		$backend = CourseBackend::getInstance($servertype);
		$tmptypeArray = $backend->getCredentials();

		$credentialsJson = array();
		$counter = 0;
		foreach ($tmptypeArray as $key => $value) {
			$credentialsJson[$key] = Request::post($counter);
			$counter++;
		}
		if ($serverid == 0) {
			Database::exec('INSERT INTO `setting_location_info` (servername, serverurl, servertype, credentials) VALUES (:name, :url, :type, :credentials)',
				array('name' => $servername,
					'url' => $serverurl,
					'type' => $servertype,
					'credentials' => json_encode($credentialsJson, true)));

			$dbresult = Database::queryFirst('SELECT serverid FROM `setting_location_info` WHERE servername = :name AND serverurl = :url AND servertype = :type AND credentials = :credentials',
				array('name' => $servername,
					'url' => $serverurl,
					'type' => $servertype,
					'credentials' => json_encode($credentialsJson, true)));

			$this->checkConnection($dbresult['serverid']);
		} else {
			Database::exec('UPDATE `setting_location_info` SET servername = :name, serverurl = :url, servertype = :type, credentials = :credentials WHERE serverid = :id',
				array('id' => $serverid,
					'name' => $servername,
					'url' => $serverurl,
					'type' => $servertype,
					'credentials' => json_encode($credentialsJson, true)));
			$this->checkConnection();
		}
	}

	/**
	 * Updates the opening time in the db from the expert mode.
	 */
	private function updateOpeningTimeExpert()
	{
		$days = Request::post('days', array(), 'array');
		$locationid = Request::post('id', 0, 'int');
		$openingtime = Request::post('openingtime', array(), 'array');
		$closingtime = Request::post('closingtime', array(), 'array');
		$easyMode = Request::post('easyMode', false, 'bool');
		$delete = Request::post('delete', array(), 'array');
		$dontadd = Request::post('dontadd', array(), 'array');
		$count = 0;
		$result = array();
		$resulttmp = array();
		$deleteCounter = 0;

		if (!$easyMode) {
			$dbquery = Database::simpleQuery("SELECT openingtime FROM `location_info` WHERE locationid = :id", array('id' => $locationid));
			while ($dbdata = $dbquery->fetch(PDO::FETCH_ASSOC)) {
				$resulttmp = json_decode($dbdata['openingtime'], true);
			}

			$index = 0;

			foreach ($resulttmp as $day) {
				$skip = false;

				foreach ($delete as $del) {
					if ($del == $index) {
						$skip = true;
						break;
					}
				}
				if ($skip == true) {
					$index++;
					$deleteCounter++;
					continue;
				}

				$result[] = $day;
				$index++;
			}
		}

		if (!empty($days) && !is_array($days)) {
			Message::addError('no-days-selected');
			Util::redirect('?do=locationinfo');
		} else {

			$dayz = array();
			$da = array();

			foreach ($days as $d) {
				if ($d != '-') {
					$da[] = $d;
				} else {
					$dayz[$count] = $da;
					$da = array();
					$count++;
				}
			}

			$optime = array();
			for ($x = 0; $x < $count; $x++) {
				if ($dontadd[$x] == 'dontadd') {
					continue;
				}
				$optime['days'] = $dayz[$x];
				$optime['openingtime'] = $openingtime[$x];
				$optime['closingtime'] = $closingtime[$x];
				$result[] = $optime;
			}
		}

		Database::exec("INSERT INTO `location_info` (locationid, openingtime) VALUES (:id, :openingtime) ON DUPLICATE KEY UPDATE openingtime=:openingtime",
			array('id' => $locationid, 'openingtime' => json_encode($result, true)));

		if ($deleteCounter > 0) {
			Message::addSuccess('deleted-x-entries', $deleteCounter);
		}
		if ($count > 0) {
			Message::addSuccess('added-x-entries', $count);
		}

		Util::redirect('?do=locationinfo');
	}

	/**
	 * Updates the opening time in the db from the easy mode.
	 */
	private function updateOpeningTimeEasy()
	{
		$locationid = Request::post('id', 0, 'int');
		$openingtime = Request::post('openingtime');
		$closingtime = Request::post('closingtime');
		$result = array();

		$opt0['days'] = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
		if (isset($openingtime[0]) && isset($closingtime[0])) {
			if ($closingtime[0] != "" && $openingtime[0] != "") {
				$opt0['openingtime'] = $openingtime[0];
				$opt0['closingtime'] = $closingtime[0];
				$result[] = $opt0;
			}
		}

		$opt1['days'] = array("Saturday");
		if (isset($openingtime[1]) && isset($closingtime[1])) {
			if ($closingtime[1] != "" && $closingtime[1] != "") {
				$opt1['openingtime'] = $openingtime[1];
				$opt1['closingtime'] = $closingtime[1];
				$result[] = $opt1;
			}
		}

		$opt2['days'] = array("Sunday");
		if (isset($openingtime[2]) && isset($closingtime[2])) {
			if ($closingtime[2] != "" && $openingtime[2] != "") {
				$opt2['openingtime'] = $openingtime[2];
				$opt2['closingtime'] = $closingtime[2];
				$result[] = $opt2;
			}
		}

		Database::exec("INSERT INTO `location_info` (locationid, openingtime) VALUES (:id, :openingtime) ON DUPLICATE KEY UPDATE openingtime=:openingtime",
			array('id' => $locationid, 'openingtime' => json_encode($result, true)));

		Message::addSuccess('openingtime-updated');
		Util::redirect('?do=locationinfo');
	}

	/**
	 * Checks if the server connection to a backend is valid.
	 *
	 * @param int $id Server id which connection should be checked.
	 */
	private function checkConnection($id = 0)
	{
		$serverid = Request::post('id', 0, 'int');
		if ($id != 0) {
			$serverid = $id;
		}

		if ($serverid != 0) {
			$dbresult = Database::queryFirst("SELECT * FROM `setting_location_info` WHERE serverid = :serverid", array('serverid' => $serverid));

			$serverInstance = CourseBackend::getInstance($dbresult['servertype']);
			$setCredentials = $serverInstance->setCredentials(json_decode($dbresult['credentials'], true), $dbresult['serverurl'], $serverid);

			if ($setCredentials) {
				$setCred = $serverInstance->checkConnection();
			}

			if (!$setCredentials || !$setCred) {
				$error['timestamp'] = time();
				$error['error'] = $serverInstance->getError();
				Database::exec("UPDATE `setting_location_info` SET error=:error WHERE serverid=:id", array('id' => $serverid,
					'error' => json_encode($error, true)));
			} else {
				Database::exec("UPDATE `setting_location_info` SET error=NULL WHERE serverid=:id", array('id' => $serverid));
			}
		}
	}

	/**
	 * Sets the new hidden value and checks childs and parents.
	 *
	 * @param $id The location id which was toggled
	 * @param $val The hidden value true / false
	 */
	protected function toggleHidden($id, $val)
	{
		Database::exec("INSERT INTO `location_info` (locationid, hidden) VALUES (:id, :hidden) ON DUPLICATE KEY UPDATE hidden=:hidden", array('id' => $id,
			'hidden' => $val));

		$this->checkChildRecursive($id, $val);
		$this->checkParentRecursive($id);

	}

	/**
	 * Recursivly sets all hidden values to all childs.
	 *
	 * @param $id The location id which childs should be checked
	 * @param $val The hidden value
	 */
	protected function checkChildRecursive($id, $val)
	{
		$dbquery = Database::simpleQuery("SELECT locationid FROM `location` WHERE parentlocationid = :locationid", array('locationid' => $id));
		$childs = array();
		while ($dbdata = $dbquery->fetch(PDO::FETCH_ASSOC)) {
			$childs[] = $dbdata['locationid'];
		}

		foreach ($childs as $key) {
			Database::exec("INSERT INTO `location_info` (locationid, hidden) VALUES (:id, :hidden) ON DUPLICATE KEY UPDATE hidden=:hidden", array('id' => $key,
				'hidden' => $val));

			$this->checkChildRecursive($key, $val);
		}
	}

	/**
	 * Recursively check all parent locations and updates the hidden values if necessary
	 *
	 * @param $id The id of the location which was toggled.
	 */
	protected function checkParentRecursive($id)
	{
		$dbquery = Database::simpleQuery("SELECT parentlocationid FROM `location` WHERE locationid = :locationid", array('locationid' => $id));
		$parent = 0;
		while ($dbdata = $dbquery->fetch(PDO::FETCH_ASSOC)) {
			$parent = (int)$dbdata['parentlocationid'];
		}
		if ($parent === 0) {
			return;
		} else {
			$dbq = Database::simpleQuery("SELECT COUNT(CASE li.hidden WHEN '0' THEN 1 ELSE NULL END) AS '0',
			 															COUNT(CASE li.hidden WHEN '1' THEN 1 ELSE NULL END) AS '1',
																		 COUNT(*) - COUNT(CASE li.hidden WHEN '0' THEN 1 ELSE NULL END) - COUNT(CASE li.hidden WHEN '1' THEN 1 ELSE NULL END) AS 'NULL'
																		 FROM `location` AS l LEFT JOIN `location_info` AS li ON l.locationid=li.locationid
																		 WHERE parentlocationid = :parentId;", array('parentId' => $parent));
			$amountofzero = 0;
			$amountofnull = 0;

			while ($dbd = $dbq->fetch(PDO::FETCH_ASSOC)) {
				$amountofzero = (int)$dbd['0'];
				$amountofnull = (int)$dbd['NULL'];
			}

			if ($amountofzero == 0 AND $amountofnull == 0) {
				Database::exec("INSERT INTO `location_info` (locationid, hidden) VALUES (:id, :hidden) ON DUPLICATE KEY UPDATE hidden=:hidden", array('id' => $parent,
					'hidden' => 1));
			} else {
				Database::exec("INSERT INTO `location_info` (locationid, hidden) VALUES (:id, :hidden) ON DUPLICATE KEY UPDATE hidden=:hidden", array('id' => $parent,
					'hidden' => 0));
			}

			$this->checkParentRecursive($parent);
		}
	}

	/**
	 * Loads the Infoscreen page in the admin-panel and passes all needed information.
	 */
	protected function getInfoScreenTable()
	{

		// Get a table with the needed location info. name, id, hidden, pcState (Count of pcs that are in use), total pcs
		$dbquery = Database::simpleQuery("SELECT l.locationname, l.locationid, li.hidden, m.pcState, m.total FROM `location_info` AS li
		RIGHT JOIN `location` AS l ON li.locationid=l.locationid LEFT JOIN
		(SELECT locationid, Count(CASE m.logintime WHEN NOT 1 THEN NULL ELSE 1 END) AS pcState, Count(*) AS total FROM `machine` AS m
		WHERE locationid IS NOT NULL GROUP BY locationid) AS m ON l.locationid=m.locationid");
		$pcs = array();

		if (Module::isAvailable('locations')) {
			foreach (Location::getLocations() as $loc) {
				$data = array();
				$data['locationid'] = (int)$loc['locationid'];
				$data['locationname'] = $loc['locationname'];
				$data['depth'] = $loc['depth'];
				$data['hidden'] = null;
				$locid = (int)$loc['locationid'];
				$pcs[$locid] = $data;
			}
		}

		while ($roominfo = $dbquery->fetch(PDO::FETCH_ASSOC)) {
			$locid = (int)$roominfo['locationid'];

			if ($roominfo['hidden'] == null) {
				$pcs[$locid]['hidden'] = 0;
			} else {
				$pcs[$locid]['hidden'] = $roominfo['hidden'];
			}

			if ($roominfo['pcState'] != null) {
				$pcs[$locid]['pcState'] = $roominfo['pcState'];
			}
			if ($roominfo['total'] != null) {
				$pcs[$locid]['total'] = $roominfo['total'];
				$pcs[$locid]['hasPcs'] = true;
			} else {
				$pcs[$locid]['hasPcs'] = false;
			}
		}

		// Get a list of all the backend types.
		$servertypes = array();
		$s_list = CourseBackend::getList();
		foreach ($s_list as $s) {
			$t['type'] = $s;
			$typeInstance = CourseBackend::getInstance($s);
			$t['display'] = $typeInstance->getDisplayName();
			$servertypes[] = $t;
		}

		// Get the Serverlist from the DB and make it mustache accesable
		$serverlist = array();
		$dbquery2 = Database::simpleQuery("SELECT * FROM `setting_location_info`");
		while ($db = $dbquery2->fetch(PDO::FETCH_ASSOC)) {
			$server['id'] = $db['serverid'];
			$server['name'] = $db['servername'];
			$server['type'] = $db['servertype'];
			foreach ($servertypes as $type) {
				if ($server['type'] == $type['type']) {
					$server['display'] = $type['display'];
					break;
				}
			}

			if ($db['error'] == null) {
				$server['auth'] = true;
			} else {
				$server['auth'] = false;
				$error = json_decode($db['error'], true);

				$time = date('Y/m/d H:i:s', $error['timestamp']);

				Message::addError('auth-failed', $server['name'], $time, $error['error']);
			}

			$server['url'] = $db['serverurl'];
			$serverlist[] = $server;
		}

		// Pass the data to the html and render it.
		Render::addTemplate('location-info', array(
			'list' => array_values($pcs),
			'serverlist' => array_values($serverlist),
		));
	}

	/**
	 * AJAX
	 */
	protected function doAjax()
	{
		User::load();
		if (!User::isLoggedIn()) {
			die('Unauthorized');
		}
		$action = Request::any('action');
		if ($action === 'timetable') {
			$id = Request::any('id', 0, 'int');
			$this->ajaxTimeTable($id);
		} elseif ($action === 'config') {
			$id = Request::any('id', 0, 'int');
			$this->ajaxConfig($id);
		} elseif ($action === 'serverSettings') {
			$id = Request::any('id', 0, 'int');
			$this->ajaxServerSettings($id);
		}
	}

	/**
	 * Ajax the server settings.
	 *
	 * @param $id Serverid
	 */
	private function ajaxServerSettings($id)
	{
		$dbresult = Database::queryFirst('SELECT servername, serverurl, servertype, credentials FROM `setting_location_info` WHERE serverid = :id', array('id' => $id));

		// Credentials stuff.
		$dbcredentials = json_decode($dbresult['credentials'], true);

		// Get a list of all the backend types.
		$serverBackends = array();
		$s_list = CourseBackend::getList();
		foreach ($s_list as $s) {
			$backend['typ'] = $s;
			$backendInstance = CourseBackend::getInstance($s);
			$backend['display'] = $backendInstance->getDisplayName();

			if ($backend['typ'] == $dbresult['servertype']) {
				$backend['active'] = true;
			} else {
				$backend['active'] = false;
			}

			$credentials = $backendInstance->getCredentials();
			$backend['credentials'] = array();

			$counter = 0;
			foreach ($credentials as $key => $value) {
				$credential['uid'] = $counter;
				$credential['name'] = Dictionary::translateFile($s, $key);;
				$credential['type'] = $value;
				$credential['title'] = Dictionary::translateFile($s, $key . "_title");

				if (Property::getPasswordFieldType() === 'text') {
					$credential['mask'] = false;
				} else {
					if ($value == "password") {
						$credential['mask'] = true;
					}
				}

				if ($backend['typ'] == $dbresult['servertype']) {
					foreach ($dbcredentials as $k => $v) {
						if ($k == $key) {
							$credential['value'] = $v;
							break;
						}
					}
				}

				$selection = array();

				if (is_array($value)) {

					$selfirst = true;
					foreach ($value as $opt) {
						$option['option'] = $opt;
						if (isset($credential['value'])) {
							if ($opt == $credential['value']) {
								$option['active'] = true;
							} else {
								$option['active'] = false;
							}
						} else {
							if ($selfirst) {
								$option['active'] = true;
								$selfirst = false;
							} else {
								$option['active'] = false;
							}
						}

						$selection[] = $option;
					}
					$credential['type'] = "array";
					$credential['array'] = $selection;
				}

				$backend['credentials'][] = $credential;

				$counter++;
			}
			$serverBackends[] = $backend;
		}

		echo Render::parse('server-settings', array('id' => $id,
			'name' => $dbresult['servername'],
			'url' => $dbresult['serverurl'],
			'servertype' => $dbresult['servertype'],
			'backendList' => array_values($serverBackends)));
	}

	/**
	 * Ajax the time table
	 *
	 * @param $id id of the location
	 */
	private function ajaxTimeTable($id)
	{
		$array = array();
		$dbquery = Database::simpleQuery("SELECT openingtime FROM `location_info` WHERE locationid = :id", array('id' => $id));
		$dbresult = array();
		while ($dbdata = $dbquery->fetch(PDO::FETCH_ASSOC)) {
			$dbresult[] = json_decode($dbdata['openingtime'], true);
		}
		if ($this->isEasyMode($dbresult)) {
			echo Render::parse('timetable', array('id' => $id,
				'openingtime0' => $dbresult[0][0]['openingtime'],
				'closingtime0' => $dbresult[0][0]['closingtime'],
				'openingtime1' => $dbresult[0][1]['openingtime'],
				'closingtime1' => $dbresult[0][1]['closingtime'],
				'openingtime2' => $dbresult[0][2]['openingtime'],
				'closingtime2' => $dbresult[0][2]['closingtime'],
				'easyMode' => true,
				'expertMode' => false));

		} else {
			foreach ($dbresult as $db) {
				$index = 0;
				foreach ($db as $key) {
					$str = "";

					$first = true;
					foreach ($key['days'] as $val) {
						if ($first) {
							$first = false;
						} else {
							$str .= ", ";
						}
						$str .= $val;
					}
					$ar = array();
					$ar['days'] = $str;
					$ar['openingtime'] = $key['openingtime'];
					$ar['closingtime'] = $key['closingtime'];
					$ar['index'] = $index;
					$array[] = $ar;
					$index++;
				}
			}
			echo Render::parse('timetable', array('id' => $id,
				'openingtimes' => array_values($array),
				'easyMode' => false,
				'expertMode' => true));
		}
	}

	/**
	 * Checks if easymode or expert mode is active.
	 *
	 * @param $array Array of the saved openingtimes.
	 * @return bool True if easy mode, false if expert mode
	 */
	private function isEasyMode($array)
	{
		if (count($array[0]) == 3) {
			if ($array[0][0]['days'] == array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
				&& $array[0][1]['days'] == array("Saturday") && $array[0][2]['days'] == array("Sunday")
			) {
				return true;
			} else {
				return false;
			}
		} elseif ($array[0] == 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Ajax the config of a location.
	 *
	 * @param $id Location ID
	 */
	private function ajaxConfig($id)
	{
		$array = array();

		// Get Config data from db
		$dbquery = Database::simpleQuery("SELECT config, serverid, serverroomid FROM `location_info` WHERE locationid = :id", array('id' => $id));
		$serverid;
		$serverroomid;
		while ($dbdata = $dbquery->fetch(PDO::FETCH_ASSOC)) {
			$array = json_decode($dbdata['config'], true);
			$serverid = $dbdata['serverid'];
			$serverroomid = $dbdata['serverroomid'];
		}

		// get Server / ID list
		$dbq = Database::simpleQuery("SELECT serverid, servername FROM `setting_location_info`");
		$serverList = array();
		while ($dbd = $dbq->fetch(PDO::FETCH_ASSOC)) {
			$d['sid'] = $dbd['serverid'];
			$d['sname'] = $dbd['servername'];
			$serverList[] = $d;
		}

		echo Render::parse('config', array(
			'id' => $id,
			'language' => $array['language'],
			'mode' => 'mode' . $array['mode'],
			'vertical' => $array['vertical'],
			'eco' => $array['eco'],
			'daystoshow' => 'day' . $array['daystoshow'],
			'scaledaysauto' => $array['scaledaysauto'],
			'rotation' => 'rotation' . $array['rotation'],
			'scale' => $array['scale'],
			'switchtime' => $array['switchtime'],
			'calupdate' => $array['calupdate'],
			'roomupdate' => $array['roomupdate'],
			'configupdate' => $array['configupdate'],
			'serverlist' => array_values($serverList),
			'serverid' => $serverid,
			'serverroomid' => $serverroomid
		));
	}

}