? 182071-flag-services-12.patch
Index: flag.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.inc,v
retrieving revision 1.1.2.30.2.12
diff -u -p -r1.1.2.30.2.12 flag.inc
--- flag.inc	4 Oct 2009 02:42:10 -0000	1.1.2.30.2.12
+++ flag.inc	6 Oct 2009 10:06:55 -0000
@@ -508,7 +508,7 @@ class flag_flag {
   }
 
   /**
-   * Flags, on unflags, an item.
+   * Flags, or unflags, an item.
    *
    * @param $action
    *   Either 'flag' or 'unflag'.
@@ -810,7 +810,7 @@ class flag_flag {
   function rules_get_element_argument_definition() {
     return array();
   }
-  
+
   /**
    * @} End of "addtogroup rules".
    */
@@ -1110,7 +1110,7 @@ class flag_node extends flag_flag {
       ),
     );
   }
-  
+
   function rules_get_element_argument_definition() {
     return array('type' => 'node', 'label' => t('Flagged content'));
   }
@@ -1229,7 +1229,7 @@ class flag_comment extends flag_flag  {
       ),
     );
   }
-  
+
   function rules_get_element_argument_definition() {
     return array('type' => 'comment', 'label' => t('Flagged comment'));
   }
@@ -1376,7 +1376,7 @@ class flag_user extends flag_flag {
       ),
     );
   }
-  
+
   function rules_get_element_argument_definition() {
     return array('type' => 'user', 'label' => t('Flagged user'));
   }
Index: flag.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.module,v
retrieving revision 1.11.2.72.2.18
diff -u -p -r1.11.2.72.2.18 flag.module
--- flag.module	1 Oct 2009 04:36:44 -0000	1.11.2.72.2.18
+++ flag.module	6 Oct 2009 10:06:55 -0000
@@ -88,6 +88,12 @@ function flag_init() {
   if (module_exists('token')) {
     include_once $path .'/includes/flag.token.inc';
   }
+  if (module_exists('services')) {
+    // Services module doesn't allow us to place the include file in a
+    // subdirectory, so we include it ourself.
+    include_once $path .'/includes/flag.services.inc';
+  }
+
 }
 
 /**
@@ -104,7 +110,11 @@ function flag_views_api() {
  * Implementation of hook_perm().
  */
 function flag_perm() {
-  return array('administer flags');
+  $perms = array('administer flags');
+  if (module_exists('services')) {
+    $perms += array('flag and unflag via services', 'check is flagged via services');
+  }
+  return $perms;
 }
 
 /**
@@ -784,6 +794,135 @@ function flag_theme() {
 }
 
 /**
+ * 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',
+    '#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'),
+    '#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) {
+  global $user;
+
+  $flag = flag_get_flag($flag_name);
+  if (!$flag) {
+    // Flag does not exist.
+    return FALSE;
+  }
+
+  if (empty($action)) {
+    return user_access('check is flagged via services');
+  }
+  else {
+    // Check action is valid.
+    if (!in_array($action, array('flag', 'unflag'))) {
+      return FALSE;
+    }
+    if (!user_access('flag and unflag via services')) {
+      return FALSE;
+    }
+    if ($uid) {
+      $account = user_load($uid);
+    }
+    else {
+      $account = $user;
+    }
+
+    if (!$flag->user_access($action, $account)) {
+      // User has no permission to use this flag.
+      return FALSE;
+    }
+    if (!$flag->applies_to_content_id($content_id)) {
+      // Flag does not apply to this content.
+      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	6 Oct 2009 10:06:55 -0000
@@ -0,0 +1,71 @@
+<?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);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
