1
0

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
This commit is contained in:
Bharat Mediratta 2010-04-04 11:58:05 -07:00
parent 199417ff77
commit bd28e04f84
2 changed files with 33 additions and 13 deletions

View File

@ -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 key
* @param string value * @param string value
@ -81,6 +81,18 @@ class Gallery3 {
return $this; 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. * Attach a file to the remote resource.
* *
@ -100,7 +112,7 @@ class Gallery3 {
*/ */
public function create($url, $token) { public function create($url, $token) {
$response = Gallery3_Helper::request( $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->url = $response->url;
$this->token = $token; $this->token = $token;
return $this->load(); return $this->load();
@ -115,7 +127,8 @@ class Gallery3 {
public function save() { public function save() {
$response = Gallery3_Helper::request( $response = Gallery3_Helper::request(
"put", $this->url, $this->token, "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(); return $this->load();
} }
@ -154,7 +167,7 @@ class Gallery3_Helper {
$req->addHeader("X-Gallery-Request-Key", $token); $req->addHeader("X-Gallery-Request-Key", $token);
} }
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
$req->addPostData($key, $value); $req->addPostData($key, is_string($value) ? $value : json_encode($value));
} }
if ($file) { if ($file) {
$req->addFile("file", $file, mime_content_type($file)); $req->addFile("file", $file, mime_content_type($file));

View File

@ -19,7 +19,6 @@ $tag = Gallery3::factory()
->create($tags->url, $auth); ->create($tags->url, $auth);
alert("Created tag: <b>{$tag->url}</b>"); alert("Created tag: <b>{$tag->url}</b>");
$album = Gallery3::factory() $album = Gallery3::factory()
->set("type", "album") ->set("type", "album")
->set("name", "Sample Album") ->set("name", "Sample Album")
@ -34,16 +33,24 @@ $album
->save(); ->save();
alert("New title: <b>{$album->data->entity->title}</b>"); alert("New title: <b>{$album->data->entity->title}</b>");
for ($i = 0; $i < 2; $i++) {
$photo = Gallery3::factory() $photo = Gallery3::factory()
->set("type", "photo") ->set("type", "photo")
->set("name", "Sample Photo.png") ->set("name", "Sample Photo.png")
->set("title", "Sample Photo") ->set("title", "Sample Photo")
->set_file("gallery.png") ->set_file("gallery.png")
->create($album->url, $auth); ->create($album->url, $auth);
alert("Uploaded photo: <b>{$photo->url}</b>"); alert("Uploaded photo: <b>{$photo->url}</b>");
}
$album->load();
alert("Album members: <b>" . join(", ", $album->data->members) . "</b>"); alert("Album members: <b>" . join(", ", $album->data->members) . "</b>");
alert("Reorder the album");
$album
->set_members(array($album->data->members[1], $album->data->members[0]))
->set("sort_column", "weight")
->save();
alert("New order: <b>" . join(", ", $album->data->members) . "</b>");
alert("Search for the photo"); alert("Search for the photo");
$photos = Gallery3::factory($root->url, $auth) $photos = Gallery3::factory($root->url, $auth)