diff options
author | Simon Rettberg | 2019-04-03 18:10:28 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-04-03 18:11:43 +0200 |
commit | 6e89e15c7844afe054ca9c214e200a44e23e1a78 (patch) | |
tree | c38ad51c578cafa71c52bd6efae346495cccaa49 /modules-available/locations/inc | |
parent | [locationinfo] Style checkboxes, use icon in save button (diff) | |
download | slx-admin-6e89e15c7844afe054ca9c214e200a44e23e1a78.tar.gz slx-admin-6e89e15c7844afe054ca9c214e200a44e23e1a78.tar.xz slx-admin-6e89e15c7844afe054ca9c214e200a44e23e1a78.zip |
[statistics/locations] Add locationid constraints; update on delete
We didn't update subnetlocationid when deleting a location,
leading to machines pointing to invalid locations.
Diffstat (limited to 'modules-available/locations/inc')
-rw-r--r-- | modules-available/locations/inc/autolocation.inc.php | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/modules-available/locations/inc/autolocation.inc.php b/modules-available/locations/inc/autolocation.inc.php index cccfe9b1..61507ebf 100644 --- a/modules-available/locations/inc/autolocation.inc.php +++ b/modules-available/locations/inc/autolocation.inc.php @@ -3,11 +3,23 @@ class AutoLocation { - public static function rebuildAll() + /** + * Rebuild the assigned subnetlocationid for clients that currently map to + * the given locationids (if given), or all clients, if locationid is false + * + * @param int[]|false $locations Locations to rebuild, or false for everything + */ + public static function rebuildAll($locations = false) { if (Module::get('statistics') === false) return; // Nothing to do - $res = Database::simpleQuery("SELECT machineuuid, clientip FROM machine"); + if ($locations === false) { + // All + $res = Database::simpleQuery("SELECT machineuuid, clientip FROM machine"); + } else { + $res = Database::simpleQuery("SELECT machineuuid, clientip FROM machine + WHERE fixedlocationid IN(:lid) OR subnetlocationid IN(:lid)", ['lid' => $locations]); + } $updates = array(); $nulls = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { @@ -22,13 +34,14 @@ class AutoLocation } } if (!empty($nulls)) { - $qs = '?' . str_repeat(',?', count($nulls) - 1); - Database::exec("UPDATE machine SET subnetlocationid = NULL WHERE machineuuid IN ($qs)", $nulls); + Database::exec("UPDATE machine SET subnetlocationid = NULL WHERE machineuuid IN (:nulls)", + ['nulls' => $nulls]); } foreach ($updates as $lid => $machines) { - $qs = '?' . str_repeat(',?', count($machines) - 1); - $lid = (int)$lid; - Database::exec("UPDATE machine SET subnetlocationid = $lid WHERE machineuuid IN ($qs)", $machines); + if (empty($machines)) + continue; + Database::exec("UPDATE machine SET subnetlocationid = :lid WHERE machineuuid IN (:machines)", + ['lid' => $lid, 'machines' => $machines]); } } |