Skip to content

Commit

Permalink
Change the item rest update processing to call the item::move(source,…
Browse files Browse the repository at this point in the history
… target) helper when the parent member has changed. Using the move method insures that names and slugs that could conflict in the target album are resolved properly. Changed the item::move method so it returns a message to be displayed if the caller chooses. And changed the move controller to display the message returned by the move if the item name was renamed as part of the move.
  • Loading branch information
Tim Almdal committed Jun 15, 2010
1 parent 207f6be commit 2492280
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
10 changes: 4 additions & 6 deletions modules/gallery/controllers/move.php
Expand Up @@ -34,12 +34,10 @@ public function save($source_id) {
$source = ORM::factory("item", $source_id);
$target = ORM::factory("item", Input::instance()->post("target_id"));

access::required("view", $source);
access::required("edit", $source);
access::required("view", $target);
access::required("edit", $target);

item::move($source, $target);
$message = item::move($source, $target);
if (!empty($message)) {
message.info($message);
}

print json_encode(
array("result" => "success",
Expand Down
15 changes: 9 additions & 6 deletions modules/gallery/helpers/item.php
Expand Up @@ -47,27 +47,28 @@ static function move($source, $target) {
$orig_name_filename = pathinfo($source->name, PATHINFO_FILENAME);
$orig_name_extension = pathinfo($source->name, PATHINFO_EXTENSION);
$orig_slug = $source->slug;
$message = "";
for ($i = 0; $i < 5; $i++) {
try {
$source->save();
if ($orig_name != $source->name) {
switch ($source->type) {
case "album":
message::info(
$message =
t("Album <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict",
array("old_name" => $orig_name, "new_name" => $source->name)));
array("old_name" => $orig_name, "new_name" => $source->name));
break;

case "photo":
message::info(
$message =
t("Photo <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict",
array("old_name" => $orig_name, "new_name" => $source->name)));
array("old_name" => $orig_name, "new_name" => $source->name));
break;

case "movie":
message::info(
$message =
t("Movie <b>%old_name</b> renamed to <b>%new_name</b> to avoid a conflict",
array("old_name" => $orig_name, "new_name" => $source->name)));
array("old_name" => $orig_name, "new_name" => $source->name));
break;
}
}
Expand Down Expand Up @@ -95,6 +96,8 @@ static function move($source, $target) {
if ($target->album_cover_item_id == null) {
item::make_album_cover($source);
}

return $message;
}

static function make_album_cover($item) {
Expand Down
16 changes: 9 additions & 7 deletions modules/gallery/helpers/item_rest.php
Expand Up @@ -99,7 +99,7 @@ static function put($request) {
if ($entity = $request->params->entity) {
// Only change fields from a whitelist.
foreach (array("album_cover", "captured", "description",
"height", "mime_type", "name", "parent", "rand_key", "resize_dirty",
"height", "mime_type", "name", "rand_key", "resize_dirty",
"resize_height", "resize_width", "slug", "sort_column", "sort_order",
"thumb_dirty", "thumb_height", "thumb_width", "title", "view_count",
"width") as $key) {
Expand All @@ -113,20 +113,22 @@ static function put($request) {
break;

case "parent":
if (property_exists($entity, "parent")) {
$parent = rest::resolve($entity->parent);
access::required("edit", $parent);
$item->parent_id = $parent->id;
}
break;
default:
if (property_exists($entity, $key)) {
$item->$key = $entity->$key;
}
}
}

// There is an explicit save in item::move
if (property_exists($entity, "parent")) {
$parent = rest::resolve($entity->parent);
item::move($item, $parent);
} else {
$item->save();
}
}
$item->save();

if (isset($request->params->members) && $item->sort_column == "weight") {
$weight = 0;
Expand Down

0 comments on commit 2492280

Please sign in to comment.