Permalink
Browse files

Changes to support updating the child elements within an album. In th…

…is change the urls of the children are sent up asan array of post fields children[0].... children[n]. If an existing child is not included it is deleted. Including a url to an child in another album will move the child. Changing the order of the children will respect the order of the children, if the sort column is 'weight'
  • Loading branch information...
1 parent 9314533 commit 5467e21e8b9941a2b64aa093c0cf0f591ef5ca82 @talmdal talmdal committed Mar 13, 2010
Showing with 79 additions and 13 deletions.
  1. +67 −11 modules/gallery/helpers/item_rest.php
  2. +11 −1 modules/gallery/helpers/items_rest.php
  3. +1 −1 modules/rest/controllers/rest.php
@@ -88,35 +88,91 @@ static function put($request) {
$params = $request->params;
+ $sort_order_changed_to_weight = false;
+ // Start the batch
+ batch::start();
+
// Only change fields from a whitelist.
foreach (array("album_cover", "captured", "description",
"height", "mime_type", "name", "parent", "rand_key", "resize_dirty",
"resize_height", "resize_width", "slug", "sort_column", "sort_order",
"thumb_dirty", "thumb_height", "thumb_width", "title", "view_count",
"weight", "width") as $key) {
- switch ($key) {
- case "album_cover":
- if (property_exists($request->params, "album_cover")) {
+ if (property_exists($request->params, $key)) {
+ switch ($key) {
+ case "album_cover":
$album_cover_item = rest::resolve($request->params->album_cover);
access::required("view", $album_cover_item);
$item->album_cover_item_id = $album_cover_item->id;
- }
- break;
-
- case "parent":
- if (property_exists($request->params, "parent")) {
+ break;
+
+ case "sort_column":
+ if ($request->params->sort_column == "weight" && $item->sort_column != "weight") {
+ $sort_order_changed_to_weight = true;
+ $item->sort_column = "weight";
+ }
+ break;
+ case "parent":
$parent = rest::resolve($request->params->parent);
access::required("edit", $parent);
$item->parent_id = $parent->id;
- }
break;
- default:
- if (property_exists($request->params, $key)) {
+ default:
$item->$key = $request->params->$key;
}
}
}
$item->save();
+
+ // If children are supplied, then update the children based on that client tells us.
+ // if the sort order changed, then update the weights if there are no children to be updated
+ if (property_exists($request->params, "children")) {
+ // Map the existing children by their restful urls
+ $children = array();
+ foreach ($item->children() as $child) {
+ $children[rest::url("item", $child)] = $child;
+ }
+ $update_weight = $item->sort_column == "weight";
+ $weight = $item->sort_order == "ASC" ? -1 : $request->params->url->length;
+ $weight_increment = $item->sort_order == "ASC" ? 1 : -1;
+
+ foreach($request->params->children as $url) {
+ if (isset($children[$url])) {
+ $child = $children[$url];
+ unset($children[$url]);
+ } else {
+ $child = rest::resolve($url);
+ $child->parent_id = $item->id;
+ }
+ $child->save();
+ if ($update_weight) {
+ $weight += $weight_increment;
+ db::build()
+ ->update("items")
+ ->set("weight", $weight)
+ ->where("id", "=", $child->id)
+ ->execute();
+ }
+ }
+ // Anything left in the mapping needs to be deleted
+ foreach ($children as $child) {
+ $child->delete();
+ }
+ } else if ($sort_order_changed_to_weight) {
+ $weight = $item->sort_order == "ASC" ? -1 : $request->params->url->length;
+ $weight_increment = $item->sort_order == "ASC" ? 1 : -1;
+ foreach ($item->children() as $child) {
+ // Do this directly in the database to avoid sending notifications
+ $weight += $weight_increment;
+ db::build()
+ ->update("items")
+ ->set("weight", $weight)
+ ->where("id", "=", $child->id)
+ ->execute();
+ }
+ }
+
+ batch::stop();
}
static function post($request) {
@@ -19,10 +19,12 @@
*/
class items_rest_Core {
static function get($request) {
+ $parent = rest::resolve($request->url);
+ access::required("view", $parent);
$items = array();
if (isset($request->params->url)) {
- foreach($request->params->url as $url) {
+ foreach ($request->params->url as $url) {
$item = rest::resolve($url);
if (access::can("view", $item)) {
$members = array();
@@ -41,4 +43,12 @@ static function get($request) {
return $items;
}
+
+ static function resolve($id) {
+ $item = ORM::factory("item", $id);
+ if (!access::can("view", $item)) {
+ throw new Kohana_404_Exception();
+ }
+ return $item;
+ }
}
@@ -46,7 +46,7 @@ public function __call($function, $args) {
$request->params = (object) $input->get();
break;
- case "post":
+ default:
$request->params = (object) $input->post();
if (isset($_FILES["file"])) {
$request->file = upload::save("file");

0 comments on commit 5467e21

Please sign in to comment.