Skip to content

Commit

Permalink
Fix for ticket #1118. Create a item::save_with_retries helper method,…
Browse files Browse the repository at this point in the history
… which encapsulates saving an item and handling name and slug conflicts. Call this instead of doing a save directly.
  • Loading branch information
Tim Almdal committed Jun 15, 2010
1 parent 2492280 commit 9504f71
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
10 changes: 7 additions & 3 deletions modules/gallery/controllers/simple_uploader.php
Expand Up @@ -65,12 +65,16 @@ public function add_photo($id) {
if (array_key_exists("extension", $path_info) &&
in_array(strtolower($path_info["extension"]), array("flv", "mp4"))) {
$item->type = "movie";
$item->save();
} else {
$item->type = "photo";
}

item::save_with_retries($item);

if ($item->type == "movie") {
log::success("content", t("Added a movie"),
html::anchor("movies/$item->id", t("view movie")));
} else {
$item->type = "photo";
$item->save();
log::success("content", t("Added a photo"),
html::anchor("photos/$item->id", t("view photo")));
}
Expand Down
17 changes: 11 additions & 6 deletions modules/gallery/helpers/item.php
Expand Up @@ -43,6 +43,17 @@ static function move($source, $target) {

// Moving may result in name or slug conflicts. If that happens, try up to 5 times to pick a
// random name (or slug) to avoid the conflict.
$message = item::save_with_retries($source);

// If the target has no cover item, make this it.
if ($target->album_cover_item_id == null) {
item::make_album_cover($source);
}

return $message;
}

static function save_with_retries($source, $retries=5) {
$orig_name = $source->name;
$orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME);
$orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION);
Expand Down Expand Up @@ -91,12 +102,6 @@ static function move($source, $target) {
}
}
}

// If the target has no cover item, make this it.
if ($target->album_cover_item_id == null) {
item::make_album_cover($source);
}

return $message;
}

Expand Down
8 changes: 3 additions & 5 deletions modules/gallery/helpers/item_rest.php
Expand Up @@ -112,8 +112,6 @@ static function put($request) {
}
break;

case "parent":
break;
default:
if (property_exists($entity, $key)) {
$item->$key = $entity->$key;
Expand All @@ -126,7 +124,7 @@ static function put($request) {
$parent = rest::resolve($entity->parent);
item::move($item, $parent);
} else {
$item->save();
$item::save_with_retries($item);
}
}

Expand Down Expand Up @@ -157,7 +155,7 @@ static function post($request) {
$item->title = isset($entity->title) ? $entity->title : $entity->name;
$item->description = isset($entity->description) ? $entity->description : null;
$item->slug = isset($entity->slug) ? $entity->slug : null;
$item->save();
$item::save_with_retries($item);
break;

case "photo":
Expand All @@ -172,7 +170,7 @@ static function post($request) {
$item->title = isset($entity->title) ? $entity->title : $entity->name;
$item->description = isset($entity->description) ? $entity->description : null;
$item->slug = isset($entity->slug) ? $entity->slug : null;
$item->save();
$item::save_with_retries($item);
break;

default:
Expand Down

0 comments on commit 9504f71

Please sign in to comment.