Add support for Photos methods.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 27 Feb 2009 18:33:16 +0000 (13:33 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 27 Feb 2009 18:33:16 +0000 (13:33 -0500)
(Uploading/Viewing/Dealing with Galleries etc...)

framework/Service_Facebook/lib/Horde/Service/Facebook.php
framework/Service_Facebook/lib/Horde/Service/Facebook/Photos.php [new file with mode: 0644]
framework/Service_Facebook/package.xml

index 86eac68..e5bfae5 100644 (file)
@@ -421,142 +421,6 @@ class Horde_Service_Facebook
                   'note_ids' => json_encode($note_ids)));
     }
 
-    /**
-     * Adds a tag with the given information to a photo. See the wiki for details:
-     *
-     *  http://wiki.developers.facebook.com/index.php/Photos.addTag
-     *
-     * @param int $pid          The ID of the photo to be tagged
-     * @param int $tag_uid      The ID of the user being tagged. You must specify
-     *                          either the $tag_uid or the $tag_text parameter
-     *                          (unless $tags is specified).
-     * @param string $tag_text  Some text identifying the person being tagged.
-     *                          You must specify either the $tag_uid or $tag_text
-     *                          parameter (unless $tags is specified).
-     * @param float $x          The horizontal position of the tag, as a
-     *                          percentage from 0 to 100, from the left of the
-     *                          photo.
-     * @param float $y          The vertical position of the tag, as a percentage
-     *                          from 0 to 100, from the top of the photo.
-     * @param array $tags       (Optional) An array of maps, where each map
-     *                          can contain the tag_uid, tag_text, x, and y
-     *                          parameters defined above.  If specified, the
-     *                          individual arguments are ignored.
-     * @param int $owner_uid    (Optional)  The user ID of the user whose photo
-     *                          you are tagging. If this parameter is not
-     *                          specified, then it defaults to the session user.
-     *
-     * @return bool  true on success
-     */
-    public function &photos_addTag($pid, $tag_uid, $tag_text, $x, $y, $tags, $owner_uid = 0)
-    {
-        return $this->call_method('facebook.photos.addTag',
-            array('pid' => $pid,
-                  'tag_uid' => $tag_uid,
-                  'tag_text' => $tag_text,
-                  'x' => $x,
-                  'y' => $y,
-                  'tags' => (is_array($tags)) ? json_encode($tags) : null,
-                  'owner_uid' => $this->get_uid($owner_uid)));
-    }
-
-    /**
-     * Creates and returns a new album owned by the specified user or the current
-     * session user.
-     *
-     * @param string $name         The name of the album.
-     * @param string $description  (Optional) A description of the album.
-     * @param string $location     (Optional) A description of the location.
-     * @param string $visible      (Optional) A privacy setting for the album.
-     *                             One of 'friends', 'friends-of-friends',
-     *                             'networks', or 'everyone'.  Default 'everyone'.
-     * @param int $uid             (Optional) User id for creating the album; if
-     *                             not specified, the session user is used.
-     *
-     * @return array  An album object
-     */
-    public function &photos_createAlbum($name, $description='', $location='', $visible='', $uid=0)
-    {
-        return $this->call_method('facebook.photos.createAlbum',
-            array('name' => $name,
-                  'description' => $description,
-                'location' => $location,
-                'visible' => $visible,
-                'uid' => $this->get_uid($uid)));
-    }
-
-    /**
-     * Returns photos according to the filters specified.
-     *
-     * @param int $subj_id  (Optional) Filter by uid of user tagged in the photos.
-     * @param int $aid      (Optional) Filter by an album, as returned by
-     *                      photos_getAlbums.
-     * @param string $pids   (Optional) Restrict to a comma-separated list of pids
-     *
-     * Note that at least one of these parameters needs to be specified, or an
-     * error is returned.
-     *
-     * @return array  An array of photo objects.
-     */
-    public function &photos_get($subj_id, $aid, $pids)
-    {
-        return $this->call_method('facebook.photos.get',
-            array('subj_id' => $subj_id, 'aid' => $aid, 'pids' => $pids));
-    }
-
-    /**
-     * Returns the albums created by the given user.
-     *
-     * @param int $uid      (Optional) The uid of the user whose albums you want.
-     *                       A null will return the albums of the session user.
-     * @param string $aids  (Optional) A comma-separated list of aids to restricti
-     *                       the query.
-     *
-     * Note that at least one of the (uid, aids) parameters must be specified.
-     *
-     * @returns an array of album objects.
-     */
-    public function &photos_getAlbums($uid, $aids)
-    {
-        return $this->call_method('facebook.photos.getAlbums',
-            array('uid' => $uid,
-                  'aids' => $aids));
-    }
-
-    /**
-     * Returns the tags on all photos specified.
-     *
-     * @param string $pids  A list of pids to query
-     *
-     * @return array  An array of photo tag objects, which include pid,
-     *                subject uid, and two floating-point numbers (xcoord, ycoord)
-     *                for tag pixel location.
-     */
-    public function &photos_getTags($pids)
-    {
-        return $this->call_method('facebook.photos.getTags', array('pids' => $pids));
-    }
-
-    /**
-     * Uploads a photo.
-     *
-     * @param string $file     The location of the photo on the local filesystem.
-     * @param int $aid         (Optional) The album into which to upload the
-     *                         photo.
-     * @param string $caption  (Optional) A caption for the photo.
-     * @param int uid          (Optional) The user ID of the user whose photo you
-     *                         are uploading
-     *
-     * @return array  An array of user objects
-     */
-    public function photos_upload($file, $aid = null, $caption = null, $uid = null)
-    {
-        return $this->call_upload_method('facebook.photos.upload',
-                                     array('aid' => $aid,
-                                           'caption' => $caption,
-                                           'uid' => $uid),
-                                     $file);
-    }
 
 
     /**
diff --git a/framework/Service_Facebook/lib/Horde/Service/Facebook/Photos.php b/framework/Service_Facebook/lib/Horde/Service/Facebook/Photos.php
new file mode 100644 (file)
index 0000000..c7b35b2
--- /dev/null
@@ -0,0 +1,205 @@
+<?php
+/**
+ * Photos methods for Horde_Service_Facebook
+ *
+ */
+class Horde_Service_Facebook_Photos
+{
+    /**
+     * Adds a tag with the given information to a photo. See the wiki for details:
+     *
+     *  http://wiki.developers.facebook.com/index.php/Photos.addTag
+     *
+     * @param int $pid          The ID of the photo to be tagged
+     * @param int $tag_uid      The ID of the user being tagged. You must specify
+     *                          either the $tag_uid or the $tag_text parameter
+     *                          (unless $tags is specified).
+     * @param string $tag_text  Some text identifying the person being tagged.
+     *                          You must specify either the $tag_uid or $tag_text
+     *                          parameter (unless $tags is specified).
+     * @param float $x          The horizontal position of the tag, as a
+     *                          percentage from 0 to 100, from the left of the
+     *                          photo.
+     * @param float $y          The vertical position of the tag, as a percentage
+     *                          from 0 to 100, from the top of the photo.
+     * @param array $tags       (Optional) An array of maps, where each map
+     *                          can contain the tag_uid, tag_text, x, and y
+     *                          parameters defined above.  If specified, the
+     *                          individual arguments are ignored.
+     * @param int $owner_uid    (Optional)  The user ID of the user whose photo
+     *                          you are tagging. If this parameter is not
+     *                          specified, then it defaults to the session user.
+     *
+     * @return bool  true on success
+     */
+    public function &addTag($pid, $tag_uid, $tag_text, $x, $y, $tags, $uid = 0)
+    {
+        // Requires either a owner_uid or a session_key
+        if (empty($uid) && !$this->_facebook->auth->getSessionKey()) {
+            throw new Horde_Service_Facebook_Exception('photos.addTag requires either a uid or a session_key',
+                                               Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
+        }
+
+        $params = array('pid' => $pid,
+                  'tag_uid' => $tag_uid,
+                  'tag_text' => $tag_text,
+                  'x' => $x,
+                  'y' => $y,
+                  'tags' => (is_array($tags)) ? json_encode($tags) : null);
+        if (!empty($owner_uid)) {
+            $params['owner_uid'] = $uid;
+        }
+        if ($skey = $this->_facebook->auth->getSessionKey()) {
+            $params['session_key'] = $skey;
+        }
+
+        return $this->call_method('facebook.photos.addTag', $params);
+    }
+
+    /**
+     * Creates and returns a new album owned by the specified user or the current
+     * session user.
+     *
+     * @param string $name         The name of the album.
+     * @param string $description  (Optional) A description of the album.
+     * @param string $location     (Optional) A description of the location.
+     * @param string $visible      (Optional) A privacy setting for the album.
+     *                             One of 'friends', 'friends-of-friends',
+     *                             'networks', or 'everyone'.  Default 'everyone'.
+     * @param int $uid             (Optional) User id for creating the album; if
+     *                             not specified, the session user is used.
+     *
+     * @return array  An album object
+     */
+    public function &createAlbum($name, $description = '', $location = '', $visible = '', $uid = 0)
+    {
+        // Requires either a owner_uid or a session_key
+        if (empty($owner_uid) && !$this->_facebook->auth->getSessionKey()) {
+            throw new Horde_Service_Facebook_Exception('photos.addTag requires either a owner_uid or a session_key',
+                                               Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
+        }
+
+        $params = array('name' => $name,
+                  'description' => $description,
+                  'location' => $location,
+                  'visible' => $visible);
+
+        // Yes, this method uses 'uid' while some of the others use
+        // 'owner_uid' - don't ask me...
+        if (!empty($uid)) {
+            $params['uid'] = $uid;
+        }
+
+        if ($skey = $this->_facebook->auth->getSessionKey()) {
+            $params['session_key'] = $skey;
+        }
+
+        return $this->call_method('facebook.photos.createAlbum', $params);
+    }
+
+    /**
+     * Returns photos according to the filters specified.
+     *
+     * @param int $subj_id  (Optional) Filter by uid of user tagged in the photos.
+     * @param int $aid      (Optional) Filter by an album, as returned by
+     *                      photos_getAlbums.
+     * @param string $pids   (Optional) Restrict to a comma-separated list of pids
+     *
+     * Note that at least one of these parameters needs to be specified, or an
+     * error is returned.
+     *
+     * @return array  An array of photo objects.
+     */
+    public function &get($subj_id, $aid, $pids)
+    {
+        // Requires a session_key
+        if (!$skey = $this->_facebook->auth->getSessionKey()) {
+            throw new Horde_Service_Facebook_Exception('photos.addTag requires a session_key',
+                                               Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
+        }
+        return $this->call_method('facebook.photos.get',
+            array('subj_id' => $subj_id,
+                  'aid' => $aid,
+                  'pids' => $pids,
+                  'session_key' => $skey));
+    }
+
+    /**
+     * Returns the albums created by the given user.
+     *
+     * @param int $uid      (Optional) The uid of the user whose albums you want.
+     *                       A null will return the albums of the session user.
+     * @param string $aids  (Optional) A comma-separated list of aids to restricti
+     *                       the query.
+     *
+     * Note that at least one of the (uid, aids) parameters must be specified.
+     *
+     * @returns an array of album objects.
+     */
+    public function &getAlbums($uid, $aids)
+    {
+        // Requires a session_key
+        if (!$skey = $this->_facebook->auth->getSessionKey()) {
+            throw new Horde_Service_Facebook_Exception('photos.addTag requires a session_key',
+                                               Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
+        }
+
+        return $this->call_method('facebook.photos.getAlbums',
+            array('uid' => $uid,
+                  'aids' => $aids,
+                  'session_key' => $skey));
+    }
+
+    /**
+     * Returns the tags on all photos specified.
+     *
+     * @param string $pids  A list of pids to query
+     *
+     * @return array  An array of photo tag objects, which include pid,
+     *                subject uid, and two floating-point numbers (xcoord, ycoord)
+     *                for tag pixel location.
+     */
+    public function &photos_getTags($pids)
+    {
+        // Requires a session_key
+        if (!$skey = $this->_facebook->auth->getSessionKey()) {
+            throw new Horde_Service_Facebook_Exception('photos.addTag requires a session_key',
+                                               Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
+        }
+
+        return $this->call_method('facebook.photos.getTags', array('pids' => $pids, 'session_key' => $skey));
+    }
+
+    /**
+     * Uploads a photo.
+     *
+     * @param string $file     The location of the photo on the local filesystem.
+     * @param int $aid         (Optional) The album into which to upload the
+     *                         photo.
+     * @param string $caption  (Optional) A caption for the photo.
+     * @param int uid          (Optional) The user ID of the user whose photo you
+     *                         are uploading
+     *
+     * @return array  An array of user objects
+     */
+    public function photos_upload($file, $aid = null, $caption = null, $uid = null)
+    {
+        // Requires either a owner_uid or a session_key
+        if (empty($uid) && !$skey = $this->_facebook->auth->getSessionKey()) {
+            throw new Horde_Service_Facebook_Exception('photos.addTag requires either a uid or a session_key',
+                                               Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
+        }
+
+        $params = array('aid' => $aid, 'caption' => $caption);
+        if (!empty($uid)) {
+            $params['uid'] = $uid;
+        }
+
+        if (!empty($skey)) {
+            $params['session_key'] = $skey;
+        }
+
+        return $this->call_upload_method('facebook.photos.upload', $params, $file);
+    }
+
+}
\ No newline at end of file
index 4596bc9..25f282f 100644 (file)
@@ -42,6 +42,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="BatchRequest.php" role="php" />
        <file name="Auth.php" role="php" />
        <file name="Feeds.php" role="php" />
+       <file name="Photos.php" role="php" />
        <file name="Exception.php" role="php" />
        <file name="ErrorCodes.php" role="php" />
       </dir> <!-- /lib/Horde/Service/Facebook -->
@@ -76,6 +77,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Service/Facebook/BatchRequest.php" as="Horde/Service/Facebook/BatchRequest.php" />
    <install name="lib/Horde/Service/Facebook/Auth.php" as="Horde/Service/Facebook/Auth.php" />   
    <install name="lib/Horde/Service/Facebook/Feeds.php" as="Horde/Service/Facebook/Feeds.php" />   
+   <install name="lib/Horde/Service/Facebook/Photos.php" as="Horde/Service/Facebook/Photos.php" />
    <install name="lib/Horde/Service/Facebook/Exception.php" as="Horde/Service/Facebook/Exception.php" />
    <install name="lib/Horde/Service/Facebook/ErrorCodes.php" as="Horde/Service/Facebook/ErrorCodes.php" />
    <install name="lib/Horde/Service/Facebook.php" as="Horde/Service/Facebook.php" />