Skip to content

Commit

Permalink
When we delete an item, make sure that we scrub it from any other
Browse files Browse the repository at this point in the history
items that may have it in the album_cover_item_id column.  Fixes ticket #1172.
  • Loading branch information
bharat committed Jul 3, 2010
1 parent 3feb561 commit 0d424a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions modules/gallery/helpers/gallery_event.php
Expand Up @@ -98,6 +98,15 @@ static function item_created($item) {
static function item_deleted($item) {
access::delete_item($item);

// Find any other albums that had the deleted item as the album cover and null it out.
// In some cases this may leave us with a missing album cover up in this item's parent
// hierarchy, but in most cases it'll work out fine.
foreach (ORM::factory("item")
->where("album_cover_item_id", "=", $item->id)
->find_all() as $parent) {
item::remove_album_cover($parent);
}

$parent = $item->parent();
if (!$parent->album_cover_item_id) {
// Assume we deleted the album cover and pick a new one. Choosing the first photo in the
Expand Down
7 changes: 5 additions & 2 deletions modules/gallery/tests/Item_Helper_Test.php
Expand Up @@ -111,15 +111,18 @@ public function move_conflicts_result_in_a_rename_test() {
$this->assert_not_same($rand, $photo2->slug);
}

public function delete_cover_photo_picks_new_album_cover() {
$album = test::random_album();
public function delete_cover_photo_picks_new_album_cover_test() {
$parent = test::random_album();
$album = test::random_album($parent);
$photo1 = test::random_photo($album);
// At this point, $photo1 is the album cover. We verify this in
// Item_Model_Test::first_photo_becomes_album_cover
$photo2 = test::random_photo($album);
$photo1->delete();
$album->reload();
$parent->reload();

$this->assert_same($photo2->id, $album->album_cover_item_id);
$this->assert_same($photo2->id, $parent->album_cover_item_id);
}
}

0 comments on commit 0d424a6

Please sign in to comment.