? 182071-flag-services-19.patch
? flag-user-access.patch
Index: flag.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.inc,v
retrieving revision 1.1.2.30.2.16
diff -u -p -r1.1.2.30.2.16 flag.inc
--- flag.inc	28 Oct 2009 02:38:10 -0000	1.1.2.30.2.16
+++ flag.inc	28 Oct 2009 08:09:54 -0000
@@ -615,7 +615,7 @@ class flag_flag {
    *
    * @private
    */
-  function _is_flagged($content_id, $uid, $sid) { 
+  function _is_flagged($content_id, $uid, $sid) {
     return db_result(db_query("SELECT fid FROM {flag_content} WHERE fid = %d AND uid = %d AND sid = %d AND content_id = %d", $this->fid, $uid, $sid, $content_id));
   }
 
Index: flag.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.module,v
retrieving revision 1.11.2.72.2.22
diff -u -p -r1.11.2.72.2.22 flag.module
--- flag.module	28 Oct 2009 02:38:10 -0000	1.11.2.72.2.22
+++ flag.module	28 Oct 2009 08:09:54 -0000
@@ -782,6 +782,128 @@ function flag_trim_flag($flag, $uid, $cu
 }
 
 /**
+ * Implementation of hook_service().
+ */
+function flag_service() {
+  $items = array();
+
+  $items['flag_service_flag'] = array(
+    '#method'           => 'flag.flag',
+    '#callback'         => 'flag_service_flag',
+    '#access callback'  => 'flag_service_flag_access',
+    '#file'             => array('file' => 'inc', 'module' => 'flag', 'file name' => '/includes/flag.services'),
+    '#args'             => array(
+      array(
+        '#name'           => 'flag_name',
+        '#type'           => 'string',
+        '#description'    => t('The name of the flag.'),
+      ),
+      array(
+        '#name'           => 'content_id',
+        '#type'           => 'int',
+        '#description'    => t('The content ID.'),
+      ),
+      array(
+        '#name'           => 'uid',
+        '#type'           => 'int',
+        '#description'    => t('The user ID for which to flag.'),
+        '#optional'        => TRUE,
+      ),
+      array(
+        '#name'           => 'action',
+        '#type'           => 'string',
+        '#description'    => t('Optional; The action to perform, default is "flag". Should be "flag" or "unflag".'),
+        '#optional'        => TRUE,
+      ),
+      array(
+        '#name'           => 'skip_permission_check',
+        '#type'           => 'boolean',
+        '#description'    => t('Optional; Flag the content even if the user does not have permission to do so. FALSE by default'),
+        '#optional'        => TRUE,
+      ),
+    ),
+    '#return'           => 'boolean',
+    '#help'             => t('Flags (or unflags) a content.')
+  );
+
+  $items['flag_service_is_flagged'] = array(
+    '#method'           => 'flag.is_flagged',
+    '#callback'         => 'flag_service_is_flagged',
+    '#access callback'  => 'flag_service_flag_access',
+    '#file'             => array('file' => 'inc', 'module' => 'flag', 'file name' => '/includes/flag.services'),
+    '#args'             => array(
+      array(
+        '#name'           => 'flag_name',
+        '#type'           => 'string',
+        '#description'    => t('The name of the flag.'),
+      ),
+      array(
+        '#name'           => 'content_id',
+        '#type'           => 'int',
+        '#description'    => t('The content ID.'),
+      ),
+      array(
+        '#name'           => 'uid',
+        '#type'           => 'int',
+        '#description'    => t('The user ID that might have flagged the content.'),
+        '#optional'        => TRUE,
+      ),
+    ),
+    '#return'           => 'boolean',
+    '#help'             => t('Check if a content was flagged by a user.')
+  );
+
+  return $items;
+
+}
+
+/**
+ * Access callback to check a user has access to a flag operation via Services.
+ *
+ * @param $flag_name
+ *   The flag name.
+ * @param $content_id
+ *   The content ID that should be flagged.
+ * @param $uid
+ *   Optional; The user ID on behalf to flag the content.
+ * @param $action
+ *   Optioanl; If the method is "flag", then pass the desired action which
+ *   should be "flag" or "unflag".
+ * @return
+ *   TRUE if access is allowed.
+ */
+function flag_service_flag_access($flag_name, $content_id, $uid = NULL, $action = NULL) {
+  $flag = flag_get_flag($flag_name);
+  if (!$flag) {
+    // Flag does not exist.
+    return FALSE;
+  }
+  if (empty($action)) {
+    // We are trying to check if a glaf is flagged.
+    return TRUE;
+  }
+  else {
+    // Check action is valid.
+    if (!in_array($action, array('flag', 'unflag'))) {
+      return FALSE;
+    }
+    if ($uid) {
+      $account = user_load($uid);
+    }
+    else {
+      global $user;
+      $account = $user;
+    }
+
+    if (!$flag->access($content_id, $action, $account)) {
+      // User has no permission to use this flag.
+      return FALSE;
+    }
+    return TRUE;
+  }
+}
+
+/**
  * Implementation of hook_node_operations().
  *
  * Add additional options on the admin/build/node page.
Index: includes/flag.services.inc
===================================================================
RCS file: includes/flag.services.inc
diff -N includes/flag.services.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/flag.services.inc	28 Oct 2009 08:09:54 -0000
@@ -0,0 +1,54 @@
+<?php
+// $Id:$
+
+/**
+ * @file
+ * Services integration for the Flag module.
+ */
+
+/**
+ * Service wrapper to flag a content.
+ *
+ * @param $flag_name
+ *   The flag name.
+ * @param $content_id
+ *   The content ID to check if it was flagged.
+ * @param $uid
+ *   The user ID to check if they flagged the content.
+ * @param $action
+ *   The action. Should be "flag" or "unflag".
+ * @return
+ *   TRUE if content was flagged.
+ */
+function flag_service_flag($flag_name, $content_id, $uid = NULL, $action = 'flag', $skip_permission_check = FALSE) {
+  global $user;
+  if (!empty($uid)) {
+    if (!($account = user_load($uid))) {
+      // User was not loaded.
+      return FALSE;
+    }
+  }
+  else {
+    $account = $user;
+  }
+
+  $flag = flag_get_flag($flag_name);
+  return $flag->flag($action, $content_id, $account, (boolean)$skip_permission_check);
+}
+
+/**
+ * Service wrapper to check if a content is flagged by a user.
+ *
+ * @param $flag_name
+ *   The flag name.
+ * @param $content_id
+ *   The content ID to check if it was flagged.
+ * @param $uid
+ *   The user ID to check if they flagged the content.
+ * @return
+ *   TRUE if content was flagged.
+ */
+function flag_service_is_flagged($flag_name, $content_id, $uid = NULL) {
+  $flag = flag_get_flag($flag_name);
+  return $flag->is_flagged($content_id, $uid);
+}
\ No newline at end of file
