From e78495e5a42f7044e41600016ccbcc089a4ada90 Mon Sep 17 00:00:00 2001
From: Matthew Grasmick <madmatter23@gmail.com>
Date: Wed, 17 Oct 2012 11:44:40 -0400
Subject: [PATCH] adding flag informatio nto  entity properties

---
 flag.module |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/flag.module b/flag.module
index 0cf4240..0a05048 100644
--- a/flag.module
+++ b/flag.module
@@ -2096,3 +2096,111 @@ function flag_set_sid($uid = NULL, $create = TRUE) {
 function flag_get_sid($uid = NULL, $create = FALSE) {
   return flag_set_sid($uid, $create);
 }
+
+/**
+ * Implements hook_entity_property_info_alter().
+ */
+function flag_entity_property_info_alter(&$info) {
+  // Loop through all flag types.
+  foreach (flag_get_flags() as $fid => $flag) {
+    // Set up variables to be used for translation.
+    $vars = array('@entity_type' => $flag->content_type, '@flag_name' => $flag->title);
+
+    // For a given user, attach an array of items flagged by that user.
+    $info['user']['bundles']['user']['properties']['flag_' . $flag->name . '_flagged'] = array(
+      'label' => t('Flag: @entity_types flagged by user with @flag_name flag', $vars),
+      'description' => t('Returns a list of @entity-types a user has flagged with @flag_name flag ', $vars),
+      'type' => 'list<' . $flag->content_type . '>',
+      'getter callback' => 'flag_properties_get_flagged',
+      'computed' => TRUE,
+      'data' => array('flag' => $flag),
+    );
+
+    // For users, we must specify the default user bundle.
+    if ($flag->content_type == 'user') {
+      $bundles[] = 'user';
+    }
+    elseif (count($flag->types)) {
+      $bundles = $flag->types;
+    }
+    // If there are no bundles for this content type, move to next flag.
+    else {
+      continue;
+    }
+
+    // Cycle through all applicable bundles.
+    foreach ($bundles as $bundle) {
+      // Set up variables to be used for translation.
+      $vars = array('@bundle' => $bundle, '@flag_name' => $flag->name);
+
+      // For a given item, attach an array of users who have flagged that item.
+      $info[$flag->content_type]['bundles'][$bundle]['properties']['flag_' . $flag->name . '_users'] = array(
+        'label' => t('Flag: Users who flagged @bundle with @flag_name flag', $vars),
+        'description' => t('Returns a list of users who have flagged a given @bundle with @flag_name flag', $vars),
+        'type' => 'list<user>',
+        'getter callback' => 'flag_properties_get_users',
+        'computed' => TRUE,
+        'data' => array('flag' => $flag),
+      );
+
+      // For a given item, add count of users that have flagged that item.
+      $info[$flag->content_type]['bundles'][$bundle]['properties']['flag_' . $flag->name . '_count'] = array(
+        'label' => t('Flag: @flag_name flag count', $vars),
+        'description' => t('Flag: The total number of @flag_name flags for this @bundle', $vars),
+        'type' => 'integer',
+        'getter callback' => 'flag_properties_get_count',
+        'computed' => TRUE,
+        'data' => array('flag' => $flag),
+      );
+    }
+  }
+}
+
+/**
+ * Returns the flag count for a given node.
+ */
+function flag_properties_get_count($entity, $options, $name, $entity_type, &$info) {
+  $flag = $info['data']['flag'];
+  
+  $query = db_select('flag_counts', 'fc');
+  $query->fields('fc', array('count'));
+  $query->condition('fc.fid', $flag->fid);
+  $query->condition('fc.content_type', $flag->content_type);
+  $query->condition('fc.content_id', $flag->get_content_id($entity));
+  $count = $query->execute()->fetchColumn();
+  $count = !empty($count) ? $count : 0;
+
+  return $count;
+}
+
+/**
+ * Returns entities the user flagged.
+ */
+function flag_properties_get_flagged($entity, $options, $name, $entity_type, &$info) {
+  $flag = $info['data']['flag'];
+
+  $query = db_select('flag_content', 'fc');
+  $query->fields('fc', array('content_id'));
+  $query->condition('fc.fid', $flag->fid);
+  $query->condition('fc.uid', $entity->uid);
+  $result = $query->execute()->fetchCol();
+
+  return $result;
+}
+
+/**
+ * Returns users who flagged an entity.
+ */
+function flag_properties_get_users($entity, $options, $name, $entity_type, &$info)  {
+  $flag = $info['data']['flag'];
+
+  $result = db_select('flag_content', 'fc')
+    ->fields('fc', array('uid'))
+    ->condition('fid', $flag->fid)
+    ->condition('content_type', $flag->content_type)
+    ->condition('content_id', $flag->get_content_id($entity))
+    ->condition('uid', 0, '!=')
+    ->execute();
+
+  return $result->fetchCol();
+}
\ No newline at end of file
-- 
1.7.7.5 (Apple Git-26)

