From 2f6471b0b1c039e653a8787e5faeaa13dabb2e46 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sat, 17 Oct 2015 12:13:22 +0200 Subject: Refactor pkgbase_set_comaintainers() This makes the code slightly more efficient and allows for easily determining the users that were added/removed to the co-maintainer list. Signed-off-by: Lukas Fleischer --- web/lib/pkgbasefuncs.inc.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'web') diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index cb756f6..bb9d241 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -1023,7 +1023,7 @@ function pkgbase_set_comaintainers($base_id, $users) { $dbh = DB::connect(); - $uids = array(); + $uids_new = array(); foreach($users as $user) { $q = "SELECT ID FROM Users "; $q .= "WHERE UserName = " . $dbh->quote($user); @@ -1034,18 +1034,32 @@ function pkgbase_set_comaintainers($base_id, $users) { return array(false, __("Invalid user name: %s", $user)); } - $uids[] = $uid; + $uids_new[] = $uid; } - $q = sprintf("DELETE FROM PackageComaintainers WHERE PackageBaseID = %d", $base_id); - $dbh->exec($q); + $q = sprintf("SELECT UsersID FROM PackageComaintainers WHERE PackageBaseID = %d", $base_id); + $result = $dbh->query($q); + $uids_old = $result->fetchAll(PDO::FETCH_COLUMN, 0); + + $uids_add = array_diff($uids_new, $uids_old); + $uids_rem = array_diff($uids_old, $uids_new); $i = 1; - foreach ($uids as $uid) { - $q = sprintf("INSERT INTO PackageComaintainers (PackageBaseID, UsersID, Priority) VALUES (%d, %d, %d)", $base_id, $uid, $i); + foreach ($uids_new as $uid) { + if (in_array($uid, $uids_add)) { + $q = sprintf("INSERT INTO PackageComaintainers (PackageBaseID, UsersID, Priority) VALUES (%d, %d, %d)", $base_id, $uid, $i); + } else { + $q = sprintf("UPDATE PackageComaintainers SET Priority = %d WHERE PackageBaseID = %d AND UsersID = %d", $i, $base_id, $uid); + } + $dbh->exec($q); $i++; } + foreach ($uids_rem as $uid) { + $q = sprintf("DELETE FROM PackageComaintainers WHERE PackageBaseID = %d AND UsersID = %d", $base_id, $uid); + $dbh->exec($q); + } + return array(true, __("The package base co-maintainers have been updated.")); } -- cgit v1.2.3-70-g09d2