From fe5971548e05413012eedc91aa828fd80a871eb1 Mon Sep 17 00:00:00 2001
From: Steven Jones <steven.jones@computerminds.co.uk>
Date: Wed, 6 Apr 2011 15:47:18 +0100
Subject: [PATCH] Issue #1118254 by Steven Jones: Added Page manager node relationship.

---
 .../ctools/relationships/nat_term_from_node.inc    |   80 ++++++++++++++++++++
 nat.module                                         |    9 ++
 2 files changed, 89 insertions(+), 0 deletions(-)
 create mode 100644 includes/ctools/relationships/nat_term_from_node.inc

diff --git a/includes/ctools/relationships/nat_term_from_node.inc b/includes/ctools/relationships/nat_term_from_node.inc
new file mode 100644
index 0000000..843b4ea
--- /dev/null
+++ b/includes/ctools/relationships/nat_term_from_node.inc
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * @file
+ * Provides a CTools (Panels) relationship that gets a term context from the
+ * node context based on terms used by Taxonomy Node.
+ */
+
+/**
+ * Implementation of specially named hook_ctools_relationships().
+ */
+function nat_nat_term_from_node_ctools_relationships() {
+  $args['nat_term_from_node'] = array(
+    'title' => t("Term from node (NAT)"),
+    'keyword' => 'nat_term',
+    'description' => t('Adds a NAT term from node context'),
+    'required context' => new ctools_context_required(t('Node'), 'node'),
+    'context' => 'nat_nat_term_from_node_ctools_context',
+    'edit form' => 'nat_nat_term_from_node_settings_form',
+    'defaults' => array('vid' => array(), 'concatenator' => ','),
+  );
+  
+  return $args;
+}
+
+/**
+ * Return a new context based on an existing context.
+ */
+function nat_nat_term_from_node_ctools_context($context, $conf) {
+  // If unset it wants a generic, unfilled context, which is just NULL.
+  if (empty($context->data)) {
+    return ctools_context_create_empty('terms', NULL);
+  }
+
+  // Collect all terms for the chosen vocabulary and concatenate them.
+  if (isset($context->data->nat)) {
+    $terms = array();
+    foreach ($context->data->nat as $term) {
+      if (in_array($term->vid, $conf['vid'])) {
+        $terms[] = $term->tid;
+      }
+    }
+
+    if (!empty($terms)) {
+      $all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
+      return ctools_context_create('terms', $all_terms);
+    }
+  }
+}
+
+/**
+ * Settings form for the relationship.
+ */
+function nat_nat_term_from_node_settings_form($form, &$form_state) {
+  $conf = $form_state['conf'];
+
+  $options = array();
+  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
+    $options[$vid] = $vocabulary->name;
+  }
+  $form['vid'] = array(
+    '#title' => t('Vocabulary'),
+    '#type' => 'checkboxes',
+    '#options' => $options,
+    '#default_value' => $conf['vid'],
+    '#prefix' => '<div class="clearfix">',
+    '#suffix' => '</div>',
+  );
+  $form['concatenator'] = array(
+    '#title' => t('Concatenator'),
+    '#type' => 'select',
+    '#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
+    '#default_value' => $conf['concatenator'],
+    '#prefix' => '<div class="clearfix">',
+    '#suffix' => '</div>',
+    '#description' => t("When the value from this context is passed on to a view as argument, the terms can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
+  );
+
+  return $form;
+}
\ No newline at end of file
diff --git a/nat.module b/nat.module
index 794be5a..8b87c29 100755
--- a/nat.module
+++ b/nat.module
@@ -771,3 +771,12 @@ function _nat_get_term_hierarchies($node) {
 
   return $hierarchy;
 }
+
+/**
+ * Implementation of hook_ctools_plugin_directory().
+ */
+function nat_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'ctools' && $plugin == 'relationships') {
+    return 'includes/ctools/' . $plugin;
+  }
+}
-- 
1.7.1

