Index: context_ui_contrib.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/context/context_ui/context_ui_contrib.module,v
retrieving revision 1.4
diff -u -p -r1.4 context_ui_contrib.module
--- context_ui_contrib.module	5 Aug 2008 00:31:49 -0000	1.4
+++ context_ui_contrib.module	16 Aug 2008 01:11:44 -0000
@@ -61,6 +61,17 @@ function context_ui_contrib_context_item
     );
   }
 
+  // Nodereference
+  if (module_exists('nodereference')) {
+    $items['nodereference'] = array(
+      '#title' => t('Node reference'),
+      '#description' => t('Set this context when a node references the selected node(s).'),
+      '#options' => _context_ui_contrib_get_nodereference(),
+      '#type' => 'checkboxes',
+      '#context_ui' => 'setter',
+    );
+  }
+
   return $items;
 }
 
@@ -90,6 +101,21 @@ function context_ui_contrib_nodeapi(&$no
     if (module_exists('outline') && $vol = $node->volume_id) {
       context_ui_set('outline', $vol);
     }
+
+    // Implementation of context_ui_set for nodereference.
+    if (module_exists('nodereference')) {
+      $result = db_query("SELECT field_name FROM {content_node_field} WHERE type = 'nodereference'");
+
+      while($r = db_fetch_array($result)) {
+        if (isset($node->$r['field_name'])) {
+          foreach ($node->$r['field_name'] as $nodereference_field) {
+            if (isset($nodereference_field['nid'])) {
+              context_ui_set('nodereference', $nodereference_field['nid']);
+            }
+          }
+        }
+      }
+    }
   }
 }
 
@@ -146,3 +172,26 @@ function _context_ui_contrib_css_injecto
     return;
   }
 }
+
+/**
+ * Helper function to generate a list of referenced nodes.
+ */
+function _context_ui_contrib_get_nodereference() {
+  $list = array();
+
+  $result = db_query("SELECT field_name FROM {content_node_field} WHERE type = 'nodereference'");
+
+  while($r = db_fetch_array($result)) {
+    $field_name = $r['field_name'];
+    $result2 = db_query("SELECT DISTINCT %s_nid FROM {content_%s} WHERE %s_nid > 0", $field_name, $field_name, $field_name);
+    while($r2 = db_fetch_array($result2)) {
+      $nid = $r2[$field_name .'_nid'];
+      if (!isset($list[$nid])) {
+        $list[$nid] = check_plain(_nodereference_titles($nid));
+      }
+    }
+  }
+  ksort($list);
+  return $list;
+}
+
