diff --git a/flag.info.inc b/flag.info.inc new file mode 100644 index 0000000..3e6e55e --- /dev/null +++ b/flag.info.inc @@ -0,0 +1,84 @@ +name . '_flagged'] = array( + 'label' => t('Flagged @entity-type with flag @flag', array('@entity-type' => $flag->entity_type, '@flag' => $flag->name)), + 'description' => t('Returns a list of entities a user flagged with flag @flag.', array('@flag' => $flag->name)), + 'type' => 'list<' . $flag->entity_type . '>', + 'getter callback' => 'flag_properties_get_flagged_content', + 'computed' => TRUE, + 'flag_name' => $flag->name, + ); + if (count($flag->types)) { + // Bundle specific property. + foreach ($flag->types as $type) { + $info[$flag->entity_type]['bundles'][$type]['properties']['flag_' . $flag->name . '_user'] = flag_entity_property_user_definition($type, $flag); + } + } + else { + // Generic property, applies for all bundles. + $info[$flag->entity_type]['properties']['flag_' . $flag->name . '_user'] = flag_entity_property_user_definition($flag->entity_type, $flag); + } + } +} + + +/** + * Helper function that returns the entity property definition for user who + * flagged a certain entity. + * + * @param $type + * The type can either be the bundle or the entity type. + * @param $flag + * The flag object. + * + * @return + * An array containg the property information. + */ +function flag_entity_property_user_definition($type, $flag) { + return array( + 'label' => t('Users who flagged the @type with flag @flag', array('@type' => $type, '@flag' => $flag->name)), + 'description' => t('Returns a list of users who flagged an enttiy with flag @flag.', array('@flag' => $flag->name)), + 'type' => 'list', + 'getter callback' => 'flag_properties_get_flagging_users', + 'computed' => TRUE, + 'flag_name' => $flag->name, + ); +} + +/** + * Getter callback that returns entities the user flagged. + */ +function flag_properties_get_flagged_content($entity, array $options, $name, $entity_type, $property_info) { + $flag = flag_get_flag($property_info['flag_name']); + + $result = db_select('flagging', 'f') + ->fields('f', array('entity_id')) + ->condition('fid', $flag->fid) + ->condition('uid', $entity->uid) + ->execute(); + return $result->fetchCol(); +} + +/** + * Getter callback that returns users who flagged an entity. + */ +function flag_properties_get_flagging_users($entity, array $options, $name, $entity_type, $property_info) { + $flag = flag_get_flag($property_info['flag_name']); + + $result = db_select('flagging', 'f') + ->fields('f', array('uid')) + ->condition('fid', $flag->fid) + ->condition('entity_id', $flag->get_entity_id($entity)) + ->condition('uid', 0, '!=') + ->execute(); + return $result->fetchCol(); +} diff --git a/flag.module b/flag.module index 5c2a196..54cd329 100644 --- a/flag.module +++ b/flag.module @@ -5,6 +5,10 @@ * The Flag module. */ +// Manually include the properties info file, because it contains the properties +// getter callbacks. +require_once 'flag.info.inc'; + define('FLAG_API_VERSION', 3); define('FLAG_ADMIN_PATH', 'admin/structure/flags');