From 39df43b46682257ee65a2da7351499f713ac5cac Mon Sep 17 00:00:00 2001
From: Jaime Herencia <jherencia@360690.no-reply.drupal.org>
Date: Tue, 2 Oct 2012 11:43:25 +0200
Subject: [PATCH] Issue #332956 by jherencia, Amitaibu, jastraat, mansspams
 and mooffie: Flag Ctools integration

---
 flag.module                                        |    9 ++
 plugins/access/flag_is_flagged/flag_is_flagged.inc |   96 ++++++++++++++++
 plugins/content_types/flag_link/flag_link.inc      |  116 ++++++++++++++++++++
 3 files changed, 221 insertions(+)
 create mode 100644 plugins/access/flag_is_flagged/flag_is_flagged.inc
 create mode 100644 plugins/content_types/flag_link/flag_link.inc

diff --git a/flag.module b/flag.module
index 41f0872..5556855 100644
--- a/flag.module
+++ b/flag.module
@@ -2212,3 +2212,12 @@ function flag_set_sid($uid = NULL, $create = TRUE) {
 function flag_get_sid($uid = NULL, $create = FALSE) {
   return flag_set_sid($uid, $create);
 }
+
+/**
+ * Implements hook_ctools_plugin_directory().
+ */
+function flag_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'ctools' && !empty($plugin)) {
+    return "plugins/$plugin";
+  }
+}
diff --git a/plugins/access/flag_is_flagged/flag_is_flagged.inc b/plugins/access/flag_is_flagged/flag_is_flagged.inc
new file mode 100644
index 0000000..fca22f9
--- /dev/null
+++ b/plugins/access/flag_is_flagged/flag_is_flagged.inc
@@ -0,0 +1,96 @@
+<?php
+
+/**
+ * Plugin definition.
+ */
+$plugin = array(
+  'title' => t("Entity is flagged"),
+  'description' => t('Control access by whether or not a node is flagged.'),
+  'callback' => 'flag_flag_is_flagged_access_check',
+  'default' => array('flag_name' => ''),
+  'settings form' => 'flag_flag_is_flagged_access_settings',
+  'summary' => 'flag_flag_is_flagged_access_summary',
+  'get child' => 'flag_flag_is_flagged_access_get_child',
+  'get children' => 'flag_flag_is_flagged_access_get_children',
+);
+
+/*
+ * Implements plugin get_child hook.
+ */
+function flag_flag_is_flagged_access_get_child($plugin, $parent, $child) {
+  $plugins = flag_flag_is_flagged_access_get_children($plugin, $parent);
+  return $plugins[$parent . ':' . $child];
+}
+
+/*
+ * Implements plugin get_children hook.
+ */
+function flag_flag_is_flagged_access_get_children($plugin, $parent) {
+  $plugins = array();
+  $entities = entity_get_info();
+
+  foreach ($entities as $entity_type => $info) {
+    if (entity_type_supports($entity_type, 'view')) {
+      $plugin['title'] =  t('@entity_type is flagged', array('@entity_type' => $info['label']));
+      $plugin['keyword'] = $entity_type;
+      $plugin['name'] = $parent . ':' . $entity_type;
+      $plugin['required context'] = new ctools_context_required(t('Entity'), $entity_type);
+      $plugins[$parent . ':' . $entity_type] = $plugin;
+    }
+  }
+
+  return $plugins;
+}
+
+/**
+ * Settings form
+ */
+function flag_flag_is_flagged_access_settings(&$form, &$form_state, $conf) {
+  $options = array();
+
+  $plugin = $form_state['plugin'];
+  $entity_type = explode(':', $plugin['name']);
+  $entity_type = $entity_type[1];
+
+  foreach (flag_get_flags($entity_type) as $flag) {
+    $options[$flag->name] = check_plain($flag->title);
+  }
+
+  $form['settings']['flag_name'] = array(
+    '#title' => t('Flag name'),
+    '#type' => 'radios',
+    '#options' => $options,
+    '#description' => t('Include only flagged content.'),
+    '#default_value' => $conf['flag_name'],
+  );
+  return $form;
+}
+
+/**
+ * Check for access.
+ */
+function flag_flag_is_flagged_access_check($conf, $context) {
+  $flag = flag_get_flag($conf['flag_name']);
+  if (!$flag || empty($context->data)) {
+    return FALSE;
+  }
+
+  // Get the ID of the entity.
+  list($id) = entity_extract_ids($flag->entity_type, $context->data);
+
+  return $flag->is_flagged($id);
+}
+
+/**
+ * Provide a summary description based upon the specified context
+ */
+function flag_flag_is_flagged_access_summary($conf, $context) {
+  $flag = flag_get_flag($conf['flag_name']);
+
+  if ($flag) {
+    return t('@identifier is flagged with "@flag"', array('@flag' => check_plain($flag->title), '@identifier' => $context->identifier));
+  }
+  else {
+    return t('Missing/ deleted flag "@flag"', array('@flag' => $conf['flag_name']));
+  }
+}
diff --git a/plugins/content_types/flag_link/flag_link.inc b/plugins/content_types/flag_link/flag_link.inc
new file mode 100644
index 0000000..df46956
--- /dev/null
+++ b/plugins/content_types/flag_link/flag_link.inc
@@ -0,0 +1,116 @@
+<?php
+
+/**
+ * Plugin definition.
+ */
+$plugin = array(
+  'title' => t('Flag link'),
+  'description' => t('Add a flag link of a certain flag type for entities.'),
+  'defaults' => array('flag_name' => ''),
+  'content type' => 'flag_flag_link_content_type_info',
+);
+
+/**
+ * Get the entity content type info.
+ */
+function flag_flag_link_content_type_info($entity_type) {
+  $types = flag_flag_link_content_type_content_types();
+  if (isset($types[$entity_type])) {
+    return $types[$entity_type];
+  }
+}
+
+/**
+ * Implements hook_PLUGIN_content_type_content_types().
+ */
+function flag_flag_link_content_type_content_types() {
+  $types = array();
+  $entities = entity_get_info();
+
+  foreach ($entities as $entity_type => $info) {
+    if (entity_type_supports($entity_type, 'view')) {
+      $types[$entity_type] = array(
+        'title' => t('Flag for @entity_type', array('@entity_type' => $info['label'])),
+        'category' => t('Entity'),
+        'required context' => new ctools_context_required(t('Entity'), $entity_type),
+      );
+    }
+  }
+
+  return $types;
+}
+
+/**
+ * Render callback.
+ */
+function flag_flag_link_content_type_render($subtype, $conf, $args, $context) {
+  $flag = flag_get_flag($conf['flag_name']);
+
+  if (!$flag) {
+    return;
+  }
+
+  if (empty($context->data)) {
+    return;
+  }
+
+  // Get the ID of the entity.
+  list($id) = entity_extract_ids($flag->entity_type, $context->data);
+  $link = flag_create_link($flag->name, $id);
+
+  if (!$link) {
+    return;
+  }
+
+  $block = new stdClass();
+  $block->module = 'flag';
+  $block->title = t('Flag link');
+  $block->delta = $flag->name;
+
+  $block->content = $link;
+  return $block;
+}
+
+/**
+ * Form callback.
+ */
+function flag_flag_link_content_type_edit_form($form, &$form_state) {
+  $conf = $form_state['conf'];
+  $entity_type = $form_state['subtype_name'];
+  $options = array();
+
+  foreach (flag_get_flags($entity_type) as $flag) {
+    $options[$flag->name] = $flag->title;
+  }
+
+  $form['flag_name'] = array(
+    '#type' => 'select',
+    '#title' => t('Flag name'),
+    '#default_value' => $conf['flag_name'],
+    '#options' => $options,
+    '#description' => t('Select a flag.'),
+    '#required' => TRUE,
+    '#disabled' => !$options,
+  );
+
+  return $form;
+}
+
+/**
+ * Form submit.
+ */
+function flag_flag_link_content_type_edit_form_submit($form, &$form_state) {
+  // Copy everything from our defaults.
+  foreach (array_keys($form_state['plugin']['defaults']) as $key) {
+    $form_state['conf'][$key] = $form_state['values'][$key];
+  }
+}
+
+/**
+ * Returns the administrative title for a flag link.
+ */
+function flag_flag_link_content_type_admin_title($subtype, $conf) {
+  if ($flag = flag_get_flag($conf['flag_name'])) {
+    return t('Flag link of @flag', array('@flag' => $flag->title));
+  }
+}
-- 
1.7.9.6 (Apple Git-31.1)

