From d7179dfb288196e1c24ac41a4366ef0b3cad03b3 Mon Sep 17 00:00:00 2001
From: Robert Anderson <randerson@lbsd.net>
Date: Thu, 16 Jul 2009 10:59:32 +0000
Subject: [PATCH] Remove topups belonging to user when deleting

---
 webgui/include/ajax/functions/AdminUsers.php | 45 +++++++++++++++++++-
 webgui/include/ajax/functions/WiSPUsers.php  | 43 +++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/webgui/include/ajax/functions/AdminUsers.php b/webgui/include/ajax/functions/AdminUsers.php
index 29496488..97563bf7 100644
--- a/webgui/include/ajax/functions/AdminUsers.php
+++ b/webgui/include/ajax/functions/AdminUsers.php
@@ -82,7 +82,50 @@ function removeAdminUser($params) {
 	if ($res !== FALSE) {
 		$res = DBDo("DELETE FROM users_to_groups WHERE UserID = ?",array($params[0]));
 	}
-	
+
+	# Get list of topups and delete summaries
+	if ($res !== FALSE) {
+		$topupList = array();
+		$res = DBSelect("
+			SELECT
+				topups_summary.TopupID
+			FROM
+				topups_summary, topups
+			WHERE
+				topups_summary.TopupID = topups.ID
+				AND topups.UserID = ?",
+				array($params[0])
+		);
+
+		if (!is_object($res)) {
+			$res = FALSE;
+		} else {
+			while ($row = $res->fetchObject()) {
+				array_push($topupList,$row->topupid);
+			}
+		}
+
+		if ($res !== FALSE && sizeof($topupList) > 0) {
+			# Remove topup summaries
+			foreach ($topupList as $id) {
+				if ($res !== FALSE) {
+					$res = DBDo("
+						DELETE FROM
+							topups_summary
+						WHERE
+							TopupID = ?",
+							array($id)
+					);
+				}
+			}
+		}
+	}
+
+	# Remove topups
+	if ($res !== FALSE) {
+		$res = DBDo("DELETE FROM topups WHERE UserID = ?",array($params[0]));
+	}
+
 	# Delete user
 	if ($res !== FALSE) {
 		$res = DBDo("DELETE FROM users WHERE ID = ?",array($params[0]));
diff --git a/webgui/include/ajax/functions/WiSPUsers.php b/webgui/include/ajax/functions/WiSPUsers.php
index 51d5bec8..91c42ca8 100644
--- a/webgui/include/ajax/functions/WiSPUsers.php
+++ b/webgui/include/ajax/functions/WiSPUsers.php
@@ -181,6 +181,49 @@ function removeWiSPUser($params) {
 		$res = DBDo("DELETE FROM users_to_groups WHERE UserID = ?",array($params[0]));
 	}
 	
+	# Get list of topups and delete summaries
+	if ($res !== FALSE) {
+		$topupList = array();
+		$res = DBSelect("
+			SELECT
+				topups_summary.TopupID
+			FROM
+				topups_summary, topups
+			WHERE
+				topups_summary.TopupID = topups.ID
+				AND topups.UserID = ?",
+				array($params[0])
+		);
+
+		if (!is_object($res)) {
+			$res = FALSE;
+		} else {
+			while ($row = $res->fetchObject()) {
+				array_push($topupList,$row->topupid);
+			}
+		}
+
+		if ($res !== FALSE && sizeof($topupList) > 0) {
+			# Remove topup summaries
+			foreach ($topupList as $id) {
+				if ($res !== FALSE) {
+					$res = DBDo("
+						DELETE FROM
+							topups_summary
+						WHERE
+							TopupID = ?",
+							array($id)
+					);
+				}
+			}
+		}
+	}
+
+	# Remove topups
+	if ($res !== FALSE) {
+		$res = DBDo("DELETE FROM topups WHERE UserID = ?",array($params[0]));
+	}
+
 	# Delete user
 	if ($res !== FALSE) {
 		$res = DBDo("DELETE FROM users WHERE ID = ?",array($params[0]));
-- 
GitLab