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 -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	29 Oct 2009 03:17:08 -0000
@@ -895,6 +895,135 @@
 }
 
 /**
+ * Implementation of hook_service().
+ */
+function flag_service() {
+  $items = array();
+
+  $items[] = 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[] = 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
+ *   Optional. 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)) {
+    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 (empty($account) || !$flag->access($content_id, $action, $account)) {
+      // User has no permission to use this flag.
+      return FALSE;
+    }
+    return TRUE;
+  }
+}
+
+/**
  * A preprocess function for our theme('flag'). It generates the
  * variables needed there.
  *
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	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,55 @@
+<?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);
+  // Prevent PHP notice in case we are calling user ID if it wasn't supplied.
+  return !empty($uid) ? $flag->is_flagged($content_id, $uid) : $flag->is_flagged($content_id);
+}
