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
This commit is contained in:
parent
1ccc26ebd4
commit
023384149f
3 changed files with 59 additions and 6 deletions
13
DBupdate.php
13
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<br>") : print_r("failed<br>");
|
||||
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<br>"):print_r("failed<br>");
|
||||
echo "cleaning, the table from deleted image favorites...<br>";
|
||||
$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<br>"):print_r("failed<br>");
|
||||
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<br>"):print_r("failed<br>");
|
||||
echo "DONE!!!!";
|
||||
?>
|
|
@ -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"));
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
|
|
@ -180,11 +180,15 @@ class UserPageTheme extends Themelet {
|
|||
Admin: <input name='admin' type='checkbox'$h_is_admin>
|
||||
<input type='submit' value='Set'>
|
||||
</form>
|
||||
";
|
||||
$html .="
|
||||
<p>".make_form(make_link("user_admin/delete_user"))."
|
||||
|
||||
".make_form(make_link("user_admin/delete_user"))."
|
||||
<input type='hidden' name='id' value='$i_user_id'>
|
||||
<input type='submit' value='Delete User' onclick='confirm(\"Delete the user?\");' />
|
||||
</form>
|
||||
|
||||
".make_form(make_link("user_admin/delete_user_with_images"))."
|
||||
<input type='hidden' name='id' value='$i_user_id'>
|
||||
<input type='submit' value='Delete User with images' onclick='confirm(\"Delete the user with his uploaded images?\");' />
|
||||
</form>";
|
||||
}
|
||||
return $html;
|
||||
|
|
Reference in a new issue