summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/pages/cleanup.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2019-07-31 16:58:14 +0200
committerSimon Rettberg2019-07-31 16:58:14 +0200
commit7c539fd8736b0ff9acafe32d857b2a2021d778e6 (patch)
treeb6a44076cba8443b2840340256b9829451214229 /modules-available/locations/pages/cleanup.inc.php
parent[locations] Optimize some functions in Location class (diff)
downloadslx-admin-7c539fd8736b0ff9acafe32d857b2a2021d778e6.tar.gz
slx-admin-7c539fd8736b0ff9acafe32d857b2a2021d778e6.tar.xz
slx-admin-7c539fd8736b0ff9acafe32d857b2a2021d778e6.zip
[locations] Add warnings/cleanup for bad machine to roomplan mappings
Diffstat (limited to 'modules-available/locations/pages/cleanup.inc.php')
-rw-r--r--modules-available/locations/pages/cleanup.inc.php93
1 files changed, 93 insertions, 0 deletions
diff --git a/modules-available/locations/pages/cleanup.inc.php b/modules-available/locations/pages/cleanup.inc.php
new file mode 100644
index 00000000..d10dbac0
--- /dev/null
+++ b/modules-available/locations/pages/cleanup.inc.php
@@ -0,0 +1,93 @@
+<?php
+
+class SubPage
+{
+
+ public static function doPreprocess($action)
+ {
+ if ($action === 'resetmachines') {
+ self::resetMachines();
+ return true;
+ }
+ if ($action === 'movemachines') {
+ self::moveMachines();
+ return true;
+ }
+ return false;
+ }
+
+ public static function doRender($action)
+ {
+ $list = self::loadForLocation();
+ if ($list === false)
+ return true;
+ Permission::addGlobalTags($list['perms'], NULL, ['subnets.edit', 'location.view']);
+ Render::addTemplate('mismatch-cleanup', $list);
+ return true;
+ }
+
+ public static function doAjax($action)
+ {
+ 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;
+ }
+
+} \ No newline at end of file