? flag1_context-d6.patch
? plugins
Index: flag_context.info
===================================================================
RCS file: flag_context.info
diff -N flag_context.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ flag_context.info	29 Jul 2010 07:52:25 -0000
@@ -0,0 +1,7 @@
+; $Id$
+name = Flag context
+description = Adds context conditions to Flag module.
+core = 6.x
+dependencies[] = context
+dependencies[] = flag
+package = Flags
\ No newline at end of file
Index: flag_context.module
===================================================================
RCS file: flag_context.module
diff -N flag_context.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ flag_context.module	29 Jul 2010 07:52:25 -0000
@@ -0,0 +1,83 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Context support for the Flag module.
+ */
+
+/**
+ * Implements hook_ctools_plugin_api().
+ */
+function flag_context_ctools_plugin_api($module, $api) {
+  if ($module == 'context' && $api == 'plugins') {
+    return array('version' => 3);
+  }
+}
+
+/**
+ * Implements hook_context_plugins().
+ */
+function flag_context_context_plugins() {
+  $plugins = array();
+  $plugins['flag_context_condition_node'] = array(
+    'handler' => array(
+      'path' => drupal_get_path('module', 'flag_context') .'/plugins',
+      'file' => 'flag_context_condition_node.inc',
+      'class' => 'flag_context_condition_node',
+      'parent' => 'context_condition',
+    ),
+  );
+  $plugins['flag_context_condition_user'] = array(
+    'handler' => array(
+      'path' => drupal_get_path('module', 'flag_context') .'/plugins',
+      'file' => 'flag_context_condition_user.inc',
+      'class' => 'flag_context_condition_user',
+      'parent' => 'context_condition',
+    ),
+  );
+  return $plugins;
+}
+
+/**
+ * Implements hook_context_registry().
+ */
+function flag_context_context_registry() {
+  $registry = array();
+  $registry['conditions']['node_flag'] = array(
+    'title' => t('Node flag'),
+    'description' => t('Set this context when viewing a flagged node.'),
+    'plugin' => 'flag_context_condition_node',
+  );
+  $registry['conditions']['user_flag'] = array(
+    'title' => t('User flag'),
+    'description' => t('Set this context when viewing a flagged user.'),
+    'plugin' => 'flag_context_condition_user',
+  );
+  return $registry;
+}
+
+/**
+ * Implements hook_nodeapi().
+ */
+function flag_context_nodeapi(&$node, $op, $teaser, $page) {
+  if ($op == 'view' && $page) {
+    $object = menu_get_object();
+    if (isset($object->nid) && $object->nid === $node->nid) {
+      if ($plugin = context_get_plugin('condition', 'node_flag')) {
+        $plugin->execute($node, $op);
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_user().
+ */
+function flag_context_user($op, &$edit, &$account, $category = NULL) {
+  if (in_array($op, array('view', 'form'))) {
+    if ($plugin = context_get_plugin('condition', 'user_flag')) {
+      $plugin->execute($account, $op);
+    }
+  }
+}
Index: plugins/flag_context_condition_node.inc
===================================================================
RCS file: plugins/flag_context_condition_node.inc
diff -N plugins/flag_context_condition_node.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ plugins/flag_context_condition_node.inc	29 Jul 2010 07:52:25 -0000
@@ -0,0 +1,43 @@
+<?php
+// $Id$
+
+/**
+ * Exposes flagged node views as a context condition.
+ */
+class flag_context_condition_node extends context_condition {
+  function condition_values() {
+    $values = array();
+    // Get flags allowed for nodes.
+    $flags = flag_get_flags('node');
+    $node_types = node_get_types('names');
+    foreach ($flags as $flag) {
+      $values[$flag->name] = $flag->get_title();
+      foreach ($flag->types as $node_type) {
+        $values[$flag->name .':'. $node_type] = '-- '. $node_type .' ('. $node_types[$node_type] .')';
+      }
+    }
+    return $values;
+  }
+
+  function execute($node, $op) {
+    // TODO: replace with $this->condition_used() on context version >= 3b6
+    $map = context_condition_map();
+    // Only view $op supported.
+    if ($op == 'view' && !empty($map['node_flag'])) {
+      // Get flags for the node's type.
+      $flags = flag_get_flags('node', $node->type);
+      foreach ($flags as $flag) {
+        foreach ($this->get_contexts($flag->name) as $context) {
+          if ($flag->is_flagged($node->nid)) {
+            $this->condition_met($context, $flag->name);
+          }
+        }
+        foreach ($this->get_contexts($flag->name .':'. $node->type) as $context) {
+          if ($flag->is_flagged($node->nid)) {
+            $this->condition_met($context, $flag->name .':'. $node->type);
+          }
+        }
+      }
+    }
+  }
+}
Index: plugins/flag_context_condition_user.inc
===================================================================
RCS file: plugins/flag_context_condition_user.inc
diff -N plugins/flag_context_condition_user.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ plugins/flag_context_condition_user.inc	29 Jul 2010 07:52:25 -0000
@@ -0,0 +1,34 @@
+<?php
+// $Id$
+
+/**
+ * Expose flagged user pages as a context condition.
+ */
+class flag_context_condition_user extends context_condition {
+  function condition_values() {
+    $values = array();
+    // Get flags for user content type.
+    $flags = flag_get_flags('user');
+    foreach ($flags as $flag) {
+      $values[$flag->name] = $flag->get_title();
+    }
+    return $values;
+  }
+
+  function execute($account, $op) {
+    // TODO: replace with $this->condition_used() on context version >= 3b6
+    $map = context_condition_map();
+    // Only view $op supported.
+    if (($op == 'view' || $op == 'form') && !empty($map['user_flag'])) {
+      // Get flags for user content type.
+      $flags = flag_get_flags('user');
+      foreach ($flags as $flag) {
+        foreach ($this->get_contexts($flag->name) as $context) {
+          if ($flag->is_flagged($account->uid)) {
+            $this->condition_met($context, $flag->name);
+          }
+        }
+      }
+    }
+  }
+}
