From 023384149ff1965eac297548e7a1024ebaec3f4f Mon Sep 17 00:00:00 2001 From: NaGeL Date: Sun, 22 Jan 2012 18:55:52 +0100 Subject: [PATCH] User deletion (Deletes the user with comments, favorites and private messages) User deletion with uploaded images ( all above plus the images the user uploaded) Also the Database fix that is all needed for this in DBupdate.php --- DBupdate.php | 13 ++++++++++++- ext/user/main.php | 42 ++++++++++++++++++++++++++++++++++++++++-- ext/user/theme.php | 10 +++++++--- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/DBupdate.php b/DBupdate.php index 29a14b65..183aa044 100644 --- a/DBupdate.php +++ b/DBupdate.php @@ -5,6 +5,17 @@ include_once "config.php"; $db = new Database(); echo "Fixing user_favorites table...."; ($db->Execute("ALTER TABLE user_favorites ENGINE=InnoDB;")) ? print_r("ok
") : print_r("failed
"); -echo "adding Foreign key to users..."; +echo "adding Foreign key to user ids..."; ($db->Execute("ALTER TABLE user_favorites ADD FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;"))? print_r("ok
"):print_r("failed
"); +echo "cleaning, the table from deleted image favorites...
"; +$rows = $db->get_all("SELECT * FROM user_favorites WHERE image_id NOT IN ( SELECT id FROM images );"); +foreach( $rows as $key => $value) + $db->Execute("DELETE FROM user_favorites WHERE image_id = :image_id;", array("image_id" => $value["image_id"])); +echo "adding forign key to image ids..."; +($db->Execute("ALTER TABLE user_favorites ADD FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE;"))? print_r("ok
"):print_r("failed
"); +echo "adding foreign keys to private messages..."; +($db->Execute("ALTER TABLE private_message +ADD FOREIGN KEY (from_id) REFERENCES users(id) ON DELETE CASCADE, +ADD FOREIGN KEY (to_id) REFERENCES users(id) ON DELETE CASCADE;")) ? print_r("ok
"):print_r("failed
"); +echo "DONE!!!!"; ?> \ No newline at end of file diff --git a/ext/user/main.php b/ext/user/main.php index 0f1e8027..4e2620c0 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -147,6 +147,9 @@ class UserPage extends SimpleExtension { else if($event->get_arg(0) == "delete_user") { $this->delete_user($page); } + else if($event->get_arg(0) == "delete_user_with_images") { + $this->delete_user_with_images($page); + } } if(($event instanceof PageRequestEvent) && $event->page_matches("user")) { @@ -487,9 +490,44 @@ class UserPage extends SimpleExtension { $database->execute("DELETE FROM users WHERE id = :id" , array("id"=>$_POST['id'])); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/list")); + } + } + + private function delete_user_with_images($page) { + global $user; + global $config; + global $database; + + $page->set_title("Error"); + $page->set_heading("Error"); + $page->add_block(new NavBlock()); + + if (!$user->is_admin()) { + $page->add_block(new Block("Not Admin", "Only admins can delete accounts")); + } + else if(!isset($_POST['id']) || !is_numeric($_POST['id'])) { + $page->add_block(new Block("No ID Specified", + "You need to specify the account number to edit")); + } + else{ + $rows = $database->get_all("SELECT * FROM images WHERE owner_id = :owner_id", array("owner_id" => $_POST['id'])); + foreach ($rows as $key => $value) + { + $image = Image::by_id($value['id']); + if($image) { + send_event(new ImageDeletionEvent($image)); + } + } + $database->execute("DELETE FROM users + WHERE id = :id" + , array("id"=>$_POST['id'])); + + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/list")); } - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/list")); } // }}} diff --git a/ext/user/theme.php b/ext/user/theme.php index 424099f4..304ff541 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -180,11 +180,15 @@ class UserPageTheme extends Themelet { Admin: - "; - $html .=" -

".make_form(make_link("user_admin/delete_user"))." + + ".make_form(make_link("user_admin/delete_user"))." + + + ".make_form(make_link("user_admin/delete_user_with_images"))." + + "; } return $html;