? 182071-flag-services-16.patch
? includes/flag.services.inc
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	15 Oct 2009 08:43:29 -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.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.install,v
retrieving revision 1.2.2.32.2.3
diff -u -p -r1.2.2.32.2.3 flag.install
--- flag.install	28 Sep 2009 18:56:49 -0000	1.2.2.32.2.3
+++ flag.install	15 Oct 2009 08:43:30 -0000
@@ -7,24 +7,17 @@
  */
 
 /**
- * Implementation of hook_install().
+ * Implementation of hook_enable().
+ *
+ * We create the demonstration flag on enable, so hook implementation in flag
+ * module will be used.
  */
-function flag_install() {
-  // Load the flag API in case we want to use it when installing.
+function flag_enable() {
+  // Load the flag API in case we want to use it when enabling.
   include_once(drupal_get_path('module', 'flag') .'/flag.module');
 
-  // If Views Bookmark is available, skip the install and do an upgrade instead.
-  if (strpos($GLOBALS['db_type'], 'mysql') === 0) {
-    include_once(drupal_get_path('module', 'flag') .'/includes/flag.views_bookmark.inc');
-    if (flag_views_bookmark_update_needed()) {
-      flag_views_bookmark_update();
-      return;
-    }
-  }
-
-  $success = drupal_install_schema('flag');
+  if (!flag_get_flag('bookmarks')) {
 
-  if ($success) {
     // Install a demonstration flag.
     $flag = flag_flag::factory_by_content_type('node');
     $configuration = array(
@@ -48,6 +41,25 @@ function flag_install() {
     $flag->form_input($configuration);
     $flag->save();
   }
+}
+
+/**
+ * Implementation of hook_install().
+ */
+function flag_install() {
+  // Load the flag API in case we want to use it when installing.
+  include_once(drupal_get_path('module', 'flag') .'/flag.module');
+
+  // If Views Bookmark is available, skip the install and do an upgrade instead.
+  if (strpos($GLOBALS['db_type'], 'mysql') === 0) {
+    include_once(drupal_get_path('module', 'flag') .'/includes/flag.views_bookmark.inc');
+    if (flag_views_bookmark_update_needed()) {
+      flag_views_bookmark_update();
+      return;
+    }
+  }
+
+  $success = drupal_install_schema('flag');
 
   if ($success) {
     drupal_set_message(st('Flag module installed tables successfully.'));
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	15 Oct 2009 08:43:30 -0000
@@ -784,6 +784,127 @@ 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',
+    '#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)) {
+    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;
+  }
+}
+
+/**
  * A preprocess function for our theme('flag'). It generates the
  * variables needed there.
  *
@@ -1342,4 +1463,4 @@ function flag_get_token($nid) {
  */
 function flag_check_token($token, $seed) {
   return drupal_get_token($seed) == $token;
-}
+}
\ No newline at end of file
