Preventing album deletion from within item_before_delete event

spags

Joined: 2010-03-26
Posts: 120
Posted: Sun, 2012-11-11 08:14

I'm trying to work on a module to move photos from one particular album (the "Import" album) into other albums based on their dates. Obviously I'd like to try and prevent the user from deleting the Import album to make the code robust. Unfortunately I'm not sure how to make Gallery abort a pending deletion. The below sample code demonstrates what I want to do:

static function item_before_delete($itembd) {
  /* Catch deletion of the Import album */
  $import_album = module::get_var("calendar_auto_structure","import_location");
  if ($itembd->is_album()) {
    if ($itembd->id==$import_album) {
      // Do something here to abort deletion.
      
      // Display a warning
      message::error(t("Cannot delete album because it is being used for import."));
    }
  }
}

I'm just not sure what code I need to do to tell Gallery to abort the deletion. I can't seem to find any other similar examples of this situation in other modules either. Can somebody please point me in the right direction?

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Sun, 2012-11-11 17:30

// Do something here to abort deletion.
you could just return to the page the user is on I guess. Is that what you want to do?
url::redirect($item->url());

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
spags

Joined: 2010-03-26
Posts: 120
Posted: Mon, 2012-11-12 10:46

I didn't think of that but unfortunately it doesn't work because Gallery shows the Confirm Delete form/dialog beforehand. As a result, the redirect takes place in that dialog box. Since my post, I've found the following interesting snippets.

In the Gallery module within ./models/item.php I noticed in function "delete" there is code regarding the deletion of the root album:

if ($this->id == 1) {
  $v = new Validation(array("id"));
  $v->add_error("id", "cant_delete_root_album");
  ORM_Validation_Exception::handle_validation($this->table_name, $v);
}

I feel this is a step closer to the desired outcome, but again it doesn't really work cleanly (that dialog seems to get in the way, and probably because I don't really understand what the code is doing).

Also in ./helpers/item.php there is a function called get_delete_form, which actually returns the code to display the Confirm Delete form.

I may just have to keep hunting around in the code.

 
spags

Joined: 2010-03-26
Posts: 120
Posted: Wed, 2012-11-14 11:44

I can't see any obvious way around this and I'm not familiar enough with the framework or Gallery to deal with the problem nicely. An ugly solution is something like:

message::error(t("Album in use and cannot be deleted"));
throw new Exception("@todo " . t("Album in use and cannot be deleted")");
 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Wed, 2012-11-14 20:22

I can't think of another way.

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team