From 62d926e022aaf35be029f20568e55533caee3bf7 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sat, 7 Jul 2018 13:26:02 +0000 Subject: Added up/down links to reorder entries. --- www/add.php | 10 ++++- www/get.php | 3 +- www/reorder.php | 75 ++++++++++++++++++++++++++++++++ www/scripts/main.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 www/reorder.php (limited to 'www') diff --git a/www/add.php b/www/add.php index 7b9d99f..e5c1a18 100644 --- a/www/add.php +++ b/www/add.php @@ -34,7 +34,7 @@ try $cmd = $db->prepare("INSERT INTO pm_store " . "(description, groupname, username, password) ". - "values " . + "VALUES " . "(:description, :group, :username, :password)"); $cmd->bindParam(":description", $description); $cmd->bindParam(":group", $group); @@ -42,7 +42,13 @@ try $cmd->bindParam(":password", $password); $cmd->execute(); - $result["id"] = $db->lastInsertId(); + $id = $db->lastInsertId(); + + $result["id"] = $id; + + $update = $db->prepare("UPDATE pm_store SET display = :id WHERE id = :id"); + $update->bindParam(":id", $id); + $update->execute(); $db = null; } diff --git a/www/get.php b/www/get.php index d42e57e..c41fafc 100644 --- a/www/get.php +++ b/www/get.php @@ -29,7 +29,7 @@ try $index = 0; - foreach ($db->query("SELECT * FROM pm_store ORDER BY id") as $row) + foreach ($db->query("SELECT * FROM pm_store ORDER BY display") as $row) { $data = array(); $data["id"] = $row["id"]; @@ -37,6 +37,7 @@ try $data["group"] = $row["groupname"]; $data["username"] = $row["username"]; $data["password"] = $row["password"]; + $data["display"] = $row["display"]; $result[$index++] = $data; } diff --git a/www/reorder.php b/www/reorder.php new file mode 100644 index 0000000..375ee62 --- /dev/null +++ b/www/reorder.php @@ -0,0 +1,75 @@ +. +*/ + +require 'config.php'; +require 'common.php'; + +$result = array(); + +try +{ + $db = ConnectDB(); + + $from_id = $_REQUEST["from_id"]; + $to_id = $_REQUEST["to_id"]; + $from_display = 0; + $to_display = 0; + + $cmd = $db->prepare("SELECT display FROM pm_store ". + "WHERE id = :id"); + $cmd->bindParam(":id", $from_id); + $cmd->execute(); + + $data = $cmd->fetch(); + + $from_display = $data["display"]; + + $cmd->bindParam(":id", $to_id); + $cmd->execute(); + + $data = $cmd->fetch(); + + $to_display = $data["display"]; + + $from_update = $db->prepare("UPDATE pm_store SET display = :to_display " . + "WHERE id = :from_id"); + $from_update->bindParam(":to_display", $to_display); + $from_update->bindParam(":from_id", $from_id); + $from_update->execute(); + + $to_update = $db->prepare("UPDATE pm_store SET display = :from_display " . + "WHERE id = :to_id"); + $to_update->bindParam(":from_display", $from_display); + $to_update->bindParam(":to_id", $to_id); + $to_update->execute(); + + $result["status"] = true; + + $db = null; +} +catch(PDOException $e) +{ + $result["error"] = "DB error:" . $e->getMessage(); +} + +header("Content-type:application/json"); +echo json_encode($result); + +?> diff --git a/www/scripts/main.js b/www/scripts/main.js index 0ed2cf5..79fb0b7 100644 --- a/www/scripts/main.js +++ b/www/scripts/main.js @@ -170,6 +170,21 @@ function AddSmallLink(node, text, func, id) node.appendChild(anchor); } +function AddSmallLinkPrevNext(node, text, func, id, prev, next) +{ + var anchor = document.createElement("a"); + + anchor.href = "#"; + anchor.onclick = func; + anchor.text = text; + anchor.className = "smalltext"; + anchor.dataset.pmId = id; + anchor.dataset.pmPrevId = prev; + anchor.dataset.pmNextId = next; + + node.appendChild(anchor); +} + function AddSmallLinkUpDown(node, text, down_func, up_func, id) { var anchor = document.createElement("a"); @@ -197,7 +212,7 @@ function AddElement(node, elementname) node.appendChild(elem); } -function AddRow(table, id, desc, username, password, rowcount) +function AddRow(table, id, prev_id, next_id, desc, username, password, rowcount) { var tr = document.createElement("tr"); @@ -232,7 +247,26 @@ function AddRow(table, id, desc, username, password, rowcount) AddText(td, "\u00a0"); AddText(td, "\u00a0"); AddSmallLinkUpDown(td, "Show", DoShow, DoHide, id); + + if (next_id != -1 || prev_id != -1) + { + if (prev_id != -1) + { + AddText(td, "\u00a0"); + AddText(td, "\u00a0"); + AddSmallLinkPrevNext(td, "Up", DoUp, id, prev_id, next_id); + } + + if (next_id != -1) + { + AddText(td, "\u00a0"); + AddText(td, "\u00a0"); + AddSmallLinkPrevNext(td, "Down", DoDown, id, prev_id, next_id); + } + } + AddElement(td, "br"); + AddSmallLink(td, "Copy Username", DoCopyUsername, id); AddText(td, "\u00a0"); AddText(td, "\u00a0"); @@ -244,6 +278,21 @@ function AddRow(table, id, desc, username, password, rowcount) table.appendChild(tr); } +function GetGroupRows() +{ + var result = []; + + for(var f = 0; f < globalDb.length; f++) + { + if (globalDb[f].group == globalCurrentGroup) + { + result.push(globalDb[f]); + } + } + + return result; +} + function LoadTable() { var table = document.getElementById("DataTable"); @@ -252,20 +301,34 @@ function LoadTable() var rowcount = 0; - for(var f = 0; f < globalDb.length; f++) + var rows = GetGroupRows(); + + for(var f = 0; f < rows.length; f++) { - if (globalDb[f].group == globalCurrentGroup) + var prevId = -1; + var nextId = -1; + + if (f > 0) { - AddRow(table, - globalDb[f].id, - globalDb[f].description, - globalDb[f].username, - Decrypt(globalDb[f].password), - rowcount++); + prevId = rows[f-1].id; } + + if (f < rows.length - 1) + { + nextId = rows[f+1].id; + } + + AddRow(table, + rows[f].id, + prevId, + nextId, + rows[f].description, + rows[f].username, + Decrypt(rows[f].password), + rowcount++); } - AddRow(table, -1, "", "", "", rowcount++); + AddRow(table, -1, -1, -1, "", "", "", rowcount++); } function RefreshDataAsync() @@ -466,4 +529,44 @@ function DoHide() elem.type = "password"; } +function DoReorderAsync() +{ + var response = JSON.parse(this.responseText); + + if (response.error) + { + Error(response.error); + } + else + { + RefreshData(); + } +} + +function DoUp() +{ + var id = this.dataset.pmId; + var prev = this.dataset.pmPrevId; + + WebRequest("reorder.php", DoReorderAsync, + [ + "from_id", id, + "to_id", prev + ] + ); +} + +function DoDown() +{ + var id = this.dataset.pmId; + var next = this.dataset.pmNextId; + + WebRequest("reorder.php", DoReorderAsync, + [ + "from_id", id, + "to_id", next + ] + ); +} + // vim: sw=4 ts=4 -- cgit v1.2.3