Delete a Page Action prototype

This actions works on databases that don't do foreign keys.

delete (.delete)

An action to delete the page that is currently being viewed. This action is limited to terminal nodes. i.e. if the page has any children it won't work. Start from a terminal node and work your way up the tree if you want.


sub delete : Global {
    my ( $self$c ) = @_;

    my $id = $c->stash->{page}->id;
    if ( $id == 1 ) {
        return $c->response->body(
"You are trying to delete the root node or delete a page twice.<br />
 Sorry, you can't do that."
        );
    }
    my @descendants = $c->stash->{page}->descendants;
    if ( scalar @descendants > 1 ) {
        return $c->response->body(
            "This page, id=$id, has children .<br />
 This action <strong>does not delete a page with children</strong>.<br />
 It's just too cruel.  ;)"
        );
    }

    my $page = $c->model('DBIC::Page')->find( { id => $id } );

    my $original_page_name = $page->name_orig;
    warn "Erasing page: $original_page_name\n";

    my $attachment_rs =
      $c->model('DBIC::Attachment')->search( { page => $id } )->delete_all;
    my $comment_rs =
      $c->model('DBIC::Comment')->search( { page => $id } )->delete_all;
    my $link_rs =
      $c->model('DBIC::Link')
      ->search( [ { from_page => $id }, { to_page => $id } ] )->delete_all;
    my $role_privilege_rs =
      $c->model('DBIC::RolePrivilege')->search( { page => $id } )->delete_all;
    my $tag_rs = $c->model('DBIC::Tag')->search( { page => $id } )->delete_all;
    my $wanted_page =
      $c->model('DBIC::WantedPage')->search( { from_page => $id } )->delete_all;
    my $journal_rs =
      $c->model('DBIC::Journal')->search( { pageid => $id } )->delete_all;
    my $entry_rs =
      $c->model('DBIC::Entry')->search( { journal => $id } )->delete_all;
    my $content_rs =
      $c->model('DBIC::Content')->search( { page => $id } )->delete_all;
          my $page_version_rs =
      $c->model('DBIC::PageVersion')->search( { page => $id } )->delete_all;
    my $page_rs = $c->model('DBIC::Page')->search( { id => $id } )->delete_all;
    return $c->response->redirect(
        $c->uri_for(
            '/',
            { page_deleted => "$original_page_name" }
        )
    );
    return $c->response->body(
"Just <strong>deleted page, $original_page_name</strong> of id <em>$id</em>\n"
    );
}

My tags:
 
Popular tags:
 
Powered by Catalyst