summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/pages/cleanup.inc.php
blob: 423d6a6bbae670b145d3966515d989cdbbf232dc (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
<?php

class SubPage
{

	public static function doPreprocess($action): bool
	{
		if ($action === 'resetmachines') {
			self::resetMachines();
			return true;
		}
		if ($action === 'movemachines') {
			self::moveMachines();
			return true;
		}
		return false;
	}

	public static function doRender($action): bool
	{
		$list = self::loadForLocation();
		if ($list === false)
			return true;
		$list['canmove'] = array_reduce($list['clients'], function (bool $carry, array $item): bool {
			return $carry || ($item['canmove'] ?? false);
		}, false);
		Permission::addGlobalTags($list['perms'], NULL, ['subnets.edit', 'location.view']);
		Render::addTemplate('mismatch-cleanup', $list);
		return true;
	}

	public static function doAjax($action): bool
	{
		return false;
	}

	private static function resetMachines()
	{
		$delete = self::getSelectedMachines(true);
		if ($delete === false)
			return;
		$num = Database::exec("UPDATE machine SET fixedlocationid = NULL, position = '' WHERE machineuuid IN (:machines)",
			['machines' => $delete]);
		Message::addSuccess('reset-n-machines', $num);
	}

	private static function moveMachines()
	{
		$move = self::getSelectedMachines(false);
		if ($move === false)
			return;
		// Move to subnet's location, or NULL if position field was empty (Which should never be the case)
		$num = Database::exec("UPDATE machine SET fixedlocationid = If(Length(position) > 0, subnetlocationid, NULL) WHERE machineuuid IN (:machines)",
			['machines' => $move]);
		Message::addSuccess('moved-n-machines', $num);
	}

	private static function getSelectedMachines($forDelete)
	{
		$list = self::loadForLocation();
		if ($list === false)
			return false;
		$machines = Request::post('machines', false, 'array');
		if ($machines === false) {
			Message::addError('main.parameter-missing', 'machines');
			return false;
		}
		$valid = array_map(function($item) use ($forDelete) {
			return $item['canmove'] || $forDelete ? $item['machineuuid'] : 'x';
		}, $list['clients']);
		$retList = array_filter($machines, function($item) use ($valid) {
			return in_array($item, $valid);
		});
		if (empty($retList)) {
			Message::addError('no-valid-machines-selected');
			return false;
		}
		return $retList;
	}

	private static function loadForLocation()
	{
		$locationid = Request::any('locationid', false, 'int');
		if ($locationid === false) {
			Message::addError('main.parameter-missing', 'locationid');
			return false;
		}
		$list = LocationUtil::getMachinesWithLocationMismatch($locationid, true);
		if (empty($list)) {
			Message::addInfo('no-mismatch-location');
			return false;
		}
		return $list;
	}

}