summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2022-07-20 14:13:45 +0200
committerErich Eckner <git@eckner.net>2022-07-20 14:13:45 +0200
commit9c9a42d5ab08ef70a0a43b2f9f323f35e65b6771 (patch)
tree03dc3e424289b120eb3cca948db62aeea2e312fa
parent27def9d12e939794f5fc9e42375bb6340abd7bdc (diff)
downloadcolocation-9c9a42d5ab08ef70a0a43b2f9f323f35e65b6771.tar.xz
update.php: set the correct values
-rw-r--r--html/update.php49
1 files changed, 40 insertions, 9 deletions
diff --git a/html/update.php b/html/update.php
index 9e9d39b..39e8d64 100644
--- a/html/update.php
+++ b/html/update.php
@@ -13,24 +13,55 @@ if (!array_key_exists('machine', $_GET)) {
include "db.php";
$stm = $db -> prepare(
- 'SELECT permissions.machine_id, permissions.state_id' .
+ 'SELECT' .
+ ' upd_values.id as value_id,' .
+ ' IIF(upd_values.state_id <= "values".state_id, 1, 0) as downgrade,' .
+ ' IIF(upd_values.state_id >= "values".state_id, 1, 0) as upgrade,' .
+ ' IIF(upd_values.state_id = "values".state_id, 1, 0) as exact,' .
+ ' states.name as state' .
' FROM permissions' .
- ' JOIN machines ON permissions.machine_id = machines.id' .
- ' JOIN states ON permissions.state_id = states.id' .
+ ' JOIN "values" ON permissions.value_id = "values".id' .
+ ' JOIN "values" as upd_values ON upd_values.machine_id = "values".machine_id' .
+ ' JOIN machines ON "values".machine_id = machines.id' .
+ ' JOIN states ON "values".state_id = states.id' .
' JOIN keys ON permissions.key_id = keys.id' .
' WHERE machines.name = :machine' .
- ' AND states.name = :state' .
' AND keys.key = :key'
);
$stm -> bindValue('key', $_GET['key']);
$stm -> bindValue('machine', $_GET['machine']);
+$result = $stm -> execute();
-foreach ($_GET as $state => $value) {
- if (($state == 'key') || ($state == 'machine'))
+$updstm = $db -> prepare(
+ 'UPDATE "values"' .
+ ' SET value = :value' .
+ ' WHERE "values".id = :value_id' .
+ ' AND ("values".value != :global_value OR "values".value IS NULL)'
+);
+
+while ($row = $result -> fetchArray()) {
+ if (! array_key_exists($row['state'], $_GET))
continue;
- $stm -> bindValue('state', $state);
- $stm -> bindValue('value', $value);
- $stm -> execute();
+ $value = $_GET[$row['state']];
+ if (($value != 0) && ($value != 1))
+ continue;
+ if ($value == 0) {
+ if ($row['downgrade'] != 1)
+ continue;
+ }
+ if ($value == 1) {
+ if ($row['upgrade'] != 1)
+ continue;
+ }
+ if ($row['exact'] == 1)
+ $updstm -> bindValue('value', $value);
+ else
+ $updstm -> bindValue('value', NULL);
+ $updstm -> bindValue('global_value', $value);
+ $updstm -> bindValue('value_id', $row['value_id']);
+ $updstm -> execute();
}
+$stm -> close();
+
echo 'ok' . "\n";