From bd28e04f84e40d981c3efcf2a3896f34cc0e41ba Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 4 Apr 2010 11:58:05 -0700 Subject: [PATCH] Updated for REST changes which allow PUT and POST requests to modify members, not just entity. - Example code updated to demonstrate how to reorder - Added Gallery3::set_members() - Fixed a weird array_diff problem -- not sure how this was working before - JSON encode any complex values in our post data --- client/Gallery3.php | 21 +++++++++++++++++---- client/example.php | 25 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/client/Gallery3.php b/client/Gallery3.php index 991e85de..ecd6b431 100644 --- a/client/Gallery3.php +++ b/client/Gallery3.php @@ -69,7 +69,7 @@ class Gallery3 { } /** - * Set a value on the remote resource. You must call save for it to take effect. + * Set a value on the remote resource's entity. You must call save for it to take effect. * * @param string key * @param string value @@ -81,6 +81,18 @@ class Gallery3 { return $this; } + /** + * Replace the members for the remote resource + * + * @param array members + * @return object Gallery3 + * @chainable + */ + public function set_members($members) { + $this->data->members = $members; + return $this; + } + /** * Attach a file to the remote resource. * @@ -100,7 +112,7 @@ class Gallery3 { */ public function create($url, $token) { $response = Gallery3_Helper::request( - "post", $url, $token, $this->data->entity, $this->file); + "post", $url, $token, array("entity" => $this->data->entity), $this->file); $this->url = $response->url; $this->token = $token; return $this->load(); @@ -115,7 +127,8 @@ class Gallery3 { public function save() { $response = Gallery3_Helper::request( "put", $this->url, $this->token, - array_diff($this->original_entity, (array)$this->data->entity)); + array("entity" => array_diff((array)$this->data->entity, $this->original_entity), + "members" => $this->data->members)); return $this->load(); } @@ -154,7 +167,7 @@ class Gallery3_Helper { $req->addHeader("X-Gallery-Request-Key", $token); } foreach ($params as $key => $value) { - $req->addPostData($key, $value); + $req->addPostData($key, is_string($value) ? $value : json_encode($value)); } if ($file) { $req->addFile("file", $file, mime_content_type($file)); diff --git a/client/example.php b/client/example.php index 71f4a0fb..64597d2e 100644 --- a/client/example.php +++ b/client/example.php @@ -19,7 +19,6 @@ $tag = Gallery3::factory() ->create($tags->url, $auth); alert("Created tag: {$tag->url}"); - $album = Gallery3::factory() ->set("type", "album") ->set("name", "Sample Album") @@ -34,16 +33,24 @@ $album ->save(); alert("New title: {$album->data->entity->title}"); - -$photo = Gallery3::factory() - ->set("type", "photo") - ->set("name", "Sample Photo.png") - ->set("title", "Sample Photo") - ->set_file("gallery.png") - ->create($album->url, $auth); -alert("Uploaded photo: {$photo->url}"); +for ($i = 0; $i < 2; $i++) { + $photo = Gallery3::factory() + ->set("type", "photo") + ->set("name", "Sample Photo.png") + ->set("title", "Sample Photo") + ->set_file("gallery.png") + ->create($album->url, $auth); + alert("Uploaded photo: {$photo->url}"); +} +$album->load(); alert("Album members: " . join(", ", $album->data->members) . ""); +alert("Reorder the album"); +$album + ->set_members(array($album->data->members[1], $album->data->members[0])) + ->set("sort_column", "weight") + ->save(); +alert("New order: " . join(", ", $album->data->members) . ""); alert("Search for the photo"); $photos = Gallery3::factory($root->url, $auth)